com.caucho.server.http
Class Response

java.lang.Object
  |
  +--com.caucho.server.http.Response
All Implemented Interfaces:
CauchoResponse, HttpServletResponse, ServletResponse
Direct Known Subclasses:
HttpResponse, RunnerResponse

public abstract class Response
extends java.lang.Object
implements CauchoResponse

Encapsulates the servlet response, controlling response headers and the response stream.


Field Summary
protected  boolean allowCache
           
protected  boolean allowKeepalive
           
protected  QDate calendar
           
protected  CharBuffer cb
           
protected  java.lang.String charEncoding
           
protected  long contentLength
           
protected  java.lang.String contentPrefix
           
protected  java.lang.String contentType
           
protected  java.util.ArrayList cookiesOut
           
protected  boolean disableCaching
           
protected  boolean disableHeaders
           
protected  java.lang.String filter
           
protected  boolean forbidForward
           
protected  boolean hasError
           
protected  boolean hasMsieHack
           
protected  boolean hasOutputStream
           
protected  boolean hasSentLog
           
protected  boolean hasWriter
           
protected  java.util.ArrayList headerKeys
           
protected  java.util.ArrayList headerValues
           
protected  boolean isChaining
           
protected  boolean isClosed
           
protected  boolean isNoCache
           
protected  boolean isTopCache
           
protected  AbstractRequest request
           
protected  int statusCode
           
protected  java.lang.String statusMessage
           
 
Fields inherited from interface javax.servlet.http.HttpServletResponse
SC_ACCEPTED, SC_BAD_GATEWAY, SC_BAD_REQUEST, SC_CONFLICT, SC_CONTINUE, SC_CREATED, SC_EXPECTATION_FAILED, SC_FORBIDDEN, SC_GATEWAY_TIMEOUT, SC_GONE, SC_HTTP_VERSION_NOT_SUPPORTED, SC_INTERNAL_SERVER_ERROR, SC_LENGTH_REQUIRED, SC_METHOD_NOT_ALLOWED, SC_MOVED_PERMANENTLY, SC_MOVED_TEMPORARILY, SC_MULTIPLE_CHOICES, SC_NO_CONTENT, SC_NON_AUTHORITATIVE_INFORMATION, SC_NOT_ACCEPTABLE, SC_NOT_FOUND, SC_NOT_IMPLEMENTED, SC_NOT_MODIFIED, SC_OK, SC_PARTIAL_CONTENT, SC_PAYMENT_REQUIRED, SC_PRECONDITION_FAILED, SC_PROXY_AUTHENTICATION_REQUIRED, SC_REQUEST_ENTITY_TOO_LARGE, SC_REQUEST_TIMEOUT, SC_REQUEST_URI_TOO_LONG, SC_REQUESTED_RANGE_NOT_SATISFIABLE, SC_RESET_CONTENT, SC_SEE_OTHER, SC_SERVICE_UNAVAILABLE, SC_SWITCHING_PROTOCOLS, SC_UNAUTHORIZED, SC_UNSUPPORTED_MEDIA_TYPE, SC_USE_PROXY
 
