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.1 2001/09/02 01:13:08 gregwilkins Exp $
Author:
Greg Wilkins (gregw)

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[] __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 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.
 void commit()
          Commit the message.
 void commitHeader()
           
 void complete()
           
 boolean containsField(java.lang.String name)
          Does the header or trailer contain a field?
 void destroy()
          Destroy the message.
 java.lang.String getCharacterEncoding()
          Character Encoding.
 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()
           
 ChunkableInputStream getInputStream()
           
 int getIntField(java.lang.String name)
          Get a field as an integer value.
 java.lang.String getMimeType()
          Mime Type.
 ChunkableOutputStream 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.
 boolean isCommitted()
           
 boolean isDirty()
           
 void recycle(HttpConnection connection)
          Recycle the message.
 java.lang.String removeField(java.lang.String name)
          Remove a field.
 void setAcceptTrailer(boolean acceptTrailer)
          Set if trailers are accepted.
 void setCurrentTime(java.lang.String name)
          Sets the value of a date field to the current time.
 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
 java.lang.String toString()
          Convert to String.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

__MSG_EDITABLE

public static final int __MSG_EDITABLE
Message States.

__MSG_BAD

public static final int __MSG_BAD
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 java.lang.String[] __state

__HTTP_0_9

public static final java.lang.String __HTTP_0_9

__HTTP_1_0

public static final java.lang.String __HTTP_1_0

__HTTP_1_1

public static final java.lang.String __HTTP_1_1

__HTTP_1_X

public static final java.lang.String __HTTP_1_X
Method Detail

getHttpConnection

public HttpConnection getHttpConnection()

getInputStream

public ChunkableInputStream getInputStream()

getOutputStream

public ChunkableOutputStream 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:
 

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
Returns:
Old 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
value - 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
value - 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
value - the field date value
Throws:
java.lang.IllegalStateException - Not editable or sending 1.1 with trailers

setCurrentTime

public void setCurrentTime(java.lang.String name)
Sets the value of a date field to the current time. Header or Trailer fields are set depending on message state. Uses efficient DateCache mechanism.
Parameters:
name - the field name
value - 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.
Parameters:
acceptTrailer - If true, setField() may use trailers.

getCharacterEncoding

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

getMimeType

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

recycle

public void recycle(HttpConnection connection)
Recycle the message.

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

commitHeader

public void commitHeader()
                  throws java.io.IOException

commit

public void commit()
            throws java.io.IOException,
                   java.lang.IllegalStateException
Commit the message. Take whatever actions possible to move the message to the SENDING state.

isCommitted

public boolean isCommitted()
Returns:
 

isDirty

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

complete

public void complete()
              throws java.io.IOException


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