Class WebResponseWrapper
- All Implemented Interfaces:
Serializable
WebResponse 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:
-
Constructor Summary
ConstructorsConstructorDescriptionWebResponseWrapper(WebResponse webResponse) Constructs a WebResponse object wrapping provided WebResponse. -
Method Summary
Modifier and TypeMethodDescriptionvoidcleanUp()Clean up the response data.voidDeprecated.Returns the response content as an input stream.INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.Returns the response content as a string, using the charset/encoding specified in the server response.getContentAsString(Charset encoding) Returns the response content as a string, using the specified charset, rather than the charset/encoding specified in the server response.Returns the content charset for this response, even if no charset was specified explicitly.Deprecated.as of version 4.0.0; usegetContentCharset()insteadlongReturns length of the content data.Returns the content type returned from the server, e.g.Returns the content charset specified explicitly in theContent-Typeheader ornullif none was specified.longReturns the time it took to load this web response, in milliseconds.Returns the response headers as a list ofNameValuePairs.getResponseHeaderValue(String headerName) Returns the value of the specified response header.intReturns the status code that was returned by the server.Returns the status message that was returned from the server.Returns the request used to load this response.booleanbooleanbooleanbooleanReturns whether the charset of the previous call toWebResponse.getContentCharset()was "tentative".Methods inherited from class org.htmlunit.WebResponse
getBlockReason, getProtocolVersion, getRawSize, markAsBlocked, setProtocolVersion, setRawSize, wasBlocked
-
Constructor Details
-
WebResponseWrapper
Constructs a WebResponse object wrapping provided WebResponse.- Parameters:
webResponse- the webResponse that does the real work- Throws:
IllegalArgumentException- if the webResponse isnull
-
-
Method Details
-
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:
getContentLengthin classWebResponse- Returns:
- the length
-
getContentAsStream
Returns the response content as an input stream. The default behavior of this method is to return getContentAsStream() on the wrapped webResponse object.- Overrides:
getContentAsStreamin classWebResponse- Returns:
- the response content as an input stream
- Throws:
IOException- in case of IOProblems
-
wasContentCharsetTentative
public boolean wasContentCharsetTentative()Returns whether the charset of the previous call toWebResponse.getContentCharset()was "tentative".A charset is classed as "tentative" if its detection is prone to false positive/negatives.
For example, HTML meta-tag sniffing can be fooled by text that looks-like-a-meta-tag inside JavaScript code (false positive) or if the meta-tag is after the first 1024 bytes (false negative). The default behavior of this method is to return wasContentCharsetTentative() on the wrapped webResponse object.
- Overrides:
wasContentCharsetTentativein classWebResponse- Returns:
trueif the charset of the previous call toWebResponse.getContentCharset()was "tentative".- See Also:
-
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:
getContentAsStringin 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
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:
getContentAsStringin 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
-
getHeaderContentCharset
Returns the content charset specified explicitly in theContent-Typeheader ornullif none was specified. The default behavior of this method is to return getHeaderContentCharset() on the wrapped webResponse object.- Overrides:
getHeaderContentCharsetin classWebResponse- Returns:
- the content charset specified header or
nullif none was specified
-
getContentCharsetOrNull
Deprecated.as of version 4.0.0; usegetContentCharset()insteadReturns the content charset specified explicitly in the header or in the content, ornullif none was specified. The default behavior of this method is to return getContentCharsetOrNull() on the wrapped webResponse object.- Overrides:
getContentCharsetOrNullin classWebResponse- Returns:
- the content charset specified explicitly in the header or in the content,
or
nullif none was specified
-
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 the
Content-Typeheader or in the content BOM for viable charset. If not found, it attempts to determine the charset based on the type of the content. As a last resort, this method returns the value ofWebRequest.getDefaultResponseContentCharset()which isStandardCharsets.UTF_8by default. The default behavior of this method is to return getContentCharset() on the wrapped webResponse object.- Overrides:
getContentCharsetin classWebResponse- Returns:
- the content charset for this response
-
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:
getContentTypein 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:
getLoadTimein classWebResponse- Returns:
- the time it took to load this web response, in milliseconds
-
getResponseHeaders
Returns the response headers as a list ofNameValuePairs. The default behavior of this method is to return getResponseHeaders() on the wrapped webResponse object.- Overrides:
getResponseHeadersin classWebResponse- Returns:
- the response headers as a list of
NameValuePairs
-
getResponseHeaderValue
Returns the value of the specified response header. The default behavior of this method is to return getResponseHeaderValue() on the wrapped webResponse object.- Overrides:
getResponseHeaderValuein classWebResponse- Parameters:
headerName- the name of the header whose value is to be returned- Returns:
- the header value,
nullif 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:
getStatusCodein classWebResponse- Returns:
- the status code that was returned by the server
-
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:
getStatusMessagein classWebResponse- Returns:
- the status message that was returned from the server
-
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:
getWebRequestin 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:
cleanUpin classWebResponse
-
defaultCharsetUtf8
Deprecated.as of version 4.0.0; useWebRequest.setDefaultResponseContentCharset(Charset)insteadMark 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:
defaultCharsetUtf8in classWebResponse
-
getContentAsStreamWithBomIfApplicable
Description copied from class:WebResponseINTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.- Overrides:
getContentAsStreamWithBomIfApplicablein classWebResponse- Returns:
- the associated InputStream wrapped with a bom input stream if applicable
- Throws:
IOException- in case of IO problems
-
isSuccess
public boolean isSuccess()- Overrides:
isSuccessin classWebResponse- Returns:
- true if the 2xx
-
isSuccessOrUseProxy
public boolean isSuccessOrUseProxy()- Overrides:
isSuccessOrUseProxyin classWebResponse- Returns:
- true if the 2xx or 305
-
isSuccessOrUseProxyOrNotModified
public boolean isSuccessOrUseProxyOrNotModified()- Overrides:
isSuccessOrUseProxyOrNotModifiedin classWebResponse- Returns:
- true if the 2xx or 305
-
WebRequest.setDefaultResponseContentCharset(Charset)instead