Package org.htmlunit.util
Class WebResponseWrapper
- java.lang.Object
-
- org.htmlunit.WebResponse
-
- org.htmlunit.util.WebResponseWrapper
-
- All Implemented Interfaces:
java.io.Serializable
public class WebResponseWrapper extends WebResponse
Provides a convenient implementation of theWebResponse
interface that can be subclassed by developers wishing to adapt a particular WebResponse. This class implements the Wrapper or Decorator pattern. Methods default to calling through to the wrapped web response object.- See Also:
- Serialized Form
-
-
Field Summary
-
Fields inherited from class org.htmlunit.WebResponse
FORBIDDEN, INTERNAL_SERVER_ERROR, NO_CONTENT, NOT_FOUND, OK
-
-
Constructor Summary
Constructors Constructor Description WebResponseWrapper(WebResponse webResponse)
Constructs a WebResponse object wrapping provided WebResponse.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
cleanUp()
Clean up the response data.void
defaultCharsetUtf8()
Mark this response for using UTF-8 as default charset.java.io.InputStream
getContentAsStream()
Returns the response content as an input stream.java.io.InputStream
getContentAsStreamWithBomIfApplicable()
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.java.lang.String
getContentAsString()
Returns the response content as a string, using the charset/encoding specified in the server response.java.lang.String
getContentAsString(java.nio.charset.Charset encoding)
Returns the response content as a string, using the specified charset, rather than the charset/encoding specified in the server response.java.lang.String
getContentAsString(java.nio.charset.Charset encoding, boolean ignoreUtf8Bom)
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Returns the response content as a string, using the specified charset, rather than the charset/encoding specified in the server response.java.nio.charset.Charset
getContentCharset()
Returns the content charset for this response, even if no charset was specified explicitly.java.nio.charset.Charset
getContentCharsetOrNull()
Returns the content charset specified explicitly in the header or in the content, ornull
if none was specified.long
getContentLength()
Returns length of the content data.java.lang.String
getContentType()
Returns the content type returned from the server, e.g.long
getLoadTime()
Returns the time it took to load this web response, in milliseconds.java.util.List<NameValuePair>
getResponseHeaders()
Returns the response headers as a list ofNameValuePair
s.java.lang.String
getResponseHeaderValue(java.lang.String headerName)
Returns the value of the specified response header.int
getStatusCode()
Returns the status code that was returned by the server.java.lang.String
getStatusMessage()
Returns the status message that was returned from the server.WebRequest
getWebRequest()
Returns the request used to load this response.boolean
isSuccess()
boolean
isSuccessOrUseProxy()
boolean
isSuccessOrUseProxyOrNotModified()
-
Methods inherited from class org.htmlunit.WebResponse
getBlockReason, getProtocolVersion, getRawSize, markAsBlocked, setProtocolVersion, setRawSize, wasBlocked
-
-
-
-
Constructor Detail
-
WebResponseWrapper
public WebResponseWrapper(WebResponse webResponse) throws java.lang.IllegalArgumentException
Constructs a WebResponse object wrapping provided WebResponse.- Parameters:
webResponse
- the webResponse that does the real work- Throws:
java.lang.IllegalArgumentException
- if the webResponse isnull
-
-
Method Detail
-
getContentLength
public long getContentLength()
Returns length of the content data. The default behavior of this method is to return getContentLength() on the wrapped webResponse object.- Overrides:
getContentLength
in classWebResponse
- Returns:
- the length
-
getContentAsStream
public java.io.InputStream getContentAsStream() throws java.io.IOException
Returns the response content as an input stream. The default behavior of this method is to return getContentAsStream() on the wrapped webResponse object.- Overrides:
getContentAsStream
in classWebResponse
- Returns:
- the response content as an input stream
- Throws:
java.io.IOException
- in case of IOProblems
-
getContentAsString
public java.lang.String getContentAsString()
Returns the response content as a string, using the charset/encoding specified in the server response. The default behavior of this method is to return getContentAsString() on the wrapped webResponse object.- Overrides:
getContentAsString
in classWebResponse
- Returns:
- the response content as a string, using the charset/encoding specified in the server response or null if the content retrieval was failing
-
getContentAsString
public java.lang.String getContentAsString(java.nio.charset.Charset encoding)
Returns the response content as a string, using the specified charset, rather than the charset/encoding specified in the server response. If there is a bom header the charset parameter will be overwritten by the bom. The default behavior of this method is to return getContentAsString(Charset) on the wrapped webResponse object.- Overrides:
getContentAsString
in classWebResponse
- Parameters:
encoding
- the charset/encoding to use to convert the response content into a string- Returns:
- the response content as a string or null if the content retrieval was failing
-
getContentAsString
public java.lang.String getContentAsString(java.nio.charset.Charset encoding, boolean ignoreUtf8Bom)
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Returns the response content as a string, using the specified charset, rather than the charset/encoding specified in the server response. If there is a bom header the charset parameter will be overwritten by the bom. The default behavior of this method is to return getContentAsString(Charset, boolean) on the wrapped webResponse object.- Overrides:
getContentAsString
in classWebResponse
- Parameters:
encoding
- the charset/encoding to use to convert the response content into a stringignoreUtf8Bom
- if true utf8 bom header will be ignored- Returns:
- the response content as a string or null if the content retrieval was failing
-
getContentCharsetOrNull
public java.nio.charset.Charset getContentCharsetOrNull()
Returns the content charset specified explicitly in the header or in the content, ornull
if none was specified. The default behavior of this method is to return getContentCharsetOrNull() on the wrapped webResponse object.- Overrides:
getContentCharsetOrNull
in classWebResponse
- Returns:
- the content charset specified explicitly in the header or in the content,
or
null
if none was specified
-
getContentCharset
public java.nio.charset.Charset getContentCharset()
Returns the content charset for this response, even if no charset was specified explicitly. This method always returns a valid charset. This method first checks theContent-Type
header; if not found, it checks the request charset; as a last resort, this method returnsStandardCharsets.ISO_8859_1
. If no charset is defined for an xml response, then UTF-8 is used The default behavior of this method is to return getContentCharset() on the wrapped webResponse object.- Overrides:
getContentCharset
in classWebResponse
- Returns:
- the content charset for this response
- See Also:
- Character Encoding
-
getContentType
public java.lang.String getContentType()
Returns the content type returned from the server, e.g. "text/html". The default behavior of this method is to return getContentType() on the wrapped webResponse object.- Overrides:
getContentType
in classWebResponse
- Returns:
- the content type returned from the server, e.g. "text/html"
-
getLoadTime
public long getLoadTime()
Returns the time it took to load this web response, in milliseconds. The default behavior of this method is to return getLoadTime() on the wrapped webResponse object.- Overrides:
getLoadTime
in classWebResponse
- Returns:
- the time it took to load this web response, in milliseconds
-
getResponseHeaders
public java.util.List<NameValuePair> getResponseHeaders()
Returns the response headers as a list ofNameValuePair
s. The default behavior of this method is to return getResponseHeaders() on the wrapped webResponse object.- Overrides:
getResponseHeaders
in classWebResponse
- Returns:
- the response headers as a list of
NameValuePair
s
-
getResponseHeaderValue
public java.lang.String getResponseHeaderValue(java.lang.String headerName)
Returns the value of the specified response header. The default behavior of this method is to return getResponseHeaderValue() on the wrapped webResponse object.- Overrides:
getResponseHeaderValue
in classWebResponse
- Parameters:
headerName
- the name of the header whose value is to be returned- Returns:
- the header value,
null
if no response header exists with this name
-
getStatusCode
public int getStatusCode()
Returns the status code that was returned by the server. The default behavior of this method is to return getStatusCode() on the wrapped webResponse object.- Overrides:
getStatusCode
in classWebResponse
- Returns:
- the status code that was returned by the server
-
getStatusMessage
public java.lang.String getStatusMessage()
Returns the status message that was returned from the server. The default behavior of this method is to return getStatusMessage() on the wrapped webResponse object.- Overrides:
getStatusMessage
in classWebResponse
- Returns:
- the status message that was returned from the server
-
getWebRequest
public WebRequest getWebRequest()
Returns the request used to load this response. The default behavior of this method is to return getWebRequest() on the wrapped webResponse object.- Overrides:
getWebRequest
in classWebResponse
- Returns:
- the request used to load this response
-
cleanUp
public void cleanUp()
Clean up the response data. The default behavior of this method is to call cleanUp() on the wrapped webResponse object.- Overrides:
cleanUp
in classWebResponse
-
defaultCharsetUtf8
public void defaultCharsetUtf8()
Mark this response for using UTF-8 as default charset. The default behavior of this method is to call defaultCharsetUtf8() on the wrapped webResponse object.- Overrides:
defaultCharsetUtf8
in classWebResponse
-
getContentAsStreamWithBomIfApplicable
public java.io.InputStream getContentAsStreamWithBomIfApplicable() throws java.io.IOException
Description copied from class:WebResponse
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.- Overrides:
getContentAsStreamWithBomIfApplicable
in classWebResponse
- Returns:
- the associated InputStream wrapped with a bom input stream if applicable
- Throws:
java.io.IOException
- in case of IO problems
-
isSuccess
public boolean isSuccess()
- Overrides:
isSuccess
in classWebResponse
- Returns:
- true if the 2xx
-
isSuccessOrUseProxy
public boolean isSuccessOrUseProxy()
- Overrides:
isSuccessOrUseProxy
in classWebResponse
- Returns:
- true if the 2xx or 305
-
isSuccessOrUseProxyOrNotModified
public boolean isSuccessOrUseProxyOrNotModified()
- Overrides:
isSuccessOrUseProxyOrNotModified
in classWebResponse
- Returns:
- true if the 2xx or 305
-
-