Package net.targetr.wtm3.net.http
Class HttpResponse
java.lang.Object
net.targetr.wtm3.net.http.HttpResponse
- Direct Known Subclasses:
ClientFileProxyResponse,HttpByteArrayResponse,HttpFastStringResponse,ProxyResponse,WebSocketResponse
Parses, stores and writes HTTP responses.
- Author:
- Dr Michael Gardiner
-
Field Summary
FieldsModifier and TypeFieldDescriptionfinal AtomicLongNumber of bytes written to the response.intAn HTTP code that signifies how the request was handled.longNumber of bytes in the response headers.final HeadersThe headers with information about the response.The HTTP response information describing how the request was handled.The HTTP version of the server, or version in use.An optional handler to be called if there is an exception writing this response -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new empty HttpResponse object for holding parsed metadata about a HTTP response.HttpResponse(int code, String info) Constructs a new HttpResponse object for holding parsed metadata about a HTTP response.HttpResponse(int code, String info, Headers headers) Constructs a new HttpResponse object for holding parsed metadata about a HTTP response including headers. -
Method Summary
Modifier and TypeMethodDescriptionReturns an InputStream for reading the response body data.longReturns the content length of the response if set in the headers.static voidparseResponse(PushbackInputStream in, HttpResponse res) Parses a response on an InputStream into a HTTPResponse object.toString()Returns the status line and headers, including the terminating blank line.voidwrite(HttpRequest request, OutputStream out) Writes the status line and headers.
-
Field Details
-
version
The HTTP version of the server, or version in use. -
code
public int codeAn 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. -
bytesWritten
Number of bytes written to the response. -
headerLength
public long headerLengthNumber of bytes in the response headers. -
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
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
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
Returns the status line and headers, including the terminating blank line. -
write
Writes the status line and headers. Subclasses append any response body. The caller must setversionbefore writing.- Parameters:
request- the associated request, available to subclasses for response handlingout- 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:
- the declared body length in bytes, or -1 if absent
-
parseResponse
Parses a response on an InputStream into a HTTPResponse object.- Parameters:
in- InputStream to read the raw response from.res- The response object to populate.- Throws:
IOException- If an error occurs while reading the InputStream.
-
getBodyInputStream
Returns an InputStream for reading the response body data.- Returns:
- the parsed body stream, with gzip decoding when specified, or null before parsing, for status 204/304, or without supported body framing
-