public class ResponseWrapper extends Object implements Response
ResponseWrapper
object is used so that the original
Response
object can be wrapped in a filtering proxy
object. This allows a container to interact with an implementation
of this with overridden methods providing specific functionality.
the Response
object in a concurrent environment.
public void handle(Request req, Response resp) { handler.handle(req, new ZipResponse(resp)); }The above is an example of how the
ResponseWrapper
can
be used to provide extra functionality to a Response
in a transparent manner. Such an implementation could apply a
Content-Encoding header and compress the response for performance
over a slow network. Filtering can be applied with the use of
layered Container
objects.Container
Modifier and Type | Field and Description |
---|---|
protected Response |
response
This is the response instance that is being wrapped.
|
Constructor and Description |
---|
ResponseWrapper(Response response)
Constructor for
ResponseWrapper object. |
Modifier and Type | Method and Description |
---|---|
void |
addDate(String name,
long date)
This is used as a convenience method for adding a header that
needs to be parsed into a HTTPdate string.
|
void |
addInteger(String name,
int value)
This can be used to add a HTTP message header to this object.
|
void |
addValue(String name,
String value)
This can be used to add a HTTP message header to this object.
|
void |
close()
This is used to close the connection and commit the request.
|
void |
commit()
This is used to write the headers that where given to the
Response . |
WritableByteChannel |
getByteChannel()
Used to write a message body with the
Response . |
WritableByteChannel |
getByteChannel(int size)
Used to write a message body with the
Response . |
int |
getCode()
This represents the status code of the HTTP response.
|
long |
getContentLength()
This is a convenience method that can be used to determine
the length of the message body.
|
ContentType |
getContentType()
This is a convenience method that can be used to determine the
content type of the message body.
|
Cookie |
getCookie(String name)
This returns the
Cookie object stored under the
specified name. |
List<Cookie> |
getCookies()
This returns all
Cookie objects stored under the
specified name. |
long |
getDate(String name)
This can be used to get the value of the first message header
that has the specified name.
|
String |
getDescription()
This can be used to retrieve the text of a HTTP status
line.
|
CharSequence |
getHeader()
This method returns a
CharSequence holding the header
created for the request. |
int |
getInteger(String name)
This can be used to get the value of the first message header
that has the specified name.
|
int |
getMajor()
This can be used to get the major number from a HTTP version.
|
int |
getMinor()
This can be used to get the minor number from a HTTP version.
|
List<String> |
getNames()
This is used to acquire the names of the of the headers that
have been set in the response.
|
OutputStream |
getOutputStream()
Used to write a message body with the
Response . |
OutputStream |
getOutputStream(int size)
Used to write a message body with the
Response . |
PrintStream |
getPrintStream()
This method is provided for convenience so that the HTTP content
can be written using the
print methods provided by
the PrintStream . |
PrintStream |
getPrintStream(int size)
This method is provided for convenience so that the HTTP content
can be written using the
print methods provided by
the PrintStream . |
long |
getResponseTime()
This represents the time at which the response has fully written.
|
Status |
getStatus()
This is used to acquire the status from the response.
|
String |
getTransferEncoding()
This is a convenience method that can be used to determine the
content type of the message body.
|
String |
getValue(String name)
This can be used to get the value of the first message header
that has the specified name.
|
String |
getValue(String name,
int index)
This can be used to get the value of the first message header
that has the specified name.
|
List<String> |
getValues(String name)
This can be used to get the values of HTTP message headers
that have the specified name.
|
boolean |
isCommitted()
This can be used to determine whether the
Response
has been committed. |
boolean |
isKeepAlive()
This is used to determine if the HTTP response message is a
keep alive message or if the underlying socket was closed.
|
void |
reset()
This can be used to determine whether the
Response
has been committed. |
void |
setCode(int code)
This method allows the status for the response to be
changed.
|
void |
setContentLength(long length)
This should be used when the size of the message body is known.
|
void |
setContentType(String type)
This is used to set the content type for the response.
|
Cookie |
setCookie(Cookie cookie)
The
setCookie method is used to set a cookie value
with the cookie name. |
Cookie |
setCookie(String name,
String value)
The
setCookie method is used to set a cookie value
with the cookie name. |
void |
setDate(String name,
long date)
This is used as a convenience method for adding a header that
needs to be parsed into a HTTP date string.
|
void |
setDescription(String text)
This is used to set the text of the HTTP status line.
|
void |
setInteger(String name,
int value)
This can be used to set a HTTP message header to this object.
|
void |
setLong(String name,
long value)
This can be used to set a HTTP message header to this object.
|
void |
setMajor(int major)
This can be used to set the major number from a HTTP version.
|
void |
setMinor(int minor)
This can be used to get the minor number from a HTTP version.
|
void |
setStatus(Status status)
This is used to set the status code and description
for this response.
|
void |
setValue(String name,
String value)
This can be used to set a HTTP message header to this object.
|
String |
toString()
This method returns a string representing the header that was
generated for this header.
|
protected Response response
public ResponseWrapper(Response response)
ResponseWrapper
object. This allows
the original Response
object to be wrapped so that
adjustments to the behavior of a request object handed to the
container can be provided by a subclass implementation.response
- the response object that is being wrappedpublic int getCode()
getCode
in interface StatusLine
public void setCode(int code)
setCode
in interface StatusLine
code
- the new status code for the HTTP responsepublic String getDescription()
getDescription
in interface StatusLine
public void setDescription(String text)
setDescription
in interface StatusLine
text
- the descriptive text message of the statuspublic Status getStatus()
Status
object returns represents the
code that has been set on the response, it does not
necessarily represent the description in the response.getStatus
in interface StatusLine
public void setStatus(Status status)
setStatus
in interface StatusLine
status
- this is the status to set on the responsepublic int getMajor()
getMajor
in interface StatusLine
public void setMajor(int major)
setMajor
in interface StatusLine
major
- the major version number for the request messagepublic int getMinor()
getMinor
in interface StatusLine
public void setMinor(int minor)
setMinor
in interface StatusLine
minor
- the minor version number for the request messagepublic long getResponseTime()
getResponseTime
in interface Response
public List<String> getNames()
getNames
in interface ResponseHeader
public void addValue(String name, String value)
getValue
in combination with the get methods.addValue
in interface ResponseHeader
name
- the name of the HTTP message header to be addedvalue
- the value the HTTP message header will havepublic void addInteger(String name, int value)
getInteger
in combination with the get methods.addInteger
in interface ResponseHeader
name
- the name of the HTTP message header to be addedvalue
- the value the HTTP message header will havepublic void addDate(String name, long date)
addDate
in interface ResponseHeader
name
- the name of the HTTP message header to be addeddate
- the value constructed as an RFC 1123 date stringpublic void setValue(String name, String value)
getValue
in combination with the get methods.
This will perform a remove
using the issued header
name before the header value is set.setValue
in interface ResponseHeader
name
- the name of the HTTP message header to be addedvalue
- the value the HTTP message header will havepublic void setInteger(String name, int value)
getValue
in combination with the get methods.
This will perform a remove
using the issued header
name before the header value is set.setInteger
in interface ResponseHeader
name
- the name of the HTTP message header to be addedvalue
- the value the HTTP message header will havepublic void setLong(String name, long value)
getValue
in combination with the get methods.
This will perform a remove
using the issued header
name before the header value is set.setLong
in interface ResponseHeader
name
- the name of the HTTP message header to be addedvalue
- the value the HTTP message header will havepublic void setDate(String name, long date)
remove
using the issued header
name before the header value is set.setDate
in interface ResponseHeader
name
- the name of the HTTP message header to be addeddate
- the value constructed as an RFC 1123 date stringpublic String getValue(String name)
getValue
in interface ResponseHeader
name
- the HTTP message header to get the value frompublic String getValue(String name, int index)
getValue
in interface ResponseHeader
name
- the HTTP message header to get the value fromindex
- used if there are multiple headers presentpublic int getInteger(String name)
getInteger
in interface ResponseHeader
name
- the HTTP message header to get the value frompublic long getDate(String name)
getDate
in interface ResponseHeader
name
- the HTTP message header to get the value frompublic List<String> getValues(String name)
substring
and trim
calls.
The tokens returned by this method are ordered according to there HTTP quality values, or "q" values, see RFC 2616 section 3.9. This also strips out the quality parameter from tokens returned. So "image/html; q=0.9" results in "image/html". If there are no "q" values present then order is by appearance.
The result from this is either the trimmed header value, that is, the header value with no leading or trailing whitespace or an array of trimmed tokens ordered with the most preferred in the lower indexes, so index 0 is has highest preference.
getValues
in interface ResponseHeader
name
- the name of the headers that are to be retrievedpublic Cookie setCookie(Cookie cookie)
setCookie
method is used to set a cookie value
with the cookie name. This will add a cookie to the response
stored under the name of the cookie, when this is committed it
will be added as a Set-Cookie header to the resulting response.setCookie
in interface ResponseHeader
cookie
- this is the cookie to be added to the responsepublic Cookie setCookie(String name, String value)
setCookie
method is used to set a cookie value
with the cookie name. This will add a cookie to the response
stored under the name of the cookie, when this is committed it
will be added as a Set-Cookie header to the resulting response.
This is a convenience method that avoids cookie creation.setCookie
in interface ResponseHeader
name
- this is the cookie to be added to the responsevalue
- this is the cookie value that is to be usedpublic Cookie getCookie(String name)
Cookie
object stored under the
specified name. This is used to retrieve cookies that have been
set with the setCookie
methods. If the cookie does
not exist under the specified name this will return null.getCookie
in interface ResponseHeader
name
- this is the name of the cookie to be retrievedpublic List<Cookie> getCookies()
Cookie
objects stored under the
specified name. This is used to retrieve cookies that have been
set with the setCookie
methods. If there are no
cookies then this will return an empty list.getCookies
in interface ResponseHeader
public ContentType getContentType()
Content-Type
header, if there is then
this will parse that header and represent it as a typed object
which will expose the various parts of the HTTP header.getContentType
in interface ResponseHeader
public String getTransferEncoding()
Transfer-Encoding
header, if there is
then this will parse that header and return the first token in
the comma separated list of values, which is the primary value.getTransferEncoding
in interface ResponseHeader
public long getContentLength()
Content-Length
header, if it does then the
length can be determined, if not then this returns -1.getContentLength
in interface ResponseHeader
public void setContentLength(long length)
This removes any previous Content-Length headers from the message
header. This will then set the appropriate Content-Length header with
the correct length. If a the Connection header is set with the close
token then the semantics of the connection are such that the server
will close it once the OutputStream.close
is used.
setContentLength
in interface Response
length
- this is the length of the HTTP message bodypublic void setContentType(String type)
setContentType
in interface Response
type
- this is the type that is to be set in the responsepublic CharSequence getHeader()
CharSequence
holding the header
created for the request. A character sequence is returned as it
can provide a much more efficient means of representing the header
data by just wrapping the the data generated.getHeader
in interface ResponseHeader
public OutputStream getOutputStream() throws IOException
Response
. The
semantics of this OutputStream
will be determined
by the HTTP version of the client, and whether or not the content
length has been set, through the setContentLength
method. If the length of the output is not known then the output
is chunked for HTTP/1.1 clients and closed for HTTP/1.0 clients.
The OutputStream
issued must be thread safe so that
it can be used in a concurrent environment.getOutputStream
in interface Response
IOException
- this is thrown if there was an I/O errorpublic OutputStream getOutputStream(int size) throws IOException
Response
. The
semantics of this OutputStream
will be determined
by the HTTP version of the client, and whether or not the content
length has been set, through the setContentLength
method. If the length of the output is not known then the output
is chunked for HTTP/1.1 clients and closed for HTTP/1.0 clients.
The OutputStream
issued must be thread safe so that
it can be used in a concurrent environment.
This will ensure that there is buffering done so that the output
can be reset using the reset
method. This will
enable the specified number of bytes to be written without
committing the response. This specified size is the minimum size
that the response buffer must be.
getOutputStream
in interface Response
size
- the minimum size that the response buffer must beIOException
- this is thrown if there was an I/O errorpublic PrintStream getPrintStream() throws IOException
print
methods provided by
the PrintStream
. This will basically wrap the
getOutputStream
with a buffer size of zero.
The retrieved PrintStream
uses the charset used to
describe the content, with the Content-Type header. This will
check the charset parameter of the contents MIME type. So if
the Content-Type was text/plain; charset=UTF-8
the
resulting PrintStream
would encode the written data
using the UTF-8 encoding scheme. Care must be taken to ensure
that bytes written to the stream are correctly encoded.
Implementations of the Response
must guarantee
that this can be invoked repeatedly without effecting any issued
OutputStream
or PrintStream
object.
getPrintStream
in interface Response
IOException
- this is thrown if there was an I/O errorpublic PrintStream getPrintStream(int size) throws IOException
print
methods provided by
the PrintStream
. This will basically wrap the
getOutputStream
with a specified buffer size.
The retrieved PrintStream
uses the charset used to
describe the content, with the Content-Type header. This will
check the charset parameter of the contents MIME type. So if
the Content-Type was text/plain; charset=UTF-8
the
resulting PrintStream
would encode the written data
using the UTF-8 encoding scheme. Care must be taken to ensure
that bytes written to the stream are correctly encoded.
Implementations of the Response
must guarantee
that this can be invoked repeatedly without effecting any issued
OutputStream
or PrintStream
object.
getPrintStream
in interface Response
size
- the minimum size that the response buffer must beIOException
- this is thrown if there was an I/O errorpublic WritableByteChannel getByteChannel() throws IOException
Response
. The
semantics of this WritableByteChannel
are determined
by the HTTP version of the client, and whether or not the content
length has been set, through the setContentLength
method. If the length of the output is not known then the output
is chunked for HTTP/1.1 clients and closed for HTTP/1.0 clients.getByteChannel
in interface Response
IOException
public WritableByteChannel getByteChannel(int size) throws IOException
Response
. The
semantics of this WritableByteChannel
are determined
by the HTTP version of the client, and whether or not the content
length has been set, through the setContentLength
method. If the length of the output is not known then the output
is chunked for HTTP/1.1 clients and closed for HTTP/1.0 clients.
This will ensure that there is buffering done so that the output
can be reset using the reset
method. This will
enable the specified number of bytes to be written without
committing the response. This specified size is the minimum size
that the response buffer must be.
getByteChannel
in interface Response
size
- the minimum size that the response buffer must beIOException
public boolean isKeepAlive()
Connection
HTTP header, it can also be
implicitly indicated by using version HTTP/1.0.isKeepAlive
in interface Response
public boolean isCommitted()
Response
has been committed. This is true if the Response
was committed, either due to an explicit invocation of the
commit
method or due to the writing of content. If
the Response
has committed the reset
method will not work in resetting content already written.isCommitted
in interface Response
public void commit() throws IOException
Response
. Any further attempts to give headers
to the Response
will be futile as only the headers
that were given at the time of the first commit will be used
in the message header.
This also performs some final checks on the headers submitted. This is done to determine the optimal performance of the output. If no specific Connection header has been specified this will set the connection so that HTTP/1.0 closes by default.
commit
in interface Response
IOException
- thrown if there was a problem writingpublic void reset() throws IOException
Response
has been committed. This is true if the Response
was committed, either due to an explicit invocation of the
commit
method or due to the writing of content. If
the Response
has committed the reset
method will not work in resetting content already written.reset
in interface Response
IOException
- thrown if there is a problem resettingpublic void close() throws IOException
close
in interface Response
IOException
- thrown if there is a problem writingpublic String toString()
toString
in interface ResponseHeader
toString
in class Object
Copyright © 2022. All rights reserved.