Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* |
michael@0 | 2 | * ==================================================================== |
michael@0 | 3 | * Licensed to the Apache Software Foundation (ASF) under one |
michael@0 | 4 | * or more contributor license agreements. See the NOTICE file |
michael@0 | 5 | * distributed with this work for additional information |
michael@0 | 6 | * regarding copyright ownership. The ASF licenses this file |
michael@0 | 7 | * to you under the Apache License, Version 2.0 (the |
michael@0 | 8 | * "License"); you may not use this file except in compliance |
michael@0 | 9 | * with the License. You may obtain a copy of the License at |
michael@0 | 10 | * |
michael@0 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 12 | * |
michael@0 | 13 | * Unless required by applicable law or agreed to in writing, |
michael@0 | 14 | * software distributed under the License is distributed on an |
michael@0 | 15 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
michael@0 | 16 | * KIND, either express or implied. See the License for the |
michael@0 | 17 | * specific language governing permissions and limitations |
michael@0 | 18 | * under the License. |
michael@0 | 19 | * ==================================================================== |
michael@0 | 20 | * |
michael@0 | 21 | * This software consists of voluntary contributions made by many |
michael@0 | 22 | * individuals on behalf of the Apache Software Foundation. For more |
michael@0 | 23 | * information on the Apache Software Foundation, please see |
michael@0 | 24 | * <http://www.apache.org/>. |
michael@0 | 25 | * |
michael@0 | 26 | */ |
michael@0 | 27 | |
michael@0 | 28 | package ch.boye.httpclientandroidlib.message; |
michael@0 | 29 | |
michael@0 | 30 | import java.io.Serializable; |
michael@0 | 31 | import java.util.ArrayList; |
michael@0 | 32 | import java.util.List; |
michael@0 | 33 | import java.util.Locale; |
michael@0 | 34 | |
michael@0 | 35 | import ch.boye.httpclientandroidlib.Header; |
michael@0 | 36 | import ch.boye.httpclientandroidlib.HeaderIterator; |
michael@0 | 37 | import ch.boye.httpclientandroidlib.util.CharArrayBuffer; |
michael@0 | 38 | |
michael@0 | 39 | /** |
michael@0 | 40 | * A class for combining a set of headers. |
michael@0 | 41 | * This class allows for multiple headers with the same name and |
michael@0 | 42 | * keeps track of the order in which headers were added. |
michael@0 | 43 | * |
michael@0 | 44 | * |
michael@0 | 45 | * @since 4.0 |
michael@0 | 46 | */ |
michael@0 | 47 | public class HeaderGroup implements Cloneable, Serializable { |
michael@0 | 48 | |
michael@0 | 49 | private static final long serialVersionUID = 2608834160639271617L; |
michael@0 | 50 | |
michael@0 | 51 | /** The list of headers for this group, in the order in which they were added */ |
michael@0 | 52 | private final List headers; |
michael@0 | 53 | |
michael@0 | 54 | /** |
michael@0 | 55 | * Constructor for HeaderGroup. |
michael@0 | 56 | */ |
michael@0 | 57 | public HeaderGroup() { |
michael@0 | 58 | this.headers = new ArrayList(16); |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | /** |
michael@0 | 62 | * Removes any contained headers. |
michael@0 | 63 | */ |
michael@0 | 64 | public void clear() { |
michael@0 | 65 | headers.clear(); |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | /** |
michael@0 | 69 | * Adds the given header to the group. The order in which this header was |
michael@0 | 70 | * added is preserved. |
michael@0 | 71 | * |
michael@0 | 72 | * @param header the header to add |
michael@0 | 73 | */ |
michael@0 | 74 | public void addHeader(Header header) { |
michael@0 | 75 | if (header == null) { |
michael@0 | 76 | return; |
michael@0 | 77 | } |
michael@0 | 78 | headers.add(header); |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | /** |
michael@0 | 82 | * Removes the given header. |
michael@0 | 83 | * |
michael@0 | 84 | * @param header the header to remove |
michael@0 | 85 | */ |
michael@0 | 86 | public void removeHeader(Header header) { |
michael@0 | 87 | if (header == null) { |
michael@0 | 88 | return; |
michael@0 | 89 | } |
michael@0 | 90 | headers.remove(header); |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | /** |
michael@0 | 94 | * Replaces the first occurence of the header with the same name. If no header with |
michael@0 | 95 | * the same name is found the given header is added to the end of the list. |
michael@0 | 96 | * |
michael@0 | 97 | * @param header the new header that should replace the first header with the same |
michael@0 | 98 | * name if present in the list. |
michael@0 | 99 | */ |
michael@0 | 100 | public void updateHeader(Header header) { |
michael@0 | 101 | if (header == null) { |
michael@0 | 102 | return; |
michael@0 | 103 | } |
michael@0 | 104 | for (int i = 0; i < this.headers.size(); i++) { |
michael@0 | 105 | Header current = (Header) this.headers.get(i); |
michael@0 | 106 | if (current.getName().equalsIgnoreCase(header.getName())) { |
michael@0 | 107 | this.headers.set(i, header); |
michael@0 | 108 | return; |
michael@0 | 109 | } |
michael@0 | 110 | } |
michael@0 | 111 | this.headers.add(header); |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | /** |
michael@0 | 115 | * Sets all of the headers contained within this group overriding any |
michael@0 | 116 | * existing headers. The headers are added in the order in which they appear |
michael@0 | 117 | * in the array. |
michael@0 | 118 | * |
michael@0 | 119 | * @param headers the headers to set |
michael@0 | 120 | */ |
michael@0 | 121 | public void setHeaders(Header[] headers) { |
michael@0 | 122 | clear(); |
michael@0 | 123 | if (headers == null) { |
michael@0 | 124 | return; |
michael@0 | 125 | } |
michael@0 | 126 | for (int i = 0; i < headers.length; i++) { |
michael@0 | 127 | this.headers.add(headers[i]); |
michael@0 | 128 | } |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | /** |
michael@0 | 132 | * Gets a header representing all of the header values with the given name. |
michael@0 | 133 | * If more that one header with the given name exists the values will be |
michael@0 | 134 | * combined with a "," as per RFC 2616. |
michael@0 | 135 | * |
michael@0 | 136 | * <p>Header name comparison is case insensitive. |
michael@0 | 137 | * |
michael@0 | 138 | * @param name the name of the header(s) to get |
michael@0 | 139 | * @return a header with a condensed value or <code>null</code> if no |
michael@0 | 140 | * headers by the given name are present |
michael@0 | 141 | */ |
michael@0 | 142 | public Header getCondensedHeader(String name) { |
michael@0 | 143 | Header[] headers = getHeaders(name); |
michael@0 | 144 | |
michael@0 | 145 | if (headers.length == 0) { |
michael@0 | 146 | return null; |
michael@0 | 147 | } else if (headers.length == 1) { |
michael@0 | 148 | return headers[0]; |
michael@0 | 149 | } else { |
michael@0 | 150 | CharArrayBuffer valueBuffer = new CharArrayBuffer(128); |
michael@0 | 151 | valueBuffer.append(headers[0].getValue()); |
michael@0 | 152 | for (int i = 1; i < headers.length; i++) { |
michael@0 | 153 | valueBuffer.append(", "); |
michael@0 | 154 | valueBuffer.append(headers[i].getValue()); |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | return new BasicHeader(name.toLowerCase(Locale.ENGLISH), valueBuffer.toString()); |
michael@0 | 158 | } |
michael@0 | 159 | } |
michael@0 | 160 | |
michael@0 | 161 | /** |
michael@0 | 162 | * Gets all of the headers with the given name. The returned array |
michael@0 | 163 | * maintains the relative order in which the headers were added. |
michael@0 | 164 | * |
michael@0 | 165 | * <p>Header name comparison is case insensitive. |
michael@0 | 166 | * |
michael@0 | 167 | * @param name the name of the header(s) to get |
michael@0 | 168 | * |
michael@0 | 169 | * @return an array of length >= 0 |
michael@0 | 170 | */ |
michael@0 | 171 | public Header[] getHeaders(String name) { |
michael@0 | 172 | ArrayList headersFound = new ArrayList(); |
michael@0 | 173 | |
michael@0 | 174 | for (int i = 0; i < headers.size(); i++) { |
michael@0 | 175 | Header header = (Header) headers.get(i); |
michael@0 | 176 | if (header.getName().equalsIgnoreCase(name)) { |
michael@0 | 177 | headersFound.add(header); |
michael@0 | 178 | } |
michael@0 | 179 | } |
michael@0 | 180 | |
michael@0 | 181 | return (Header[]) headersFound.toArray(new Header[headersFound.size()]); |
michael@0 | 182 | } |
michael@0 | 183 | |
michael@0 | 184 | /** |
michael@0 | 185 | * Gets the first header with the given name. |
michael@0 | 186 | * |
michael@0 | 187 | * <p>Header name comparison is case insensitive. |
michael@0 | 188 | * |
michael@0 | 189 | * @param name the name of the header to get |
michael@0 | 190 | * @return the first header or <code>null</code> |
michael@0 | 191 | */ |
michael@0 | 192 | public Header getFirstHeader(String name) { |
michael@0 | 193 | for (int i = 0; i < headers.size(); i++) { |
michael@0 | 194 | Header header = (Header) headers.get(i); |
michael@0 | 195 | if (header.getName().equalsIgnoreCase(name)) { |
michael@0 | 196 | return header; |
michael@0 | 197 | } |
michael@0 | 198 | } |
michael@0 | 199 | return null; |
michael@0 | 200 | } |
michael@0 | 201 | |
michael@0 | 202 | /** |
michael@0 | 203 | * Gets the last header with the given name. |
michael@0 | 204 | * |
michael@0 | 205 | * <p>Header name comparison is case insensitive. |
michael@0 | 206 | * |
michael@0 | 207 | * @param name the name of the header to get |
michael@0 | 208 | * @return the last header or <code>null</code> |
michael@0 | 209 | */ |
michael@0 | 210 | public Header getLastHeader(String name) { |
michael@0 | 211 | // start at the end of the list and work backwards |
michael@0 | 212 | for (int i = headers.size() - 1; i >= 0; i--) { |
michael@0 | 213 | Header header = (Header) headers.get(i); |
michael@0 | 214 | if (header.getName().equalsIgnoreCase(name)) { |
michael@0 | 215 | return header; |
michael@0 | 216 | } |
michael@0 | 217 | } |
michael@0 | 218 | |
michael@0 | 219 | return null; |
michael@0 | 220 | } |
michael@0 | 221 | |
michael@0 | 222 | /** |
michael@0 | 223 | * Gets all of the headers contained within this group. |
michael@0 | 224 | * |
michael@0 | 225 | * @return an array of length >= 0 |
michael@0 | 226 | */ |
michael@0 | 227 | public Header[] getAllHeaders() { |
michael@0 | 228 | return (Header[]) headers.toArray(new Header[headers.size()]); |
michael@0 | 229 | } |
michael@0 | 230 | |
michael@0 | 231 | /** |
michael@0 | 232 | * Tests if headers with the given name are contained within this group. |
michael@0 | 233 | * |
michael@0 | 234 | * <p>Header name comparison is case insensitive. |
michael@0 | 235 | * |
michael@0 | 236 | * @param name the header name to test for |
michael@0 | 237 | * @return <code>true</code> if at least one header with the name is |
michael@0 | 238 | * contained, <code>false</code> otherwise |
michael@0 | 239 | */ |
michael@0 | 240 | public boolean containsHeader(String name) { |
michael@0 | 241 | for (int i = 0; i < headers.size(); i++) { |
michael@0 | 242 | Header header = (Header) headers.get(i); |
michael@0 | 243 | if (header.getName().equalsIgnoreCase(name)) { |
michael@0 | 244 | return true; |
michael@0 | 245 | } |
michael@0 | 246 | } |
michael@0 | 247 | |
michael@0 | 248 | return false; |
michael@0 | 249 | } |
michael@0 | 250 | |
michael@0 | 251 | /** |
michael@0 | 252 | * Returns an iterator over this group of headers. |
michael@0 | 253 | * |
michael@0 | 254 | * @return iterator over this group of headers. |
michael@0 | 255 | * |
michael@0 | 256 | * @since 4.0 |
michael@0 | 257 | */ |
michael@0 | 258 | public HeaderIterator iterator() { |
michael@0 | 259 | return new BasicListHeaderIterator(this.headers, null); |
michael@0 | 260 | } |
michael@0 | 261 | |
michael@0 | 262 | /** |
michael@0 | 263 | * Returns an iterator over the headers with a given name in this group. |
michael@0 | 264 | * |
michael@0 | 265 | * @param name the name of the headers over which to iterate, or |
michael@0 | 266 | * <code>null</code> for all headers |
michael@0 | 267 | * |
michael@0 | 268 | * @return iterator over some headers in this group. |
michael@0 | 269 | * |
michael@0 | 270 | * @since 4.0 |
michael@0 | 271 | */ |
michael@0 | 272 | public HeaderIterator iterator(final String name) { |
michael@0 | 273 | return new BasicListHeaderIterator(this.headers, name); |
michael@0 | 274 | } |
michael@0 | 275 | |
michael@0 | 276 | /** |
michael@0 | 277 | * Returns a copy of this object |
michael@0 | 278 | * |
michael@0 | 279 | * @return copy of this object |
michael@0 | 280 | * |
michael@0 | 281 | * @since 4.0 |
michael@0 | 282 | */ |
michael@0 | 283 | public HeaderGroup copy() { |
michael@0 | 284 | HeaderGroup clone = new HeaderGroup(); |
michael@0 | 285 | clone.headers.addAll(this.headers); |
michael@0 | 286 | return clone; |
michael@0 | 287 | } |
michael@0 | 288 | |
michael@0 | 289 | public Object clone() throws CloneNotSupportedException { |
michael@0 | 290 | HeaderGroup clone = (HeaderGroup) super.clone(); |
michael@0 | 291 | clone.headers.clear(); |
michael@0 | 292 | clone.headers.addAll(this.headers); |
michael@0 | 293 | return clone; |
michael@0 | 294 | } |
michael@0 | 295 | |
michael@0 | 296 | public String toString() { |
michael@0 | 297 | return this.headers.toString(); |
michael@0 | 298 | } |
michael@0 | 299 | |
michael@0 | 300 | } |