org.simpleframework.http.message
Class MessageHeader

java.lang.Object
  extended by org.simpleframework.http.message.MessageHeader
All Implemented Interfaces:
Message

public class MessageHeader
extends Object
implements Message

The Message object is used to store an retrieve the headers for both a request and response. Headers are stored and retrieved in a case insensitive manner according to RFC 2616. The message also allows multiple header values to be added to a single header name, headers such as Cookie and Set-Cookie can be added multiple times with different values.

Author:
Niall Gallagher

Constructor Summary
MessageHeader()
          Constructor for the Message object.
 
Method Summary
 void addDate(String name, long date)
          This is used as a convenience method for adding a header that needs to be parsed into a HTTPdate string.
 void addInteger(String name, int value)
          This can be used to add a HTTP message header to this object.
 void addValue(String name, String value)
          This can be used to add a HTTP message header to this object.
 List<String> getAll(String name)
          This is used to acquire all the individual header values from the message.
 Cookie getCookie(String name)
          This returns the Cookie object stored under the specified name.
 List<Cookie> getCookies()
          This returns all Cookie objects stored under the specified name.
 long getDate(String name)
          This can be used to get the value of the first message header that has the specified name.
 int getInteger(String name)
          This can be used to get the value of the first message header that has the specified name.
 long getLong(String name)
          This can be used to get the value of the first message header that has the specified name.
 List<String> getNames()
          This is used to acquire the names of the of the headers that have been set in the response.
 String getValue(String name)
          This can be used to get the value of the first message header that has the specified name.
 String getValue(String name, int index)
          This can be used to get the value of the first message header that has the specified name.
 List<String> getValues(List<String> list)
          This can be used to get the values of HTTP message headers that have the specified name.
 List<String> getValues(String name)
          This can be used to get the values of HTTP message headers that have the specified name.
 Cookie setCookie(Cookie cookie)
          The setCookie method is used to set a cookie value with the cookie name.
 Cookie setCookie(String name, String value)
          The setCookie method is used to set a cookie value with the cookie name.
 void setDate(String name, long date)
          This is used as a convenience method for adding a header that needs to be parsed into a HTTP date string.
 void setInteger(String name, int value)
          This can be used to set a HTTP message header to this object.
 void setLong(String name, long value)
          This can be used to set a HTTP message header to this object.
 void setValue(String name, String value)
          This can be used to set a HTTP message header to this object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MessageHeader

public MessageHeader()
Constructor for the Message object. This is used to create a case insensitive means for storing HTTP header names and values. Dates can also be added to message as a long value and is converted to RFC 1123 compliant date string.

Method Detail

getNames

public List<String> getNames()
This is used to acquire the names of the of the headers that have been set in the response. This can be used to acquire all header values by name that have been set within the response. If no headers have been set this will return an empty list.

Specified by:
getNames in interface Message
Returns:
a list of strings representing the set header names

setValue

public void setValue(String name,
                     String value)
This can be used to set a HTTP message header to this object. The name and value of the HTTP message header will be used to create a HTTP message header object which can be retrieved using the getValue in combination with the get methods. This will perform a remove using the issued header name before the header value is set.

Specified by:
setValue in interface Message
Parameters:
name - the name of the HTTP message header to be added
value - the value the HTTP message header will have

setInteger

public void setInteger(String name,
                       int value)
This can be used to set a HTTP message header to this object. The name and value of the HTTP message header will be used to create a HTTP message header object which can be retrieved using the getValue in combination with the get methods. This will perform a remove using the issued header name before the header value is set.

Specified by:
setInteger in interface Message
Parameters:
name - the name of the HTTP message header to be added
value - the value the HTTP message header will have

setLong

public void setLong(String name,
                    long value)
This can be used to set a HTTP message header to this object. The name and value of the HTTP message header will be used to create a HTTP message header object which can be retrieved using the getValue in combination with the get methods. This will perform a remove using the issued header name before the header value is set.

Parameters:
name - the name of the HTTP message header to be added
value - the value the HTTP message header will have

setDate

public void setDate(String name,
                    long date)
This is used as a convenience method for adding a header that needs to be parsed into a HTTP date string. This will convert the date given into a date string defined in RFC 2616 sec 3.3.1. This will perform a remove using the issued header name before the header value is set.

Specified by:
setDate in interface Message
Parameters:
name - the name of the HTTP message header to be added
date - the value constructed as an RFC 1123 date string

addValue

public void addValue(String name,
                     String value)
This can be used to add a HTTP message header to this object. The name and value of the HTTP message header will be used to create a HTTP message header object which can be retrieved using the getValue in combination with the get methods.

Specified by:
addValue in interface Message
Parameters:
name - the name of the HTTP message header to be added
value - the value the HTTP message header will have

addInteger

public void addInteger(String name,
                       int value)
This can be used to add a HTTP message header to this object. The name and value of the HTTP message header will be used to create a HTTP message header object which can be retrieved using the getInteger in combination with the get methods.

Specified by:
addInteger in interface Message
Parameters:
name - the name of the HTTP message header to be added
value - the value the HTTP message header will have

addDate

public void addDate(String name,
                    long date)
