org.mortbay.http
Class HttpMessage

java.lang.Object
  |
  +--org.mortbay.http.HttpMessage
Direct Known 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.26.2.2 2004/01/30 06:27:16 gregwilkins Exp $
Author:
Greg Wilkins (gregw)

Nested Class Summary
static interface HttpMessage.HeaderWriter
           
 
Field Summary
static java.lang.String __HTTP_0_9
           
static java.lang.String __HTTP_1_0
           
static java.lang.String __HTTP_1_1
           
static java.lang.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 java.lang.String __SCHEME
           
static java.lang.String __SSL_SCHEME
           
static java.lang.String[] __state
           
 
Method Summary
 boolean acceptTrailer()
          Set if trailers are accepted.
 void addDateField(java.lang.String name, java.util.Date date)
          Adds the value of a date field.
 void addDateField(java.lang.String name, long date)
          Add the value of a date field.
 void addField(java.lang.String name, java.lang.String value)
          Add to a multi-value field value.
 void addIntField(java.lang.String name, int value)
          Adds the value of an integer field.
 boolean containsField(java.lang.String name)
          Does the header or trailer contain a field?
 void destroy()
          Destroy the message.
 java.lang.Object getAttribute(java.lang.String name)
          Get a request attribute.
 java.util.Enumeration getAttributeNames()
          Get Attribute names.
 java.lang.String getCharacterEncoding()
          Character Encoding.
 int getContentLength()
           
 java.lang.String getContentType()
           
 long getDateField(java.lang.String name)
          Get a header as a date value.
 int getDotVersion()
          Get the protocol version.
 java.lang.String getField(java.lang.String name)
          Get a message field.
 java.util.Enumeration getFieldNames()
          Get field names.
 java.util.Enumeration getFieldValues(java.lang.String name)
          Get a multi valued message field.
 java.util.Enumeration getFieldValues(java.lang.String name, java.lang.String separators)
          Get a multi valued message field.
 HttpFields getHeader()
          Get the HTTP header fields.
 HttpConnection getHttpConnection()
           
 java.io.InputStream getInputStream()
           
 int getIntField(java.lang.String name)
          Get a field as an integer value.
 java.lang.String getMimeType()
          Mime Type.
 java.io.OutputStream getOutputStream()
           
 int getState()
          Get the message state.
 HttpFields getTrailer()
          Get the HTTP chunked trailer (also called trailer).
 java.lang.String getVersion()
          Get the protocol version.
 java.lang.Object getWrapper()
          Get an associated wrapper object.
 boolean isCommitted()
           
 boolean isDirty()
           
 void removeAttribute(java.lang.String name)
          Remove a request attribute.
 java.lang.String removeField(java.lang.String name)
          Remove a field.
 void setAcceptTrailer(boolean acceptTrailer)
          Set if trailers are accepted.
 java.lang.Object setAttribute(java.lang.String name, java.lang.Object attribute)
          Set a request attribute.
 void setCharacterEncoding(java.lang.String encoding)
          Set Character Encoding.
 void setContentLength(int len)
           
 void setContentType(java.lang.String contentType)
           
 void setDateField(java.lang.String name, java.util.Date date)
          Sets the value of a date field.
 void setDateField(java.lang.String name, long date)
          Sets the value of a date field.
 void setField(java.lang.String name, java.util.List value)
          Set a multi-value field value.
 java.lang.String setField(java.lang.String name, java.lang.String value)
          Set a field value.
 void setIntField(java.lang.String name, int value)
          Sets the value of an integer field.
 int setState(int state)
          Set the message state.
 void setVersion(java.lang.String version)
          Set the request version
 void setWrapper(java.lang.Object wrapper)
          Set a wrapper object.
 java.lang.String toString()
          Convert to String.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

__SCHEME

public static final java.lang.String __SCHEME
See Also:
Constant Field Values

__SSL_SCHEME

public static final java.lang.String __SSL_SCHEME
See Also:
Constant Field Values

__HTTP_0_9

public static final java.lang.String __HTTP_0_9
See Also:
Constant Field Values

__HTTP_1_0

public static final java.lang.String __HTTP_1_0
See Also:
Constant Field Values

__HTTP_1_1

public static final java.lang.String __HTTP_1_1
See Also:
Constant Field Values

__HTTP_1_X

public static final java.lang.String __HTTP_1_X
See Also:
Constant Field Values

__MSG_EDITABLE

public static final int __MSG_EDITABLE
Message States.

See Also:
Constant Field Values

__MSG_BAD

public static final int __MSG_BAD
Message States.

See Also:
Constant Field Values

__MSG_RECEIVED

public static final int __MSG_RECEIVED
Message States.

See Also:
Constant Field Values

__MSG_SENDING

public static final int __MSG_SENDING
Message States.

See Also:
Constant Field Values

__MSG_SENT

public static final int __MSG_SENT
Message States.

See Also:
Constant Field Values

__state

