com.mortbay.HTTP
Class HttpResponse

java.lang.Object
  |
  +--com.mortbay.HTTP.HttpHeader
        |
        +--com.mortbay.HTTP.HttpResponse

public class HttpResponse
extends HttpHeader
implements javax.servlet.http.HttpServletResponse

Handling of a HTTP response. Implements and extends the javax.servlet.http.HttpServletResponse interface. The extensions are for HttpHandler instances that need to modify the response or have better access to the IO.

Note

By default, responses will only use chunking if requested by the by setting the transfer encoding header. However, if the chunkByDefault is set, then chunking is used if no content length is set.

Version:
$Id: HttpResponse.java,v 2.33 2000/06/02 23:41:05 gregw Exp $
Author:
Greg Wilkins
See Also:
HttpServer

Field Summary
static java.util.Hashtable __errorCodeMap
           
static java.lang.String Allow
           
static java.lang.String Expires
           
static java.lang.String Location
           
static java.lang.String MIME_Version
           
static java.lang.String Server
           
 
Fields inherited from class com.mortbay.HTTP.HttpHeader
__COLON, __CRLF, __dateReceive, __dateSend, __singleValuedMap, Authorization, Chunked, Close, COLON, Connection, ContentLength, ContentType, Cookie, CRLF, Date, Expires, Host, HTTP_1_0, HTTP_1_1, IfModifiedSince, IfUnmodifiedSince, LastModified, Referer, SetCookie, SingleValued, TransferEncoding, UserAgent, WwwAuthenticate, WwwFormUrlEncode
 
Fields inherited from interface javax.servlet.http.HttpServletResponse
SC_ACCEPTED, SC_BAD_GATEWAY, SC_BAD_REQUEST, SC_CONFLICT, SC_CONTINUE, SC_CREATED, 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_RESET_CONTENT, SC_SEE_OTHER, SC_SERVICE_UNAVAILABLE, SC_SWITCHING_PROTOCOLS, SC_UNAUTHORIZED, SC_UNSUPPORTED_MEDIA_TYPE, SC_USE_PROXY
 
Constructor Summary
HttpResponse(java.io.OutputStream out, HttpRequest request)
          Construct a response
 
Method Summary
 void addCookie(javax.servlet.http.Cookie cookie)
          Adds the specified cookie to the response.
 void addObserver(java.util.Observer o)
          Add an observer to the response.
 void complete()
          Complete the response.
 boolean containsHeader(java.lang.String headerKey)
          Checks whether the response message header has a field with the specified name.
 void deleteObserver(java.util.Observer o)
           
 void destroy()
          Destroy the header.
 java.lang.String encodeRedirectUrl(java.lang.String url)
          Encodes the specified URL for use in the sendRedirect method or, if encoding is not needed, returns the URL unchanged.
 java.lang.String encodeRedirectURL(java.lang.String url)
          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 url)
          Encodes the specified URL by including the session ID in it, or, if encoding is not needed, returns the URL unchanged.
 java.lang.String encodeURL(java.lang.String url)
          Encode session ID in URL if required.
 java.lang.String getCharacterEncoding()
          Returns the character set encoding for the input of this request.
 Cookies getCookies()
          Deprecated. Use addCookie()
 javax.servlet.ServletOutputStream getOutputStream()
          Get the OutputStream of the response.
 java.lang.String getReason()
           
 HttpRequest getRequest()
          Get the HttpRequest for this response
 java.lang.String getResponseLine()
           
 java.lang.String getStatus()
           
 java.lang.String getVersion()
           
 java.io.PrintWriter getWriter()
          Returns a PrintWriter object that you can use to send character text to the client.
 boolean headersWritten()
          Return true if the headers have already been written for this response
 boolean requestHandled()
          Return true if the headers have already been written for this response
 void sendError(int code)
          Sends an error response to the client using the specified status code and no default message.
 void sendError(int code, java.lang.String msg)
          Sends an error response to the client using the specified status code and detail message.
 void sendRedirect(java.lang.String location)
          Sends a redirect response to the client using the specified redirect location URL.
 void setChunkByDefault(boolean chunk)
           
 void setContentLength(int len)
          Set the content length of the response
 void setContentType(java.lang.String type)
          Set the content type of the response
 void setStatus(int code)
          Sets the status code and a default message for this response.
 void setStatus(int code, java.lang.String msg)
          Deprecated.  
 void setVersion(java.lang.String version)
           
 void writeHeaders()
          If the headers have not already been written, write them.
 void writeInputStream(java.io.InputStream stream, long length)
          Copy all data from an input stream to the HttpResponse.
 void writeInputStream(java.io.InputStream stream, long length, boolean streamIncludesHeaders)
          Copy all data from an input stream to the HttpResponse.
 
