Class Response

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public class Response
    extends java.lang.Object
    implements java.io.Closeable
    An HTTP response.

    This response may contain a non-null body stream of the HttpUrlConnection. If so, this body must be closed to avoid leaking resources. Use either getBody() or close() to close the body.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.String body  
      private java.io.Closeable[] closeables  
      private boolean closed  
      private int code  
      private java.util.Map<java.lang.String,​java.lang.String> headers  
      private java.lang.String message  
      private java.io.InputStream stream  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private Response​(int code, java.lang.String message, java.util.Map<java.lang.String,​java.lang.String> headers)  
        Response​(int code, java.lang.String message, java.util.Map<java.lang.String,​java.lang.String> headers, java.io.InputStream stream, java.io.Closeable... closeables)  
        Response​(int code, java.lang.String message, java.util.Map<java.lang.String,​java.lang.String> headers, java.lang.String body)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      java.lang.String getBody()
      Returns the response body as a string, closing the stream that backs it.
      int getCode()
      Obtains the HTTP status code
      java.lang.String getHeader​(java.lang.String name)
      Obtains a single HTTP Header value, or null if undefined
      java.util.Map<java.lang.String,​java.lang.String> getHeaders()
      Obtains a Map containing the HTTP Response Headers
      java.lang.String getMessage()
      Obtains the HTTP status message.
      java.io.InputStream getStream()
      Obtains the meaningful stream of the HttpUrlConnection, either inputStream or errorInputStream, depending on the status code
      boolean isSuccessful()  
      private java.lang.String parseBodyContents()  
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • code

        private final int code
      • message

        private final java.lang.String message
      • headers

        private final java.util.Map<java.lang.String,​java.lang.String> headers
      • body

        private java.lang.String body
      • stream

        private java.io.InputStream stream
      • closeables

        private java.io.Closeable[] closeables
      • closed

        private boolean closed
    • Constructor Detail

      • Response

        private Response​(int code,
                         java.lang.String message,
                         java.util.Map<java.lang.String,​java.lang.String> headers)
      • Response

        public Response​(int code,
                        java.lang.String message,
                        java.util.Map<java.lang.String,​java.lang.String> headers,
                        java.io.InputStream stream,
                        java.io.Closeable... closeables)
      • Response

        public Response​(int code,
                        java.lang.String message,
                        java.util.Map<java.lang.String,​java.lang.String> headers,
                        java.lang.String body)
    • Method Detail

      • parseBodyContents

        private java.lang.String parseBodyContents()
                                            throws java.io.IOException
        Throws:
        java.io.IOException
      • isSuccessful

        public boolean isSuccessful()
      • getBody

        public java.lang.String getBody()
                                 throws java.io.IOException
        Returns the response body as a string, closing the stream that backs it. Idempotent.
        Returns:
        body as string
        Throws:
        java.io.IOException - IO Exception
      • getStream

        public java.io.InputStream getStream()
        Obtains the meaningful stream of the HttpUrlConnection, either inputStream or errorInputStream, depending on the status code
        Returns:
        input stream / error stream
      • getCode

        public int getCode()
        Obtains the HTTP status code
        Returns:
        the status code
      • getMessage

        public java.lang.String getMessage()
        Obtains the HTTP status message. Returns null if the message can not be discerned from the response (not valid HTTP)
        Returns:
        the status message
      • getHeaders

        public java.util.Map<java.lang.String,​java.lang.String> getHeaders()
        Obtains a Map containing the HTTP Response Headers
        Returns:
        headers
      • getHeader

        public java.lang.String getHeader​(java.lang.String name)
        Obtains a single HTTP Header value, or null if undefined
        Parameters:
        name - the header name.
        Returns:
        header value or null.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException