org.apache.commons.httpclient

Class HeaderElement

Implemented Interfaces:
Serializable

public class HeaderElement
extends NameValuePair

One element of an HTTP header's value.

Some HTTP headers (such as the set-cookie header) have values that can be decomposed into multiple elements. Such headers must be in the following form:

 header  = [ element ] *( "," [ element ] )
 element = name [ "=" [ value ] ] *( ";" [ param ] )
 param   = name [ "=" [ value ] ]

 name    = token
 value   = ( token | quoted-string )

 token         = 1*<any char except "=", ",", ";", <"> and
                       white space>
 quoted-string = <"> *( text | quoted-char ) <">
 text          = any char except <">
 quoted-char   = "\" char
 

Any amount of white space is allowed between any part of the header, element or param and is ignored. A missing value in any element or param will be stored as the empty String; if the "=" is also missing null will be stored instead.

This class represents an individual header element, containing both a name/value pair (value may be null) and optionally a set of additional parameters.

This class also exposes a parse(String) method for parsing a Header value into an array of elements.

Since:
1.0
See Also:
Header, Serialized Form

Constructor Summary

HeaderElement()
Default constructor.
HeaderElement(char[] chars)
Constructor with array of characters.
HeaderElement(char[] chars, int offset, int length)
Constructor with array of characters.
HeaderElement(String name, String value)
Constructor.
HeaderElement(String name, String value, NameValuePair[] parameters)
Constructor with name, value and parameters.

Method Summary

NameValuePair
getParameterByName(String name)
Returns parameter with the given name, if found.
NameValuePair[]
getParameters()
Get parameters, if any.
static HeaderElement[]
parse(String headerValue)
Deprecated. Use #parseElements(String).
static HeaderElement[]
parseElements(char[] headerValue)
This parses the value part of a header.
static HeaderElement[]
parseElements(String headerValue)
This parses the value part of a header.

Methods inherited from class org.apache.commons.httpclient.NameValuePair

equals, getName, getValue, hashCode, setName, setValue, toString

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Details

HeaderElement

public HeaderElement()
Default constructor.

HeaderElement

public HeaderElement(char[] chars)
Constructor with array of characters.
Parameters:
chars - the array of characters
Since:
3.0
Usages and Demos :

