org.glassfish.grizzly.http.server
Class Response

java.lang.Object
  extended by org.glassfish.grizzly.http.server.Response

public class Response
extends Object

Wrapper object for the Coyote response.

Version:
$Revision: 1.2 $ $Date: 2006/11/02 20:01:44 $
Author:
Remy Maucherat, Craig R. McClanahan

Nested Class Summary
 class Response.SuspendedContextImpl
           
protected  class Response.SuspendTimeout
           
 
Field Summary
protected  boolean appCommitted
          The application commit flag.
protected  List<Cookie> cookies
          The set of Cookies associated with this Response.
protected  FilterChainContext ctx
          Grizzly FilterChain context, related to this HTTP request/response
protected  DelayedExecutor.DelayQueue<Response.SuspendTimeout> delayQueue
           
protected  boolean error
          The error flag.
protected  SimpleDateFormat format
          The date format we will use for creating date headers.
protected static String info
          Descriptive information about this Response implementation.
protected  OutputBuffer outputBuffer
          The associated output buffer.
protected  CharChunk redirectURLCC
          Recyclable buffer to hold the redirect URL.
protected  Request request
          The request with which this response is associated.
protected  HttpResponsePacket response
          Coyote response.
protected  UEncoder urlEncoder
          URL encoder.
protected  boolean usingOutputStream
          Using output stream flag.
protected  boolean usingWriter
          Using writer flag.
 
Constructor Summary
protected Response()
           
 
Method Summary
 void addCookie(Cookie cookie)
          Add the specified Cookie to those that will be included with this Response.
 void addDateHeader(Header header, long value)
          Add the specified date header to the specified value.
 void addDateHeader(String name, long value)
          Add the specified date header to the specified value.
 void addHeader(Header header, String value)
          Add the specified header to the specified value.
 void addHeader(String name, String value)
          Add the specified header to the specified value.
 void addIntHeader(Header header, int value)
          Add the specified integer header to the specified value.
 void addIntHeader(String name, int value)
          Add the specified integer header to the specified value.
 void cancel()
          Deprecated. pls. use resume()
 boolean containsHeader(Header header)
          Has the specified header been set already in this response?
 boolean containsHeader(String name)
          Has the specified header been set already in this response?
 NIOOutputStream createOutputStream()
          Create and return a ServletOutputStream to write the content associated with this Response.
 String encodeURL(String url)
          Encode the session identifier associated with this response into the specified URL, if necessary.
static String filter(String message)
          Filter the specified message string for characters that are sensitive in HTML.
 void finish()
          Perform whatever actions are required to flush and close the output stream or writer, in a single operation.
 void flush()
          Flush the current buffered content to the network.
 int getBufferSize()
          Return the actual buffer size used for this Response.
 String getCharacterEncoding()
          Return the character encoding used for this Response.
 int getContentLength()
          Return the content length that was set or calculated for this Response.
 long getContentLengthLong()
          Return the content length that was set or calculated for this Response.
 String getContentType()
          Return the content type that was set or calculated for this response, or null if no content type was set.
 Cookie[] getCookies()
          Return an array of all cookies set for this response, or a zero-length array if no cookies have been set.
static Response getCurrent()
           
 String getDetailMessage()
          Gets detail error message.
 String getHeader(String name)
          Return the value for the specified header, or null if this header has not been set.
 String[] getHeaderNames()
          Return an array of all the header names set for this response, or a zero-length array if no headers have been set.
 String[] getHeaderValues(String name)
          Return an array of all the header values associated with the specified header name, or an zero-length array if there are no such header values.
 String getInfo()
          Return descriptive information about this Response implementation and the corresponding version number, in the format <description>/<version>.
 Locale getLocale()
          Return the Locale assigned to this response.
 String getMessage()
          Return the error message that was set with sendError() for this Response.
 NIOOutputStream getNIOOutputStream()
           Return the NIOOutputStream associated with this Response.
 NIOWriter getNIOWriter()
           Return the NIOWriter associated with this Response.
 OutputBuffer getOutputBuffer()
           
 OutputStream getOutputStream()
           Return the OutputStream associated with this Response.
 Request getRequest()
          Return the Request with which this Response is associated.
 HttpResponsePacket getResponse()
          Get the HttpResponsePacket.
 int getStatus()
          Return the HTTP status code associated with this Response.
 SuspendContext getSuspendContext()
          Get the context of the suspended Response.
 Writer getWriter()
           Return the NIOWriter associated with this Response.
 SuspendStatus initialize(Request request, HttpResponsePacket response, FilterChainContext ctx, DelayedExecutor.DelayQueue<Response.SuspendTimeout> delayQueue, HttpServerFilter serverFilter)
           
 boolean isCacheEnabled()
          Is the file cache enabled?
 boolean isCommitted()
          Has the output of this response already been committed?
