1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/message/HeaderGroup.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,300 @@ 1.4 +/* 1.5 + * ==================================================================== 1.6 + * Licensed to the Apache Software Foundation (ASF) under one 1.7 + * or more contributor license agreements. See the NOTICE file 1.8 + * distributed with this work for additional information 1.9 + * regarding copyright ownership. The ASF licenses this file 1.10 + * to you under the Apache License, Version 2.0 (the 1.11 + * "License"); you may not use this file except in compliance 1.12 + * with the License. You may obtain a copy of the License at 1.13 + * 1.14 + * http://www.apache.org/licenses/LICENSE-2.0 1.15 + * 1.16 + * Unless required by applicable law or agreed to in writing, 1.17 + * software distributed under the License is distributed on an 1.18 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1.19 + * KIND, either express or implied. See the License for the 1.20 + * specific language governing permissions and limitations 1.21 + * under the License. 1.22 + * ==================================================================== 1.23 + * 1.24 + * This software consists of voluntary contributions made by many 1.25 + * individuals on behalf of the Apache Software Foundation. For more 1.26 + * information on the Apache Software Foundation, please see 1.27 + * <http://www.apache.org/>. 1.28 + * 1.29 + */ 1.30 + 1.31 +package ch.boye.httpclientandroidlib.message; 1.32 + 1.33 +import java.io.Serializable; 1.34 +import java.util.ArrayList; 1.35 +import java.util.List; 1.36 +import java.util.Locale; 1.37 + 1.38 +import ch.boye.httpclientandroidlib.Header; 1.39 +import ch.boye.httpclientandroidlib.HeaderIterator; 1.40 +import ch.boye.httpclientandroidlib.util.CharArrayBuffer; 1.41 + 1.42 +/** 1.43 + * A class for combining a set of headers. 1.44 + * This class allows for multiple headers with the same name and 1.45 + * keeps track of the order in which headers were added. 1.46 + * 1.47 + * 1.48 + * @since 4.0 1.49 + */ 1.50 +public class HeaderGroup implements Cloneable, Serializable { 1.51 + 1.52 + private static final long serialVersionUID = 2608834160639271617L; 1.53 + 1.54 + /** The list of headers for this group, in the order in which they were added */ 1.55 + private final List headers; 1.56 + 1.57 + /** 1.58 + * Constructor for HeaderGroup. 1.59 + */ 1.60 + public HeaderGroup() { 1.61 + this.headers = new ArrayList(16); 1.62 + } 1.63 + 1.64 + /** 1.65 + * Removes any contained headers. 1.66 + */ 1.67 + public void clear() { 1.68 + headers.clear(); 1.69 + } 1.70 + 1.71 + /** 1.72 + * Adds the given header to the group. The order in which this header was 1.73 + * added is preserved. 1.74 + * 1.75 + * @param header the header to add 1.76 + */ 1.77 + public void addHeader(Header header) { 1.78 + if (header == null) { 1.79 + return; 1.80 + } 1.81 + headers.add(header); 1.82 + } 1.83 + 1.84 + /** 1.85 + * Removes the given header. 1.86 + * 1.87 + * @param header the header to remove 1.88 + */ 1.89 + public void removeHeader(Header header) { 1.90 + if (header == null) { 1.91 + return; 1.92 + } 1.93 + headers.remove(header); 1.94 + } 1.95 + 1.96 + /** 1.97 + * Replaces the first occurence of the header with the same name. If no header with 1.98 + * the same name is found the given header is added to the end of the list. 1.99 + * 1.100 + * @param header the new header that should replace the first header with the same 1.101 + * name if present in the list. 1.102 + */ 1.103 + public void updateHeader(Header header) { 1.104 + if (header == null) { 1.105 + return; 1.106 + } 1.107 + for (int i = 0; i < this.headers.size(); i++) { 1.108 + Header current = (Header) this.headers.get(i); 1.109 + if (current.getName().equalsIgnoreCase(header.getName())) { 1.110 + this.headers.set(i, header); 1.111 + return; 1.112 + } 1.113 + } 1.114 + this.headers.add(header); 1.115 + } 1.116 + 1.117 + /** 1.118 + * Sets all of the headers contained within this group overriding any 1.119 + * existing headers. The headers are added in the order in which they appear 1.120 + * in the array. 1.121 + * 1.122 + * @param headers the headers to set 1.123 + */ 1.124 + public void setHeaders(Header[] headers) { 1.125 + clear(); 1.126 + if (headers == null) { 1.127 + return; 1.128 + } 1.129 + for (int i = 0; i < headers.length; i++) { 1.130 + this.headers.add(headers[i]); 1.131 + } 1.132 + } 1.133 + 1.134 + /** 1.135 + * Gets a header representing all of the header values with the given name. 1.136 + * If more that one header with the given name exists the values will be 1.137 + * combined with a "," as per RFC 2616. 1.138 + * 1.139 + * <p>Header name comparison is case insensitive. 1.140 + * 1.141 + * @param name the name of the header(s) to get 1.142 + * @return a header with a condensed value or <code>null</code> if no 1.143 + * headers by the given name are present 1.144 + */ 1.145 + public Header getCondensedHeader(String name) { 1.146 + Header[] headers = getHeaders(name); 1.147 + 1.148 + if (headers.length == 0) { 1.149 + return null; 1.150 + } else if (headers.length == 1) { 1.151 + return headers[0]; 1.152 + } else { 1.153 + CharArrayBuffer valueBuffer = new CharArrayBuffer(128); 1.154 + valueBuffer.append(headers[0].getValue()); 1.155 + for (int i = 1; i < headers.length; i++) { 1.156 + valueBuffer.append(", "); 1.157 + valueBuffer.append(headers[i].getValue()); 1.158 + } 1.159 + 1.160 + return new BasicHeader(name.toLowerCase(Locale.ENGLISH), valueBuffer.toString()); 1.161 + } 1.162 + } 1.163 + 1.164 + /** 1.165 + * Gets all of the headers with the given name. The returned array 1.166 + * maintains the relative order in which the headers were added. 1.167 + * 1.168 + * <p>Header name comparison is case insensitive. 1.169 + * 1.170 + * @param name the name of the header(s) to get 1.171 + * 1.172 + * @return an array of length >= 0 1.173 + */ 1.174 + public Header[] getHeaders(String name) { 1.175 + ArrayList headersFound = new ArrayList(); 1.176 + 1.177 + for (int i = 0; i < headers.size(); i++) { 1.178 + Header header = (Header) headers.get(i); 1.179 + if (header.getName().equalsIgnoreCase(name)) { 1.180 + headersFound.add(header); 1.181 + } 1.182 + } 1.183 + 1.184 + return (Header[]) headersFound.toArray(new Header[headersFound.size()]); 1.185 + } 1.186 + 1.187 + /** 1.188 + * Gets the first header with the given name. 1.189 + * 1.190 + * <p>Header name comparison is case insensitive. 1.191 + * 1.192 + * @param name the name of the header to get 1.193 + * @return the first header or <code>null</code> 1.194 + */ 1.195 + public Header getFirstHeader(String name) { 1.196 + for (int i = 0; i < headers.size(); i++) { 1.197 + Header header = (Header) headers.get(i); 1.198 + if (header.getName().equalsIgnoreCase(name)) { 1.199 + return header; 1.200 + } 1.201 + } 1.202 + return null; 1.203 + } 1.204 + 1.205 + /** 1.206 + * Gets the last header with the given name. 1.207 + * 1.208 + * <p>Header name comparison is case insensitive. 1.209 + * 1.210 + * @param name the name of the header to get 1.211 + * @return the last header or <code>null</code> 1.212 + */ 1.213 + public Header getLastHeader(String name) { 1.214 + // start at the end of the list and work backwards 1.215 + for (int i = headers.size() - 1; i >= 0; i--) { 1.216 + Header header = (Header) headers.get(i); 1.217 + if (header.getName().equalsIgnoreCase(name)) { 1.218 + return header; 1.219 + } 1.220 + } 1.221 + 1.222 + return null; 1.223 + } 1.224 + 1.225 + /** 1.226 + * Gets all of the headers contained within this group. 1.227 + * 1.228 + * @return an array of length >= 0 1.229 + */ 1.230 + public Header[] getAllHeaders() { 1.231 + return (Header[]) headers.toArray(new Header[headers.size()]); 1.232 + } 1.233 + 1.234 + /** 1.235 + * Tests if headers with the given name are contained within this group. 1.236 + * 1.237 + * <p>Header name comparison is case insensitive. 1.238 + * 1.239 + * @param name the header name to test for 1.240 + * @return <code>true</code> if at least one header with the name is 1.241 + * contained, <code>false</code> otherwise 1.242 + */ 1.243 + public boolean containsHeader(String name) { 1.244 + for (int i = 0; i < headers.size(); i++) { 1.245 + Header header = (Header) headers.get(i); 1.246 + if (header.getName().equalsIgnoreCase(name)) { 1.247 + return true; 1.248 + } 1.249 + } 1.250 + 1.251 + return false; 1.252 + } 1.253 + 1.254 + /** 1.255 + * Returns an iterator over this group of headers. 1.256 + * 1.257 + * @return iterator over this group of headers. 1.258 + * 1.259 + * @since 4.0 1.260 + */ 1.261 + public HeaderIterator iterator() { 1.262 + return new BasicListHeaderIterator(this.headers, null); 1.263 + } 1.264 + 1.265 + /** 1.266 + * Returns an iterator over the headers with a given name in this group. 1.267 + * 1.268 + * @param name the name of the headers over which to iterate, or 1.269 + * <code>null</code> for all headers 1.270 + * 1.271 + * @return iterator over some headers in this group. 1.272 + * 1.273 + * @since 4.0 1.274 + */ 1.275 + public HeaderIterator iterator(final String name) { 1.276 + return new BasicListHeaderIterator(this.headers, name); 1.277 + } 1.278 + 1.279 + /** 1.280 + * Returns a copy of this object 1.281 + * 1.282 + * @return copy of this object 1.283 + * 1.284 + * @since 4.0 1.285 + */ 1.286 + public HeaderGroup copy() { 1.287 + HeaderGroup clone = new HeaderGroup(); 1.288 + clone.headers.addAll(this.headers); 1.289 + return clone; 1.290 + } 1.291 + 1.292 + public Object clone() throws CloneNotSupportedException { 1.293 + HeaderGroup clone = (HeaderGroup) super.clone(); 1.294 + clone.headers.clear(); 1.295 + clone.headers.addAll(this.headers); 1.296 + return clone; 1.297 + } 1.298 + 1.299 + public String toString() { 1.300 + return this.headers.toString(); 1.301 + } 1.302 + 1.303 +}