KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > httpclient > HeaderGroup


1 /*
2  * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HeaderGroup.java,v 1.3.2.2 2004/02/22 18:21:13 olegk Exp $
3  * $Revision: 1.3.2.2 $
4  * $Date: 2004/02/22 18:21:13 $
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  * [Additional notices, if required by prior licensing conditions]
29  *
30  */

31
32 package org.apache.commons.httpclient;
33
34 import java.util.ArrayList JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.List JavaDoc;
37
38 /**
39  * A class for combining a set of headers. This class allows for multiple
40  * headers with the same name and keeps track of the order in which headers were
41  * added.
42  *
43  * @author Michael Becke
44  *
45  * @since 2.0beta1
46  */

47 public class HeaderGroup {
48
49     /** The list of headers for this group, in the order in which they were added */
50     private List JavaDoc headers;
51
52     /**
53      * Constructor for HeaderGroup.
54      */

55     public HeaderGroup() {
56         this.headers = new ArrayList JavaDoc();
57     }
58     
59     /**
60      * Removes any contained headers.
61      */

62     public void clear() {
63         headers.clear();
64     }
65     
66     /**
67      * Adds the given header to the group. The order in which this header was
68      * added is preserved.
69      *
70      * @param header the header to add
71      */

72     public void addHeader(Header header) {
73         headers.add(header);
74     }
75     
76     /**
77      * Removes the given header.
78      *
79      * @param header the header to remove
80      */

81     public void removeHeader(Header header) {
82         headers.remove(header);
83     }
84
85     /**
86      * Sets all of the headers contained within this group overriding any
87      * existing headers. The headers are added in the order in which they appear
88      * in the array.
89      *
90      * @param headers the headers to set
91      */

92     public void setHeaders(Header[] headers) {
93         clear();
94         
95         for (int i = 0; i < headers.length; i++) {
96             addHeader(headers[i]);
97         }
98     }
99     
100     /**
101      * Gets a header representing all of the header values with the given name.
102      * If more that one header with the given name exists the values will be
103      * combined with a "," as per RFC 2616.
104      *
105      * <p>Header name comparison is case insensitive.
106      *
107      * @param name the name of the header(s) to get
108      * @return a header with a condensed value or <code>null</code> if no
109      * headers by the given name are present
110      */

111     public Header getCondensedHeader(String JavaDoc name) {
112         Header[] headers = getHeaders(name);
113         
114         if (headers.length == 0) {
115             return null;
116         } else if (headers.length == 1) {
117             return new Header(headers[0].getName(), headers[0].getValue());
118         } else {
119             StringBuffer JavaDoc valueBuffer = new StringBuffer JavaDoc(headers[0].getValue());
120             
121             for (int i = 1; i < headers.length; i++) {
122                 valueBuffer.append(", ");
123                 valueBuffer.append(headers[i].getValue());
124             }
125             
126             return new Header(name.toLowerCase(), valueBuffer.toString());
127         }
128     }
129     
130     /**
131      * Gets all of the headers with the given name. The returned array
132      * maintains the relative order in which the headers were added.
133      *
134      * <p>Header name comparison is case insensitive.
135      *
136      * @param name the name of the header(s) to get
137      *
138      * @return an array of length >= 0
139      */

140     public Header[] getHeaders(String JavaDoc name) {
141         ArrayList JavaDoc headersFound = new ArrayList JavaDoc();
142         
143         for (Iterator JavaDoc headerIter = headers.iterator(); headerIter.hasNext();) {
144             Header header = (Header) headerIter.next();
145             if (header.getName().equalsIgnoreCase(name)) {
146                 headersFound.add(header);
147             }
148         }
149         
150         return (Header[]) headersFound.toArray(new Header[headersFound.size()]);
151     }
152     
153     /**
154      * Gets the first header with the given name.
155      *
156      * <p>Header name comparison is case insensitive.
157      *
158      * @param name the name of the header to get
159      * @return the first header or <code>null</code>
160      */

161     public Header getFirstHeader(String JavaDoc name) {
162         for (Iterator JavaDoc headerIter = headers.iterator(); headerIter.hasNext();) {
163             Header header = (Header) headerIter.next();
164             if (header.getName().equalsIgnoreCase(name)) {
165                 return header;
166             }
167         }
168         
169         return null;
170     }
171     
172     /**
173      * Gets the last header with the given name.
174      *
175      * <p>Header name comparison is case insensitive.
176      *
177      * @param name the name of the header to get
178      * @return the last header or <code>null</code>
179      */

180     public Header getLastHeader(String JavaDoc name) {
181         // start at the end of the list and work backwards
182
for (int i = headers.size() - 1; i >= 0; i--) {
183             Header header = (Header) headers.get(i);
184             if (header.getName().equalsIgnoreCase(name)) {
185                 return header;
186             }
187         }
188         
189         return null;
190     }
191     
192     /**
193      * Gets all of the headers contained within this group.
194      *
195      * @return an array of length >= 0
196      */

197     public Header[] getAllHeaders() {
198         return (Header[]) headers.toArray(new Header[headers.size()]);
199     }
200     
201     /**
202      * Tests if headers with the given name are contained within this group.
203      *
204      * <p>Header name comparison is case insensitive.
205      *
206      * @param name the header name to test for
207      * @return <code>true</code> if at least one header with the name is
208      * contained, <code>false</code> otherwise
209      */

210     public boolean containsHeader(String JavaDoc name) {
211         for (Iterator JavaDoc headerIter = headers.iterator(); headerIter.hasNext();) {
212             Header header = (Header) headerIter.next();
213             if (header.getName().equalsIgnoreCase(name)) {
214                 return true;
215             }
216         }
217         
218         return false;
219     }
220
221 }
222
Popular Tags