org.mortbay.http

Class HttpMessage

public abstract class HttpMessage extends Object

HTTP Message base. This class forms the basis of HTTP requests and replies. It provides header fields, content and optional trailer fields, while managing the state of the message.

Version: $Id: HttpMessage.java,v 1.41 2006/04/04 22:28:02 gregwilkins Exp $

Author: Greg Wilkins (gregw)

Nested Class Summary
interfaceHttpMessage.HeaderWriter
Field Summary
protected Map_attributes
protected String_characterEncoding
protected HttpConnection_connection
protected int_dotVersion
protected HttpFields_header
protected String_mimeType
protected int_state
protected String_version
protected Object_wrapper
static String__HTTP_0_9
static String__HTTP_1_0
static String__HTTP_1_1
static String__HTTP_1_X
static int__MSG_BAD
Message States.
static int__MSG_EDITABLE
Message States.
static int__MSG_RECEIVED
Message States.
static int__MSG_SENDING
Message States.
static int__MSG_SENT
Message States.
static String[]__state
static String__SCHEME
static String__SSL_SCHEME
Constructor Summary
protected HttpMessage()
Constructor.
protected HttpMessage(HttpConnection connection)
Constructor.
Method Summary
voidaddDateField(String name, Date date)
Adds the value of a date field.
voidaddDateField(String name, long date)
Add the value of a date field.
voidaddField(String name, String value)
Add to a multi-value field value.
voidaddIntField(String name, int value)
Adds the value of an integer field.
booleancontainsField(String name)
Does the header or trailer contain a field?
voiddestroy()
Destroy the message.
ObjectgetAttribute(String name)
Get a request attribute.
EnumerationgetAttributeNames()
Get Attribute names.
StringgetCharacterEncoding()
Character Encoding.
intgetContentLength()
StringgetContentType()
longgetDateField(String name)
Get a header as a date value.
intgetDotVersion()
Get the protocol version.
StringgetField(String name)
Get a message field.
EnumerationgetFieldNames()
Get field names.
EnumerationgetFieldValues(String name)
Get a multi valued message field.
EnumerationgetFieldValues(String name, String separators)
Get a multi valued message field.
HttpFieldsgetHeader()
Get the HTTP header fields.
HttpConnectiongetHttpConnection()
InputStreamgetInputStream()
intgetIntField(String name)
Get a field as an integer value.
StringgetMimeType()
Mime Type.
OutputStreamgetOutputStream()
intgetState()
Get the message state.
StringgetVersion()
Get the protocol version.
ObjectgetWrapper()
Get an associated wrapper object.
booleanisCommitted()
booleanisDirty()
voidremoveAttribute(String name)
Remove a request attribute.
StringremoveField(String name)
Remove a field.
protected voidreset()
ObjectsetAttribute(String name, Object attribute)
Set a request attribute.
voidsetCharacterEncoding(String encoding, boolean setField)
Set Character Encoding.
voidsetContentLength(int len)
voidsetContentType(String contentType)
voidsetDateField(String name, Date date)
Sets the value of a date field.
voidsetDateField(String name, long date)
Sets the value of a date field.
StringsetField(String name, String value)
Set a field value.
voidsetField(String name, List value)
Set a multi-value field value.
voidsetIntField(String name, int value)
Sets the value of an integer field.
intsetState(int state)
Set the message state.
voidsetVersion(String version)
Set the request version
voidsetWrapper(Object wrapper)
Set a wrapper object.
StringtoString()
Convert to String.
voidupdateMimeType()

Field Detail

_attributes

protected Map _attributes

_characterEncoding

protected String _characterEncoding

_connection

protected HttpConnection _connection

_dotVersion

protected int _dotVersion

_header

protected HttpFields _header

_mimeType

protected String _mimeType

_state

protected int _state

_version

protected String _version

_wrapper

protected Object _wrapper

__HTTP_0_9

public static final String __HTTP_0_9

__HTTP_1_0

public static final String __HTTP_1_0

__HTTP_1_1

public static final String __HTTP_1_1

__HTTP_1_X

public static final String __HTTP_1_X

__MSG_BAD

public static final int __MSG_BAD
Message States.

__MSG_EDITABLE

public static final int __MSG_EDITABLE
Message States.

__MSG_RECEIVED

public static final int __MSG_RECEIVED
Message States.

__MSG_SENDING

public static final int __MSG_SENDING
Message States.

__MSG_SENT

public static final int __MSG_SENT
Message States.

__state

public static final String[] __state

__SCHEME

public static final String __SCHEME

__SSL_SCHEME

public static final String __SSL_SCHEME

Constructor Detail

HttpMessage

protected HttpMessage()
Constructor.

HttpMessage

protected HttpMessage(HttpConnection connection)
Constructor.

Method Detail

addDateField

public void addDateField(String name, Date date)
Adds the value of a date field. Header or Trailer fields are set depending on message state.

Parameters: name the field name date the field date value

addDateField

public void addDateField(String name, long date)
Add the value of a date field. Header or Trailer fields are set depending on message state.

Parameters: name the field name date the field date value

Throws: IllegalStateException Not editable or sending 1.1 with trailers

addField

public void addField(String name, String value)
Add to a multi-value field value. If the message is editable, then a header field is set. Otherwise if the meesage is sending and a HTTP/1.1 version, then a trailer field is set.

Parameters: name Name of field value New value to add to the field

Throws: IllegalStateException Not editable or sending 1.1 with trailers

addIntField

public void addIntField(String name, int value)
Adds the value of an integer field. Header or Trailer fields are set depending on message state.