Constructor Summary
protected Response()
           
 
Method Summary
 void addCookie(Cookie cookie)
          Adds a cookie to the response.
 void addDateHeader(java.lang.String key, long value)
          Convenience for adding a date header.
 void addHeader(java.lang.String key, java.lang.String value)
          Adds a new header.
 void addIntHeader(java.lang.String key, int value)
          Convenience for adding an integer header.
 void clearBuffer()
           
 void close()
          Closes the request.
 boolean containsHeader(java.lang.String name)
          Returns true if the response already contains the named header.
 boolean disableCaching(boolean disable)
           
 boolean disableHeaders(boolean disable)
           
 java.lang.String encodeRedirectUrl(java.lang.String string)
          Deprecated.  
 java.lang.String encodeRedirectURL(java.lang.String string)
          Encodes the specified URL for use in the sendRedirect method or, if encoding is not needed, returns the URL unchanged.
 java.lang.String encodeUrl(java.lang.String string)
          Deprecated.  
 java.lang.String encodeURL(java.lang.String string)
          Encode the URL with the session jd.
 boolean fillCookie(CharBuffer cb, Cookie cookie, long date, int version)
          Fills the response for a cookie
 void flushBuffer()
          Forces any content in the buffer to be written to the client.
 int getBufferSize()
          Returns the actual buffer size used for the response.
 java.lang.String getChain()
           
 java.lang.String getCharacterEncoding()
          Returns the name of the charset used for the MIME body sent in this response.
 int getContentLength()
          Returns the number of bytes send to the output.
 Cookie getCookie(java.lang.String name)
           
 java.util.ArrayList getCookies()
           
 boolean getForbidForward()
          Returns true if RequestDispatcher.forward() is disallowed on this stream.
 java.lang.String getHeader(java.lang.String name)
          Returns the value of an already set output header.
 java.util.Locale getLocale()
          Returns the locale assigned to the response.
 WriteStream getOriginalStream()
           
 ServletOutputStream getOutputStream()
          Returns the ServletOutputStream for the response.
 int getRemaining()
           
 int getStatusCode()
           
 WriteStream getStream()
          Returns the underlying WriteStream beneath the request.
 java.io.PrintWriter getWriter()
          Returns a PrintWriter for the response.
 boolean hasError()
          Returns true if we're processing an error.
