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.- Author:
- Niall Gallagher
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidThis can be used to add a HTTP message header to this object.voidThis can be used to add a HTTP message header to this object.voidThis is used as a convenience method for adding a header that needs to be parsed into a HTTPdate string.voidclose()This is used to close the connection and commit the request.voidcommit()This is used to write the headers that where given to theResponse.booleanThis is used to see if there is a HTTP message header with the given name in this container.Used to write a message body with theResponse.getByteChannel(int size) Used to write a message body with theResponse.intgetCode()This represents the status code of the HTTP response.intThis 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 theCookieobject stored under the specified name.This returns allCookieobjects stored under the specified name.longThis can be used to get the value of the first message header that has the specified name.intgetInteger(String name) This can be used to get the value of the first message header that has the specified name.intgetMajor()This can be used to get the major number from a HTTP version.intgetMinor()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 theprintmethods provided by thePrintStream.getPrintStream(int size) This method is provided for convenience so that the HTTP content can be written using theprintmethods provided by thePrintStream.getText()This can be used to retrieve the text of a HTTP status line.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 values of HTTP message headers that have the specified name.booleanThis can be used to determine whether theResponsehas been committed.voidThis is used to remove the named header from the response.voidreset()This can be used to determine whether theResponsehas been committed.voidThis can be used to set a HTTP message header to this object.voidThis can be used to set a HTTP message header to this object.voidsetCode(int code) This method allows the status for the response to be changed.voidsetContentLength(int length) This should be used when the size of the message body is known.ThesetCookiemethod is used to set a cookie value with the cookie name.ThesetCookiemethod is used to set a cookie value with the cookie name.voidThis is used as a convenience method for adding a header that needs to be parsed into a HTTP date string.voidsetMajor(int major) This can be used to set the major number from a HTTP version.voidsetMinor(int minor) This can be used to get the minor number from a HTTP version.voidThis is used to set the text of the HTTP status line.
-
Field Details
-
response
This is the response instance that is being wrapped.
-
-
Constructor Details
-
ResponseWrapper
Constructor forResponseWrapperobject. This allows the originalResponseobject 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:
getCodein 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:
setCodein interfaceStatusLine- Parameters:
code- the new status code for the HTTP response
-
getText
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:
getTextin interfaceStatusLine- Returns:
- the message description of the response
-
setText
This is used to set the text of the HTTP status line. This should match the status code specified by the RFC.- Specified by:
setTextin interfaceStatusLine- Parameters:
text- the descriptive text message of the status
-
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:
getMajorin 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:
setMajorin 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:
getMinorin 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:
setMinorin interfaceStatusLine- Parameters:
minor- the minor version number for the request message
-
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:
getNamesin interfaceResponseHeader- Returns:
- a list of strings representing the set header names
-
add
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 thegetValuein combination with the get methods.- Specified by:
addin interfaceResponseHeader- Parameters:
name- the name of the HTTP message header to be addedvalue- the value the HTTP message header will have
-
add
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 thegetIntegerin combination with the get methods.- Specified by:
addin 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:
addDatein interfaceResponseHeader- Parameters:
name- the name of the HTTP message header to be addeddate- the value constructed as an RFC 1123 date string
-
set
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 thegetValuein combination with the get methods. This will perform aremoveusing the issued header name before the header value is set.- Specified by:
setin interfaceResponseHeader- Parameters:
name- the name of the HTTP message header to be addedvalue- the value the HTTP message header will have
-
set
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 thegetValuein combination with the get methods. This will perform aremoveusing the issued header name before the header value is set.- Specified by:
setin 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 aremoveusing the issued header name before the header value is set.- Specified by:
setDatein interfaceResponseHeader- Parameters:
name- the name of the HTTP message header to be addeddate- the value constructed as an RFC 1123 date string
-
remove
This is used to remove the named header from the response. This removes all header values assigned to the specified name. If it does not exist then this will return without modifying the HTTP response. Headers names removed are case insensitive.- Specified by:
removein interfaceResponseHeader- Parameters:
name- the HTTP message header to remove from the response
-
contains
This is used to see if there is a HTTP message header with the given name in this container. If there is a HTTP message header with the specified name then this returns true otherwise false.- Specified by:
containsin interfaceResponseHeader- Parameters:
name- the HTTP message header to get the value from- Returns:
- this returns true if the HTTP message header exists
-
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:
getValuein interfaceResponseHeader- Parameters:
name- the HTTP message header to get the value from- 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:
getIntegerin 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:
getDatein 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 withsubstringandtrimcalls.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:
getValuesin interfaceResponseHeader- Parameters:
name- the name of the headers that are to be retrieved- Returns:
- ordered list of tokens extracted from the header(s)
-
setCookie
ThesetCookiemethod 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:
setCookiein 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
ThesetCookiemethod 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:
setCookiein 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 theCookieobject stored under the specified name. This is used to retrieve cookies that have been set with thesetCookiemethods. If the cookie does not exist under the specified name this will return null.- Specified by:
getCookiein 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 allCookieobjects stored under the specified name. This is used to retrieve cookies that have been set with thesetCookiemethods. If there are no cookies then this will return an empty list.- Specified by:
getCookiesin 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-Typeheader, 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:
getContentTypein 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-Encodingheader, 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:
getTransferEncodingin interfaceResponseHeader- Returns:
- this returns the transfer encoding value if it exists
-
getContentLength
public int 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-Lengthheader, if it does then the length can be determined, if not then this returns -1.- Specified by:
getContentLengthin interfaceResponseHeader- Returns:
- content length, or -1 if it cannot be determined
-
setContentLength
public void setContentLength(int 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.closeis used.- Specified by:
setContentLengthin interfaceResponse- Parameters:
length- this is the length of the HTTP message body
-
getOutputStream
Used to write a message body with theResponse. The semantics of thisOutputStreamwill be determined by the HTTP version of the client, and whether or not the content length has been set, through thesetContentLengthmethod. 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. TheOutputStreamissued must be thread safe so that it can be used in a concurrent environment.- Specified by:
getOutputStreamin 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 thisOutputStreamwill be determined by the HTTP version of the client, and whether or not the content length has been set, through thesetContentLengthmethod. 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. TheOutputStreamissued 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
resetmethod. 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:
getOutputStreamin 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 theprintmethods provided by thePrintStream. This will basically wrap thegetOutputStreamwith a buffer size of zero.The retrieved
PrintStreamuses 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-8the resultingPrintStreamwould 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
Responsemust guarantee that this can be invoked repeatedly without effecting any issuedOutputStreamorPrintStreamobject.- Specified by:
getPrintStreamin 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 theprintmethods provided by thePrintStream. This will basically wrap thegetOutputStreamwith a specified buffer size.The retrieved
PrintStreamuses 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-8the resultingPrintStreamwould 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
Responsemust guarantee that this can be invoked repeatedly without effecting any issuedOutputStreamorPrintStreamobject.- Specified by:
getPrintStreamin 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 thisWritableByteChannelare determined by the HTTP version of the client, and whether or not the content length has been set, through thesetContentLengthmethod. 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:
getByteChannelin 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 thisWritableByteChannelare determined by the HTTP version of the client, and whether or not the content length has been set, through thesetContentLengthmethod. 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
resetmethod. 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:
getByteChannelin 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
-
isCommitted
public boolean isCommitted()This can be used to determine whether theResponsehas been committed. This is true if theResponsewas committed, either due to an explicit invocation of thecommitmethod or due to the writing of content. If theResponsehas committed theresetmethod will not work in resetting content already written.- Specified by:
isCommittedin 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 theResponsewill 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:
commitin interfaceResponse- Throws:
IOException- thrown if there was a problem writing
-
reset
This can be used to determine whether theResponsehas been committed. This is true if theResponsewas committed, either due to an explicit invocation of thecommitmethod or due to the writing of content. If theResponsehas committed theresetmethod will not work in resetting content already written.- Specified by:
resetin 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:
closein interfaceResponse- Throws:
IOException- thrown if there is a problem writing
-