protected  boolean isEncodeable(String location)
          Return true if the specified URL should be encoded with a session identifier.
 boolean isError()
          Error flag accessor.
 boolean isSendFileEnabled()
           
 boolean isSuspended()
          Return true if that suspend() has been invoked and set to true
protected  void recycle()
          Release all object references, and initialize instance variables, in preparation for reuse of this object.
 void removeSessionCookies()
          Removes any Set-Cookie response headers whose value contains the string "JSESSIONID=" or "JSESSIONIDSSO="
 void reset()
          Clears any data that exists in the buffer as well as the status code and headers.
 void reset(int status, String message)
          Reset this response, and specify the values for the HTTP status code and corresponding message.
 void resetBuffer()
          Reset the data buffer but not any status or header information.
 void resetBuffer(boolean resetWriterStreamFlags)
          Reset the data buffer and the using Writer/Stream flags but not any status or header information.
 void resume()
          Complete the Response and finish/commit it.
 void sendAcknowledgement()
          Send an acknowledgment of a request.
 void sendError(int status)
          Send an error response with the specified status and a default message.
 void sendError(int status, String message)
          Send an error response with the specified status and message.
 void sendRedirect(String location)
          Send a temporary redirect to the specified redirect location URL.
 void setBufferSize(int size)
          Set the buffer size to be used for this Response.
 void setCharacterEncoding(String charset)
           
 void setContentLength(int length)
          Set the content length (in bytes) for this Response.
 void setContentLengthLong(long length)
          Set the content length (in bytes) for this Response.
 void setContentType(String type)
          Set the content type for this Response.
static void setCurrent(Response response)
           
 void setDateHeader(Header header, long value)
          Set the specified date header to the specified value.
 void setDateHeader(String name, long value)
          Set the specified date header to the specified value.
 void setDetailMessage(String message)
          Sets detail error message.
 void setError()
          Set the error flag.
 void setHeader(Header header, String value)
          Set the specified header to the specified value.
 void setHeader(String name, String value)
          Set the specified header to the specified value.
 void setIntHeader(Header header, int value)
          Set the specified integer header to the specified value.
 void setIntHeader(String name, int value)
          Set the specified integer header to the specified value.
 void setLocale(Locale locale)
          Set the Locale that is appropriate for this response, including setting the appropriate character encoding.
 void setStatus(HttpStatus status)
          Set the HTTP status and message to be returned with this response.
 void setStatus(int status)
          Set the HTTP status to be returned with this response.
 void setStatus(int status, String message)
          Set the HTTP status and message to be returned with this response.
 void suspend()
          Suspend the Response.
 void suspend(long timeout, TimeUnit timeunit)
          Deprecated. timeout parameters don't make any sense without CompletionHandler
 void suspend(long timeout, TimeUnit timeunit, CompletionHandler<Response> completionHandler)
          Suspend the Response.
 void suspend(long timeout, TimeUnit timeunit, CompletionHandler<Response> completionHandler, TimeoutHandler timeoutHandler)
          Suspend the Response.
protected  String toAbsolute(String location, boolean normalize)
          Convert (if necessary) and return the absolute URL that represents the resource referenced by this possibly relative URL.
protected  String toEncoded(String url, String sessionId)
          Return the specified URL with the specified session identifier suitably encoded.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

