Enhydra 5.1 API

org.enhydra.servlet.connectionMethods.http
Class HttpResponse

java.lang.Object
  |
  +--org.enhydra.servlet.connectionMethods.http.HttpResponse
All Implemented Interfaces:
javax.servlet.http.HttpServletResponse, org.apache.tomcat.core.ResponseAdapter, javax.servlet.ServletResponse

public class HttpResponse
extends java.lang.Object
implements javax.servlet.http.HttpServletResponse, org.apache.tomcat.core.ResponseAdapter

Implementation of a HTTP servlet response for the HTTP connection method.

Version:
$Revision: 1.1 $
Author:
Mark Diekhans
See Also:
HttpServletResponse, ServletResponse

Field Summary
 
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_TEMPORARY_REDIRECT, SC_UNAUTHORIZED, SC_UNSUPPORTED_MEDIA_TYPE, SC_USE_PROXY
 
Constructor Summary
HttpResponse()
           
 
Method Summary
 void addCookie(javax.servlet.http.Cookie cookie)
          Adds the specified cookie to the response.
 void addDateHeader(java.lang.String name, long value)
           
 void addHeader(java.lang.String name, java.lang.String value)
           
 void addIntHeader(java.lang.String name, int value)
           
 void addMimeHeaders(org.apache.tomcat.util.MimeHeaders headers)
           
protected  void close()
          Close the socket associate with this object.
 boolean containsHeader(java.lang.String name)
          Checks whether the response message header has a field with the specified name.
 void doWrite(byte[] buffer, int pos, int count)
          Write a chunk of bytes.
 java.lang.String encodeRedirectUrl(java.lang.String url)
          Encodes the specified URL for use in the sendRedirect method.
 java.lang.String encodeRedirectURL(java.lang.String url)
           
 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)
           
 void endHeaders()
          Signal that we're done with the headers, and body will follow.
 void endResponse()
          Signal that we're done with a particular request, the server can go on and read more requests or close the socket
protected  void finish()
          Indicate the end of handing a request.
 void flushBuffer()
           
 int getBufferSize()
           
 java.lang.String getCharacterEncoding()
          Returns the character set encoding used for this MIME body.
 java.util.Locale getLocale()
           
 javax.servlet.ServletOutputStream getOutputStream()
          Returns an output stream for writing binary response data.
 javax.servlet.ServletOutputStream getServletOutputStream()
           
 java.io.PrintWriter getWriter()
          Returns a print writer for writing formatted text responses.
protected  boolean haveHeadersBeenWritten()
          Check if headers have been written to the socket.
 boolean isCommitted()
           
protected  boolean keepAlivePossible()
          Determine if keep alive is possible.
protected  void next(boolean keepAliveRequested)
          Initialize for the next response on a socket.
 void recycle()
           
 void reset()
           
 void sendError(int sc)
          Sends an error response to the client using the specified status code and a default message.
 void sendError(int sc, java.lang.String msg)
          Sends an error response to the client using the specified status code and descriptive message.
 void sendRedirect(java.lang.String location)
          Sends a temporary redirect response to the client using the specified redirect location URL.
 void setBufferSize(int size)
           
protected  void setConnection(java.net.Socket responseSocket)
          Associate a connected socket with this object.
 void setContentLength(int len)
          Sets the content length for this response.
 void setContentType(java.lang.String type)
          Sets the content type for this response.
 void setDateHeader(java.lang.String name, long date)
          Adds a field to the response header with the given name and date-valued field.
 void setHeader(java.lang.String name, java.lang.String value)
          Adds a field to the response header with the given name and value.
 void setIntHeader(java.lang.String name, int value)
          Adds a field to the response header with the given name and integer value.
 void setLocale(java.util.Locale locale)
           
 void setStatus(int sc)
          Sets the status code for this response.
 void setStatus(int sc, java.lang.String sm)
          Sets the status code and message for this response.
protected  void writeHeaders()
          Write out the headers.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HttpResponse

public HttpResponse()
Method Detail

setConnection

protected void setConnection(java.net.Socket responseSocket)
                      throws java.io.IOException
