Interface Response
-
- All Superinterfaces:
ResponseHeader
,StatusLine
- All Known Implementing Classes:
ResponseWrapper
public interface Response extends ResponseHeader
This is used to represent the HTTP response. This provides methods that can be used to set various characteristics of the response. AnOutputStream
can be acquired via this interface which can be used to write the response body. A buffer size can be specified when acquiring the output stream which allows data to be buffered until it over flows or is flushed explicitly. This buffering allows a partially written response body to be reset.This should never allow the message body be sent if it should not be sent with the headers as of RFC 2616 rules for the presence of a message body. A message body must not be included with a HEAD request or with a 304 or a 204 response. A proper implementation of this will prevent a message body being sent if the response is to a HEAD request of if there is a 304 or 204 response code.
It is important to note that the
Response
controls the processing of the HTTP pipeline. The next HTTP request is not processed until the response has been sent. To ensure that the response is sent theclose
method of the response or the output stream should be used. This will notify the server to dispatch the next request in the pipeline for processing.- Author:
- Niall Gallagher
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description 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
.java.nio.channels.WritableByteChannel
getByteChannel()
Used to write a message body with theResponse
.java.nio.channels.WritableByteChannel
getByteChannel(int size)
Used to write a message body with theResponse
.java.io.OutputStream
getOutputStream()
Used to write a message body with theResponse
.java.io.OutputStream
getOutputStream(int size)
Used to write a message body with theResponse
.java.io.PrintStream
getPrintStream()
This method is provided for convenience so that the HTTP content can be written using theprint
methods provided by thePrintStream
.java.io.PrintStream
getPrintStream(int size)
This method is provided for convenience so that the HTTP content can be written using theprint
methods provided by thePrintStream
.long
getResponseTime()
This represents the time at which the response has fully written.boolean
isCommitted()
This can be used to determine whether theResponse
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 theResponse
has been committed.void
setContentLength(long length)
This should be used when the size of the message body is known.void
setContentType(java.lang.String type)
This is used to set the content type for the response.-
Methods inherited from interface org.simpleframework.http.ResponseHeader
addDate, addInteger, addValue, getContentLength, getContentType, getCookie, getCookies, getDate, getHeader, getInteger, getNames, getTransferEncoding, getValue, getValue, getValues, setCookie, setCookie, setDate, setInteger, setLong, setValue, toString
-
Methods inherited from interface org.simpleframework.http.StatusLine
getCode, getDescription, getMajor, getMinor, getStatus, setCode, setDescription, setMajor, setMinor, setStatus
-
-
-
-
Method Detail
-
setContentLength
void setContentLength(long length)
This should be used when the size of the message body 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 output stream or request is closed.
- Parameters:
length
- this is the length of the HTTP message body
-
setContentType
void setContentType(java.lang.String type)
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.- Parameters:
type
- this is the type that is to be set in the response
-
getOutputStream
java.io.OutputStream getOutputStream() throws java.io.IOException
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.- Returns:
- an output stream object with the specified semantics
- Throws:
java.io.IOException
-
getOutputStream
java.io.OutputStream getOutputStream(int size) throws java.io.IOException
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.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.- Returns:
- an output stream object with the specified semantics
- Throws:
java.io.IOException
-
getPrintStream
java.io.PrintStream getPrintStream() throws java.io.IOException
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.- Returns:
- a print stream that provides convenience writing
- Throws:
java.io.IOException
-
getPrintStream
java.io.PrintStream getPrintStream(int size) throws java.io.IOException
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.- Parameters:
size
- the minimum size that the response buffer must be- Returns:
- a print stream that provides convenience writing
- Throws:
java.io.IOException
-
getByteChannel
java.nio.channels.WritableByteChannel getByteChannel() throws java.io.IOException
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.- Returns:
- a writable byte channel used to write the message body
- Throws:
java.io.IOException
-
getByteChannel
java.nio.channels.WritableByteChannel getByteChannel(int size) throws java.io.IOException
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.- Parameters:
size
- the minimum size that the response buffer must be- Returns:
- a writable byte channel used to write the message body
- Throws:
java.io.IOException
-
getResponseTime
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.- Returns:
- this is the time taken to complete the response
-
isKeepAlive
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.- Returns:
- this returns true if the connection was closed
-
isCommitted
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.- Returns:
- true if the response headers have been committed
-
commit
void commit() throws java.io.IOException
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.
- Throws:
java.io.IOException
- thrown if there was a problem writing
-
reset
void reset() throws java.io.IOException
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.- Throws:
java.io.IOException
- thrown if there is a problem resetting
-
close
void close() throws java.io.IOException
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.- Throws:
java.io.IOException
- thrown if there is a problem writing
-
-