format

protected SimpleDateFormat format
The date format we will use for creating date headers.


info

protected static final String info
Descriptive information about this Response implementation.

See Also:
Constant Field Values

request

protected Request request
The request with which this response is associated.


response

protected HttpResponsePacket response
Coyote response.


ctx

protected FilterChainContext ctx
Grizzly FilterChain context, related to this HTTP request/response


outputBuffer

protected final OutputBuffer outputBuffer
The associated output buffer.


appCommitted

protected boolean appCommitted
The application commit flag.


error

protected boolean error
The error flag.


cookies

protected final List<Cookie> cookies
The set of Cookies associated with this Response.


usingOutputStream

protected boolean usingOutputStream
Using output stream flag.


usingWriter

protected boolean usingWriter
Using writer flag.


urlEncoder

protected final UEncoder urlEncoder
URL encoder.


redirectURLCC

protected final CharChunk redirectURLCC
Recyclable buffer to hold the redirect URL.


delayQueue

protected DelayedExecutor.DelayQueue<Response.SuspendTimeout> delayQueue
Constructor Detail

Response

protected Response()
Method Detail

initialize

public SuspendStatus initialize(Request request,
                                HttpResponsePacket response,
                                FilterChainContext ctx,
                                DelayedExecutor.DelayQueue<Response.SuspendTimeout> delayQueue,
                                HttpServerFilter serverFilter)

getRequest

public Request getRequest()
Return the Request with which this Response is associated.


getResponse

public HttpResponsePacket getResponse()
Get the HttpResponsePacket.


recycle

protected void recycle()
Release all object references, and initialize instance variables, in preparation for reuse of this object.


encodeURL

public String encodeURL(String url)
Encode the session identifier associated with this response into the specified URL, if necessary.

Parameters:
url - URL to be encoded

isEncodeable

protected boolean isEncodeable(String location)
Return true if the specified URL should be encoded with a session identifier. This will be true if all of the following conditions are met:

Parameters:
location - Absolute URL to be validated

getInfo

public String getInfo()
Return descriptive information about this Response implementation and the corresponding version number, in the format <description>/<version>.


setError

public void setError()
Set the error flag.


isError

public boolean isError()
Error flag accessor.


setDetailMessage

public void setDetailMessage(String message)
Sets detail error message.

Parameters:
message - detail error message

getDetailMessage

public String getDetailMessage()
Gets detail error message.

Returns:
the detail error message

finish

public void finish()
Perform whatever actions are required to flush and close the output stream or writer, in a single operation.


getContentLength

public int getContentLength()
Return the content length that was set or calculated for this Response.


getContentLengthLong

public long getContentLengthLong()
Return the content length that was set or calculated for this Response.


getContentType

public String getContentType()
Return the content type that was set or calculated for this response, or null if no content type was set.


getBufferSize

public int getBufferSize()
Return the actual buffer size used for this Response.


getCharacterEncoding

public String getCharacterEncoding()
Return the character encoding used for this Response.


setCharacterEncoding

public void setCharacterEncoding(String charset)

createOutputStream

public NIOOutputStream createOutputStream()
Create and return a ServletOutputStream to write the content associated with this Response.


getNIOOutputStream

public NIOOutputStream getNIOOutputStream()

Return the NIOOutputStream associated with this Response. This NIOOutputStream will write content in a non-blocking manner.

Throws:
IllegalStateException - if getWriter() or getNIOWriter() were already invoked.

getOutputStream

public OutputStream getOutputStream()

Return the OutputStream associated with this Response.

By default the returned NIOOutputStream will work as blocking OutputStream, but it will be possible to call NIOOutputSink.canWrite() or NIOOutputSink.notifyCanWrite(org.glassfish.grizzly.WriteHandler) to avoid blocking.

Returns:
the NIOOutputStream associated with this Response.
Throws:
IllegalStateException - if getWriter() or getNIOWriter() were already invoked.
Since:
2.1.2

getLocale

public Locale getLocale()
Return the Locale assigned to this response.


