com.flat502.rox.http
Class HttpMessage

java.lang.Object
  extended by com.flat502.rox.http.HttpMessage
Direct Known Subclasses:
HttpRequest, HttpResponse

public abstract class HttpMessage
extends java.lang.Object

This class represents an abstract HTTP message and provides support for various aspects of an HTTP message common to both requests and responses.

This class is not thread-safe.


Constructor Summary
protected HttpMessage(Encoding encoding)
          Constructs an instance
 
Method Summary
 java.lang.String addHeader(java.lang.String name, java.lang.String value)
          Add a header and value to this instance.
 java.lang.String formatDate(java.util.Date date)
          Formats a Date instance as described in section 3.1.1 of RFC 2616.
protected  java.lang.String getHeaderValue(java.lang.String name)
           
protected  java.lang.String getStartLine()
          Implementation should return the HTTP start line for this message (without a newline marker).
protected  java.lang.String getVersionString()
          Get the HTTP version string for this instance.
protected  boolean isHeaderSet(java.lang.String name)
          Check if a header has been set.
 byte[] marshal()
          A convenience routine that constructs a byte array representing this instance.
 void marshal(java.io.OutputStream os)
          Writes the HTTP message represented by this instance to a given OutputStream.
protected  void marshalContent(java.io.OutputStream contentStream)
          Writes this instance's content to the given stream.
protected  void marshalHeaders(java.io.OutputStream os)
          Writes all defined HTTP headers to the given stream.
protected abstract  void marshalStartLine(java.io.OutputStream os)
          Writes the HTTP start line for this message.
protected  java.lang.String normalizeHeaderName(java.lang.String name)
          Normalizes an HTTP header name.
 void setContent(byte[] content)
          Set the content for this HTTP message.
 java.lang.String setHeader(java.lang.String name, java.lang.String value)
          Set a header value, replacing any previously set value.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

HttpMessage

protected HttpMessage(Encoding encoding)
Constructs an instance

This method sets the Date header to the current date.

Parameters:
encoding - An Encoding describing the encoding to use when constructing this message. This also informs the Content-Encoding header value. May be null.
Method Detail

formatDate

public java.lang.String formatDate(java.util.Date date)
Formats a Date instance as described in section 3.1.1 of RFC 2616.

The actual format used is that described in RC 822 and mentioned in section 3.1.1 of RFC 2616 as the preferred format.

Parameters:
date - The date to format.
Returns:
A date formatted for use in an HTTP header.

setHeader

public java.lang.String setHeader(java.lang.String name,
                                  java.lang.String value)
Set a header value, replacing any previously set value.

Parameters:
name - The header name.
value - The header value.
Returns:
The previous value of the indicated header or null if no value was set.
See Also:
addHeader(String, String)

addHeader

public java.lang.String addHeader(java.lang.String name,
                                  java.lang.String value)
Add a header and value to this instance.

Header order is preserved. Headers are marshalled in the order in which they are added to this instance, with one exception: duplicate headers.

If this method is called multiple times for the same header values are folded into a single comma-separated value as allowed for by section 4 of RFC 2616.

Header names are normalized before being stored. As such, case is unimportant.

setHeader(String, String) allows for the replacement of headers.

Parameters:
name - The header name.
value - The header value.
Returns:
The new header value (including previous values if the header already existed).
See Also:
HttpConstants.Headers, setHeader(String, String), normalizeHeaderName(String)

getHeaderValue

protected java.lang.String getHeaderValue(java.lang.String name)

normalizeHeaderName

protected java.lang.String normalizeHeaderName(java.lang.String name)
Normalizes an HTTP header name.

Normalization produces the logical equivalent of the following algorithm.

The name is split into words using the '-' character. Each word is then normalized by converting the first character to uppercase and all remaining characters to lowercase. The resulting words are joined again using the '-' character. The result is returned.

Parameters:
name - The header name.
Returns:
The normalized header name.

setContent

public void setContent(byte[] content)
Set the content for this HTTP message.

This method sets the Content-Length header (using setHeader(String, String)) to the length of content.

Parameters:
content - The content to include in this HTTP message.

marshal

public void marshal(java.io.OutputStream os)
             throws java.io.IOException
Writes the HTTP message represented by this instance to a given OutputStream.

Parameters:
os - The stream to write to.
Throws:
java.io.IOException - If an error occurs writing to the stream or if any required character set cannot be loaded.

marshalStartLine

protected abstract void marshalStartLine(java.io.OutputStream os)
                                  throws java.io.IOException
Writes the HTTP start line for this message.

This implementation writes the string returned by getStartLine() followed by a newline marker.

The "start line" is described in section 4.1 of RFC 2616.

Parameters:
os - The stream to write the start line to.
Throws:
java.io.IOException

getStartLine

protected java.lang.String getStartLine()
Implementation should return the HTTP start line for this message (without a newline marker).

Returns:
The start line for this message.

marshalHeaders

protected void marshalHeaders(java.io.OutputStream os)
                       throws java.io.IOException
Writes all defined HTTP headers to the given stream.

If the Content-Type header has not yet been set it is set to 0 (but see setContent(byte[]) as well).

Headers are rendered in the order in which they were first added (using either addHeader(String, String) or setHeader(String, String)). If an existing header is replaced with a call to setHeader(String, String) it's rendered order is not affected.

Parameters:
os - The stream to write the start line to.
Throws:
java.io.IOException

marshalContent

protected void marshalContent(java.io.OutputStream contentStream)
                       throws java.io.IOException
Writes this instance's content to the given stream.

If setContent(byte[]) has not been invoked this method returns without doing anything.

Parameters:
contentStream - The stream to write to.
Throws:
java.io.IOException - if an error occurs writing to the stream.

marshal

public byte[] marshal()
               throws java.io.IOException
A convenience routine that constructs a byte array representing this instance.

This method is equivalent to invoking marshal(OutputStream) to a ByteArrayOutputStream instance and retrieving the byte array.

Returns:
A byte array representing this instance "on the wire".
Throws:
java.io.IOException - If an error occurs writing to the stream or if any required character set cannot be loaded.

getVersionString

protected java.lang.String getVersionString()
Get the HTTP version string for this instance.

Returns:
Always returns HTTP/1.1.

isHeaderSet

protected boolean isHeaderSet(java.lang.String name)
Check if a header has been set.

Case is unimportant.

Parameters:
name - The header name.
Returns:
true if the named header has been set.

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object