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 a CloseableHttpResponse which provides a couple of convenient methods for working with the response
Since:
7.0.0
Author:
XIMA MEDIA GmbH
  • 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 - the CloseableHttpResponse that should be wrapped
      outputCharset - Charset used for the response content. If null, a default charset is used.
  • Method Details

    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException
    • getContent

      public String getContent() throws IOException
      Parses the raw content as a string with the current encoding.
      Returns:
      the content from the body of the underlying HTTP entity
      Throws:
      org.apache.http.client.HttpResponseException - signals a non 2xx HTTP response
      IOException - 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 response
      IOException - 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 otherwise false
    • 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 update
      contentType - the content type of the update content
      Throws:
      IOException - if an error occurs while reading the input stream on the specified entity.