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 $
4
* $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
6
* ====================================================================
8
* Licensed to the Apache Software Foundation (ASF) under one or more
9
* contributor license agreements. See the NOTICE file distributed with
10
* this work for additional information regarding copyright ownership.
11
* The ASF licenses this file to You under the Apache License, Version 2.0
12
* (the "License"); you may not use this file except in compliance with
13
* 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, software
18
* distributed under the License is distributed on an "AS IS" BASIS,
19
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
* See the License for the specific language governing permissions and
21
* limitations under the License.
22
* ====================================================================
24
* This software consists of voluntary contributions made by many
25
* individuals on behalf of the Apache Software Foundation. For more
26
* information on the Apache Software Foundation, please see
27
* <http://www.apache.org/>.
31
package org.apache.commons.httpclient;
33
import java.util.ArrayList;
34
import java.util.Iterator;
35
import java.util.List;
38
* A class for combining a set of headers. This class allows for multiple
39
* headers with the same name and keeps track of the order in which headers were
42
* @author Michael Becke
46
public class HeaderGroup {
48
/** The list of headers for this group, in the order in which they were added */
52
* Constructor for HeaderGroup.
54
public HeaderGroup() {
55
this.headers = new ArrayList();
59
* Removes any contained headers.
66
* Adds the given header to the group. The order in which this header was
69
* @param header the header to add
71
public void addHeader(Header header) {
76
* Removes the given header.
78
* @param header the header to remove
80
public void removeHeader(Header header) {
81
headers.remove(header);
85
* Sets all of the headers contained within this group overriding any
86
* existing headers. The headers are added in the order in which they appear
89
* @param headers the headers to set
91
public void setHeaders(Header[] headers) {
94
for (int i = 0; i < headers.length; i++) {
95
addHeader(headers[i]);
100
* Gets a header representing all of the header values with the given name.
101
* If more that one header with the given name exists the values will be
102
* combined with a "," as per RFC 2616.
104
* <p>Header name comparison is case insensitive.
106
* @param name the name of the header(s) to get
107
* @return a header with a condensed value or <code>null</code> if no
108
* headers by the given name are present
110
public Header getCondensedHeader(String name) {
111
Header[] headers = getHeaders(name);
113
if (headers.length == 0) {
115
} else if (headers.length == 1) {
116
return new Header(headers[0].getName(), headers[0].getValue());
118
StringBuffer valueBuffer = new StringBuffer(headers[0].getValue());
120
for (int i = 1; i < headers.length; i++) {
121
valueBuffer.append(", ");
122
valueBuffer.append(headers[i].getValue());
125
return new Header(name.toLowerCase(), valueBuffer.toString());
130
* Gets all of the headers with the given name. The returned array
131
* maintains the relative order in which the headers were added.
133
* <p>Header name comparison is case insensitive.
135
* @param name the name of the header(s) to get
137
* @return an array of length >= 0
139
public Header[] getHeaders(String name) {
140
ArrayList headersFound = new ArrayList();
142
for (Iterator headerIter = headers.iterator(); headerIter.hasNext();) {
143
Header header = (Header) headerIter.next();
144
if (header.getName().equalsIgnoreCase(name)) {
145
headersFound.add(header);
149
return (Header[]) headersFound.toArray(new Header[headersFound.size()]);
153
* Gets the first header with the given name.
155
* <p>Header name comparison is case insensitive.
157
* @param name the name of the header to get
158
* @return the first header or <code>null</code>
160
public Header getFirstHeader(String name) {
161
for (Iterator headerIter = headers.iterator(); headerIter.hasNext();) {
162
Header header = (Header) headerIter.next();
163
if (header.getName().equalsIgnoreCase(name)) {
172
* Gets the last header with the given name.
174
* <p>Header name comparison is case insensitive.
176
* @param name the name of the header to get
177
* @return the last header or <code>null</code>
179
public Header getLastHeader(String name) {
180
// start at the end of the list and work backwards
181
for (int i = headers.size() - 1; i >= 0; i--) {
182
Header header = (Header) headers.get(i);
183
if (header.getName().equalsIgnoreCase(name)) {
192
* Gets all of the headers contained within this group.
194
* @return an array of length >= 0
196
public Header[] getAllHeaders() {
197
return (Header[]) headers.toArray(new Header[headers.size()]);
201
* Tests if headers with the given name are contained within this group.
203
* <p>Header name comparison is case insensitive.
205
* @param name the header name to test for
206
* @return <code>true</code> if at least one header with the name is
207
* contained, <code>false</code> otherwise
209
public boolean containsHeader(String name) {
210
for (Iterator headerIter = headers.iterator(); headerIter.hasNext();) {
211
Header header = (Header) headerIter.next();
212
if (header.getName().equalsIgnoreCase(name)) {
221
* Returns an iterator over this group of headers.
223
* @return iterator over this group of headers.
227
public Iterator getIterator() {
228
return this.headers.iterator();