001: /*
002: * $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 $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030:
031: package org.apache.commons.httpclient;
032:
033: import java.util.ArrayList;
034: import java.util.Iterator;
035: import java.util.List;
036:
037: /**
038: * A class for combining a set of headers. This class allows for multiple
039: * headers with the same name and keeps track of the order in which headers were
040: * added.
041: *
042: * @author Michael Becke
043: *
044: * @since 2.0beta1
045: */
046: public class HeaderGroup {
047:
048: /** The list of headers for this group, in the order in which they were added */
049: private List headers;
050:
051: /**
052: * Constructor for HeaderGroup.
053: */
054: public HeaderGroup() {
055: this .headers = new ArrayList();
056: }
057:
058: /**
059: * Removes any contained headers.
060: */
061: public void clear() {
062: headers.clear();
063: }
064:
065: /**
066: * Adds the given header to the group. The order in which this header was
067: * added is preserved.
068: *
069: * @param header the header to add
070: */
071: public void addHeader(Header header) {
072: headers.add(header);
073: }
074:
075: /**
076: * Removes the given header.
077: *
078: * @param header the header to remove
079: */
080: public void removeHeader(Header header) {
081: headers.remove(header);
082: }
083:
084: /**
085: * Sets all of the headers contained within this group overriding any
086: * existing headers. The headers are added in the order in which they appear
087: * in the array.
088: *
089: * @param headers the headers to set
090: */
091: public void setHeaders(Header[] headers) {
092: clear();
093:
094: for (int i = 0; i < headers.length; i++) {
095: addHeader(headers[i]);
096: }
097: }
098:
099: /**
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.
103: *
104: * <p>Header name comparison is case insensitive.
105: *
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
109: */
110: public Header getCondensedHeader(String name) {
111: Header[] headers = getHeaders(name);
112:
113: if (headers.length == 0) {
114: return null;
115: } else if (headers.length == 1) {
116: return new Header(headers[0].getName(), headers[0]
117: .getValue());
118: } else {
119: StringBuffer valueBuffer = new StringBuffer(headers[0]
120: .getValue());
121:
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
128: .toString());
129: }
130: }
131:
132: /**
133: * Gets all of the headers with the given name. The returned array
134: * maintains the relative order in which the headers were added.
135: *
136: * <p>Header name comparison is case insensitive.
137: *
138: * @param name the name of the header(s) to get
139: *
140: * @return an array of length >= 0
141: */
142: public Header[] getHeaders(String name) {
143: ArrayList headersFound = new ArrayList();
144:
145: for (Iterator headerIter = headers.iterator(); headerIter
146: .hasNext();) {
147: Header header = (Header) headerIter.next();
148: if (header.getName().equalsIgnoreCase(name)) {
149: headersFound.add(header);
150: }
151: }
152:
153: return (Header[]) headersFound.toArray(new Header[headersFound
154: .size()]);
155: }
156:
157: /**
158: * Gets the first header with the given name.
159: *
160: * <p>Header name comparison is case insensitive.
161: *
162: * @param name the name of the header to get
163: * @return the first header or <code>null</code>
164: */
165: public Header getFirstHeader(String name) {
166: for (Iterator headerIter = headers.iterator(); headerIter
167: .hasNext();) {
168: Header header = (Header) headerIter.next();
169: if (header.getName().equalsIgnoreCase(name)) {
170: return header;
171: }
172: }
173:
174: return null;
175: }
176:
177: /**
178: * Gets the last header with the given name.
179: *
180: * <p>Header name comparison is case insensitive.
181: *
182: * @param name the name of the header to get
183: * @return the last header or <code>null</code>
184: */
185: public Header getLastHeader(String name) {
186: // start at the end of the list and work backwards
187: for (int i = headers.size() - 1; i >= 0; i--) {
188: Header header = (Header) headers.get(i);
189: if (header.getName().equalsIgnoreCase(name)) {
190: return header;
191: }
192: }
193:
194: return null;
195: }
196:
197: /**
198: * Gets all of the headers contained within this group.
199: *
200: * @return an array of length >= 0
201: */
202: public Header[] getAllHeaders() {
203: return (Header[]) headers.toArray(new Header[headers.size()]);
204: }
205:
206: /**
207: * Tests if headers with the given name are contained within this group.
208: *
209: * <p>Header name comparison is case insensitive.
210: *
211: * @param name the header name to test for
212: * @return <code>true</code> if at least one header with the name is
213: * contained, <code>false</code> otherwise
214: */
215: public boolean containsHeader(String name) {
216: for (Iterator headerIter = headers.iterator(); headerIter
217: .hasNext();) {
218: Header header = (Header) headerIter.next();
219: if (header.getName().equalsIgnoreCase(name)) {
220: return true;
221: }
222: }
223:
224: return false;
225: }
226:
227: /**
228: * Returns an iterator over this group of headers.
229: *
230: * @return iterator over this group of headers.
231: *
232: * @since 3.0
233: */
234: public Iterator getIterator() {
235: return this.headers.iterator();
236: }
237: }
|