This is used as a convenience method for adding a header that needs to be parsed into a HTTPdate string. This will convert the date given into a date string defined in RFC 2616 sec 3.3.1.

Specified by:
addDate in interface Message
Parameters:
name - the name of the HTTP message header to be added
date - the value constructed as an RFC 1123 date string

getValue

public String getValue(String name)
This can be used to get the value of the first message header that has the specified name. This will return the full string representing the named header value. If the named header does not exist then this will return a null value.

Specified by:
getValue in interface Message
Parameters:
name - the HTTP message header to get the value from
Returns:
this returns the value that the HTTP message header

getValue

public String getValue(String name,
                       int index)
This can be used to get the value of the first message header that has the specified name. This will return the full string representing the named header value. If the named header does not exist then this will return a null value.

Specified by:
getValue in interface Message
Parameters:
name - the HTTP message header to get the value from
index - this is the index to get the value from
Returns:
this returns the value that the HTTP message header

getInteger

public int getInteger(String name)
This can be used to get the value of the first message header that has the specified name. This will return the integer representing the named header value. If the named header does not exist then this will return a value of minus one, -1.

Specified by:
getInteger in interface Message
Parameters:
name - the HTTP message header to get the value from
Returns:
this returns the value that the HTTP message header

getLong

public long getLong(String name)
This can be used to get the value of the first message header that has the specified name. This will return the long representing the named header value. If the named header does not exist then this will return a value of minus one, -1.

Parameters:
name - the HTTP message header to get the value from
Returns:
this returns the value that the HTTP message header

getDate

public long getDate(String name)
This can be used to get the value of the first message header that has the specified name. This will return the long value representing the named header value. If the named header does not exist then this will return a value of minus one, -1.

Specified by:
getDate in interface Message
Parameters:
name - the HTTP message header to get the value from
Returns:
this returns the value that the HTTP message header

getCookie

public Cookie getCookie(String name)
This returns the Cookie object stored under the specified name. This is used to retrieve cookies that have been set with the setCookie methods. If the cookie does not exist under the specified name this will return null.

Specified by:
getCookie in interface Message
Parameters:
name - this is the name of the cookie to be retrieved
Returns:
returns the Cookie by the given name

getCookies

public List<Cookie> getCookies()
This returns all Cookie objects stored under the specified name. This is used to retrieve cookies that have been set with the setCookie methods. If there are no cookies then this will return an empty list.

Specified by:
getCookies in interface Message
Returns:
returns all the Cookie in the response

setCookie

public Cookie setCookie(String name,
                        String value)
The setCookie method is used to set a cookie value with the cookie name. This will add a cookie to the response stored under the name of the cookie, when this is committed it will be added as a Set-Cookie header to the resulting response. This is a convenience method that avoids cookie creation.

Specified by:
setCookie in interface Message
Parameters:
name - this is the cookie to be added to the response
value - this is the cookie value that is to be used
Returns:
returns the cookie that has been set in the response

setCookie

public Cookie setCookie(Cookie cookie)
The setCookie method is used to set a cookie value with the cookie name. This will add a cookie to the response stored under the name of the cookie, when this is committed it will be added as a Set-Cookie header to the resulting response.

Specified by:
setCookie in interface Message
Parameters:
cookie - this is the cookie to be added to the response
Returns:
returns the cookie that has been set in the response

getValues

public List<String> getValues(String name)
This can be used to get the values of HTTP message headers that have the specified name. This is a convenience method that will present that values as tokens extracted from the header. This has obvious performance benefits as it avoids having to deal with substring and trim calls.

The tokens returned by this method are ordered according to there HTTP quality values, or "q" values, see RFC 2616 section 3.9. This also strips out the quality parameter from tokens returned. So "image/html; q=0.9" results in "image/html". If there are no "q" values present then order is by appearance.

The result from this is either the trimmed header value, that is, the header value with no leading or trailing whitespace or an array of trimmed tokens ordered with the most preferred in the lower indexes, so index 0 is has highest preference.

Specified by:
getValues in interface Message
Parameters:
name - the name of the headers that are to be retrieved
Returns:
ordered list of tokens extracted from the header(s)

getValues

public List<String> getValues(List<String> list)
This can be used to get the values of HTTP message headers that have the specified name. This is a convenience method that will present that values as tokens extracted from the header. This has obvious performance benefits as it avoids having to deal with substring and trim calls.

The tokens returned by this method are ordered according to there HTTP quality values, or "q" values, see RFC 2616 section 3.9. This also strips out the quality parameter from tokens returned. So "image/html; q=0.9" results in "image/html". If there are no "q" values present then order is by appearance.

The result from this is either the trimmed header value, that is, the header value with no leading or trailing whitespace or an array of trimmed tokens ordered with the most preferred in the lower indexes, so index 0 is has highest preference.

Specified by:
getValues in interface Message
Parameters:
list - this is the list of individual header values
Returns:
ordered list of tokens extracted from the header(s)

getAll

public List<String> getAll(String name)
This is used to acquire all the individual header values from the message. The header values provided by this are unparsed and represent the actual string values that have been added to the message keyed by a given header name.

Specified by:
getAll in interface Message
Parameters:
name - the name of the header to get the values for
Returns:
this returns a list of the values for the header name


Copyright © 2014. All Rights Reserved.