Class GXResponse

java.lang.Object
org.gcube.common.gxhttp.request.GXResponse
All Implemented Interfaces:
AutoCloseable

public class GXResponse extends Object implements AutoCloseable
The response to a GXRequest: status, headers and body. The body is read in the same way whatever the status, an error included.

The body is a stream on the connection: read it, or close() the response, to give the connection back.

Author:
Luca Frosini (ISTI-CNR)
  • Method Details

    • getURI

      public URI getURI()
      Returns:
      the URI the response comes from, the last one when the request was redirected
    • getStatus

      public int getStatus()
      Returns:
      the HTTP status code
    • isSuccessful

      public boolean isSuccessful()
      Returns:
      whether the status is a success (2xx)
    • getHeaders

      public Map<String,List<String>> getHeaders()
      Returns:
      every header, with case-insensitive names
    • getHeader

      public String getHeader(String name)
      Parameters:
      name - the header name, case-insensitive
      Returns:
      the first value of the header, or null when the response does not have it
    • getBody

      public InputStream getBody()
      Returns:
      the body as a stream
    • getBodyAsString

      public String getBodyAsString() throws IOException
      Reads the whole body as UTF-8 text and closes it.
      Returns:
      the body
      Throws:
      IOException - if the body cannot be read
    • close

      public void close() throws IOException
      Closes the body, giving the connection back.
      Specified by:
      close in interface AutoCloseable
      Throws:
      IOException - if the body cannot be closed