View Javadoc
1 /* 2 * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HeaderGroup.java,v 1.3.2.1 2003/08/12 02:39:41 mbecke Exp $ 3 * $Revision: 1.3.2.1 $ 4 * $Date: 2003/08/12 02:39:41 $ 5 * 6 * ==================================================================== 7 * 8 * The Apache Software License, Version 1.1 9 * 10 * Copyright (c) 2003 The Apache Software Foundation. All rights 11 * reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in 22 * the documentation and/or other materials provided with the 23 * distribution. 24 * 25 * 3. The end-user documentation included with the redistribution, if 26 * any, must include the following acknowlegement: 27 * "This product includes software developed by the 28 * Apache Software Foundation (http://www.apache.org/)." 29 * Alternately, this acknowlegement may appear in the software itself, 30 * if and wherever such third-party acknowlegements normally appear. 31 * 32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software 33 * Foundation" must not be used to endorse or promote products derived 34 * from this software without prior written permission. For written 35 * permission, please contact apache@apache.org. 36 * 37 * 5. Products derived from this software may not be called "Apache" 38 * nor may "Apache" appear in their names without prior written 39 * permission of the Apache Group. 40 * 41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 52 * SUCH DAMAGE. 53 * ==================================================================== 54 * 55 * This software consists of voluntary contributions made by many 56 * individuals on behalf of the Apache Software Foundation. For more 57 * information on the Apache Software Foundation, please see 58 * <http://www.apache.org/>. 59 * 60 * [Additional notices, if required by prior licensing conditions] 61 * 62 */ 63 64 package org.apache.commons.httpclient; 65 66 import java.util.ArrayList; 67 import java.util.Iterator; 68 import java.util.List; 69 70 /*** 71 * A class for combining a set of headers. This class allows for multiple 72 * headers with the same name and keeps track of the order in which headers were 73 * added. 74 * 75 * @author Michael Becke 76 * 77 * @since 2.0beta1 78 */ 79 public class HeaderGroup { 80 81 /*** The list of headers for this group, in the order in which they were added */ 82 private List headers; 83 84 /*** 85 * Constructor for HeaderGroup. 86 */ 87 public HeaderGroup() { 88 this.headers = new ArrayList(); 89 } 90 91 /*** 92 * Removes any contained headers. 93 */ 94 public void clear() { 95 headers.clear(); 96 } 97 98 /*** 99 * Adds the given header to the group. The order in which this header was 100 * added is preserved. 101 * 102 * @param header the header to add 103 */ 104 public void addHeader(Header header) { 105 headers.add(header); 106 } 107 108 /*** 109 * Removes the given header. 110 * 111 * @param header the header to remove 112 */ 113 public void removeHeader(Header header) { 114 headers.remove(header); 115 } 116 117 /*** 118 * Sets all of the headers contained within this group overriding any 119 * existing headers. The headers are added in the order in which they appear 120 * in the array. 121 * 122 * @param headers the headers to set 123 */ 124 public void setHeaders(Header[] headers) { 125 clear(); 126 127 for (int i = 0; i < headers.length; i++) { 128 addHeader(headers[i]); 129 } 130 } 131 132 /*** 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. 136 * 137 * <p>Header name comparison is case insensitive. 138 * 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 142 */ 143 public Header getCondensedHeader(String name) { 144 Header[] headers = getHeaders(name); 145 146 if (headers.length == 0) { 147 return null; 148 } else if (headers.length == 1) { 149 return new Header(headers[0].getName(), headers[0].getValue()); 150 } else { 151 StringBuffer valueBuffer = new StringBuffer(headers[0].getValue()); 152 153 for (int i = 1; i < headers.length; i++) { 154 valueBuffer.append(", "); 155 valueBuffer.append(headers[i].getValue()); 156 } 157 158 return new Header(name.toLowerCase(), valueBuffer.toString()); 159 } 160 } 161 162 /*** 163 * Gets all of the headers with the given name. The returned array 164 * maintains the relative order in which the headers were added. 165 * 166 * <p>Header name comparison is case insensitive. 167 * 168 * @param name the name of the header(s) to get 169 * 170 * @return an array of length >= 0 171 */ 172 public Header[] getHeaders(String name) { 173 ArrayList headersFound = new ArrayList(); 174 175 for (Iterator headerIter = headers.iterator(); headerIter.hasNext();) { 176 Header header = (Header) headerIter.next(); 177 if (header.getName().equalsIgnoreCase(name)) { 178 headersFound.add(header); 179 } 180 } 181 182 return (Header[]) headersFound.toArray(new Header[headersFound.size()]); 183 } 184 185 /*** 186 * Gets the first header with the given name. 187 * 188 * <p>Header name comparison is case insensitive. 189 * 190 * @param name the name of the header to get 191 * @return the first header or <code>null</code> 192 */ 193 public Header getFirstHeader(String name) { 194 for (Iterator headerIter = headers.iterator(); headerIter.hasNext();) { 195 Header header = (Header) headerIter.next(); 196 if (header.getName().equalsIgnoreCase(name)) { 197 return header; 198 } 199 } 200 201 return null; 202 } 203 204 /*** 205 * Gets the last header with the given name. 206 * 207 * <p>Header name comparison is case insensitive. 208 * 209 * @param name the name of the header to get 210 * @return the last header or <code>null</code> 211 */ 212 public Header getLastHeader(String name) { 213 // start at the end of the list and work backwards 214 for (int i = headers.size() - 1; i >= 0; i--) { 215 Header header = (Header) headers.get(i); 216 if (header.getName().equalsIgnoreCase(name)) { 217 return header; 218 } 219 } 220 221 return null; 222 } 223 224 /*** 225 * Gets all of the headers contained within this group. 226 * 227 * @return an array of length >= 0 228 */ 229 public Header[] getAllHeaders() { 230 return (Header[]) headers.toArray(new Header[headers.size()]); 231 } 232 233 /*** 234 * Tests if headers with the given name are contained within this group. 235 * 236 * <p>Header name comparison is case insensitive. 237 * 238 * @param name the header name to test for 239 * @return <code>true</code> if at least one header with the name is 240 * contained, <code>false</code> otherwise 241 */ 242 public boolean containsHeader(String name) { 243 for (Iterator headerIter = headers.iterator(); headerIter.hasNext();) { 244 Header header = (Header) headerIter.next(); 245 if (header.getName().equalsIgnoreCase(name)) { 246 return true; 247 } 248 } 249 250 return false; 251 } 252 253 }

This page was automatically generated by Maven