Class ResponseEntity
- java.lang.Object
-
- org.simpleframework.http.message.MessageHeader
-
- org.simpleframework.http.core.ResponseMessage
-
- org.simpleframework.http.core.ResponseEntity
-
- All Implemented Interfaces:
Message,Response,ResponseHeader,StatusLine
class ResponseEntity extends ResponseMessage implements Response
This is used to represent the HTTP response. This provides methods that can be used to set various characteristics of the response. TheOutputStreamof theResponsecan be retrieved from this interface as can the I.P address of the client that will be receiving theResponse. The attributes of the connection can be retrieved also. This provides a set of methods that can be used to set the attributes of the stream so theResponsecan be transported properly. The headers can be set and will be sent once a commit is made, or when there is content sent over the output stream.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
Responsecontrols the processing of the HTTP pipeline. The next HTTP request is not processed until the response has committed. The response is committed once thecommitmethod is invoked if there is NO content body. Committing with a content body is done only if correct content is given. TheOutputStreamacts as a client and commits the response once the specified content has been written to the issuedOutputStream.
-
-
Field Summary
Fields Modifier and Type Field Description private ResponseBufferbufferThis is used to buffer the bytes that are sent to the client.private ChannelchannelThis is the underlying channel for the connected pipeline.private BodyObserverobserverThis is the observer that is used to monitor the response.private ByteWritersenderThis is the sender object used to deliver to response data.private ConversationsupportThis is the conversation used to determine connection type.private TracetraceThis is used to trace events that occur with the response
-
Constructor Summary
Constructors Constructor Description ResponseEntity(BodyObserver observer, Request request, Entity entity)Constructor for theResponseEntityobject.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description 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.java.lang.ObjectgetAttribute(java.lang.Object name)This is used as a shortcut for acquiring attributes for the response.java.util.MapgetAttributes()This can be used to retrieve certain attributes about thisResponse.java.nio.channels.WritableByteChannelgetByteChannel()Used to write a message body with theResponse.java.nio.channels.WritableByteChannelgetByteChannel(int size)Used to write a message body with theResponse.private java.lang.StringgetCharset()This determines the charset forPrintStreamobjects returned from thegetPrintStreammethod.java.io.OutputStreamgetOutputStream()Used to write a message body with theResponse.java.io.OutputStreamgetOutputStream(int size)Used to write a message body with theResponse.java.io.PrintStreamgetPrintStream()This method is provided for convenience so that the HTTP content can be written using theprintmethods provided by thePrintStream.java.io.PrintStreamgetPrintStream(int size)This method is provided for convenience so that the HTTP content can be written using theprintmethods provided by thePrintStream.private java.io.PrintStreamgetPrintStream(int size, java.lang.String charset)This is used to wrap thegetOutputStreamobject in aPrintStream, which will write content using a specified charset.longgetResponseTime()This represents the time at which the response has fully written.booleanisCommitted()This can be used to determine whether theResponsehas been committed.booleanisKeepAlive()This is used to determine if the HTTP response message is a keep alive message or if the underlying socket was closed.voidreset()This can be used to determine whether theResponsehas been committed.voidsetContentLength(long length)This should be used when the size of the message body is known.voidsetContentType(java.lang.String type)This is used to set the content type for the response.-
Methods inherited from class org.simpleframework.http.core.ResponseMessage
getCode, getContentLength, getContentType, getDescription, getHeader, getMajor, getMinor, getStatus, getTransferEncoding, setCode, setDescription, setMajor, setMinor, setStatus, toString
-
Methods inherited from class org.simpleframework.http.message.MessageHeader
addDate, addInteger, addValue, getAll, getCookie, getCookies, getDate, getInteger, getLong, getNames, getValue, getValue, getValues, getValues, setCookie, setCookie, setDate, setInteger, setLong, setValue
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
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
-
-
-
-
Field Detail
-
observer
private BodyObserver observer
This is the observer that is used to monitor the response.
-
buffer
private ResponseBuffer buffer
This is used to buffer the bytes that are sent to the client.
-
support
private Conversation support
This is the conversation used to determine connection type.
-
channel
private Channel channel
This is the underlying channel for the connected pipeline.
-
sender
private ByteWriter sender
This is the sender object used to deliver to response data.
-
trace
private Trace trace
This is used to trace events that occur with the response
-
-
Constructor Detail
-
ResponseEntity
public ResponseEntity(BodyObserver observer, Request request, Entity entity)
Constructor for theResponseEntityobject. This is used to create a response instance using the provided request, entity, and monitor object. To ensure that the response is compatible with client theRequestis used. Also to ensure the next request can be processed the provided monitor is used to signal response events to the server kernel.- Parameters:
observer- this is the observer used to signal eventsrequest- this is the request that was sent by the cliententity- this is the entity that contains the channel
-
-
Method Detail
-
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:
getResponseTimein interfaceResponse- Returns:
- this is the time taken to complete the response
-
getAttribute
public java.lang.Object getAttribute(java.lang.Object name)
This is used as a shortcut for acquiring attributes for the response. This avoids acquiring theAttributesin order to retrieve the attribute directly from that object. The attributes contain data specific to the response.- Parameters:
name- this is the name of the attribute to acquire- Returns:
- this returns the attribute for the specified name
-
getAttributes
public java.util.Map getAttributes()
This can be used to retrieve certain attributes about thisResponse. The attributes contains certain properties about theResponse. For example if this Response goes over a secure line then there may be any arbitrary attributes.- Returns:
- the response attributes of that have been set
-
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.closeis used.- Specified by:
setContentLengthin interfaceResponse- Parameters:
length- this is the length of the HTTP message body
-
setContentType
public 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.- Specified by:
setContentTypein interfaceResponse- Parameters:
type- this is the type that is to be set in the response
-
getCharset
private java.lang.String getCharset()
This determines the charset forPrintStreamobjects returned from thegetPrintStreammethod. This will return a valid charset regardless of whether the Content-Type header has been set, set without a charset, or not set at all. If unspecified, the charset returned isISO-8859-1, as suggested by RFC 2616, section 3.7.1.- Returns:
- returns the charset used by this response object
-
getOutputStream
public java.io.OutputStream getOutputStream() throws java.io.IOExceptionUsed 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.- Specified by:
getOutputStreamin interfaceResponse- Returns:
- an output stream object used to write the message body
- Throws:
java.io.IOException
-
getOutputStream
public java.io.OutputStream getOutputStream(int size) throws java.io.IOExceptionUsed 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.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 object used to write the message body
- Throws:
java.io.IOException
-
getPrintStream
public java.io.PrintStream getPrintStream() throws java.io.IOExceptionThis 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.- Specified by:
getPrintStreamin interfaceResponse- Returns:
- a print stream object used to write the message body
- Throws:
java.io.IOException
-
getPrintStream
public java.io.PrintStream getPrintStream(int size) throws java.io.IOExceptionThis 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.- Specified by:
getPrintStreamin interfaceResponse- Parameters:
size- the minimum size that the response buffer must be- Returns:
- a print stream object used to write the message body
- Throws:
java.io.IOException
-
getPrintStream
private java.io.PrintStream getPrintStream(int size, java.lang.String charset) throws java.io.IOExceptionThis is used to wrap thegetOutputStreamobject in aPrintStream, which will write content using a specified charset. ThePrintStreamcreated will not buffer the content, it will write directly to the underlyingOutputStreamwhere it is buffered (if there is a buffer size greater than zero specified). In future the buffer of thePrintStreammay be usable.- Parameters:
size- the minimum size that the response buffer must becharset- this is the charset used by the resulting stream- Returns:
- a print stream that encodes in the given charset
- Throws:
java.io.IOException
-
getByteChannel
public java.nio.channels.WritableByteChannel getByteChannel() throws java.io.IOExceptionUsed 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:
java.io.IOException
-
getByteChannel
public java.nio.channels.WritableByteChannel getByteChannel(int size) throws java.io.IOExceptionUsed 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:
java.io.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 theConnectionHTTP header, it can also be implicitly indicated by using version HTTP/1.0.- Specified by:
isKeepAlivein interfaceResponse- Returns:
- this returns true if the connection was closed
-
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
public void commit() throws java.io.IOExceptionThis 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.
-
reset
public void reset() throws java.io.IOExceptionThis 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.
-
close
public void close() throws java.io.IOExceptionThis 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.
-
-