View More Examples of HeaderElement(char[] chars)
   1:         }
   2:         HeaderElement headerelement = new HeaderElement(header.toCharArray());
   3:         Cookie cookie = new Cookie(host,
   4:                        headerelement.getName(),
   5:                        headerelement.getValue(),

View Full Code Here
   1:             headerElements = new HeaderElement[] {
   2:                     new HeaderElement(header.toCharArray())
   3:             };
   4:         } else {
   5:             headerElements = HeaderElement.parseElements(header.toCharArray());

View Full Code Here

HeaderElement

public HeaderElement(char[] chars,
                     int offset,
                     int length)
Constructor with array of characters.
Parameters:
chars - the array of characters
offset - - the initial offset.
length - - the length.
Since:
3.0

HeaderElement

public HeaderElement(String name,
                     String value)
Constructor.
Parameters:
name - my name
value - my (possibly null) value

HeaderElement

public HeaderElement(String name,
                     String value,
                     NameValuePair[] parameters)
Constructor with name, value and parameters.
Parameters:
name - my name
value - my (possibly null) value
parameters - my (possibly null) parameters

Method Details

getParameterByName

public NameValuePair getParameterByName(String name)
Returns parameter with the given name, if found. Otherwise null is returned
Parameters:
name - The name to search by.
Returns:
NameValuePair parameter with the given name

getParameters

public NameValuePair[] getParameters()
Get parameters, if any.
Returns:
parameters as an array of NameValuePairs
Since:
2.0
Usages and Demos :

View More Examples of getParameters()
   1: 
   2: import org.apache.commons.httpclient.HeaderElement;
   3: import org.apache.commons.httpclient.NameValuePair;
   4:         ...
   5:         }
   6:         HeaderElement headerelement = new HeaderElement(header.toCharArray());
   7:         Cookie cookie = new Cookie(host,
   8:         ...
   9:                        false);
  10:         NameValuePair[] parameters = headerelement.getParameters();
  11:         if (parameters != null) {

View Full Code Here
   1: import java.util.Set;
   2: import org.apache.commons.httpclient.HeaderElement;
   3: import org.apache.commons.httpclient.HttpClient;
   4:         ...
   5:         }
   6:         Parameter[] params = req.getParameters();
   7:         org.apache.commons.httpclient.NameValuePair[] p = new org.apache.commons.httpclient.NameValuePair[params.length];
   8:         ...
   9:         for (org.apache.commons.httpclient.Header h : http.getResponseHeaders()) {
  10:             HeaderElement[] els = h.getElements();
  11:             Header.Element[] elements = new Header.Element[els.length];
  12:         ...
  13:             for (int i=0; i<els.length; i++) {
  14:                 HeaderElement el = els[i];

View Full Code Here
   1: import org.apache.commons.httpclient.Header;
   2: import org.apache.commons.httpclient.HeaderElement;
   3: import org.apache.commons.httpclient.NameValuePair;
   4:         ...
   5: 
   6:         HeaderElement[] headerElements = null;
   7: 
   8:         ...
   9:         if (isNetscapeCookie) {
  10:             headerElements = new HeaderElement[] {
  11:                     new HeaderElement(header.toCharArray())
  12:         ...
  13:             }
  14:             NameValuePair[] parameters = headerelement.getParameters();

View Full Code Here
   1: import org.apache.commons.httpclient.Header;
   2: import org.apache.commons.httpclient.HeaderElement;
   3: import org.apache.commons.httpclient.NameValuePair;
   4:         ...
   5: 
   6:         HeaderElement[] headerElements =
   7:         ...
   8:                 HeaderElement.parseElements(header.toCharArray());
   9: 
  10:         ...
  11:             }
  12:             NameValuePair[] parameters = headerelement.getParameters();

View Full Code Here

parse

public static final HeaderElement[] parse(String headerValue)
            throws HttpException

Deprecated. Use #parseElements(String).

This parses the value part of a header. The result is an array of HeaderElement objects.
Parameters:
headerValue - the string representation of the header value (as received from the web server).
Returns:
array of HeaderElements.
Throws:
HttpException - if the above syntax rules are violated.

parseElements

public static final HeaderElement[] parseElements(char[] headerValue)
This parses the value part of a header. The result is an array of HeaderElement objects.
Parameters:
headerValue - the array of char representation of the header value (as received from the web server).
Returns:
array of HeaderElements.
Since:
3.0
Usages and Demos :

View More Examples of parseElements(char[] headerValue)
   1: 
   2: import org.apache.commons.httpclient.HeaderElement;
   3: import org.apache.commons.httpclient.NameValuePair;
   4:         ...
   5:         if (contentType != null) {
   6:             HeaderElement[] values = HeaderElement.parseElements(contentType);
   7:             NameValuePair charsetPair = null;
   8:             for (int i = 0; i < values.length; i++) {
   9:                 if ((charsetPair = values[i].getParameterByName("charset")) != null) {

View Full Code Here
   1: import org.apache.commons.httpclient.Header;
   2: import org.apache.commons.httpclient.HeaderElement;
   3: import org.apache.commons.httpclient.NameValuePair;
   4:         ...
   5: 
   6:         HeaderElement[] headerElements =
   7:         ...
   8:                 HeaderElement.parseElements(header.toCharArray());
   9: 
  10:         ...
  11:         for (int i = 0; i < headerElements.length; i++) {
  12:             HeaderElement headerelement = headerElements[i];

View Full Code Here

parseElements

public static final HeaderElement[] parseElements(String headerValue)
This parses the value part of a header. The result is an array of HeaderElement objects.
Parameters:
headerValue - the string representation of the header value (as received from the web server).
Returns:
array of HeaderElements.
Since:
3.0
Usages and Demos :

View More Examples of parseElements(String headerValue)
   1: 
   2: import org.apache.commons.httpclient.HeaderElement;
   3: import org.apache.commons.httpclient.NameValuePair;
   4:         ...
   5:         if (contentType != null) {
   6:             HeaderElement[] values = HeaderElement.parseElements(contentType);
   7:             NameValuePair charsetPair = null;
   8:             for (int i = 0; i < values.length; i++) {
   9:                 if ((charsetPair = values[i].getParameterByName("charset")) != null) {

View Full Code Here
   1: import org.apache.commons.httpclient.Header;
   2: import org.apache.commons.httpclient.HeaderElement;
   3: import org.apache.commons.httpclient.NameValuePair;
   4:         ...
   5: 
   6:         HeaderElement[] headerElements =
   7:         ...
   8:                 HeaderElement.parseElements(header.toCharArray());
   9: 
  10:         ...
  11:         for (int i = 0; i < headerElements.length; i++) {
  12:             HeaderElement headerelement = headerElements[i];

View Full Code Here