protected  void initChaining()
           
 boolean isCommitted()
          Returns true if some data has been sent to the browser.
 void killCache()
          Set if the page is non-cacheable.
 void removeHeader(java.lang.String key)
           
 void reset()
          Clears any data that exists in the buffer as well as the status code and headers.
 void resetBuffer()
          Clears the content of the underlying buffer in the response without clearing headers or status code.
 void sendError(int code)
          Sends an error response to the client using the specified status code and clearing the buffer.
 void sendError(int code, java.lang.String value)
          Sends an HTTP error to the browser.
 void sendRedirect(java.lang.String url)
          Sends a redirect to the browser.
 void setBufferSize(int size)
          Sets the preferred buffer size for the body of the response.
 void setContentLength(int length)
          Sets the content length of the result.
 void setContentType(java.lang.String value)
          Sets the browser content type.
 void setDateHeader(java.lang.String name, long value)
          Convenience for setting a date header.
 void setForbidForward(boolean forbid)
          When set to true, RequestDispatcher.forward() is disallowed on this stream.
 void setHasError(boolean hasError)
          Set to true while processing an error.
 void setHeader(java.lang.String key, java.lang.String value)
          Sets a header, replacing an already-existing header.
 void setIgnoreClientDisconnect(boolean ignore)
          If set true, client disconnect exceptions are no propagated to the server code.
 void setIntHeader(java.lang.String name, int value)
          Convenience for setting an integer header.
 void setLocale(java.util.Locale locale)
          Sets the locale of the response, setting the headers (including the Content-Type's charset) as appropriate.
 void setNoCache(boolean isNoCache)
          Set if the page is non-cacheable.
 void setSessionId(java.lang.String id)
           
 void setStatus(int code)
          Sets the status code for this response.
 void setStatus(int code, java.lang.String message)
          Deprecated.  
 void setStream(WriteStream os)
          Sets the underlying WriteStream beneath the request.
 TempBuffer startChaining()
          Fix up the stream and headers for the next servlet in the chain.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

request

protected AbstractRequest request

statusCode

protected int statusCode

statusMessage

protected java.lang.String statusMessage

contentType

protected java.lang.String contentType

contentPrefix

protected java.lang.String contentPrefix

charEncoding

protected java.lang.String charEncoding

filter

protected java.lang.String filter

headerKeys

protected java.util.ArrayList headerKeys

headerValues

protected java.util.ArrayList headerValues

cookiesOut

protected java.util.ArrayList cookiesOut

calendar

protected QDate calendar

cb

protected CharBuffer cb

allowKeepalive

protected boolean allowKeepalive

disableHeaders

protected boolean disableHeaders

disableCaching

protected boolean disableCaching

contentLength

protected long contentLength

isChaining

protected boolean isChaining

isClosed

protected boolean isClosed

hasSentLog

protected boolean hasSentLog

hasWriter

protected boolean hasWriter

hasOutputStream

protected boolean hasOutputStream

hasMsieHack

protected boolean hasMsieHack

isNoCache

protected boolean isNoCache

allowCache

protected boolean allowCache

forbidForward

protected boolean forbidForward

hasError

protected boolean hasError

isTopCache

protected boolean isTopCache
Constructor Detail

Response

protected Response()
Method Detail

setIgnoreClientDisconnect

public void setIgnoreClientDisconnect(boolean ignore)
If set true, client disconnect exceptions are no propagated to the server code.

close

public void close()
           throws java.io.IOException
Closes the request.
Specified by:
close in interface CauchoResponse

setForbidForward

public void setForbidForward(boolean forbid)
When set to true, RequestDispatcher.forward() is disallowed on this stream.
Specified by:
setForbidForward in interface CauchoResponse

getForbidForward

public boolean getForbidForward()
Returns true if RequestDispatcher.forward() is disallowed on this stream.
Specified by:
getForbidForward in interface CauchoResponse

setHasError

public void setHasError(boolean hasError)
Set to true while processing an error.
Specified by:
setHasError in interface CauchoResponse

hasError

public boolean hasError()
Returns true if we're processing an error.
Specified by:
hasError in interface CauchoResponse

getChain

public java.lang.String getChain()
Specified by:
getChain in interface CauchoResponse

startChaining

public TempBuffer startChaining()
                         throws java.io.IOException
Fix up the stream and headers for the next servlet in the chain. The same Response object is used for both.
Parameters:
headers - new headers for the next request.
Returns:
a buffer containing the result of the previous servlet.

setStatus

public void setStatus(int code,
                      java.lang.String message)
Deprecated.  

Specified by:
setStatus in interface HttpServletResponse
Following copied from interface: javax.servlet.http.HttpServletResponse
Parameters:
sc - the status code
sm - the status message

setStatus

public void setStatus(int code)
Description copied from interface: HttpServletResponse
Sets the status code for this response. This method is used to set the return status code when there is no error (for example, for the status codes SC_OK or SC_MOVED_TEMPORARILY). If there is an error, and the caller wishes to invoke an defined in the web applicaion, the sendError method should be used instead.

The container clears the buffer and sets the Location header, preserving cookies and other headers.

Specified by:
setStatus in interface HttpServletResponse
Following copied from interface: javax.servlet.http.HttpServletResponse
Parameters:
sc - the status code
See Also:
HttpServletResponse.sendError(int, java.lang.String)

getStatusCode

public int getStatusCode()

sendError

public void sendError(int code,
                      java.lang.String value)
               throws java.io.IOException
Sends an HTTP error to the browser.
Specified by:
sendError in interface HttpServletResponse
Parameters:
code - the HTTP error code
value - a string message

sendError

public void sendError(int code)
               throws java.io.IOException
Description copied from interface: HttpServletResponse
Sends an error response to the client using the specified status code and clearing the buffer.

If the response has already been committed, this method throws an IllegalStateException. After using this method, the response should be considered to be committed and should not be written to.

Specified by:
sendError in interface HttpServletResponse
Following copied from interface: javax.servlet.http.HttpServletResponse
Parameters:
sc - the error status code
Throws:
java.io.IOException - If an input or output exception occurs
java.lang.IllegalStateException - If the response was committed before this method call

sendRedirect

public void sendRedirect(java.lang.String url)
                  throws java.io.IOException
Sends a redirect to the browser. If the URL is relative, it gets combined with the current url.
Specified by:
sendRedirect in interface HttpServletResponse
Parameters:
url - the possibly relative url to send to the browser

containsHeader

public boolean containsHeader(java.lang.String name)
Returns true if the response already contains the named header.
Specified by:
containsHeader in interface HttpServletResponse
Parameters:
name - name of the header to test.

getHeader

public java.lang.String getHeader(java.lang.String name)
Returns the value of an already set output header.
Specified by:
getHeader in interface CauchoResponse
Parameters:
name - name of the header to get.

setHeader

public void setHeader(java.lang.String key,
                      java.lang.String value)
Sets a header, replacing an already-existing header.
Specified by:
setHeader in interface HttpServletResponse
Parameters:
key - the header key to set.
value - the header value to set.

addHeader

public void addHeader(java.lang.String key,
                      java.lang.String value)
Adds a new header. If an old header with that name exists, both headers are output.
Specified by:
addHeader in interface CauchoResponse
Parameters:
key - the header key.
value - the header value.

removeHeader

public void removeHeader(java.lang.String key)

setIntHeader

public void setIntHeader(java.lang.String name,
                         int value)
Convenience for setting an integer header. An old header with the same name will be replaced.
Specified by:
setIntHeader in interface HttpServletResponse
Parameters:
name - the header name.
value - an integer to be converted to a string for the header.

addIntHeader

public void addIntHeader(java.lang.String key,
                         int value)
Convenience for adding an integer header. If an old header already exists, both will be sent to the browser.
Specified by:
addIntHeader in interface HttpServletResponse
Parameters:
name - the header name.
value - an integer to be converted to a string for the header.

setDateHeader

public void setDateHeader(java.lang.String name,
                          long value)
Convenience for setting a date header. An old header with the same name will be replaced.
Specified by:
setDateHeader in interface HttpServletResponse
Parameters:
name - the header name.
value - an time in milliseconds to be converted to a date string.

addDateHeader

public void addDateHeader(java.lang.String key,
                          long value)
Convenience for adding a date header. If an old header with the same name exists, both will be displayed.
Specified by:
addDateHeader in interface HttpServletResponse
Parameters:
name - the header name.
value - an time in milliseconds to be converted to a date string.

setContentLength

public void setContentLength(int length)
Sets the content length of the result. In general, Resin will handle the content length, but for things like long downloads adding the length will give a valuable hint to the browser.
Specified by:
setContentLength in interface ServletResponse
Parameters:
length - the length of the content.

setContentType

public void setContentType(java.lang.String value)
Sets the browser content type. If the value contains a charset, the output encoding will be changed to match.

For example, to set the output encoding to use UTF-8 instead of the default ISO-8859-1 (Latin-1), use the following:

 setContentType("text/html; charset=UTF-8");
 
Specified by:
setContentType in interface ServletResponse
Following copied from interface: javax.servlet.ServletResponse
Parameters:
type - a String specifying the MIME type of the content
See Also:
ServletResponse.getOutputStream(), ServletResponse.getWriter()

getCharacterEncoding

public java.lang.String getCharacterEncoding()
Description copied from interface: ServletResponse
Returns the name of the charset used for the MIME body sent in this response.

If no charset has been assigned, it is implicitly set to ISO-8859-1 (Latin-1).

See RFC 2047 (http://ds.internic.net/rfc/rfc2045.txt) for more information about character encoding and MIME.

Specified by:
getCharacterEncoding in interface ServletResponse
Following copied from interface: javax.servlet.ServletResponse
Returns:
a String specifying the name of the charset, for example, ISO-8859-1

addCookie

public void addCookie(Cookie cookie)
Adds a cookie to the response.
Specified by:
addCookie in interface HttpServletResponse
Parameters:
cookie - the response cookie

getCookie

public Cookie getCookie(java.lang.String name)

getCookies

public java.util.ArrayList getCookies()

setSessionId

public void setSessionId(java.lang.String id)
Specified by:
setSessionId in interface CauchoResponse

getStream

public WriteStream getStream()
Returns the underlying WriteStream beneath the request.
Specified by:
getStream in interface CauchoResponse

setStream

public void setStream(WriteStream os)
Sets the underlying WriteStream beneath the request. Special cases like JSP BodyTags will call setStream so output will go to the tag rather than the browser stream.
Specified by:
setStream in interface CauchoResponse

getOriginalStream

public WriteStream getOriginalStream()

getOutputStream

public ServletOutputStream getOutputStream()
                                    throws java.io.IOException
Returns the ServletOutputStream for the response.
Specified by:
getOutputStream in interface ServletResponse
Following copied from interface: javax.servlet.ServletResponse
Returns:
a ServletOutputStream for writing binary data
Throws:
java.lang.IllegalStateException - if the getWriter method has been called on this response
java.io.IOException - if an input or output exception occurred
See Also:
ServletResponse.getWriter()

getWriter

public java.io.PrintWriter getWriter()
                              throws java.io.IOException
Returns a PrintWriter for the response.
Specified by:
getWriter in interface ServletResponse
Following copied from interface: javax.servlet.ServletResponse
Returns:
a PrintWriter object that can return character data to the client
Throws:
java.io.UnsupportedEncodingException - if the charset specified in setContentType cannot be used
java.lang.IllegalStateException - if the getOutputStream method has already been called for this response object
java.io.IOException - if an input or output exception occurred
See Also:
ServletResponse.getOutputStream(), ServletResponse.setContentType(java.lang.String)

encodeURL

public java.lang.String encodeURL(java.lang.String string)
Encode the URL with the session jd.
Specified by:
encodeURL in interface HttpServletResponse
Parameters:
string - the url to be encoded
Returns:
the encoded url

encodeRedirectURL

public java.lang.String encodeRedirectURL(java.lang.String string)
Description copied from interface: HttpServletResponse
Encodes the specified URL for use in the sendRedirect method or, if encoding is not needed, returns the URL unchanged. The implementation of this method includes the logic to determine whether the session ID needs to be encoded in the URL. Because the rules for making this determination can differ from those used to decide whether to encode a normal link, this method is seperate from the encodeURL method.

All URLs sent to the HttpServletResponse.sendRedirect method should be run through this method. Otherwise, URL rewriting cannot be used with browsers which do not support cookies.

Specified by:
encodeRedirectURL in interface HttpServletResponse
Following copied from interface: javax.servlet.http.HttpServletResponse
Parameters:
url - the url to be encoded.
Returns:
the encoded URL if encoding is needed; the unchanged URL otherwise.
See Also:
HttpServletResponse.sendRedirect(java.lang.String), HttpServletResponse.encodeUrl(java.lang.String)

encodeRedirectUrl

public java.lang.String encodeRedirectUrl(java.lang.String string)
Deprecated.  

Specified by:
encodeRedirectUrl in interface HttpServletResponse
Following copied from interface: javax.servlet.http.HttpServletResponse
Parameters:
url - the url to be encoded.
Returns:
the encoded URL if encoding is needed; the unchanged URL otherwise.

encodeUrl

public java.lang.String encodeUrl(java.lang.String string)
Deprecated.  

Specified by:
encodeUrl in interface HttpServletResponse
Following copied from interface: javax.servlet.http.HttpServletResponse
Parameters:
url - the url to be encoded.
Returns:
the encoded URL if encoding is needed; the unchanged URL otherwise.

setBufferSize

public void setBufferSize(int size)
Description copied from interface: ServletResponse
Sets the preferred buffer size for the body of the response. The servlet container will use a buffer at least as large as the size requested. The actual buffer size used can be found using getBufferSize.

A larger buffer allows more content to be written before anything is actually sent, thus providing the servlet with more time to set appropriate status codes and headers. A smaller buffer decreases server memory load and allows the client to start receiving data more quickly.

This method must be called before any response body content is written; if content has been written, this method throws an IllegalStateException.

Specified by:
setBufferSize in interface CauchoResponse
Following copied from interface: javax.servlet.ServletResponse
Parameters:
size - the preferred buffer size
Throws:
java.lang.IllegalStateException - if this method is called after content has been written
See Also:
ServletResponse.getBufferSize(), ServletResponse.flushBuffer(), ServletResponse.isCommitted(), ServletResponse.reset()

getBufferSize

public int getBufferSize()
Description copied from interface: ServletResponse
Returns the actual buffer size used for the response. If no buffering is used, this method returns 0.
Specified by:
getBufferSize in interface CauchoResponse
Following copied from interface: javax.servlet.ServletResponse
Returns:
the actual buffer size used
See Also:
ServletResponse.setBufferSize(int), ServletResponse.flushBuffer(), ServletResponse.isCommitted(), ServletResponse.reset()

flushBuffer

public void flushBuffer()
                 throws java.io.IOException
Description copied from interface: ServletResponse
Forces any content in the buffer to be written to the client. A call to this method automatically commits the response, meaning the status code and headers will be written.
Specified by:
flushBuffer in interface CauchoResponse
Following copied from interface: javax.servlet.ServletResponse
See Also:
ServletResponse.setBufferSize(int), ServletResponse.getBufferSize(), ServletResponse.isCommitted(), ServletResponse.reset()

isCommitted

public boolean isCommitted()
Returns true if some data has been sent to the browser.
Specified by:
isCommitted in interface ServletResponse
Following copied from interface: javax.servlet.ServletResponse
Returns:
a boolean indicating if the response has been committed
See Also:
ServletResponse.setBufferSize(int), ServletResponse.getBufferSize(), ServletResponse.flushBuffer(), ServletResponse.reset()

reset

public void reset()
Description copied from interface: ServletResponse
Clears any data that exists in the buffer as well as the status code and headers. If the response has been committed, this method throws an IllegalStateException.
Specified by:
reset in interface CauchoResponse
Following copied from interface: javax.servlet.ServletResponse
Throws:
java.lang.IllegalStateException - if the response has already been committed
See Also:
ServletResponse.setBufferSize(int), ServletResponse.getBufferSize(), ServletResponse.flushBuffer(), ServletResponse.isCommitted()

resetBuffer

public void resetBuffer()
Description copied from interface: ServletResponse
Clears the content of the underlying buffer in the response without clearing headers or status code. If the response has been committed, this method throws an IllegalStateException.
Specified by:
resetBuffer in interface ServletResponse
Following copied from interface: javax.servlet.ServletResponse
See Also:
ServletResponse.setBufferSize(int), ServletResponse.getBufferSize(), ServletResponse.isCommitted(), ServletResponse.reset()

clearBuffer

public void clearBuffer()
Specified by:
clearBuffer in interface CauchoResponse

setLocale

public void setLocale(java.util.Locale locale)
Description copied from interface: ServletResponse
Sets the locale of the response, setting the headers (including the Content-Type's charset) as appropriate. This method should be called before a call to ServletResponse.getWriter(). By default, the response locale is the default locale for the server.
Specified by:
setLocale in interface ServletResponse
Following copied from interface: javax.servlet.ServletResponse
Parameters:
loc - the locale of the response
See Also:
ServletResponse.getLocale()

getLocale

public java.util.Locale getLocale()
Description copied from interface: ServletResponse
Returns the locale assigned to the response.
Specified by:
getLocale in interface ServletResponse
Following copied from interface: javax.servlet.ServletResponse
See Also:
ServletResponse.setLocale(java.util.Locale)

getRemaining

public int getRemaining()
Specified by:
getRemaining in interface CauchoResponse

getContentLength

public int getContentLength()
Returns the number of bytes send to the output.

disableHeaders

public boolean disableHeaders(boolean disable)
Specified by:
disableHeaders in interface CauchoResponse

disableCaching

public boolean disableCaching(boolean disable)

setNoCache

public void setNoCache(boolean isNoCache)
Set if the page is non-cacheable.

killCache

public void killCache()
Set if the page is non-cacheable.
Specified by:
killCache in interface CauchoResponse

fillCookie

public boolean fillCookie(CharBuffer cb,
                          Cookie cookie,
                          long date,
                          int version)
Fills the response for a cookie
Parameters:
cb - result buffer to contain the generated string
cookie - the cookie
date - the current date

initChaining

protected void initChaining()