org.jwall.web.http.nio
Class HttpMessageChannel<T extends HttpMessage>

java.lang.Object
  extended by org.jwall.web.http.nio.HttpMessageChannel<T>
Type Parameters:
T - The message-type (i.e. HttpRequest or HttpResponse).
Direct Known Subclasses:
HttpRequestChannel, HttpResponseChannel

public abstract class HttpMessageChannel<T extends HttpMessage>
extends Object

Author:
Christian Bockermann <chris@jwall.org>

Field Summary
static int STATE_READING_BODY
          After the header has been read, this constant defines the body-reading phase
static int STATE_READING_HEADER
          This constant defines the state from the start until a complete message header has been read
 
Method Summary
 void close()
          This method closes the message-channel and also closes the underlying byte-channel.
 int getState()
          This method returns the int-code of the channel's current state.
 Integer getTimeout()
          This method returns the current timeout (in milliseconds) of the channel.
 ByteBuffer readBody(int contentLength)
          This method reads the body of a HTTP message of the given length.
 HttpHeader readHeader()
          This method tries to read a HTTP message header from the currently available bytes.
abstract  T readMessage()
           
 void setTimeout(Integer to)
          This method is used to set the timeout (in milliseconds) of the channel.
 boolean timedOut()
          This method can be used to check if the channel is timed out or not.
 void touch()
          This method simply touches the channel an sets the time of the last read to System.currentTimeMillis().
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

STATE_READING_HEADER

public static final int STATE_READING_HEADER
This constant defines the state from the start until a complete message header has been read

See Also:
Constant Field Values

STATE_READING_BODY

public static final int STATE_READING_BODY
After the header has been read, this constant defines the body-reading phase

See Also:
Constant Field Values
Method Detail

readHeader

public HttpHeader readHeader()
                      throws TimeOutException,
                             IOException,
                             ProtocolException
This method tries to read a HTTP message header from the currently available bytes. If the header cannot be completely read, then the partial header is saved and null is returned. The method is re-entrant in the way, that a later invocation resumes the previously header-read.

Returns:
The header if it can completely be read or null otherwise.
Throws:
TimeOutException - In case the channel timed out.
IOException - If a read-error occurs.
ProtocolException - In case the header could not be parsed or the method is called in a wrong state (e.g. body-reading).

readBody

public ByteBuffer readBody(int contentLength)
                    throws IOException,
                           TimeOutException,
                           ProtocolException
This method reads the body of a HTTP message of the given length.

Parameters:
contentLength -
Returns:
The complete buffer or null if less than contentLength bytes can be read without blocking.
Throws:
IOException - In case an I/O error occurred.
TimeOutException - If this method is called after the channel timed out.
ProtocolException - In case a parsing error occured (unlikely ;-) or the method was called in an incorrect state (reading header state).

getState

public int getState()
This method returns the int-code of the channel's current state.

Returns:
The current state of the channel.

close

public void close()
           throws IOException
This method closes the message-channel and also closes the underlying byte-channel.

Throws:
IOException - In case closing the underlying byte-channel resulted in an I/O error.

getTimeout

public Integer getTimeout()
This method returns the current timeout (in milliseconds) of the channel.

Returns:
The timeout in milliseconds.

setTimeout

public void setTimeout(Integer to)
This method is used to set the timeout (in milliseconds) of the channel. This does not reset the time-stamp.

Parameters:
to - The timeout (milliseconds).

timedOut

public boolean timedOut()
This method can be used to check if the channel is timed out or not.

Returns:
true, if the last read was more than timeout milliseconds ago, otherwise false.

touch

public void touch()
This method simply touches the channel an sets the time of the last read to System.currentTimeMillis().


readMessage

public abstract T readMessage()
                                           throws TimeOutException,
                                                  IOException,
                                                  ProtocolException
Throws:
TimeOutException
IOException
ProtocolException


Copyright © 2012 jwall.org. All Rights Reserved.