com.flat502.rox.http
Class HttpMessageBuffer

java.lang.Object
  extended by com.flat502.rox.http.HttpMessageBuffer
Direct Known Subclasses:
HttpRequestBuffer, HttpResponseBuffer

public abstract class HttpMessageBuffer
extends java.lang.Object

Abstract base class for a buffer built up from one or more network messages, and containing an HTTP message.


Constructor Summary
protected HttpMessageBuffer(HttpRpcProcessor processor, java.net.Socket socket)
          Construct a new buffer for the given socket.
 
Method Summary
 int addBytes(byte[] newData, int offset, int count)
          Add new data received on the socket this buffer is associated with.
 byte[] getContent()
           
 java.nio.charset.Charset getContentCharset()
           
 java.io.Reader getContentReader()
           
 java.io.InputStream getContentStream()
           
 java.lang.String getContentType()
          Get Content-Type defined in this message.
 java.util.Map<java.lang.String,java.lang.String> getHeaders()
          Provides access to the headers in the underlying HTTP message.
 java.lang.String getHeaderValue(java.lang.String name)
           
abstract  double getHttpVersion()
           
abstract  java.lang.String getHttpVersionString()
           
 HttpRpcProcessor getOrigin()
           
 java.net.Socket getSocket()
          Get the socket data for this buffer was collected from.
 boolean isComplete()
          Test if this buffer contains a complete HTTP message.
protected  java.lang.String normalizeHeaderName(java.lang.String name)
          Called to normalize header names.
 java.lang.String toString()
           
protected abstract  void unpackPreamble(java.lang.String line)
           
protected  void validateHeaders()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

HttpMessageBuffer

protected HttpMessageBuffer(HttpRpcProcessor processor,
                            java.net.Socket socket)
Construct a new buffer for the given socket.

Parameters:
processor -
socket - The socket from which data for this buffer will be gathered.
Method Detail

getSocket

public java.net.Socket getSocket()
Get the socket data for this buffer was collected from.

Returns:
The socket data for this buffer was collected from.

getOrigin

public HttpRpcProcessor getOrigin()

addBytes

public int addBytes(byte[] newData,
                    int offset,
                    int count)
             throws java.lang.Exception
Add new data received on the socket this buffer is associated with.

This method gathers message fragments together. A call to this method is typically followed by a call to isComplete() to determine whether or not the buffer contains a complete HTTP message.

Parameters:
newData - The data to add to this buffer.
offset - The offset within newData to begin copying from.
count - The number of bytes from newData to copy.
Returns:
-1 if the message is incomplete, 0 if it is complete and a positive integer if the message is complete and additional data remains in the input buffer. In the latter case the returned value is the index into newData at which the excess data begins.
Throws:
java.lang.Exception - for the same reasons described under isComplete().

isComplete

public boolean isComplete()
                   throws java.lang.Exception
Test if this buffer contains a complete HTTP message.

Roughly speaking this test consists of three steps, ordered as follows:

  1. A test if a complete set of HTTP headers are present.
  2. A test if the Content-Length header is present.
  3. A test if the Content-Length header value and the content of the HTTP message are equal.

Returns:
true if this buffer holds a complete HTTP message, otherwise false.
Throws:
java.lang.Exception - if a problem occurs applying any of the tests mention above.

getHeaders

public java.util.Map<java.lang.String,java.lang.String> getHeaders()
Provides access to the headers in the underlying HTTP message.

This method returns an unmodifiable Map containing the headers in the underlying HTTP message. This map's key set iterator will return the keys in the order in which they appeared in the request.

Duplicate headers will have been combined into a single value as per section 4.2 of RFC 2616.

Returns:
An unmodifiable Map containing the headers in the underlying HTTP message.
Throws:
java.lang.IllegalStateException - if this request is not yet complete.

getHeaderValue

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

getContent

public byte[] getContent()

getContentStream

public java.io.InputStream getContentStream()
                                     throws java.io.IOException
Throws:
java.io.IOException

getContentReader

public java.io.Reader getContentReader()
                                throws java.io.IOException
Throws:
java.io.IOException

getContentType

public java.lang.String getContentType()
Get Content-Type defined in this message.

Returns:
The Content-Type or null if it is not specified.

getContentCharset

public java.nio.charset.Charset getContentCharset()

toString

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

unpackPreamble

protected abstract void unpackPreamble(java.lang.String line)
                                throws java.lang.Exception
Throws:
java.lang.Exception

getHttpVersion

public abstract double getHttpVersion()

getHttpVersionString

public abstract java.lang.String getHttpVersionString()

validateHeaders

protected void validateHeaders()
                        throws HttpBufferException
Throws:
HttpBufferException

normalizeHeaderName

protected java.lang.String normalizeHeaderName(java.lang.String name)
Called to normalize header names.

This is somewhat cosmetic in that it is used to ensure consistent access to the underlying Map of headers without producing headers that are unappealing. Most documentation uses headers like "Content-Type" and this method aims to ensure that headers are formatted using this "convention.

The header name is normalized as though the value were converted to lowercase split on the '-' character, the first letter of each item in the resulting list capitalized, and the updated list rejoined using the '-' as a separator.

Parameters:
name - The header name to normalize.
Returns:
The header name with the leading character of each "word" capitalized.