Package de.xima.fc.http.result
Class HttpResponseWrapper
- java.lang.Object
-
- de.xima.fc.http.result.HttpResponseWrapper
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
public class HttpResponseWrapper extends Object implements Closeable
Wrapper for aCloseableHttpResponse
which provides a couple of convenient methods for working with the response- Since:
- 7.0.0
- Author:
- XIMA MEDIA GmbH
-
-
Constructor Summary
Constructors Constructor Description HttpResponseWrapper(@NotNull org.apache.http.client.methods.CloseableHttpResponse originResponse, Charset outputCharset)
The one and only constructor for this wrapper!
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
close()
String
getContent()
Parses theraw content
as a string with the currentencoding
.String
getContentType()
String
getEncoding()
String
getFirstHeader(String name)
Returns the value of the first header with a specified name of this response.Map<String,String>
getHeaders()
Gets an unmodifiable map with all headers from the response.String
getLastHeader(String name)
Returns the value of the last header with a specified name of this response.String
getLastHeaderValue(String headerName)
org.apache.http.client.methods.CloseableHttpResponse
getOriginalResponse()
org.apache.http.client.methods.CloseableHttpResponse
getOriginResponse()
Deprecated.byte[]
getRawContent()
String
getReasonPhrase()
Charset
getResolvedCharset()
Gets the resolved charset for the response.org.apache.http.entity.ContentType
getResolvedContentType()
Gets the resolved content type for the response.int
getResponseCode()
String
getResponseMessage()
boolean
isHasHeader(String name)
Checks if a certain header is present in this response.boolean
isRedirect()
void
updateContent(String content, org.apache.http.entity.ContentType contentType)
Updates the content of the underlying response.
-
-
-
Constructor Detail
-
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 Detail
-
close
public void close() throws IOException
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
getContent
public String getContent() throws IOException
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
public String getContentType()
- Returns:
- the Content-Type header of the underlying HTTP entity, or null if the content type is unknown
-
getEncoding
public String getEncoding()
- Returns:
- the Content-Encoding header of the underlying HTTP entity, or
null
if the content encoding is unknown
-
getFirstHeader
public String getFirstHeader(String name)
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
public Map<String,String> 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
public String getLastHeader(String name)
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
public String getLastHeaderValue(String headerName)
- 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 public org.apache.http.client.methods.CloseableHttpResponse getOriginResponse()
Deprecated.- Returns:
- The original response wrapped by this instance.
-
getRawContent
public byte[] getRawContent() throws IOException
- 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
public String getReasonPhrase()
- Returns:
- The reason phrase of the status line returned by the HTTP response, when available, or
null
otherwise.
-
getResolvedCharset
public Charset 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
public String getResponseMessage()
- Returns:
- response message of HTTP with status code or
null
-
isHasHeader
public boolean isHasHeader(String name)
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.
-
-