Parameters: name the field name value the field integer value

containsField

public boolean containsField(String name)
Does the header or trailer contain a field?

Parameters: name Name of the field

Returns: True if contained in header or trailer.

destroy

public void destroy()
Destroy the message. Help the garbage collector by nulling everything that we can.

getAttribute

public Object getAttribute(String name)
Get a request attribute.

Parameters: name Attribute name

Returns: Attribute value

getAttributeNames

public Enumeration getAttributeNames()
Get Attribute names.

Returns: Enumeration of Strings

getCharacterEncoding

public String getCharacterEncoding()
Character Encoding. The character encoding is extracted from the ContentType field when set.

Returns: Character Encoding or null

getContentLength

public int getContentLength()

getContentType

public String getContentType()

getDateField

public long getDateField(String name)
Get a header as a date value. Look in header and trailer fields. Returns the value of a date field, or -1 if not found. The case of the field name is ignored.

Parameters: name the case-insensitive field name

getDotVersion

public int getDotVersion()
Get the protocol version.

Returns: return the version dot (0.9=-1 1.0=0 1.1=1)

getField

public String getField(String name)
Get a message field. Get a field from a message header. If no header field is found, trailer fields are searched.

Parameters: name The field name

Returns: field value or null

getFieldNames

public Enumeration getFieldNames()
Get field names.

Returns: Enumeration of Field Names

getFieldValues

public Enumeration getFieldValues(String name)
Get a multi valued message field. Get a field from a message header.

Parameters: name The field name

Returns: Enumeration of field values or null

getFieldValues

public Enumeration getFieldValues(String name, String separators)
Get a multi valued message field. Get a field from a message header.

Parameters: name The field name separators String of separators.

Returns: Enumeration of field values or null

getHeader

public HttpFields getHeader()
Get the HTTP header fields.

Returns: Header or null

getHttpConnection

public HttpConnection getHttpConnection()

getInputStream

public InputStream getInputStream()

getIntField

public int getIntField(String name)
Get a field as an integer value. Look in header and trailer fields. Returns the value of an integer field, or -1 if not found. The case of the field name is ignored.

Parameters: name the case-insensitive field name

getMimeType

public String getMimeType()
Mime Type. The mime type is extracted from the contenttype field when set.

Returns: Content type without parameters

getOutputStream

public OutputStream getOutputStream()

getState

public int getState()
Get the message state.
 __MSG_EDITABLE = 0 - Created locally, all set methods enabled
 __MSG_BAD      = 1 - Bad message or send failure.
 __MSG_RECEIVED = 2 - Received from connection.
 __MSG_SENDING  = 3 - Headers sent.
 __MSG_SENT     = 4 - Entity and trailers sent.
 

Returns: the state.

getVersion

public String getVersion()
Get the protocol version.

Returns: return the version.

getWrapper

public Object getWrapper()
Get an associated wrapper object.

Returns: Wrapper message or null.

isCommitted

public boolean isCommitted()

isDirty

public boolean isDirty()

Returns: true if the message has been modified.

removeAttribute

public void removeAttribute(String name)
Remove a request attribute.

Parameters: name Attribute name

removeField

public String removeField(String name)
Remove a field. If the message is editable, then a header field is removed. Otherwise if the message is sending and a HTTP/1.1 version, then a trailer field is removed.

Parameters: name Name of field

Returns: Old value of field

reset

protected void reset()

setAttribute

public Object setAttribute(String name, Object attribute)
Set a request attribute.

Parameters: name Attribute name attribute Attribute value

Returns: Previous Attribute value

setCharacterEncoding

public void setCharacterEncoding(String encoding, boolean setField)
Set Character Encoding.

Parameters: encoding An encoding that can override the encoding set from the ContentType field.

setContentLength

public void setContentLength(int len)

setContentType

public void setContentType(String contentType)

setDateField

public void setDateField(String name, Date date)
Sets the value of a date field. Header or Trailer fields are set depending on message state.

Parameters: name the field name date the field date value

setDateField

public void setDateField(String name, long date)
Sets the value of a date field. Header or Trailer fields are set depending on message state.

Parameters: name the field name date the field date value

setField

public String setField(String name, String value)
Set a field value. If the message is editable, then a header field is set. Otherwise if the message is sending and a HTTP/1.1 version, then a trailer field is set.

Parameters: name Name of field value New value of field

Returns: Old value of field

setField

public void setField(String name, List value)
Set a multi-value field value. If the message is editable, then a header field is set. Otherwise if the meesage is sending and a HTTP/1.1 version, then a trailer field is set.

Parameters: name Name of field value New values of field

setIntField

public void setIntField(String name, int value)
Sets the value of an integer field. Header or Trailer fields are set depending on message state.

Parameters: name the field name value the field integer value

setState

public int setState(int state)
Set the message state. This method should be used by experts only as it can prevent normal handling of a request/response.

Parameters: state The new state

Returns: the last state.

setVersion

public void setVersion(String version)
Set the request version

Parameters: version the HTTP version string (eg HTTP/1.1)

Throws: IllegalStateException message is not EDITABLE

setWrapper

public void setWrapper(Object wrapper)
Set a wrapper object. A wrapper object is an object associated with this message and presents it with an different interface. The primary example of a HttpRequest facade is ServletHttpRequest. A single facade object may be associated with the message with this call and retrieved with the getFacade method.

toString

public String toString()
Convert to String. The message header is converted to a String.

Returns: String

updateMimeType

public void updateMimeType()
Copyright © 2004 Mortbay Consulting Pty. Ltd. All Rights Reserved.