ice.net
Class HttpMessage

java.lang.Object
  extended byice.net.HttpMessage
All Implemented Interfaces:
Cloneable, Serializable
Direct Known Subclasses:
HttpRequest, HttpResponse

public abstract class HttpMessage
extends Object
implements Cloneable, Serializable

The HttpMessage is an abstract class, which functions as the base class for HTTP messages containing the common functionality. It is responsible for all the HTTP header management and the HttpMessage.EntityBody.

Additionally, it has all the HTTP 1.0 and 1.1 general header, as well as a commonly used extension general header constant, and HTTP version constants as a convenience and uniform use of these HTTP elements.

Finally, it contains a couple of convenience methods, namely getBytes(), getLength() and getMessage() for byte related tasks with HTTP messages.

Subclasses have to implement at least the only abstract method getMessage(boolean) for the actual representation of the specific HTTP message.

For detailed information on the HyperText Transfer Protocol and some of its extensions see:

Since:
1.5.0
Version:
1.6.0
Author:
Jack van Ooststroom
See Also:
Serialized Form

Nested Class Summary
static class HttpMessage.EntityBody
          The EntityBody class represents the entity body of an HTTP message.
 
Field Summary
static String CACHE_CONTROL
          HTTP 1.1 general header: Cache-Control.
protected static String COLON
           
static String CONNECTION
          HTTP 1.1 general header: Connection.
protected static String CR_LF
           
static String DATE
          HTTP 1.0 general header: Date.
static int DISCONNECTED_OPERATION
          HTTP 1.1 warning code 112: Disconnected operation.
protected  HttpMessage.EntityBody entityBody
          This HttpMessage's entity body.
protected  HeaderMap headerMap
          The map containing all the headers for this HttpMessage.
static int HEURISTIC_EXPIRATION
          HTTP 1.1 warning code 113: Heuristic expiration.
static String HTTP_10
          HTTP 1.0 version: HTTP/1.0.
static String HTTP_11
          HTTP 1.1 version: HTTP/1.1
protected  String httpVersion
          This HttpMessage's HTTP version string.
static String KEEP_ALIVE
          HTTP general header (extension): Keep-Alive.
static int MISCELLANEOUS_PERSISTENT_WARNING
          HTTP 1.1 warning code 299: Miscellaneous persistent warning.
static int MISCELLANEOUS_WARNING
          HTTP 1.1 warning code 199: Miscellaneous warning.
static String PRAGMA
          HTTP 1.0 general header: Pragma.
static String PROXY_CONNECTION
          HTTP general header (extension): Proxy-Connection.
static int RESPONSE_IS_STALE
          HTTP 1.1 warning code 110: Response is stale.
static int REVALIDATION_FAILED
          HTTP 1.1 warning code 111: Revalidation failed.
protected static String SPACE
           
static String TRAILER
          HTTP 1.1 general header: Trailer.
static String TRANSFER_ENCODING
          HTTP 1.1 general header: Transfer-Encoding.
static int TRANSFORMATION_APPLIED
          HTTP 1.1 warning code 214: Transformation applied.
static String UPGRADE
          HTTP 1.1 general header: Upgrade.
static String VIA
          HTTP 1.1 general header: Via.
static String WARNING
          HTTP 1.1 general header: Warning.
 
Constructor Summary
protected HttpMessage(String httpVersion)
          Constructs a new HttpMessage with the specified httpVersion as the HTTP version.
 
Method Summary
 void clearHeaders()
          Removes all the headers from this HttpMessage.
 Object clone()
           
 boolean containsHeader(String fieldName)
          Returns true if this HttpMessage contains a header with the specified fieldName.
 void dispose()
          Releases all of the resources used by this HttpMessage and its subcomponent EntityBody, if it exists.
 boolean equals(Object object)
           
 byte[] getBytes()
          Convenience method for getting the byte array of this HttpMessage.
 HttpMessage.EntityBody getEntityBody()
          Returns the entity body of this HttpMessage.
 String getFieldName(int index)
          Returns the field name at the specified index in this HttpMessage.
 String getFieldValue(int index)
          Returns the field value at the specified index in this HttpMessage.
 String[] getFieldValues(String fieldName)
          Returns the field values that are mapped with the specified fieldName in this HttpMessage.
 HeaderMap.Header getHeader(int index)
          Returns the header at the specified index in this HttpMessage.
 int getHeaderCount()
          Returns the number of headers contained in this HttpMessage.
 String getHttpVersion()
          Returns the HTTP version of this HttpMessage.
 int getLength()
          Convenience method for getting the length of this HttpMessage.
 String getMessage()
          Convenience method for getting the actual String representation of this HttpMessage without the entity body.