getWriter

public Writer getWriter()

Return the NIOWriter associated with this Response.

By default the returned NIOWriter will work as blocking Writer, but it will be possible to call NIOOutputSink.canWrite() or NIOOutputSink.notifyCanWrite(org.glassfish.grizzly.WriteHandler) to avoid blocking.

Throws:
IllegalStateException - if getOutputStream() or getNIOOutputStream() were already invoked.

getNIOWriter

public NIOWriter getNIOWriter()

Return the NIOWriter associated with this Response. The NIOWriter will write content in a non-blocking manner.

Returns:
the NIOWriter associated with this Response.
Throws:
IllegalStateException - if getOutputStream() or getNIOOutputStream() were already invoked.
Since:
2.1.2

isCommitted

public boolean isCommitted()
Has the output of this response already been committed?


flush

public void flush()
           throws IOException
Flush the current buffered content to the network.

Throws:
IOException

getOutputBuffer

public OutputBuffer getOutputBuffer()
Returns:
the OutputBuffer associated with this Response.

reset

public void reset()
Clears any data that exists in the buffer as well as the status code and headers.

Throws:
IllegalStateException - if this response has already been committed

resetBuffer

public void resetBuffer()
Reset the data buffer but not any status or header information.

Throws:
IllegalStateException - if the response has already been committed

resetBuffer

public void resetBuffer(boolean resetWriterStreamFlags)
Reset the data buffer and the using Writer/Stream flags but not any status or header information.

Parameters:
resetWriterStreamFlags - true if the internal usingWriter, usingOutputStream, isCharacterEncodingSet flags should also be reset
Throws:
IllegalStateException - if the response has already been committed

setBufferSize

public void setBufferSize(int size)
Set the buffer size to be used for this Response.

Parameters:
size - The new buffer size
Throws:
IllegalStateException - if this method is called after output has been committed for this response

setContentLengthLong

public void setContentLengthLong(long length)
Set the content length (in bytes) for this Response. If the length argument is negative - then HttpPacket content-length value will be reset to -1 and Content-Length header (if present) will be removed.

Parameters:
length - The new content length

setContentLength

public void setContentLength(int length)
Set the content length (in bytes) for this Response. If the length argument is negative - then HttpPacket content-length value will be reset to -1 and Content-Length header (if present) will be removed.

Parameters:
length - The new content length

setContentType

public void setContentType(String type)
Set the content type for this Response.

Parameters:
type - The new content type

setLocale

public void setLocale(Locale locale)
Set the Locale that is appropriate for this response, including setting the appropriate character encoding.

Parameters:
locale - The new locale

getCookies

public Cookie[] getCookies()
Return an array of all cookies set for this response, or a zero-length array if no cookies have been set.


getHeader

public String getHeader(String name)
Return the value for the specified header, or null if this header has not been set. If more than one value was added for this name, only the first is returned; use getHeaderValues() to retrieve all of them.

Parameters:
name - Header name to look up

getHeaderNames

public String[] getHeaderNames()
Return an array of all the header names set for this response, or a zero-length array if no headers have been set.


getHeaderValues

public String[] getHeaderValues(String name)
Return an array of all the header values associated with the specified header name, or an zero-length array if there are no such header values.

Parameters:
name - Header name to look up

getMessage

public String getMessage()
Return the error message that was set with sendError() for this Response.


getStatus

public int getStatus()
Return the HTTP status code associated with this Response.


reset

public void reset(int status,
                  String message)
Reset this response, and specify the values for the HTTP status code and corresponding message.

Throws:
IllegalStateException - if this response has already been committed

addCookie

public void addCookie(Cookie cookie)
Add the specified Cookie to those that will be included with this Response.

Parameters:
cookie - Cookie to be added

removeSessionCookies

public void removeSessionCookies()
Removes any Set-Cookie response headers whose value contains the string "JSESSIONID=" or "JSESSIONIDSSO="


addDateHeader

public void addDateHeader(String name,
                          long value)
Add the specified date header to the specified value.

