Class ResponseWrapper
- All Implemented Interfaces:
Response
,ResponseHeader
,StatusLine
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.- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
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
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 theResponse
.Used to write a message body with theResponse
.getByteChannel
(int size) Used to write a message body with theResponse
.int
getCode()
This represents the status code of the HTTP response.long
This is a convenience method that can be used to determine the length of the message body.This is a convenience method that can be used to determine the content type of the message body.This returns theCookie
object stored under the specified name.This returns allCookie
objects stored under the specified name.long
This can be used to get the value of the first message header that has the specified name.This can be used to retrieve the text of a HTTP status line.This method returns aCharSequence
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.getNames()
This is used to acquire the names of the of the headers that have been set in the response.Used to write a message body with theResponse
.getOutputStream
(int size) Used to write a message body with theResponse
.This method is provided for convenience so that the HTTP content can be written using theprint
methods provided by thePrintStream
.getPrintStream
(int size) This method is provided for convenience so that the HTTP content can be written using theprint
methods provided by thePrintStream
.long
This represents the time at which the response has fully written.This is used to acquire the status from the response.This is a convenience method that can be used to determine the content type of the message body.This can be used to get the value of the first message header that has the specified name.This can be used to get the value of the first message header that has the specified name.This can be used to get the values of HTTP message headers that have the specified name.boolean
This can be used to determine whether theResponse
has been committed.boolean
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 theResponse
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.ThesetCookie
method is used to set a cookie value with the cookie name.ThesetCookie
method is used to set a cookie value with the cookie name.void
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
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
This is used to set the status code and description for this response.void
This can be used to set a HTTP message header to this object.toString()
This method returns a string representing the header that was generated for this header.
-
Field Details
-
response
This is the response instance that is being wrapped.
-
-
Constructor Details
-
ResponseWrapper
Constructor forResponseWrapper
object. This allows the originalResponse
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.- Parameters:
response
- the response object that is being wrapped
-
-
Method Details
-
getCode
public int getCode()This represents the status code of the HTTP response. The response code represents the type of message that is being sent to the client. For a description of the codes see RFC 2616 section 10, Status Code Definitions.- Specified by:
getCode
in interfaceStatusLine
- Returns:
- the status code that this HTTP response has
-
setCode
public void setCode(int code) This method allows the status for the response to be changed. This MUST be reflected the the response content given to the client. For a description of the codes see RFC 2616 section 10, Status Code Definitions.- Specified by:
setCode
in interfaceStatusLine
- Parameters:
code
- the new status code for the HTTP response
-
getDescription
This can be used to retrieve the text of a HTTP status line. This is the text description for the status code. This should match the status code specified by the RFC.- Specified by:
getDescription
in interfaceStatusLine
- Returns:
- the message description of the response
-
setDescription
This is used to set the text of the HTTP status line. This should match the status code specified by the RFC.- Specified by:
setDescription
in interfaceStatusLine
- Parameters:
text
- the descriptive text message of the status
-
getStatus
This is used to acquire the status from the response. TheStatus
object returns represents the code that has been set on the response, it does not necessarily represent the description in the response.- Specified by:
getStatus
in interfaceStatusLine
- Returns:
- this is the response for this status line
-
setStatus
This is used to set the status code and description for this response. Setting the code and description in this manner provides a much more convenient way to set the response status line details.- Specified by:
setStatus
in interfaceStatusLine
- Parameters:
status
- this is the status to set on the response
-
getMajor
public int getMajor()This can be used to get the major number from a HTTP version. The major version corresponds to the major type that is the 1 of a HTTP/1.0 version string.- Specified by:
getMajor
in interfaceStatusLine
- Returns:
- the major version number for the request message
-
setMajor
public void setMajor(int major) This can be used to set the major number from a HTTP version. The major version corresponds to the major type that is the 1 of a HTTP/1.0 version string.- Specified by:
setMajor
in interfaceStatusLine
- Parameters:
major
- the major version number for the request message
-
getMinor
public int getMinor()This can be used to get the minor number from a HTTP version. The minor version corresponds to the major type that is the 0 of a HTTP/1.0 version string. This is used to determine if the request message has keep alive semantics.- Specified by:
getMinor
in interfaceStatusLine
- Returns:
- the minor version number for the request message
-
setMinor
public void setMinor(int minor) This can be used to get the minor number from a HTTP version. The minor version corresponds to the major type that is the 0 of a HTTP/1.0 version string. This is used to determine if the request message has keep alive semantics.- Specified by:
setMinor
in interfaceStatusLine
- Parameters:
minor
- the minor version number for the request message
-
getResponseTime
public long getResponseTime()This represents the time at which the response has fully written. Because the response is delivered asynchronously to the client this response time does not represent the time to last byte. It simply represents the time at which the response has been fully generated and written to the output buffer or queue. This returns zero if the response has not finished.- Specified by:
getResponseTime
in interfaceResponse
- Returns:
- this is the time taken to complete the response
-
getNames
This is used to acquire the names of the of the headers that have been set in the response. This can be used to acquire all header values by name that have been set within the response. If no headers have been set this will return an empty list.- Specified by:
getNames
in interfaceResponseHeader
- Returns:
- a list of strings representing the set header names
-
addValue
This can be used to add a HTTP message header to this object. The name and value of the HTTP message header will be used to create a HTTP message header object which can be retrieved using thegetValue
in combination with the get methods.- Specified by:
addValue
in interfaceResponseHeader
- Parameters:
name
- the name of the HTTP message header to be addedvalue
- the value the HTTP message header will have
-
addInteger
This can be used to add a HTTP message header to this object. The name and value of the HTTP message header will be used to create a HTTP message header object which can be retrieved using thegetInteger
in combination with the get methods.- Specified by:
addInteger
in interfaceResponseHeader
- Parameters:
name
- the name of the HTTP message header to be addedvalue
- the value the HTTP message header will have
-
addDate
This is used as a convenience method for adding a header that needs to be parsed into a HTTPdate string. This will convert the date given into a date string defined in RFC 2616 sec 3.3.1.- Specified by:
addDate
in interfaceResponseHeader
- Parameters:
name
- the name of the HTTP message header to be addeddate
- the value constructed as an RFC 1123 date string
-
setValue
This can be used to set a HTTP message header to this object. The name and value of the HTTP message header will be used to create a HTTP message header object which can be retrieved using thegetValue
in combination with the get methods. This will perform aremove
using the issued header name before the header value is set.- Specified by:
setValue
in interfaceResponseHeader
- Parameters:
name
- the name of the HTTP message header to be addedvalue
- the value the HTTP message header will have
-
setInteger
This can be used to set a HTTP message header to this object. The name and value of the HTTP message header will be used to create a HTTP message header object which can be retrieved using thegetValue
in combination with the get methods. This will perform aremove
using the issued header name before the header value is set.- Specified by:
setInteger
in interfaceResponseHeader
- Parameters:
name
- the name of the HTTP message header to be addedvalue
- the value the HTTP message header will have
-
setLong
This can be used to set a HTTP message header to this object. The name and value of the HTTP message header will be used to create a HTTP message header object which can be retrieved using thegetValue
in combination with the get methods. This will perform aremove
using the issued header name before the header value is set.- Specified by:
setLong
in interfaceResponseHeader
- Parameters:
name
- the name of the HTTP message header to be addedvalue
- the value the HTTP message header will have
-
setDate
This is used as a convenience method for adding a header that needs to be parsed into a HTTP date string. This will convert the date given into a date string defined in RFC 2616 sec 3.3.1. This will perform aremove
using the issued header name before the header value is set.- Specified by:
setDate
in interfaceResponseHeader
- Parameters:
name
- the name of the HTTP message header to be addeddate
- the value constructed as an RFC 1123 date string
-
getValue
This can be used to get the value of the first message header that has the specified name. This will return the full string representing the named header value. If the named header does not exist then this will return a null value.- Specified by:
getValue
in interfaceResponseHeader
- Parameters:
name
- the HTTP message header to get the value from- Returns:
- this returns the value that the HTTP message header
-
getValue
This can be used to get the value of the first message header that has the specified name. This will return the full string representing the named header value. If the named header does not exist then this will return a null value.- Specified by:
getValue
in interfaceResponseHeader
- Parameters:
name
- the HTTP message header to get the value fromindex
- used if there are multiple headers present- Returns:
- this returns the value that the HTTP message header
-
getInteger
This can be used to get the value of the first message header that has the specified name. This will return the integer representing the named header value. If the named header does not exist then this will return a value of minus one, -1.- Specified by:
getInteger
in interfaceResponseHeader
- Parameters:
name
- the HTTP message header to get the value from- Returns:
- this returns the value that the HTTP message header
-
getDate
This can be used to get the value of the first message header that has the specified name. This will return the long value representing the named header value. If the named header does not exist then this will return a value of minus one, -1.- Specified by:
getDate
in interfaceResponseHeader
- Parameters:
name
- the HTTP message header to get the value from- Returns:
- this returns the value that the HTTP message header
-
getValues
This can be used to get the values of HTTP message headers that have the specified name. This is a convenience method that will present that values as tokens extracted from the header. This has obvious performance benefits as it avoids having to deal withsubstring
andtrim
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.
- Specified by:
getValues
in interfaceResponseHeader
- Parameters:
name
- the name of the headers that are to be retrieved- Returns:
- ordered list of tokens extracted from the header(s)
-
setCookie
ThesetCookie
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.- Specified by:
setCookie
in interfaceResponseHeader
- Parameters:
cookie
- this is the cookie to be added to the response- Returns:
- returns the cookie that has been set in the response
-
setCookie
ThesetCookie
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.- Specified by:
setCookie
in interfaceResponseHeader
- Parameters:
name
- this is the cookie to be added to the responsevalue
- this is the cookie value that is to be used- Returns:
- returns the cookie that has been set in the response
-
getCookie
This returns theCookie
object stored under the specified name. This is used to retrieve cookies that have been set with thesetCookie
methods. If the cookie does not exist under the specified name this will return null.- Specified by:
getCookie
in interfaceResponseHeader
- Parameters:
name
- this is the name of the cookie to be retrieved- Returns:
- returns the cookie object send with the request
-
getCookies
This returns allCookie
objects stored under the specified name. This is used to retrieve cookies that have been set with thesetCookie
methods. If there are no cookies then this will return an empty list.- Specified by:
getCookies
in interfaceResponseHeader
- Returns:
- returns all the cookie objects for this response
-
getContentType
This is a convenience method that can be used to determine the content type of the message body. This will determine whether there is aContent-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.- Specified by:
getContentType
in interfaceResponseHeader
- Returns:
- this returns the content type value if it exists
-
getTransferEncoding
This is a convenience method that can be used to determine the content type of the message body. This will determine whether there is aTransfer-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.- Specified by:
getTransferEncoding
in interfaceResponseHeader
- Returns:
- this returns the transfer encoding value if it exists
-
getContentLength
public long getContentLength()This is a convenience method that can be used to determine the length of the message body. This will determine if there is aContent-Length
header, if it does then the length can be determined, if not then this returns -1.- Specified by:
getContentLength
in interfaceResponseHeader
- Returns:
- content length, or -1 if it cannot be determined
-
setContentLength
public void setContentLength(long length) This should be used when the size of the message body is known. For performance reasons this should be used so the length of the output is known. This ensures that Persistent HTTP (PHTTP) connections can be maintained for both HTTP/1.0 and HTTP/1.1 clients. If the length of the output is not known HTTP/1.0 clients will require a connection close, which reduces performance (see RFC 2616).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.- Specified by:
setContentLength
in interfaceResponse
- Parameters:
length
- this is the length of the HTTP message body
-
setContentType
This is used to set the content type for the response. Typically a response will contain a message body of some sort. This is used to conveniently set the type for that response. Setting the content type can also be done explicitly if desired.- Specified by:
setContentType
in interfaceResponse
- Parameters:
type
- this is the type that is to be set in the response
-
getHeader
This method returns aCharSequence
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.- Specified by:
getHeader
in interfaceResponseHeader
- Returns:
- this returns the characters generated for the header
-
getOutputStream
Used to write a message body with theResponse
. The semantics of thisOutputStream
will be determined by the HTTP version of the client, and whether or not the content length has been set, through thesetContentLength
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. TheOutputStream
issued must be thread safe so that it can be used in a concurrent environment.- Specified by:
getOutputStream
in interfaceResponse
- Returns:
- an output stream used to write the response body
- Throws:
IOException
- this is thrown if there was an I/O error
-
getOutputStream
Used to write a message body with theResponse
. The semantics of thisOutputStream
will be determined by the HTTP version of the client, and whether or not the content length has been set, through thesetContentLength
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. TheOutputStream
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.- Specified by:
getOutputStream
in interfaceResponse
- Parameters:
size
- the minimum size that the response buffer must be- Returns:
- an output stream used to write the response body
- Throws:
IOException
- this is thrown if there was an I/O error
-
getPrintStream
This method is provided for convenience so that the HTTP content can be written using theprint
methods provided by thePrintStream
. This will basically wrap thegetOutputStream
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 wastext/plain; charset=UTF-8
the resultingPrintStream
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 issuedOutputStream
orPrintStream
object.- Specified by:
getPrintStream
in interfaceResponse
- Returns:
- a print stream used for writing the response body
- Throws:
IOException
- this is thrown if there was an I/O error
-
getPrintStream
This method is provided for convenience so that the HTTP content can be written using theprint
methods provided by thePrintStream
. This will basically wrap thegetOutputStream
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 wastext/plain; charset=UTF-8
the resultingPrintStream
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 issuedOutputStream
orPrintStream
object.- Specified by:
getPrintStream
in interfaceResponse
- Parameters:
size
- the minimum size that the response buffer must be- Returns:
- a print stream used for writing the response body
- Throws:
IOException
- this is thrown if there was an I/O error
-
getByteChannel
Used to write a message body with theResponse
. The semantics of thisWritableByteChannel
are determined by the HTTP version of the client, and whether or not the content length has been set, through thesetContentLength
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.- Specified by:
getByteChannel
in interfaceResponse
- Returns:
- a writable byte channel used to write the message body
- Throws:
IOException
-
getByteChannel
Used to write a message body with theResponse
. The semantics of thisWritableByteChannel
are determined by the HTTP version of the client, and whether or not the content length has been set, through thesetContentLength
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.- Specified by:
getByteChannel
in interfaceResponse
- Parameters:
size
- the minimum size that the response buffer must be- Returns:
- a writable byte channel used to write the message body
- Throws:
IOException
-
isKeepAlive
public boolean isKeepAlive()This is used to determine if the HTTP response message is a keep alive message or if the underlying socket was closed. Even if the client requests a connection keep alive and supports persistent connections, the response can still be closed by the server. This can be explicitly indicated by the presence of theConnection
HTTP header, it can also be implicitly indicated by using version HTTP/1.0.- Specified by:
isKeepAlive
in interfaceResponse
- Returns:
- this returns true if the connection was closed
-
isCommitted
public boolean isCommitted()This can be used to determine whether theResponse
has been committed. This is true if theResponse
was committed, either due to an explicit invocation of thecommit
method or due to the writing of content. If theResponse
has committed thereset
method will not work in resetting content already written.- Specified by:
isCommitted
in interfaceResponse
- Returns:
- true if the response has been fully committed
-
commit
This is used to write the headers that where given to theResponse
. Any further attempts to give headers to theResponse
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.
- Specified by:
commit
in interfaceResponse
- Throws:
IOException
- thrown if there was a problem writing
-
reset
This can be used to determine whether theResponse
has been committed. This is true if theResponse
was committed, either due to an explicit invocation of thecommit
method or due to the writing of content. If theResponse
has committed thereset
method will not work in resetting content already written.- Specified by:
reset
in interfaceResponse
- Throws:
IOException
- thrown if there is a problem resetting
-
close
This is used to close the connection and commit the request. This provides the same semantics as closing the output stream and ensures that the HTTP response is committed. This will throw an exception if the response can not be committed.- Specified by:
close
in interfaceResponse
- Throws:
IOException
- thrown if there is a problem writing
-
toString
This method returns a string representing the header that was generated for this header. For performance reasons it is better to acquire the character sequence representing the header as it does not require the allocation on new memory.- Specified by:
toString
in interfaceResponseHeader
- Overrides:
toString
in classObject
- Returns:
- this returns a string representation of this response
-