Methods inherited from class com.mortbay.HTTP.HttpHeader
dump, getDateHeader, getHeader, getHeaderNames, getIntHeader, read, setDateHeader, setHeader, setIntHeader, toString, write, write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

MIME_Version

public static final java.lang.String MIME_Version

Server

public static final java.lang.String Server

Expires

public static final java.lang.String Expires

Location

public static final java.lang.String Location

Allow

public static final java.lang.String Allow

__errorCodeMap

public static final java.util.Hashtable __errorCodeMap
Constructor Detail

HttpResponse

public HttpResponse(java.io.OutputStream out,
                    HttpRequest request)
Construct a response
Parameters:
out - The output stream that the response will be written to.
request - The HttpRequest that this response is to.
Method Detail

getVersion

public java.lang.String getVersion()

setVersion

public void setVersion(java.lang.String version)

getStatus

public java.lang.String getStatus()

getReason

public java.lang.String getReason()

getRequest

public HttpRequest getRequest()
Get the HttpRequest for this response

getResponseLine

public java.lang.String getResponseLine()

setChunkByDefault

public void setChunkByDefault(boolean chunk)

addObserver

public void addObserver(java.util.Observer o)
Add an observer to the response. Observers are notified when the HTTP headers are complete and before any output has been written. Thus observers may examine or modify the headers and activate filters. Notify is called with the request as the argument object

deleteObserver

public void deleteObserver(java.util.Observer o)

complete

public void complete()
              throws java.io.IOException
Complete the response. Will conditionally call close to indicate the end of the response

headersWritten

public boolean headersWritten()
Return true if the headers have already been written for this response

requestHandled

public boolean requestHandled()
Return true if the headers have already been written for this response

writeHeaders

public void writeHeaders()
                  throws java.io.IOException
If the headers have not already been written, write them. If any HttpFilters have been added activate them before writing.

writeInputStream

public void writeInputStream(java.io.InputStream stream,
                             long length)
                      throws java.io.IOException
Copy all data from an input stream to the HttpResponse. This method assumes that the input stream does not include HTTP headers.
Parameters:
stream - the InputStream to read
length - If greater than 0, this is the number of bytes to read and write

writeInputStream

public void writeInputStream(java.io.InputStream stream,
                             long length,
                             boolean streamIncludesHeaders)
                      throws java.io.IOException
Copy all data from an input stream to the HttpResponse.
Parameters:
stream - the InputStream to read
length - If greater than 0, this is the number of bytes to read and write
streamIncludesHeaders - True when the input stream includes HTTP headers which replace those set in this HttpResponse.

getCookies

public Cookies getCookies()
Deprecated. Use addCookie()

Return the Cookies instance These are the "Set-Cookies" that will be sent with this response. Cookies may be added, modified or deleted from the Cookies instance.

setContentLength

public void setContentLength(int len)
Set the content length of the response
Tags copied from interface: javax.servlet.ServletResponse
Parameters:
len - an integer specifying the length of the content being returned to the client; sets the Content-Length header

setContentType

public void setContentType(java.lang.String type)
Set the content type of the response
Tags copied from interface: javax.servlet.ServletResponse
Parameters:
type - a String specifying the MIME type of the content
See Also:
ServletResponse.getOutputStream(), ServletResponse.getWriter()

getOutputStream

public javax.servlet.ServletOutputStream getOutputStream()
Get the OutputStream of the response. The first write to this stream will trigger writing of the HTTP filters and potential activation of any HttpFilters.
Tags copied from interface: javax.servlet.ServletResponse
Returns:
a ServletOutputStream for writing binary data
Throws:
java.lang.IllegalStateException - if you have already called the getWriter method for the same response
java.io.IOException - if an input or output exception occurred
See Also:
ServletResponse.getWriter()

setStatus

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

Sets the status code and message for this response.
Specified by:
setStatus in interface javax.servlet.http.HttpServletResponse
Parameters:
code - the status code
msg - the status message

setStatus

public void setStatus(int code)
Sets the status code and a default message for this response.
Specified by:
setStatus in interface javax.servlet.http.HttpServletResponse
Parameters:
code - the status code

sendError

public void sendError(int code,
                      java.lang.String msg)
               throws java.io.IOException
Sends an error response to the client using the specified status code and detail message.
Specified by:
sendError in interface javax.servlet.http.HttpServletResponse
Parameters:
code - the status code
msg - the detail message
Throws:
java.io.IOException - If an I/O error has occurred.

sendError