Parameters:
name - Name of the header to set
value - Date value to be set

addDateHeader

public void addDateHeader(Header header,
                          long value)
Add the specified date header to the specified value.

Parameters:
header - the Header to set
value - Date value to be set
Since:
2.1.2

addHeader

public void addHeader(String name,
                      String value)
Add the specified header to the specified value.

Parameters:
name - Name of the header to set
value - Value to be set

addHeader

public void addHeader(Header header,
                      String value)
Add the specified header to the specified value.

Parameters:
header - the Header to set
value - Value to be set
Since:
2.1.2

addIntHeader

public void addIntHeader(String name,
                         int value)
Add the specified integer header to the specified value.

Parameters:
name - Name of the header to set
value - Integer value to be set

addIntHeader

public void addIntHeader(Header header,
                         int value)
Add the specified integer header to the specified value.

Parameters:
header - the Header to set
value - Integer value to be set
Since:
2.1.2

containsHeader

public boolean containsHeader(String name)
Has the specified header been set already in this response?

Parameters:
name - Name of the header to check

containsHeader

public boolean containsHeader(Header header)
Has the specified header been set already in this response?

Parameters:
header - the Header to check
Since:
2.1.2

sendAcknowledgement

public void sendAcknowledgement()
                         throws IOException
Send an acknowledgment of a request. An acknowledgment in this case is simply an HTTP response status line, i.e. HTTP/1.1 [STATUS] [REASON-PHRASE].

Throws:
IOException - if an input/output error occurs

sendError

public void sendError(int status)
               throws IOException
Send an error response with the specified status and a default message.

Parameters:
status - HTTP status code to send
Throws:
IllegalStateException - if this response has already been committed
IOException - if an input/output error occurs

sendError

public void sendError(int status,
                      String message)
               throws IOException
Send an error response with the specified status and message.

Parameters:
status - HTTP status code to send
message - Corresponding message to send
Throws:
IllegalStateException - if this response has already been committed
IOException - if an input/output error occurs

sendRedirect

public void sendRedirect(String location)
                  throws IOException
Send a temporary redirect to the specified redirect location URL.

Parameters:
location - Location URL to redirect to
Throws:
IllegalStateException - if this response has already been committed
IOException - if an input/output error occurs

setDateHeader

public void setDateHeader(String name,
                          long value)
Set the specified date header to the specified value.

Parameters:
name - Name of the header to set
value - Date value to be set

setDateHeader

public void setDateHeader(Header header,
                          long value)
Set the specified date header to the specified value.

Parameters:
header - the Header to set
value - Date value to be set
Since:
2.1.2

setHeader

public void setHeader(String name,
                      String value)
Set the specified header to the specified value.

Parameters:
name - Name of the header to set
value - Value to be set

setHeader

public void setHeader(Header header,
                      String value)
Set the specified header to the specified value.

Parameters:
header - the Header to set
value - Value to be set
Since:
2.1.2

setIntHeader

public void setIntHeader(String name,
                         int value)
Set the specified integer header to the specified value.

Parameters:
name - Name of the header to set
value - Integer value to be set

setIntHeader

public void setIntHeader(Header header,
                         int value)
Set the specified integer header to the specified value.

Parameters:
header - the Header to set
value - Integer value to be set
Since:
2.1.2

setStatus

public void setStatus(int status)
Set the HTTP status to be returned with this response.

Parameters:
status - The new HTTP status

setStatus

public void setStatus(int status,
                      String message)
Set the HTTP status and message to be returned with this response.

Parameters:
status - The new HTTP status
message - The associated text message

setStatus

public void setStatus(HttpStatus status)
Set the HTTP status and message to be returned with this response.

Parameters:
status - HttpStatus to set

toAbsolute

protected String toAbsolute(String location,
                            boolean normalize)
Convert (if necessary) and return the absolute URL that represents the resource referenced by this possibly relative URL. If this URL is already absolute, return it unchanged.

Parameters:
location - URL to be (possibly) converted and then returned
Throws:
IllegalArgumentException - if a MalformedURLException is thrown when converting the relative URL to an absolute one