abstract  String getMessage(boolean showBody)
          Convenience method for getting the actual String representation of this HttpMessage depending on the specified showBody with or without the entity body.
abstract  String getStartLine()
          Gets the start-line of this HttpMessage.
 void putHeader(String fieldName, String fieldValue)
          Maps the specified fieldValue with the specified fieldName in this HttpMessage.
 void removeHeader(int index)
          Removes the mapping at the specified index from this HttpMessage if present.
 void removeHeaders(String fieldName)
          Removes all mappings for the specified fieldName from this HttpMessage if one is present.
 void setEntityBody(HttpMessage.EntityBody entityBody)
          Sets the entity body of this HttpMessage to the specified entityBody.
 void setHttpVersion(String httpVersion)
          Sets the HTTP version of this HttpMessage to the specified httpVersion.
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CACHE_CONTROL

public static final String CACHE_CONTROL
HTTP 1.1 general header: Cache-Control.

See Also:
Constant Field Values

CONNECTION

public static final String CONNECTION
HTTP 1.1 general header: Connection.

See Also:
Constant Field Values

DATE

public static final String DATE
HTTP 1.0 general header: Date.

See Also:
Constant Field Values

PRAGMA

public static final String PRAGMA
HTTP 1.0 general header: Pragma.

See Also:
Constant Field Values

TRAILER

public static final String TRAILER
HTTP 1.1 general header: Trailer.

See Also:
Constant Field Values

TRANSFER_ENCODING

public static final String TRANSFER_ENCODING
HTTP 1.1 general header: Transfer-Encoding.

See Also:
Constant Field Values

UPGRADE

public static final String UPGRADE
HTTP 1.1 general header: Upgrade.

See Also:
Constant Field Values

VIA

public static final String VIA
HTTP 1.1 general header: Via.

See Also:
Constant Field Values

WARNING

public static final String WARNING
HTTP 1.1 general header: Warning.

See Also:
Constant Field Values

KEEP_ALIVE

public static final String KEEP_ALIVE
HTTP general header (extension): Keep-Alive.

See Also:
Constant Field Values

PROXY_CONNECTION

public static final String PROXY_CONNECTION
HTTP general header (extension): Proxy-Connection.

See Also:
Constant Field Values

HTTP_10

public static final String HTTP_10
HTTP 1.0 version: HTTP/1.0.

See Also:
Constant Field Values

HTTP_11

public static final String HTTP_11
HTTP 1.1 version: HTTP/1.1

See Also:
Constant Field Values

RESPONSE_IS_STALE

public static final int RESPONSE_IS_STALE
HTTP 1.1 warning code 110: Response is stale.

See Also:
Constant Field Values

REVALIDATION_FAILED

public static final int REVALIDATION_FAILED
HTTP 1.1 warning code 111: Revalidation failed.

See Also:
Constant Field Values

DISCONNECTED_OPERATION

public static final int DISCONNECTED_OPERATION
HTTP 1.1 warning code 112: Disconnected operation.

See Also:
Constant Field Values

HEURISTIC_EXPIRATION

public static final int HEURISTIC_EXPIRATION
HTTP 1.1 warning code 113: Heuristic expiration.

See Also:
Constant Field Values

MISCELLANEOUS_WARNING

public static final int MISCELLANEOUS_WARNING
HTTP 1.1 warning code 199: Miscellaneous warning.

See Also:
Constant Field Values

TRANSFORMATION_APPLIED

public static final int TRANSFORMATION_APPLIED
HTTP 1.1 warning code 214: Transformation applied.

See Also:
Constant Field Values

MISCELLANEOUS_PERSISTENT_WARNING

public static final int MISCELLANEOUS_PERSISTENT_WARNING
HTTP 1.1 warning code 299: Miscellaneous persistent warning.

See Also:
Constant Field Values

CR_LF

protected static final String CR_LF
See Also:
Constant Field Values

COLON

protected static final String COLON
See Also:
Constant Field Values

SPACE

protected static final String SPACE
See Also:
Constant Field Values

entityBody

protected HttpMessage.EntityBody entityBody
This HttpMessage's entity body.


headerMap

protected HeaderMap headerMap
The map containing all the headers for this HttpMessage.


httpVersion

protected String httpVersion
This HttpMessage's HTTP version string.

Constructor Detail

HttpMessage

protected HttpMessage(String httpVersion)
               throws IllegalArgumentException

Constructs a new HttpMessage with the specified httpVersion as the HTTP version.

Parameters:
httpVersion - the HTTP version of the HTTP message to be constructed.
Throws:
IllegalArgumentException - if the specified httpVersion is either null or empty.
Since:
1.5.0
See Also:
HTTP_10, HTTP_11
Method Detail

clearHeaders

public void clearHeaders()

Removes all the headers from this HttpMessage.

Since:
1.5.0

