org.faceless.util
Class HttpResponseWriter

java.lang.Object
  extended by org.faceless.util.HttpResponseWriter

public class HttpResponseWriter
extends Object

Represents an outgoing HTTP Response. Unlike the HttpServletResponse class, this class is not tied to a servlet context, and may be stored or manipulated with ease.

Most of the methods in this class are identical to HttpServletResponse. Several are missing, namely those that need the Servlet Context to work (for example, encodeURL) and those dealing with the response buffer, which obviously don't apply to this class.

See Also:
HttpResponseReader

Constructor Summary
HttpResponseWriter()
          Create a new HttpResponseWriter
HttpResponseWriter(HttpResponseReader reader)
          Create a new HttpResponseWriter which is a copy of the specified HttpResponseReader.
 
Method Summary
 void addCookie(javax.servlet.http.Cookie cookie)
          Adds the specified cookie to the response.
 void addCookie(String name, String value, String domain, String path, Date date, boolean secure)
          Adds the specified cookie to the response.
 void addDateHeader(String name, long val)
          Adds a response header with the given name and date-value.
 void addHeader(String name, String value)
          Adds a response header with the given name and value.
 void addIntHeader(String name, int val)
          Sets a response header with the given name and integer value.
 void clearHeader(String name)
          Remove the header from the response.
 boolean containsHeader(String name)
          Returns a boolean indicating whether the named response header has already been set.
 String getCharacterEncoding()
           Returns the name of the charset used for the MIME body sent in this response.
 String getHeader(String name)
          Returns the value of the header if it's set, or null if it's not
 Locale getLocale()
          Returns the locale assigned to the response
 OutputStream getOutputStream()
           Returns an OutputStream suitable for writing binary data in the response.
 PrintWriter getWriter()
           Returns a PrintWriter object that can send character text to the client.
 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 setContentType(String type)
          Sets the content type of the response being sent to the client.
 void setDateHeader(String name, long val)
          Sets a response header with the given name and date-value.
 void setHeader(String name, String value)
          Sets a response header with the given name and value.
 void setIntHeader(String name, int val)
          Adds a response header with the given name and integer value.
 void setLocale(Locale loc)
          Sets the locale of the response, setting the headers (including the Content-Type's charset) as appropriate.
 void setStatus(int status)
          Sets the status code for this response.
 void setStatus(int status, String statusmsg)
          Sets the status code for this response.
 void writeTo(javax.servlet.http.HttpServletResponse resp)
          Write the response to the specified HttpServletResponse.
 void writeTo(OutputStream out)
          Write the response to the specified OutputStream.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HttpResponseWriter

public HttpResponseWriter()
Create a new HttpResponseWriter


HttpResponseWriter

public HttpResponseWriter(HttpResponseReader reader)
                   throws IOException
Create a new HttpResponseWriter which is a copy of the specified HttpResponseReader. All headers and the body of the response are cloned.

Throws:
IOException
Method Detail

setHeader

public void setHeader(String name,
                      String value)
Sets a response header with the given name and value. If the header had already been set, the new value overwrites the previous one. The containsHeader method can be used to test for the presence of a header before setting its value.

Parameters:
name - the name of the header
value - the header value

getHeader

public String getHeader(String name)
Returns the value of the header if it's set, or null if it's not

Parameters:
name - the name of the header

addHeader

public void addHeader(String name,
                      String value)
Adds a response header with the given name and value. This method allows response headers to have multiple values.

Parameters:
name - the name of the header
value - the additional header value

clearHeader

public void clearHeader(String name)
Remove the header from the response. All values associated with this header name will be removed

Parameters:
name - the name of the header

containsHeader

public boolean containsHeader(String name)
Returns a boolean indicating whether the named response header has already been set.

Parameters:
name - the header name
Returns:
true if the named response header has already been set; false otherwise

setDateHeader

public void setDateHeader(String name,
                          long val)
Sets a response header with the given name and date-value. The date is specified in terms of milliseconds since the epoch. If the header had already been set, the new value overwrites the previous one. The containsHeader method can be used to test for the presence of a header before setting its value.

Parameters:
name - the name of the header to set
value - the assigned date value

addDateHeader

public void addDateHeader(String name,
                          long val)
Adds a response header with the given name and date-value. The date is specified in terms of milliseconds since the epoch. This method allows response headers to have multiple values.

Parameters:
name - the name of the header to set
value - the additional date value

addIntHeader

public void addIntHeader(String name,
                         int val)
Sets a response header with the given name and integer value. If the header had already been set, the new value overwrites the previous one. The containsHeader method can be used to test for the presence of a header before setting its value.

Parameters:
name - the name of the header
value - the assigned integer value

setIntHeader

public void setIntHeader(String name,
                         int val)
Adds a response header with the given name and integer value. This method allows response headers to have multiple values.

Parameters:
name - the name of the header
value - the assigned integer value

setLocale

public void setLocale(Locale loc)
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 getWriter(). By default, the response locale is the default locale for the server.


getLocale

public Locale getLocale()
Returns the locale assigned to the response


addCookie

public void addCookie(javax.servlet.http.Cookie cookie)
Adds the specified cookie to the response. This method can be called multiple times to set more than one cookie.

Parameters:
cookie - the cookie to add to the response

addCookie

public void addCookie(String name,
                      String value,
                      String domain,
                      String path,
                      Date date,
                      boolean secure)
Adds the specified cookie to the response. This method doesn't require the javax.servlet classes, and can be called multiple times to set more than one cookie.

Parameters:
name - the name of the cookie
value - the value of the cookie
domain - the domain of the cookie (optionally null)
path - the path of the cookie (optionally null)
date - the expiry date of the cookie (null for when the browser is closed, or a date in the past to delete the cookie)
secure - whether the cookie should only be sent over secure connections

getCharacterEncoding

public String getCharacterEncoding()

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.


setContentType

public void setContentType(String type)

Sets the content type of the response being sent to the client. The content type may include the type of character encoding used, for example, text/html; charset=ISO-8859-4.

If obtaining a PrintWriter, this method should be called first.

Parameters:
type - a String specifying the MIME type of the content

setStatus

public void setStatus(int status)
Sets the status code for this response. This method is used to set the return status code. The status message is the default message for the specified code.

Unlike the same method in the HttpServletResponse class, this method does not clear the response buffer.

Parameters:
status - the status code, between 100 and 599

setStatus

public void setStatus(int status,
                      String statusmsg)
Sets the status code for this response. This method is used to set the return status code and message.

Unlike the same method in the HttpServletResponse class, this method does not clear the response buffer.

Parameters:
status - the status code, between 100 and 599
statusmsg - the status message to send

getWriter

public PrintWriter getWriter()
                      throws UnsupportedEncodingException

Returns a PrintWriter object that can send character text to the client. The character encoding used is the one specified in the charset= property of the setContentType(java.lang.String) method, which must be called before calling this method for the charset to take effect.

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

Either this method or getOutputStream() may be called to write the body, not both.

Throws:
IllegalStateException - if the getOutputStream method has been called on this response
UnsupportedEncodingException - if the charset specified in setContentType cannot be used

getOutputStream

public OutputStream getOutputStream()

Returns an OutputStream suitable for writing binary data in the response.

Either this method or getWriter() may be called to write the body, not both.

Throws:
IllegalStateException - if the getWriter method has been called on this response

reset

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


resetBuffer

public void resetBuffer()
Clears the content of the underlying buffer in the response without clearing headers or status code.


writeTo

public void writeTo(javax.servlet.http.HttpServletResponse resp)
             throws IOException
Write the response to the specified HttpServletResponse.

Throws:
IOException

writeTo

public void writeTo(OutputStream out)
             throws IOException
Write the response to the specified OutputStream.

Throws:
IOException


Copyright © 2001-2004 Big Faceless Organization