FileDocCategorySizeDatePackage
HeaderGroup.javaAPI DocAndroid 1.5 API9179Wed May 06 22:41:10 BST 2009org.apache.http.message

HeaderGroup

public class HeaderGroup extends Object implements Cloneable
A class for combining a set of headers. This class allows for multiple headers with the same name and keeps track of the order in which headers were added.
author
Michael Becke
since
4.0

Fields Summary
private List
headers
The list of headers for this group, in the order in which they were added
Constructors Summary
public HeaderGroup()
Constructor for HeaderGroup.

        this.headers = new ArrayList(16);
    
Methods Summary
public voidaddHeader(org.apache.http.Header header)
Adds the given header to the group. The order in which this header was added is preserved.

param
header the header to add

        if (header == null) {
            return;
        }
        headers.add(header);
    
public voidclear()
Removes any contained headers.

        headers.clear();
    
public java.lang.Objectclone()

        HeaderGroup clone = (HeaderGroup) super.clone();
        clone.headers = new ArrayList(this.headers);
        return clone;
    
public booleancontainsHeader(java.lang.String name)
Tests if headers with the given name are contained within this group.

Header name comparison is case insensitive.

param
name the header name to test for
return
true if at least one header with the name is contained, false otherwise

        for (int i = 0; i < headers.size(); i++) {
            Header header = (Header) headers.get(i);
            if (header.getName().equalsIgnoreCase(name)) {
                return true;
            }
        }
        
        return false;
    
public org.apache.http.message.HeaderGroupcopy()
Returns a copy of this object

return
copy of this object

        HeaderGroup clone = new HeaderGroup();
        clone.headers.addAll(this.headers);
        return clone;
    
public org.apache.http.Header[]getAllHeaders()
Gets all of the headers contained within this group.

return
an array of length >= 0

        return (Header[]) headers.toArray(new Header[headers.size()]);
    
public org.apache.http.HeadergetCondensedHeader(java.lang.String name)
Gets a header representing all of the header values with the given name. If more that one header with the given name exists the values will be combined with a "," as per RFC 2616.

Header name comparison is case insensitive.

param
name the name of the header(s) to get
return
a header with a condensed value or null if no headers by the given name are present

        Header[] headers = getHeaders(name);
        
        if (headers.length == 0) {
            return null;   
        } else if (headers.length == 1) {
            return headers[0];
        } else {
            CharArrayBuffer valueBuffer = new CharArrayBuffer(128);
            valueBuffer.append(headers[0].getValue());
            for (int i = 1; i < headers.length; i++) {
                valueBuffer.append(", ");
                valueBuffer.append(headers[i].getValue());
            }
            
            return new BasicHeader(name.toLowerCase(Locale.ENGLISH), valueBuffer.toString());
        }
    
public org.apache.http.HeadergetFirstHeader(java.lang.String name)
Gets the first header with the given name.

Header name comparison is case insensitive.

param
name the name of the header to get
return
the first header or null

        for (int i = 0; i < headers.size(); i++) {
            Header header = (Header) headers.get(i);
            if (header.getName().equalsIgnoreCase(name)) {
                return header;
            }
        }
        return null;                
    
public org.apache.http.Header[]getHeaders(java.lang.String name)
Gets all of the headers with the given name. The returned array maintains the relative order in which the headers were added.

Header name comparison is case insensitive.

param
name the name of the header(s) to get
return
an array of length >= 0

        ArrayList headersFound = new ArrayList();
        
        for (int i = 0; i < headers.size(); i++) {
            Header header = (Header) headers.get(i);
            if (header.getName().equalsIgnoreCase(name)) {
                headersFound.add(header);
            }
        }
        
        return (Header[]) headersFound.toArray(new Header[headersFound.size()]);
    
public org.apache.http.HeadergetLastHeader(java.lang.String name)
Gets the last header with the given name.

Header name comparison is case insensitive.

param
name the name of the header to get
return
the last header or null

        // start at the end of the list and work backwards
        for (int i = headers.size() - 1; i >= 0; i--) {
            Header header = (Header) headers.get(i);
            if (header.getName().equalsIgnoreCase(name)) {
                return header;
            }            
        }
        
        return null;        
    
public org.apache.http.HeaderIteratoriterator()
Returns an iterator over this group of headers.

return
iterator over this group of headers.
since
4.0

        return new BasicListHeaderIterator(this.headers, null); 
    
public org.apache.http.HeaderIteratoriterator(java.lang.String name)
Returns an iterator over the headers with a given name in this group.

param
name the name of the headers over which to iterate, or null for all headers
return
iterator over some headers in this group.
since
4.0

        return new BasicListHeaderIterator(this.headers, name);
    
public voidremoveHeader(org.apache.http.Header header)
Removes the given header.

param
header the header to remove

        if (header == null) {
            return;
        }
        headers.remove(header);
    
public voidsetHeaders(org.apache.http.Header[] headers)
Sets all of the headers contained within this group overriding any existing headers. The headers are added in the order in which they appear in the array.

param
headers the headers to set

        clear();
        if (headers == null) {
            return;
        }
        for (int i = 0; i < headers.length; i++) {
            this.headers.add(headers[i]);
        }
    
public voidupdateHeader(org.apache.http.Header header)
Replaces the first occurence of the header with the same name. If no header with the same name is found the given header is added to the end of the list.

param
header the new header that should replace the first header with the same name if present in the list.

        if (header == null) {
            return;
        }
        for (int i = 0; i < this.headers.size(); i++) {
            Header current = (Header) this.headers.get(i);
            if (current.getName().equalsIgnoreCase(header.getName())) {
                this.headers.set(i, header);
                return;
            }
        }
        this.headers.add(header);