|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.simpleframework.http.ResponseWrapper
public class ResponseWrapper
The 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| Field Summary | |
|---|---|
protected Response |
response
This is the response instance that is being wrapped. |
| Constructor Summary | |
|---|---|
ResponseWrapper(Response response)
Constructor for ResponseWrapper object. |
|
| Method Summary | |
|---|---|
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. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
protected Response response
| Constructor Detail |
|---|
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 wrapped| Method Detail |
|---|
public int getCode()
getCode in interface StatusLinepublic void setCode(int code)
setCode in interface StatusLinecode - the new status code for the HTTP responsepublic String getDescription()
getDescription in interface StatusLinepublic void setDescription(String text)
setDescription in interface StatusLinetext - 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 StatusLinepublic void setStatus(Status status)
setStatus in interface StatusLinestatus - this is the status to set on the responsepublic int getMajor()
getMajor in interface StatusLinepublic void setMajor(int major)
setMajor in interface StatusLinemajor - the major version number for the request messagepublic int getMinor()
getMinor in interface StatusLinepublic void setMinor(int minor)
setMinor in interface StatusLineminor - the minor version number for the request messagepublic long getResponseTime()
getResponseTime in interface Responsepublic List<String> getNames()
getNames in interface ResponseHeader
public void addValue(String name,
String value)
getValue in combination with the get methods.
addValue in interface ResponseHeadername - the name of the HTTP message header to be addedvalue - the value the HTTP message header will have
public void addInteger(String name,
int value)
getInteger in combination with the get methods.
addInteger in interface ResponseHeadername - the name of the HTTP message header to be addedvalue - the value the HTTP message header will have
public void addDate(String name,
long date)
addDate in interface ResponseHeadername - the name of the HTTP message header to be addeddate - the value constructed as an RFC 1123 date string
public 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 ResponseHeadername - the name of the HTTP message header to be addedvalue - the value the HTTP message header will have
public 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 ResponseHeadername - the name of the HTTP message header to be addedvalue - the value the HTTP message header will have
public 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 ResponseHeadername - the name of the HTTP message header to be addedvalue - the value the HTTP message header will have
public void setDate(String name,
long date)
remove using the issued header
name before the header value is set.
setDate in interface ResponseHeadername - 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 ResponseHeadername - the HTTP message header to get the value from
public String getValue(String name,
int index)
getValue in interface ResponseHeadername - the HTTP message header to get the value fromindex - used if there are multiple headers present
public int getInteger(String name)
getInteger in interface ResponseHeadername - the HTTP message header to get the value from
public long getDate(String name)
getDate in interface ResponseHeadername - the HTTP message header to get the value from
public 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 ResponseHeadername - the name of the headers that are to be retrieved
public 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 ResponseHeadercookie - this is the cookie to be added to the response
public 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 ResponseHeadername - this is the cookie to be added to the responsevalue - this is the cookie value that is to be used
public 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 ResponseHeadername - this is the name of the cookie to be retrieved
public 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 ResponseHeaderpublic 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 ResponseHeaderpublic 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 ResponseHeaderpublic long getContentLength()
Content-Length header, if it does then the
length can be determined, if not then this returns -1.
getContentLength in interface ResponseHeaderpublic 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 Responselength - this is the length of the HTTP message bodypublic void setContentType(String type)
setContentType in interface Responsetype - 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 ResponseIOException - this is thrown if there was an I/O error
public 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 Responsesize - the minimum size that the response buffer must be
IOException - this is thrown if there was an I/O error
public 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 ResponseIOException - this is thrown if there was an I/O error
public 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 Responsesize - the minimum size that the response buffer must be
IOException - this is thrown if there was an I/O error
public 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 ResponseIOException
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 Responsesize - the minimum size that the response buffer must be
IOExceptionpublic boolean isKeepAlive()
Connection HTTP header, it can also be
implicitly indicated by using version HTTP/1.0.
isKeepAlive in interface Responsepublic 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 ResponseIOException - thrown if there was a problem writing
public 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 ResponseIOException - thrown if there is a problem resetting
public void close()
throws IOException
close in interface ResponseIOException - thrown if there is a problem writingpublic String toString()
toString in interface ResponseHeadertoString in class Object
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||