org.mortbay.http

Class HttpMessage

Known Direct Subclasses:
HttpRequest, HttpResponse

public abstract class HttpMessage
extends java.lang.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.40 2005/08/13 00:01:24 gregwilkins Exp $
Author:
Greg Wilkins (gregw)

Nested Class Summary

static interface
HttpMessage.HeaderWriter

Field Summary

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
__SCHEME
static String
__SSL_SCHEME
static String[]
__state
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

Constructor Summary

HttpMessage()
Constructor.
HttpMessage(HttpConnection connection)
Constructor.

Method Summary

void
addDateField(String name, Date date)
Adds the value of a date field.
void
addDateField(String name, long date)
Add the value of a date field.
void
addField(String name, String value)
Add to a multi-value field value.
void
addIntField(String name, int value)
Adds the value of an integer field.
boolean
containsField(String name)
Does the header or trailer contain a field?
void
destroy()
Destroy the message.
Object
getAttribute(String name)
Get a request attribute.
Enumeration
getAttributeNames()
Get Attribute names.
String
getCharacterEncoding()
Character Encoding.
int
getContentLength()
String
getContentType()
long
getDateField(String name)
Get a header as a date value.
int
getDotVersion()
Get the protocol version.
String
getField(String name)
Get a message field.
Enumeration
getFieldNames()
Get field names.
Enumeration
getFieldValues(String name)
Get a multi valued message field.
Enumeration
getFieldValues(String name, String separators)
Get a multi valued message field.
HttpFields
getHeader()
Get the HTTP header fields.
HttpConnection
getHttpConnection()
InputStream
getInputStream()
int
getIntField(String name)
Get a field as an integer value.
String
getMimeType()
Mime Type.
OutputStream
getOutputStream()
int
getState()
Get the message state.
String
getVersion()
Get the protocol version.
Object
getWrapper()
Get an associated wrapper object.
boolean
isCommitted()
boolean
isDirty()
void
removeAttribute(String name)
Remove a request attribute.
String
removeField(String name)
Remove a field.
protected void
reset()
Object
setAttribute(String name, Object attribute)
Set a request attribute.
void
setCharacterEncoding(String encoding, boolean setField)
Set Character Encoding.
void
setContentLength(int len)
void
setContentType(String contentType)
void
setDateField(String name, Date date)
Sets the value of a date field.
void
setDateField(String name, long date)
Sets the value of a date field.
void
setField(String name, List value)
Set a multi-value field value.
String
setField(String name, String value)
Set a field value.
void
setIntField(String name, int value)
Sets the value of an integer field.
int
setState(int state)
Set the message state.
void
setVersion(String version)
Set the request version
void
setWrapper(Object wrapper)
Set a wrapper object.
String
toString()
Convert to String.
void
updateMimeType()

Field Details

__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.
Field Value:
1

__MSG_EDITABLE

public static final int __MSG_EDITABLE
Message States.
Field Value:
0

__MSG_RECEIVED

public static final int __MSG_RECEIVED
Message States.
Field Value:
2

__MSG_SENDING

public static final int __MSG_SENDING
Message States.
Field Value:
3

__MSG_SENT

public static final int __MSG_SENT
Message States.
Field Value:
4

__SCHEME

public static final String __SCHEME

__SSL_SCHEME

public static final String __SSL_SCHEME

__state

public static final String[] __state

_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

Constructor Details

HttpMessage

protected HttpMessage()
Constructor.

HttpMessage

protected HttpMessage(HttpConnection connection)
Constructor.

Method Details

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

addField

public void addField(String name,
                     String value)
            throws IllegalStateException
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

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)
            throws IllegalStateException
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 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

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

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)

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.