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 String representation of the response according to HTTP spec.voidwrite(HttpRequest request, OutputStream out) Writes a response to the OutputStream.
-
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 String representation of the response according to HTTP spec. -
write
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
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
Returns an InputStream for reading the response body data.- Returns:
- stream of response data.
-