View Javadoc

1   /*
2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/HeaderGroup.java,v 1.8 2004/05/13 04:03:25 mbecke Exp $
3    * $Revision: 155418 $
4    * $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
5    *
6    * ====================================================================
7    *
8    *  Copyright 2003-2004 The Apache Software Foundation
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
27   *
28   */
29  
30  package org.apache.commons.httpclient;
31  
32  import java.util.ArrayList;
33  import java.util.Iterator;
34  import java.util.List;
35  
36  /***
37   * A class for combining a set of headers.  This class allows for multiple
38   * headers with the same name and keeps track of the order in which headers were
39   * added.
40   * 
41   * @author Michael Becke
42   * 
43   * @since 2.0beta1
44   */
45  public class HeaderGroup {
46  
47      /*** The list of headers for this group, in the order in which they were added */
48      private List headers;
49  
50      /***
51       * Constructor for HeaderGroup.
52       */
53      public HeaderGroup() {
54          this.headers = new ArrayList();
55      }
56      
57      /***
58       * Removes any contained headers.
59       */
60      public void clear() {
61          headers.clear();
62      }
63      
64      /***
65       * Adds the given header to the group.  The order in which this header was
66       * added is preserved.
67       * 
68       * @param header the header to add
69       */
70      public void addHeader(Header header) {
71          headers.add(header);
72      }
73      
74      /***
75       * Removes the given header.
76       *
77       * @param header the header to remove
78       */
79      public void removeHeader(Header header) {
80          headers.remove(header);
81      }
82  
83      /***
84       * Sets all of the headers contained within this group overriding any
85       * existing headers. The headers are added in the order in which they appear
86       * in the array.
87       * 
88       * @param headers the headers to set
89       */
90      public void setHeaders(Header[] headers) {
91          clear();
92          
93          for (int i = 0; i < headers.length; i++) {
94              addHeader(headers[i]);
95          }
96      }
97      
98      /***
99       * Gets a header representing all of the header values with the given name.
100      * If more that one header with the given name exists the values will be
101      * combined with a "," as per RFC 2616.
102      * 
103      * <p>Header name comparison is case insensitive.
104      * 
105      * @param name the name of the header(s) to get
106      * @return a header with a condensed value or <code>null</code> if no
107      * headers by the given name are present
108      */
109     public Header getCondensedHeader(String name) {
110         Header[] headers = getHeaders(name);
111         
112         if (headers.length == 0) {
113             return null;   
114         } else if (headers.length == 1) {
115             return new Header(headers[0].getName(), headers[0].getValue());
116         } else {
117             StringBuffer valueBuffer = new StringBuffer(headers[0].getValue());
118             
119             for (int i = 1; i < headers.length; i++) {
120                 valueBuffer.append(", ");
121                 valueBuffer.append(headers[i].getValue());
122             }
123             
124             return new Header(name.toLowerCase(), valueBuffer.toString());
125         }
126     }
127     
128     /***
129      * Gets all of the headers with the given name.  The returned array
130      * maintains the relative order in which the headers were added.  
131      * 
132      * <p>Header name comparison is case insensitive.
133      * 
134      * @param name the name of the header(s) to get
135      * 
136      * @return an array of length >= 0
137      */
138     public Header[] getHeaders(String name) {
139         ArrayList headersFound = new ArrayList();
140         
141         for (Iterator headerIter = headers.iterator(); headerIter.hasNext();) {
142             Header header = (Header) headerIter.next();
143             if (header.getName().equalsIgnoreCase(name)) {
144                 headersFound.add(header);
145             }
146         }
147         
148         return (Header[]) headersFound.toArray(new Header[headersFound.size()]);
149     }
150     
151     /***
152      * Gets the first header with the given name.
153      * 
154      * <p>Header name comparison is case insensitive.
155      * 
156      * @param name the name of the header to get
157      * @return the first header or <code>null</code>
158      */
159     public Header getFirstHeader(String name) {
160         for (Iterator headerIter = headers.iterator(); headerIter.hasNext();) {
161             Header header = (Header) headerIter.next();
162             if (header.getName().equalsIgnoreCase(name)) {
163                 return header;
164             }
165         }
166         
167         return null;                
168     }
169     
170     /***
171      * Gets the last header with the given name.
172      *
173      * <p>Header name comparison is case insensitive.
174      *
175      * @param name the name of the header to get
176      * @return the last header or <code>null</code>
177      */
178     public Header getLastHeader(String name) {
179         // start at the end of the list and work backwards
180         for (int i = headers.size() - 1; i >= 0; i--) {
181             Header header = (Header) headers.get(i);
182             if (header.getName().equalsIgnoreCase(name)) {
183                 return header;
184             }            
185         }
186         
187         return null;        
188     }
189     
190     /***
191      * Gets all of the headers contained within this group.
192      * 
193      * @return an array of length >= 0
194      */
195     public Header[] getAllHeaders() {
196         return (Header[]) headers.toArray(new Header[headers.size()]);
197     }
198     
199     /***
200      * Tests if headers with the given name are contained within this group.
201      * 
202      * <p>Header name comparison is case insensitive.
203      * 
204      * @param name the header name to test for
205      * @return <code>true</code> if at least one header with the name is
206      * contained, <code>false</code> otherwise
207      */
208     public boolean containsHeader(String name) {
209         for (Iterator headerIter = headers.iterator(); headerIter.hasNext();) {
210             Header header = (Header) headerIter.next();
211             if (header.getName().equalsIgnoreCase(name)) {
212                 return true;
213             }
214         }
215         
216         return false;
217     }
218 
219     /***
220      * Returns an iterator over this group of headers.
221      * 
222      * @return iterator over this group of headers.
223      * 
224      * @since 3.0
225      */
226     public Iterator getIterator() {
227         return this.headers.iterator(); 
228     }
229 }