public class ResponseWrapper extends java.lang.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 |
add(java.lang.String name,
int value)
This can be used to add a HTTP message header to this object.
|
void |
add(java.lang.String name,
java.lang.String value)
This can be used to add a HTTP message header to this object.
|
void |
addDate(java.lang.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 |
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 . |
boolean |
contains(java.lang.String name)
This is used to see if there is a HTTP message header with the
given name in this container.
|
java.nio.channels.WritableByteChannel |
getByteChannel()
Used to write a message body with the
Response . |
java.nio.channels.WritableByteChannel |
getByteChannel(int size)
Used to write a message body with the
Response . |
int |
getCode()
This represents the status code of the HTTP response.
|
int |
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(java.lang.String name)
This returns the
Cookie object stored under the
specified name. |
java.util.List<Cookie> |
getCookies()
This returns all
Cookie objects stored under the
specified name. |
long |
getDate(java.lang.String name)
This can be used to get the value of the first message header
that has the specified name.
|
int |
getInteger(java.lang.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.
|
java.util.List<java.lang.String> |
getNames()
This is used to acquire the names of the of the headers that
have been set in the response.
|
java.io.OutputStream |
getOutputStream()
Used to write a message body with the
Response . |
java.io.OutputStream |
getOutputStream(int size)
Used to write a message body with the
Response . |
java.io.PrintStream |
getPrintStream()
This method is provided for convenience so that the HTTP content
can be written using the
print methods provided by
the PrintStream . |
java.io.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 . |
java.lang.String |
getText()
This can be used to retrieve the text of a HTTP status
line.
|
java.lang.String |
getTransferEncoding()
This is a convenience method that can be used to determine the
content type of the message body.
|
java.lang.String |
getValue(java.lang.String name)
This can be used to get the value of the first message header
that has the specified name.
|
java.util.List<java.lang.String> |
getValues(java.lang.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. |
void |
remove(java.lang.String name)
This is used to remove the named header from the response.
|
void |
reset()
This can be used to determine whether the
Response
has been committed. |
void |
set(java.lang.String name,
int value)
This can be used to set a HTTP message header to this object.
|
void |
set(java.lang.String name,
java.lang.String value)
This can be used to set a HTTP message header to this object.
|
void |
setCode(int code)
This method allows the status for the response to be
changed.
|
void |
setContentLength(int length)
This should be used when the size of the message body is known.
|
Cookie |
setCookie(Cookie cookie)
The
setCookie method is used to set a cookie value
with the cookie name. |
Cookie |
setCookie(java.lang.String name,
java.lang.String value)
The
setCookie method is used to set a cookie value
with the cookie name. |
void |
setDate(java.lang.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 |
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 |
setText(java.lang.String text)
This is used to set the text of the HTTP status line.
|
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 java.lang.String getText()
getText
in interface StatusLine
public void setText(java.lang.String text)
setText
in interface StatusLine
text
- the descriptive text message of the statuspublic 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 java.util.List<java.lang.String> getNames()
getNames
in interface ResponseHeader
public void add(java.lang.String name, java.lang.String value)
getValue
in combination with the get methods.add
in interface ResponseHeader
name
- the name of the HTTP message header to be addedvalue
- the value the HTTP message header will havepublic void add(java.lang.String name, int value)
getInteger
in combination with the get methods.add
in interface ResponseHeader
name
- the name of the HTTP message header to be addedvalue
- the value the HTTP message header will havepublic void addDate(java.lang.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 set(java.lang.String name, java.lang.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.set
in interface ResponseHeader
name
- the name of the HTTP message header to be addedvalue
- the value the HTTP message header will havepublic void set(java.lang.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.set
in interface ResponseHeader
name
- the name of the HTTP message header to be addedvalue
- the value the HTTP message header will havepublic void setDate(java.lang.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 void remove(java.lang.String name)
remove
in interface ResponseHeader
name
- the HTTP message header to remove from the responsepublic boolean contains(java.lang.String name)
contains
in interface ResponseHeader
name
- the HTTP message header to get the value frompublic java.lang.String getValue(java.lang.String name)
getValue
in interface ResponseHeader
name
- the HTTP message header to get the value frompublic int getInteger(java.lang.String name)
getInteger
in interface ResponseHeader
name
- the HTTP message header to get the value frompublic long getDate(java.lang.String name)
getDate
in interface ResponseHeader
name
- the HTTP message header to get the value frompublic java.util.List<java.lang.String> getValues(java.lang.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(java.lang.String name, java.lang.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(java.lang.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 java.util.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 java.lang.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 int 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(int 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 java.io.OutputStream getOutputStream() throws java.io.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
java.io.IOException
- this is thrown if there was an I/O errorpublic java.io.OutputStream getOutputStream(int size) throws java.io.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 bejava.io.IOException
- this is thrown if there was an I/O errorpublic java.io.PrintStream getPrintStream() throws java.io.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
java.io.IOException
- this is thrown if there was an I/O errorpublic java.io.PrintStream getPrintStream(int size) throws java.io.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 bejava.io.IOException
- this is thrown if there was an I/O errorpublic java.nio.channels.WritableByteChannel getByteChannel() throws java.io.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
java.io.IOException
public java.nio.channels.WritableByteChannel getByteChannel(int size) throws java.io.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 bejava.io.IOException
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 java.io.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.
public void reset() throws java.io.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.public void close() throws java.io.IOException