Associate a connected socket with this object. The next function must be called before a response can be processed.

Parameters:
responseSocket - The HTTP connected socket.
Throws:
java.io.IOException - If an error occured setting up the socket input stream.

close

protected void close()
              throws java.io.IOException
Close the socket associate with this object.

Throws:
java.io.IOException - If an error occured setting up the socket input stream.

setContentLength

public void setContentLength(int len)
Sets the content length for this response.

Specified by:
setContentLength in interface javax.servlet.ServletResponse
Parameters:
len - The content length.

setContentType

public void setContentType(java.lang.String type)
Sets the content type for this response.

Specified by:
setContentType in interface javax.servlet.ServletResponse

getOutputStream

public javax.servlet.ServletOutputStream getOutputStream()
                                                  throws java.io.IOException
Returns an output stream for writing binary response data.

Specified by:
getOutputStream in interface javax.servlet.ServletResponse
Throws:
java.lang.IllegalStateException - if getWriter has been called on this same request.
java.io.IOException - if an I/O exception has occurred.

getWriter

public java.io.PrintWriter getWriter()
                              throws java.io.IOException
Returns a print writer for writing formatted text responses.

Specified by:
getWriter in interface javax.servlet.ServletResponse
Throws:
UnsupportedEncodingException - if no such encoding can be provided.
java.lang.IllegalStateException - if getOutputStream has been called on this same request.
java.io.IOException - on other errors.

getCharacterEncoding

public java.lang.String getCharacterEncoding()
Returns the character set encoding used for this MIME body.

Specified by:
getCharacterEncoding in interface javax.servlet.ServletResponse

addCookie

public void addCookie(javax.servlet.http.Cookie cookie)
Adds the specified cookie to the response.

Specified by:
addCookie in interface javax.servlet.http.HttpServletResponse
Parameters:
cookie - The Cookie to return to the client.

containsHeader

public boolean containsHeader(java.lang.String name)
Checks whether the response message header has a field with the specified name.

Specified by:
containsHeader in 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.

setStatus

public void setStatus(int sc,
                      java.lang.String sm)
Sets the status code and message for this response.

Specified by:
setStatus in interface javax.servlet.http.HttpServletResponse
Parameters:
sc - The status code.
sm - The status message.

setStatus

public void setStatus(int sc)
Sets the status code for this response.

Specified by:
setStatus in interface javax.servlet.http.HttpServletResponse
Parameters:
sc - the status code

setHeader

public void setHeader(java.lang.String name,
                      java.lang.String value)
Adds a field to the response header with the given name and value.

Specified by:
setHeader in interface javax.servlet.http.HttpServletResponse
Parameters:
name - The name of the header field.
value - The header field's value.

setIntHeader

public void setIntHeader(java.lang.String name,
                         int value)
Adds a field to the response header with the given name and integer value.

Specified by:
setIntHeader in interface javax.servlet.http.HttpServletResponse
Parameters:
name - The name of the header field.
value - The header field's integer value.

setDateHeader

public void setDateHeader(java.lang.String name,
                          long date)
Adds a field to the response header with the given name and date-valued field.

Specified by:
setDateHeader in interface javax.servlet.http.HttpServletResponse
Parameters:
name - The name of the header field.

sendError

public void sendError(int sc,
                      java.lang.String msg)
               throws java.io.IOException
Sends an error response to the client using the specified status code and descriptive message.

Specified by:
sendError in interface javax.servlet.http.HttpServletResponse
Parameters:
sc - The status code
msg - The generate a HTML response with this message.
Throws:
java.io.IOException - If an I/O error has occurred.

sendError

public void sendError(int sc)
               throws java.io.IOException
Sends an error response to the client using the specified status code and a default message.

Specified by:
sendError in interface javax.servlet.http.HttpServletResponse
Parameters:
sc - 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 temporary 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.

encodeUrl

public 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.

Specified by:
encodeUrl in 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)
Encodes the specified URL for use in the sendRedirect method.

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

next

protected void next(boolean keepAliveRequested)
             throws java.io.IOException
Initialize for the next response on a socket. With keep-alive, multiple responses can be sent on a single connection.

