2
* $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0.1/httpcore/src/main/java/org/apache/http/message/HeaderGroup.java $
4
* $Date: 2009-02-14 18:06:25 +0100 (Sat, 14 Feb 2009) $
6
* ====================================================================
7
* Licensed to the Apache Software Foundation (ASF) under one
8
* or more contributor license agreements. See the NOTICE file
9
* distributed with this work for additional information
10
* regarding copyright ownership. The ASF licenses this file
11
* to you under the Apache License, Version 2.0 (the
12
* "License"); you may not use this file except in compliance
13
* with the License. You may obtain a copy of the License at
15
* http://www.apache.org/licenses/LICENSE-2.0
17
* Unless required by applicable law or agreed to in writing,
18
* software distributed under the License is distributed on an
19
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20
* KIND, either express or implied. See the License for the
21
* specific language governing permissions and limitations
23
* ====================================================================
25
* This software consists of voluntary contributions made by many
26
* individuals on behalf of the Apache Software Foundation. For more
27
* information on the Apache Software Foundation, please see
28
* <http://www.apache.org/>.
32
package org.apache.http.message;
34
import java.util.ArrayList;
35
import java.util.List;
36
import java.util.Locale;
38
import org.apache.http.Header;
39
import org.apache.http.HeaderIterator;
40
import org.apache.http.util.CharArrayBuffer;
43
* A class for combining a set of headers.
44
* This class allows for multiple headers with the same name and
45
* keeps track of the order in which headers were added.
50
public class HeaderGroup implements Cloneable {
52
/** The list of headers for this group, in the order in which they were added */
56
* Constructor for HeaderGroup.
58
public HeaderGroup() {
59
this.headers = new ArrayList(16);
63
* Removes any contained headers.
70
* Adds the given header to the group. The order in which this header was
73
* @param header the header to add
75
public void addHeader(Header header) {
83
* Removes the given header.
85
* @param header the header to remove
87
public void removeHeader(Header header) {
91
headers.remove(header);
95
* Replaces the first occurence of the header with the same name. If no header with
96
* the same name is found the given header is added to the end of the list.
98
* @param header the new header that should replace the first header with the same
99
* name if present in the list.
101
public void updateHeader(Header header) {
102
if (header == null) {
105
for (int i = 0; i < this.headers.size(); i++) {
106
Header current = (Header) this.headers.get(i);
107
if (current.getName().equalsIgnoreCase(header.getName())) {
108
this.headers.set(i, header);
112
this.headers.add(header);
116
* Sets all of the headers contained within this group overriding any
117
* existing headers. The headers are added in the order in which they appear
120
* @param headers the headers to set
122
public void setHeaders(Header[] headers) {
124
if (headers == null) {
127
for (int i = 0; i < headers.length; i++) {
128
this.headers.add(headers[i]);
133
* Gets a header representing all of the header values with the given name.
134
* If more that one header with the given name exists the values will be
135
* combined with a "," as per RFC 2616.
137
* <p>Header name comparison is case insensitive.
139
* @param name the name of the header(s) to get
140
* @return a header with a condensed value or <code>null</code> if no
141
* headers by the given name are present
143
public Header getCondensedHeader(String name) {
144
Header[] headers = getHeaders(name);
146
if (headers.length == 0) {
148
} else if (headers.length == 1) {
151
CharArrayBuffer valueBuffer = new CharArrayBuffer(128);
152
valueBuffer.append(headers[0].getValue());
153
for (int i = 1; i < headers.length; i++) {
154
valueBuffer.append(", ");
155
valueBuffer.append(headers[i].getValue());
158
return new BasicHeader(name.toLowerCase(Locale.ENGLISH), valueBuffer.toString());
163
* Gets all of the headers with the given name. The returned array
164
* maintains the relative order in which the headers were added.
166
* <p>Header name comparison is case insensitive.
168
* @param name the name of the header(s) to get
170
* @return an array of length >= 0
172
public Header[] getHeaders(String name) {
173
ArrayList headersFound = new ArrayList();
175
for (int i = 0; i < headers.size(); i++) {
176
Header header = (Header) headers.get(i);
177
if (header.getName().equalsIgnoreCase(name)) {
178
headersFound.add(header);
182
return (Header[]) headersFound.toArray(new Header[headersFound.size()]);
186
* Gets the first header with the given name.
188
* <p>Header name comparison is case insensitive.
190
* @param name the name of the header to get
191
* @return the first header or <code>null</code>
193
public Header getFirstHeader(String name) {
194
for (int i = 0; i < headers.size(); i++) {
195
Header header = (Header) headers.get(i);
196
if (header.getName().equalsIgnoreCase(name)) {
204
* Gets the last header with the given name.
206
* <p>Header name comparison is case insensitive.
208
* @param name the name of the header to get
209
* @return the last header or <code>null</code>
211
public Header getLastHeader(String name) {
212
// start at the end of the list and work backwards
213
for (int i = headers.size() - 1; i >= 0; i--) {
214
Header header = (Header) headers.get(i);
215
if (header.getName().equalsIgnoreCase(name)) {
224
* Gets all of the headers contained within this group.
226
* @return an array of length >= 0
228
public Header[] getAllHeaders() {
229
return (Header[]) headers.toArray(new Header[headers.size()]);
233
* Tests if headers with the given name are contained within this group.
235
* <p>Header name comparison is case insensitive.
237
* @param name the header name to test for
238
* @return <code>true</code> if at least one header with the name is
239
* contained, <code>false</code> otherwise
241
public boolean containsHeader(String name) {
242
for (int i = 0; i < headers.size(); i++) {
243
Header header = (Header) headers.get(i);
244
if (header.getName().equalsIgnoreCase(name)) {
253
* Returns an iterator over this group of headers.
255
* @return iterator over this group of headers.
259
public HeaderIterator iterator() {
260
return new BasicListHeaderIterator(this.headers, null);
264
* Returns an iterator over the headers with a given name in this group.
266
* @param name the name of the headers over which to iterate, or
267
* <code>null</code> for all headers
269
* @return iterator over some headers in this group.
273
public HeaderIterator iterator(final String name) {
274
return new BasicListHeaderIterator(this.headers, name);
278
* Returns a copy of this object
280
* @return copy of this object
282
public HeaderGroup copy() {
283
HeaderGroup clone = new HeaderGroup();
284
clone.headers.addAll(this.headers);
288
public Object clone() throws CloneNotSupportedException {
289
HeaderGroup clone = (HeaderGroup) super.clone();
290
clone.headers = new ArrayList(this.headers);