Source code: org/apache/http/impl/HeaderGroup.java
1 /*
2 * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/tags/4.0-alpha2/src/java/org/apache/http/impl/HeaderGroup.java $
3 * $Revision: 366930 $
4 * $Date: 2006-01-07 22:55:04 +0100 (Sat, 07 Jan 2006) $
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.http.impl;
31
32 import java.util.ArrayList;
33 import java.util.Iterator;
34 import java.util.List;
35
36 import org.apache.http.Header;
37 import org.apache.http.io.CharArrayBuffer;
38
39 /**
40 * A class for combining a set of headers. This class allows for multiple
41 * headers with the same name and keeps track of the order in which headers were
42 * added.
43 *
44 * @author Michael Becke
45 *
46 * @since 2.0beta1
47 */
48 public class HeaderGroup {
49
50 /** The list of headers for this group, in the order in which they were added */
51 private List headers;
52
53 /**
54 * Constructor for HeaderGroup.
55 */
56 public HeaderGroup() {
57 this.headers = new ArrayList();
58 }
59
60 /**
61 * Removes any contained headers.
62 */
63 public void clear() {
64 headers.clear();
65 }
66
67 /**
68 * Adds the given header to the group. The order in which this header was
69 * added is preserved.
70 *
71 * @param header the header to add
72 */
73 public void addHeader(Header header) {
74 headers.add(header);
75 }
76
77 /**
78 * Removes the given header.
79 *
80 * @param header the header to remove
81 */
82 public void removeHeader(Header header) {
83 headers.remove(header);
84 }
85
86 /**
87 * Sets all of the headers contained within this group overriding any
88 * existing headers. The headers are added in the order in which they appear
89 * in the array.
90 *
91 * @param headers the headers to set
92 */
93 public void setHeaders(Header[] headers) {
94 clear();
95
96 for (int i = 0; i < headers.length; i++) {
97 addHeader(headers[i]);
98 }
99 }
100
101 /**
102 * Gets a header representing all of the header values with the given name.
103 * If more that one header with the given name exists the values will be
104 * combined with a "," as per RFC 2616.
105 *
106 * <p>Header name comparison is case insensitive.
107 *
108 * @param name the name of the header(s) to get
109 * @return a header with a condensed value or <code>null</code> if no
110 * headers by the given name are present
111 */
112 public Header getCondensedHeader(String name) {
113 Header[] headers = getHeaders(name);
114
115 if (headers.length == 0) {
116 return null;
117 } else if (headers.length == 1) {
118 return headers[0];
119 } else {
120 CharArrayBuffer valueBuffer = new CharArrayBuffer(128);
121 valueBuffer.append(headers[0].getValue());
122 for (int i = 1; i < headers.length; i++) {
123 valueBuffer.append(", ");
124 valueBuffer.append(headers[i].getValue());
125 }
126
127 return new Header(name.toLowerCase(), valueBuffer.toString());
128 }
129 }
130
131 /**
132 * Gets all of the headers with the given name. The returned array
133 * maintains the relative order in which the headers were added.
134 *
135 * <p>Header name comparison is case insensitive.
136 *
137 * @param name the name of the header(s) to get
138 *
139 * @return an array of length >= 0
140 */
141 public Header[] getHeaders(String name) {
142 ArrayList headersFound = new ArrayList();
143
144 for (int i = 0; i < headers.size(); i++) {
145 Header header = (Header) headers.get(i);
146 if (header.getName().equalsIgnoreCase(name)) {
147 headersFound.add(header);
148 }
149 }
150
151 return (Header[]) headersFound.toArray(new Header[headersFound.size()]);
152 }
153
154 /**
155 * Gets the first header with the given name.
156 *
157 * <p>Header name comparison is case insensitive.
158 *
159 * @param name the name of the header to get
160 * @return the first header or <code>null</code>
161 */
162 public Header getFirstHeader(String name) {
163 for (Iterator headerIter = headers.iterator(); headerIter.hasNext();) {
164 Header header = (Header) headerIter.next();
165 if (header.getName().equalsIgnoreCase(name)) {
166 return header;
167 }
168 }
169
170 return null;
171 }
172
173 /**
174 * Gets the last header with the given name.
175 *
176 * <p>Header name comparison is case insensitive.
177 *
178 * @param name the name of the header to get
179 * @return the last header or <code>null</code>
180 */
181 public Header getLastHeader(String name) {
182 // start at the end of the list and work backwards
183 for (int i = headers.size() - 1; i >= 0; i--) {
184 Header header = (Header) headers.get(i);
185 if (header.getName().equalsIgnoreCase(name)) {
186 return header;
187 }
188 }
189
190 return null;
191 }
192
193 /**
194 * Gets all of the headers contained within this group.
195 *
196 * @return an array of length >= 0
197 */
198 public Header[] getAllHeaders() {
199 return (Header[]) headers.toArray(new Header[headers.size()]);
200 }
201
202 /**
203 * Tests if headers with the given name are contained within this group.
204 *
205 * <p>Header name comparison is case insensitive.
206 *
207 * @param name the header name to test for
208 * @return <code>true</code> if at least one header with the name is
209 * contained, <code>false</code> otherwise
210 */
211 public boolean containsHeader(String name) {
212 for (int i = 0; i < headers.size(); i++) {
213 Header header = (Header) headers.get(i);
214 if (header.getName().equalsIgnoreCase(name)) {
215 return true;
216 }
217 }
218
219 return false;
220 }
221
222 /**
223 * Returns an iterator over this group of headers.
224 *
225 * @return iterator over this group of headers.
226 *
227 * @since 4.0
228 */
229 public Iterator iterator() {
230 return this.headers.iterator();
231 }
232 }