Parameters:
keepAliveRequested - Indicates if a keep-alive connection has been requested by the client.
Throws:
java.io.IOException - If an I/O error occurs.

writeHeaders

protected void writeHeaders()
                     throws java.io.IOException
Write out the headers. Does nothing if they have already been written.

Throws:
java.io.IOException - If an I/O error occurs writing the headers.

finish

protected void finish()
               throws java.io.IOException
Indicate the end of handing a request. Force data to be written if it hasn't already happened.

Throws:
java.io.IOException - If an I/O error occurs writing the headers.

keepAlivePossible

protected boolean keepAlivePossible()
Determine if keep alive is possible. That is content length was set and is correct. The finish() method must be called before this is called.

Returns:
True if a keep alive is possible.

haveHeadersBeenWritten

protected boolean haveHeadersBeenWritten()
Check if headers have been written to the socket.

Returns:
True if they have and no more can be written; false otherwise.

getLocale

public java.util.Locale getLocale()
Specified by:
getLocale in interface javax.servlet.ServletResponse

flushBuffer

public void flushBuffer()
Specified by:
flushBuffer in interface javax.servlet.ServletResponse

encodeURL

public java.lang.String encodeURL(java.lang.String url)
Specified by:
encodeURL in interface javax.servlet.http.HttpServletResponse

isCommitted

public boolean isCommitted()
Specified by:
isCommitted in interface javax.servlet.ServletResponse

getBufferSize

public int getBufferSize()
Specified by:
getBufferSize in interface javax.servlet.ServletResponse

reset

public void reset()
Specified by:
reset in interface javax.servlet.ServletResponse

encodeRedirectURL

public java.lang.String encodeRedirectURL(java.lang.String url)
Specified by:
encodeRedirectURL in interface javax.servlet.http.HttpServletResponse

addIntHeader

public void addIntHeader(java.lang.String name,
                         int value)
Specified by:
addIntHeader in interface javax.servlet.http.HttpServletResponse

addDateHeader

public void addDateHeader(java.lang.String name,
                          long value)
Specified by:
addDateHeader in interface javax.servlet.http.HttpServletResponse

addHeader

public void addHeader(java.lang.String name,
                      java.lang.String value)
Specified by:
addHeader in interface javax.servlet.http.HttpServletResponse

setBufferSize

public void setBufferSize(int size)
Specified by:
setBufferSize in interface javax.servlet.ServletResponse

setLocale

public void setLocale(java.util.Locale locale)
Specified by:
setLocale in interface javax.servlet.ServletResponse

addMimeHeaders

public void addMimeHeaders(org.apache.tomcat.util.MimeHeaders headers)
                    throws java.io.IOException
Specified by:
addMimeHeaders in interface org.apache.tomcat.core.ResponseAdapter
java.io.IOException

endResponse

public void endResponse()
                 throws java.io.IOException
Signal that we're done with a particular request, the server can go on and read more requests or close the socket

Specified by:
endResponse in interface org.apache.tomcat.core.ResponseAdapter
java.io.IOException

endHeaders

public void endHeaders()
                throws java.io.IOException
Signal that we're done with the headers, and body will follow. The adapter doesn't have to maintain state, it's done inside the engine

Specified by:
endHeaders in interface org.apache.tomcat.core.ResponseAdapter
java.io.IOException

getServletOutputStream

public javax.servlet.ServletOutputStream getServletOutputStream()
                                                         throws java.io.IOException
Specified by:
getServletOutputStream in interface org.apache.tomcat.core.ResponseAdapter
java.io.IOException

recycle

public void recycle()
Specified by:
recycle in interface org.apache.tomcat.core.ResponseAdapter

doWrite

public void doWrite(byte[] buffer,
                    int pos,
                    int count)
             throws java.io.IOException
Write a chunk of bytes. Should be called only from ServletOutputStream implementations, No need to implement it if your adapter implements ServletOutputStream. Headers and status will be written before this method is exceuted.

Specified by:
doWrite in interface org.apache.tomcat.core.ResponseAdapter
java.io.IOException

Enhydra 5.1 API