public void sendError(int code)
               throws java.io.IOException
Sends an error response to the client using the specified status code and no default message.
Specified by:
sendError in interface javax.servlet.http.HttpServletResponse
Parameters:
code - the status code
Throws:
java.io.IOException - If an I/O error has occurred.

sendRedirect

public void sendRedirect(java.lang.String location)
                  throws java.io.IOException
Sends a redirect response to the client using the specified redirect location URL.
Specified by:
sendRedirect in interface javax.servlet.http.HttpServletResponse
Parameters:
location - the redirect location URL
Throws:
java.io.IOException - If an I/O error has occurred.

containsHeader

public boolean containsHeader(java.lang.String headerKey)
Description copied from interface: javax.servlet.http.HttpServletResponse
Checks whether the response message header has a field with the specified name.
Specified by:
containsHeader in interface javax.servlet.http.HttpServletResponse
Tags copied from interface: javax.servlet.http.HttpServletResponse
Parameters:
name - the header field name
Returns:
true if the response message header has a field with the specified name; false otherwise

addCookie

public void addCookie(javax.servlet.http.Cookie cookie)
Description copied from interface: javax.servlet.http.HttpServletResponse
Adds the specified cookie to the response. It can be called multiple times to set more than one cookie.
Specified by:
addCookie in interface javax.servlet.http.HttpServletResponse
Tags copied from interface: javax.servlet.http.HttpServletResponse
Parameters:
cookie - the Cookie to return to the client

encodeRedirectUrl

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

Description copied from interface: javax.servlet.http.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 should include the logic to determine whether the session ID needs to be encoded in the URL. Because the rules for making this determination 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 canont be used with browsers which do not support cookies.

Specified by:
encodeRedirectUrl in interface javax.servlet.http.HttpServletResponse
Tags 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 url)
Deprecated.  

Description copied from interface: javax.servlet.http.HttpServletResponse
Encodes the specified URL by including the session ID in it, or, if encoding is not needed, returns the URL unchanged. The implementation of this method should include the logic to determine whether the session ID needs to be encoded in the URL. For example, if the browser supports cookies, or session tracking is turned off, URL encoding is unnecessary.

All URLs emitted by a Servlet should be run through this method. Otherwise, URL rewriting cannot be used with browsers which do not support cookies.

Specified by:
encodeUrl in interface javax.servlet.http.HttpServletResponse
Tags 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.

encodeRedirectURL

public java.lang.String encodeRedirectURL(java.lang.String url)
Description copied from interface: javax.servlet.http.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 should include the logic to determine whether the session ID needs to be encoded in the URL. Because the rules for making this determination 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 canont be used with browsers which do not support cookies.

After this method is called, the response should be considered to be committed and should not be written to.

Specified by:
encodeRedirectURL in interface javax.servlet.http.HttpServletResponse
Tags 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)

encodeURL

public java.lang.String encodeURL(java.lang.String url)
Encode session ID in URL if required. The session ID is encoded in the file part of the URL between character guards configured in SessionContext. If '/' is in the URL, the encoded session is placed after the last '/', otherwise is is placed at the start of the URL. This encoding should not disturb any browser interpretation of relative paths, file suffixes or URL references.
Specified by:
encodeURL in interface javax.servlet.http.HttpServletResponse
Parameters:
url - The URL to encode
Returns:
Encoded URL or the original URL

getCharacterEncoding

public java.lang.String getCharacterEncoding()
Returns the character set encoding for the input of this request. Checks the Content-Type header for a charset parameter and return its value if found or ISO-8859-1 otherwise.
Returns:
Character Encoding.

getWriter

public java.io.PrintWriter getWriter()
Description copied from interface: javax.servlet.ServletResponse
Returns a PrintWriter object that you can use to send character text to the client. The character encoding used is the one specified in the charset= property of the ServletResponse.setContentType(java.lang.String) method, which you must call before you call this method.

If necessary, the MIME type of the response is modified to reflect the character encoding used.

You cannot use this method if you have already called ServletResponse.getOutputStream() for this ServletResponse object.

Tags copied from interface: javax.servlet.ServletResponse
Returns:
a PrintWriter object that can return text to the client
Throws:
java.io.UnsupportedEncodingException - if the character encoding specified in setContentType cannot be used
java.lang.IllegalStateException - if the getOutputStream method has already been called for this response object; in that case, you can't use this method
java.io.IOException - if an input or output exception occurred
See Also:
ServletResponse.getOutputStream(), ServletResponse.setContentType(java.lang.String)

destroy

public void destroy()
Destroy the header. Help the garbage collector by null everything that we can.
Overrides:
destroy in class HttpHeader