Package de.xima.fc.http.result
Class HttpResponseWrapper
java.lang.Object
de.xima.fc.http.result.HttpResponseWrapper
- All Implemented Interfaces:
Closeable
,AutoCloseable
Wrapper for a
CloseableHttpResponse
which provides a couple of convenient methods for working with the
response- Since:
- 7.0.0
- Author:
- XIMA MEDIA GmbH
-
Constructor Summary
ConstructorsConstructorDescriptionHttpResponseWrapper
(@NotNull org.apache.http.client.methods.CloseableHttpResponse originResponse, Charset outputCharset) The one and only constructor for this wrapper! -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Parses theraw content
as a string with the currentencoding
.getFirstHeader
(String name) Returns the value of the first header with a specified name of this response.Gets an unmodifiable map with all headers from the response.getLastHeader
(String name) Returns the value of the last header with a specified name of this response.getLastHeaderValue
(String headerName) org.apache.http.client.methods.CloseableHttpResponse
org.apache.http.client.methods.CloseableHttpResponse
Deprecated.byte[]
Gets the resolved charset for the response.org.apache.http.entity.ContentType
Gets the resolved content type for the response.int
boolean
isHasHeader
(String name) Checks if a certain header is present in this response.boolean
void
updateContent
(String content, org.apache.http.entity.ContentType contentType) Updates the content of the underlying response.
-
Constructor Details
-
HttpResponseWrapper
public HttpResponseWrapper(@NotNull @NotNull org.apache.http.client.methods.CloseableHttpResponse originResponse, Charset outputCharset) The one and only constructor for this wrapper!- Parameters:
originResponse
- theCloseableHttpResponse
that should be wrappedoutputCharset
- Charset used for the response content. Ifnull
, a default charset is used.
-
-
Method Details
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
getContent
Parses theraw content
as a string with the currentencoding
.- Returns:
- the content from the body of the underlying HTTP entity
- Throws:
org.apache.http.client.HttpResponseException
- signals a non 2xx HTTP responseIOException
- in case of a problem or the connection was aborted
-
getContentType
- Returns:
- the Content-Type header of the underlying HTTP entity, or null if the content type is unknown
-
getEncoding
- Returns:
- the Content-Encoding header of the underlying HTTP entity, or
null
if the content encoding is unknown
-
getFirstHeader
Returns the value of the first header with a specified name of this response. If there is more than one matching header in the response, the first header of is returned. If there is no matching header in the message, the empty string is returned.- Parameters:
name
- The name of the header to return.- Returns:
- The value of first header with the given name.
-
getHeaders
Gets an unmodifiable map with all headers from the response. All header names are lower case. In case more than one header with the same name exists, the value of the last header is used.- Returns:
- A map with all headers from the response.
-
getLastHeader
Returns the value of the last header with a specified name of this response. If there is more than one matching header in the response, the last header of is returned. If there is no matching header in the message, the empty string is returned.- Parameters:
name
- The name of the header to return.- Returns:
- The value of last header with the given name.
-
getLastHeaderValue
- Parameters:
headerName
- Name of the header- Returns:
- the value of the last response header if it was provided otherwise empty string
-
getOriginalResponse
public org.apache.http.client.methods.CloseableHttpResponse getOriginalResponse()- Returns:
- The original response wrapped by this instance.
- Since:
- 7.4.0
-
getOriginResponse
Deprecated.- Returns:
- The original response wrapped by this instance.
-
getRawContent
- Returns:
- The raw binary content from the body of the underlying HTTP entity
- Throws:
org.apache.http.client.HttpResponseException
- signals a non 2xx HTTP responseIOException
- in case of a problem or the connection was aborted
-
getReasonPhrase
- Returns:
- The reason phrase of the status line returned by the HTTP response, when available, or
null
otherwise.
-
getResolvedCharset
Gets the resolved charset for the response. This is taken from the response headers, if present, and uses a sane default otherwise.- Returns:
- The charset for the response.
-
getResolvedContentType
public org.apache.http.entity.ContentType getResolvedContentType()Gets the resolved content type for the response. This is taken from the response headers, if present, and uses a sane default otherwise.- Returns:
- The content type for the response.
-
getResponseCode
public int getResponseCode()- Returns:
- the HTTP status code of the underlying HTTP entity.
-
getResponseMessage
- Returns:
- response message of HTTP with status code or
null
-
isHasHeader
Checks if a certain header is present in this response.- Parameters:
name
- The header name to check for.- Returns:
true
if at least one header with this name is present.
-
isRedirect
public boolean isRedirect()- Returns:
true
if an HTTP status code is 3xx otherwisefalse
-
updateContent
public void updateContent(String content, org.apache.http.entity.ContentType contentType) throws IOException Updates the content of the underlying response.- Parameters:
content
- the content to updatecontentType
- the content type of the update content- Throws:
IOException
- if an error occurs while reading the input stream on the specified entity.
-
getOriginalResponse()
.