clone

public Object clone()

containsHeader

public boolean containsHeader(String fieldName)

Returns true if this HttpMessage contains a header with the specified fieldName.

Parameters:
fieldName - field name whose presence in this HttpMessage is to be tested.
Returns:
true if this HttpMessage contains a header with the specified fieldName.
Since:
1.5.0

dispose

public void dispose()

Releases all of the resources used by this HttpMessage and its subcomponent EntityBody, if it exists. That is, the resources will be destroyed and any memory they consume will be returned to the OS.

Since:
1.5.0

equals

public boolean equals(Object object)

getBytes

public byte[] getBytes()

Convenience method for getting the byte array of this HttpMessage. This method has the same effect as:

    getMessage(true).getBytes()

Returns:
the byte array representation of this HttpMessage.
Since:
1.5.0

getEntityBody

public HttpMessage.EntityBody getEntityBody()

Returns the entity body of this HttpMessage.

Returns:
the entity body.
Since:
1.5.0
See Also:
#setEntityBody(EntityBody)

getHeader

public HeaderMap.Header getHeader(int index)

Returns the header at the specified index in this HttpMessage.

Parameters:
index - the index of the header to return.
Returns:
the header at the specified index in this HttpMessage, or null.
Since:
1.5.0

getHeaderCount

public int getHeaderCount()

Returns the number of headers contained in this HttpMessage.

Returns:
the number of headers.
Since:
1.5.0

getFieldName

public String getFieldName(int index)

Returns the field name at the specified index in this HttpMessage.

Parameters:
index - the index of the field name to return.
Returns:
the field name at the specified index in this HttpMessage, or null.
Since:
1.5.0
See Also:
getFieldValue(int)

getFieldValue

public String getFieldValue(int index)

Returns the field value at the specified index in this HttpMessage.

Parameters:
index - the index of the field value to return.
Returns:
the field value at the specified index in this HttpMessage, or null.
Since:
1.5.0
See Also:
getFieldName(int)

getFieldValues

public String[] getFieldValues(String fieldName)

Returns the field values that are mapped with the specified fieldName in this HttpMessage.

If this HttpMessage contains more than one mapping with the specified fieldName, all the field values are returned.

Parameters:
fieldName - the field name whose mapped field value is to be returned.
Returns:
zero or more field values to which this HttpMessage maps the specified fieldName.
Since:
1.5.0

getHttpVersion

public String getHttpVersion()

Returns the HTTP version of this HttpMessage.

Returns:
the HTTP version.
Since:
1.5.0
See Also:
setHttpVersion(String)

getLength

public int getLength()

Convenience method for getting the length of this HttpMessage. This method has the same effect as:

    getBytes().length

Returns:
the length.
Since:
1.5.0

getMessage

public String getMessage()

Convenience method for getting the actual String representation of this HttpMessage without the entity body.

Returns:
the String representation.
Since:
1.5.0
See Also:
getMessage(boolean)

getMessage

public abstract String getMessage(boolean showBody)

Convenience method for getting the actual String representation of this HttpMessage depending on the specified showBody with or without the entity body.

Returns:
the String representation.
Since:
1.5.0

getStartLine

public abstract String getStartLine()

Gets the start-line of this HttpMessage.

Returns:
the start-line.
Since:
1.6.0

putHeader

public void putHeader(String fieldName,
                      String fieldValue)

Maps the specified fieldValue with the specified fieldName in this HttpMessage. If this HttpMessage previously contained a mapping for this fieldName, an additional mapping is created for the specified fieldValue.

Parameters:
fieldName - the field name with which the specified fieldValue is to be mapped.
fieldValue - the field value to be mapped with the specified fieldName.
Since:
1.5.0

removeHeader

public void removeHeader(int index)

Removes the mapping at the specified index from this HttpMessage if present.

Parameters:
index - the index of the mapping to be removed from this HttpMessage.
Since:
1.5.0

removeHeaders

public void removeHeaders(String fieldName)

Removes all mappings for the specified fieldName from this HttpMessage if one is present.

Parameters:
fieldName - the field name whose mappings are to be removed from this HttpMessage.
Since:
1.5.0

setEntityBody

public void setEntityBody(HttpMessage.EntityBody entityBody)

Sets the entity body of this HttpMessage to the specified entityBody.

Parameters:
entityBody - the new entity body.
Since:
1.5.0
See Also:
getEntityBody()

setHttpVersion

public void setHttpVersion(String httpVersion)
                    throws IllegalArgumentException

Sets the HTTP version of this HttpMessage to the specified httpVersion.

Parameters:
httpVersion - the new HTTP version.
Throws:
IllegalArgumentException - if the specified httpVersion is either null or empty.
Since:
1.5.0
See Also:
getHttpVersion()