Class HttpResponse

java.lang.Object
net.targetr.wtm3.net.http.HttpResponse
Direct Known Subclasses:
ClientFileProxyResponse, HttpByteArrayResponse, HttpFastStringResponse, ProxyResponse, WebSocketResponse

public class HttpResponse extends Object
Parses, stores and writes HTTP responses.
Author:
Dr Michael Gardiner
  • Field Details

    • version

      public String version
      The HTTP version of the server, or version in use.
    • code

      public int code
      An HTTP code that signifies how the request was handled.
    • info

      public String info
      The HTTP response information describing how the request was handled.
    • headers

      public final Headers headers
      The headers with information about the response.
    • bytesWritten

      public final AtomicLong bytesWritten
      Number of bytes written to the response.
    • headerLength

      public long headerLength
      Number of bytes in the response headers.
    • writeErrorHandler

      public SocketExceptionHandler writeErrorHandler
      An optional handler to be called if there is an exception writing this response
  • Constructor Details

    • HttpResponse

      public HttpResponse()
      Constructs a new empty HttpResponse object for holding parsed metadata about a HTTP response.

      Default is code 200 OK.

    • HttpResponse

      public HttpResponse(int code, String info)
      Constructs a new HttpResponse object for holding parsed metadata about a HTTP response.
      Parameters:
      code - An HTTP code that signifies how the request was handled.
      info - More information about the response.
    • HttpResponse

      public HttpResponse(int code, String info, Headers headers)
      Constructs a new HttpResponse object for holding parsed metadata about a HTTP response including headers.
      Parameters:
      code - A HTTP code that signifies how the request was handled.
      info - The HTTP response information describing how the request was handled.
      headers - The headers with information about the response.
  • Method Details

    • toString

      public String toString()
      Returns the String representation of the response according to HTTP spec.
      Overrides:
      toString in class Object
      Returns:
      Response as a string.
    • write

      public void write(HttpRequest request, OutputStream out) throws Exception
      Writes a response to the OutputStream.
      Parameters:
      request - Request associated with response. Used for logging purposes.
      out - The OutputStream to write the response.
      Throws:
      Exception - If an error occurs while writing the response.
    • getContentLength

      public long getContentLength()
      Returns the content length of the response if set in the headers.
      Returns:
      number of bytes used by the content if specified.
    • parseResponse

      public static void parseResponse(PushbackInputStream in, HttpResponse res) throws IOException
      Parses a response on an InputStream into a HTTPResponse object.
      Parameters:
      in - InputStream to read the raw request from.
      res - The response object to populate.
      Throws:
      IOException - If an error occurs while reading the InputStream.
    • getBodyInputStream

      public InputStream getBodyInputStream()
      Returns an InputStream for reading the response body data.
      Returns:
      stream of response data.