filter

public static String filter(String message)
Filter the specified message string for characters that are sensitive in HTML. This avoids potential attacks caused by including JavaScript codes in the request URL that is often reported in error messages.

Parameters:
message - The message string to be filtered

toEncoded

protected String toEncoded(String url,
                           String sessionId)
Return the specified URL with the specified session identifier suitably encoded.

Parameters:
url - URL to be encoded with the session id
sessionId - Session id to be included in the encoded URL

isCacheEnabled

public boolean isCacheEnabled()
Is the file cache enabled?


getSuspendContext

public SuspendContext getSuspendContext()
Get the context of the suspended Response.

Returns:
the context of the suspended Response.

isSuspended

public boolean isSuspended()
Return true if that suspend() has been invoked and set to true

Returns:
true if that suspend() has been invoked and set to true

suspend

public void suspend()
Suspend the Response. Suspending a Response will tell the underlying container to avoid recycling objects associated with the current instance, and also to avoid committing response.


suspend

public void suspend(long timeout,
                    TimeUnit timeunit)
Deprecated. timeout parameters don't make any sense without CompletionHandler

Suspend the Response. Suspending a Response will tell the underlying container to avoid recycling objects associated with the current instance, and also to avoid committing response.

Parameters:
timeout - The maximum amount of time, a Response can be suspended. When the timeout expires (because nothing has been written or because the resume() or cancel()), the Response will be automatically resumed and committed. Usage of any methods of a Response that times out will throw an IllegalStateException.
timeunit - timeout units

suspend

public void suspend(long timeout,
                    TimeUnit timeunit,
                    CompletionHandler<Response> completionHandler)
Suspend the Response. Suspending a Response will tell the underlying container to avoid recycling objects associated with the current instance, and also to avoid committing response. When the resume() is invoked, the container will make sure CompletionHandler.completed(Object) is invoked with the original attachment. When the cancel() is invoked, the container will make sure CompletionHandler.cancelled() is invoked with the original attachment. If the timeout expires, the CompletionHandler.cancelled() is invoked with the original attachment and the Response committed.

Parameters:
timeout - The maximum amount of time the Response can be suspended. When the timeout expires (because nothing has been written or because the resume() or cancel()), the Response will be automatically resumed and committed. Usage of any methods of a Response that times out will throw an IllegalStateException.
timeunit - timeout units
completionHandler - a CompletionHandler

suspend

public void suspend(long timeout,
                    TimeUnit timeunit,
                    CompletionHandler<Response> completionHandler,
                    TimeoutHandler timeoutHandler)
Suspend the Response. Suspending a Response will tell the underlying container to avoid recycling objects associated with the current instance, and also to avoid committing response. When the resume() is invoked, the container will make sure CompletionHandler.completed(Object) is invoked with the original attachment. When the cancel() is invoked, the container will make sure CompletionHandler.cancelled() is invoked with the original attachment. If the timeout expires, the CompletionHandler.cancelled() is invoked with the original attachment and the Response committed.

Parameters:
timeout - The maximum amount of time the Response can be suspended. When the timeout expires (because nothing has been written or because the resume() or cancel()), the Response will be automatically resumed and committed. Usage of any methods of a Response that times out will throw an IllegalStateException.
timeunit - timeout units
completionHandler - a CompletionHandler
timeoutHandler - TimeoutHandler to customize the suspended Response timeout logic.

resume

public void resume()
Complete the Response and finish/commit it. If a CompletionHandler has been defined, its CompletionHandler.completed(Object) will first be invoked, then the finish(). Those operations commit the response.


cancel

public void cancel()
Deprecated. pls. use resume()

Cancel the Response and finish/commit it. If a CompletionHandler has been defined, its CompletionHandler.cancelled() will first be invoked, then the finish(). Those operations commit the response.


getCurrent

public static Response getCurrent()

setCurrent

public static void setCurrent(Response response)

isSendFileEnabled

public boolean isSendFileEnabled()


Copyright © 2012 Oracle Corporation. All Rights Reserved.