public static final java.lang.String[] __state
Method Detail

setWrapper

public void setWrapper(java.lang.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.


getWrapper

public java.lang.Object getWrapper()
Get an associated wrapper object.

Returns:
Wrapper message or null.

getHttpConnection

public HttpConnection getHttpConnection()

getInputStream

public java.io.InputStream getInputStream()

getOutputStream

public java.io.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.

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.

getVersion

public java.lang.String getVersion()
Get the protocol version.

Returns:
return the version.

getDotVersion

public int getDotVersion()
Get the protocol version.

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

getFieldNames

public java.util.Enumeration getFieldNames()
Get field names.

Returns:
Enumeration of Field Names

containsField

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

Parameters:
name - Name of the field
Returns:
True if contained in header or trailer.

getField

public java.lang.String getField(java.lang.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

getFieldValues

public java.util.Enumeration getFieldValues(java.lang.String name)
Get a multi valued 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:
Enumeration of field values or null

getFieldValues

public java.util.Enumeration getFieldValues(java.lang.String name,
                                            java.lang.String separators)
Get a multi valued message field. Get a field from a message header. If no header field is found, trailer fields are searched.

Parameters:
name - The field name
separators - String of separators.
Returns:
Enumeration of field values or null

setField

public java.lang.String setField(java.lang.String name,
                                 java.lang.String value)
                          throws java.lang.IllegalStateException
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
Throws:
java.lang.IllegalStateException - Not editable or sending 1.1 with trailers

setField

public void setField(java.lang.String name,
                     java.util.List value)
              throws java.lang.IllegalStateException
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
Throws:
java.lang.IllegalStateException - Not editable or sending 1.1 with trailers

addField

public void addField(java.lang.String name,
                     java.lang.String value)
              throws java.lang.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
Throws:
java.lang.IllegalStateException - Not editable or sending 1.1 with trailers

getIntField

public int getIntField(java.lang.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

setIntField

public void setIntField(java.lang.String name,
                        int value)
                 throws java.lang.IllegalStateException
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
Throws:
java.lang.IllegalStateException - Not editable or sending 1.1 with trailers

addIntField

public void addIntField(java.lang.String name,
                        int value)
                 throws java.lang.IllegalStateException
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
Throws:
java.lang.IllegalStateException - Not editable or sending 1.1 with trailers

getDateField

public long getDateField(java.lang.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

setDateField

public void setDateField(java.lang.String name,
                         java.util.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
Throws:
java.lang.IllegalStateException - Not editable or sending 1.1 with trailers

addDateField

public void addDateField(java.lang.String name,
                         java.util.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
Throws:
java.lang.IllegalStateException - Not editable or sending 1.1 with trailers

setDateField

public void setDateField(java.lang.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
Throws:
java.lang.IllegalStateException - Not editable or sending 1.1 with trailers

addDateField

public void addDateField(java.lang.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:
java.lang.IllegalStateException - Not editable or sending 1.1 with trailers

removeField

public java.lang.String removeField(java.lang.String name)
                             throws java.lang.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
Throws:
java.lang.IllegalStateException - Not editable or sending 1.1 with trailers

setVersion

public void setVersion(java.lang.String version)
Set the request version

Parameters:
version - the HTTP version string (eg HTTP/1.1)
Throws:
java.lang.IllegalStateException - message is not EDITABLE

getHeader

public HttpFields getHeader()
Get the HTTP header fields.

Returns:
Header or null

getTrailer

public HttpFields getTrailer()
Get the HTTP chunked trailer (also called trailer).

Returns:
Trailer or null

setAcceptTrailer

public void setAcceptTrailer(boolean acceptTrailer)
Set if trailers are accepted.

Parameters:
acceptTrailer - If true, setField() may use trailers.

acceptTrailer

public boolean acceptTrailer()
Set if trailers are accepted.


getCharacterEncoding

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

Returns:
Character Encoding or null

setCharacterEncoding

public void setCharacterEncoding(java.lang.String encoding)
Set Character Encoding.

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

getContentLength

public int getContentLength()

setContentLength

public void setContentLength(int len)

getContentType

public java.lang.String getContentType()

setContentType

public void setContentType(java.lang.String contentType)

getMimeType

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

Returns:
Content type without parameters

destroy

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


toString

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

Overrides:
toString in class java.lang.Object
Returns:
String

isCommitted

public boolean isCommitted()

isDirty

public boolean isDirty()
Returns:
true if the message has been modified.

getAttribute

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

Parameters:
name - Attribute name
Returns:
Attribute value

setAttribute

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

Parameters:
name - Attribute name
attribute - Attribute value
Returns:
Previous Attribute value

getAttributeNames

public java.util.Enumeration getAttributeNames()
Get Attribute names.

Returns:
Enumeration of Strings

removeAttribute

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

Parameters:
name - Attribute name


Copyright ? 2000 Mortbay Consulting Pty. Ltd. All Rights Reserved.