~ubuntu-branches/ubuntu/trusty/httpcomponents-core/trusty

« back to all changes in this revision

Viewing changes to httpcore/src/main/java/org/apache/http/message/HeaderGroup.java

  • Committer: Bazaar Package Importer
  • Author(s): David Paleino
  • Date: 2010-06-12 08:37:34 UTC
  • Revision ID: james.westby@ubuntu.com-20100612083734-1y8kp6qm4sjk60az
Tags: upstream-4.0.1
ImportĀ upstreamĀ versionĀ 4.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0.1/httpcore/src/main/java/org/apache/http/message/HeaderGroup.java $
 
3
 * $Revision: 744527 $
 
4
 * $Date: 2009-02-14 18:06:25 +0100 (Sat, 14 Feb 2009) $
 
5
 *
 
6
 * ====================================================================
 
7
 * Licensed to the Apache Software Foundation (ASF) under one
 
8
 * or more contributor license agreements.  See the NOTICE file
 
9
 * distributed with this work for additional information
 
10
 * regarding copyright ownership.  The ASF licenses this file
 
11
 * to you under the Apache License, Version 2.0 (the
 
12
 * "License"); you may not use this file except in compliance
 
13
 * with the License.  You may obtain a copy of the License at
 
14
 *
 
15
 *   http://www.apache.org/licenses/LICENSE-2.0
 
16
 *
 
17
 * Unless required by applicable law or agreed to in writing,
 
18
 * software distributed under the License is distributed on an
 
19
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 
20
 * KIND, either express or implied.  See the License for the
 
21
 * specific language governing permissions and limitations
 
22
 * under the License.
 
23
 * ====================================================================
 
24
 *
 
25
 * This software consists of voluntary contributions made by many
 
26
 * individuals on behalf of the Apache Software Foundation.  For more
 
27
 * information on the Apache Software Foundation, please see
 
28
 * <http://www.apache.org/>.
 
29
 *
 
30
 */
 
31
 
 
32
package org.apache.http.message;
 
33
 
 
34
import java.util.ArrayList;
 
35
import java.util.List;
 
36
import java.util.Locale;
 
37
 
 
38
import org.apache.http.Header;
 
39
import org.apache.http.HeaderIterator;
 
40
import org.apache.http.util.CharArrayBuffer;
 
41
 
 
42
/**
 
43
 * A class for combining a set of headers.
 
44
 * This class allows for multiple headers with the same name and
 
45
 * keeps track of the order in which headers were added.
 
46
 * 
 
47
 *
 
48
 * @since 4.0
 
49
 */
 
50
public class HeaderGroup implements Cloneable {
 
51
 
 
52
    /** The list of headers for this group, in the order in which they were added */
 
53
    private List headers;
 
54
 
 
55
    /**
 
56
     * Constructor for HeaderGroup.
 
57
     */
 
58
    public HeaderGroup() {
 
59
        this.headers = new ArrayList(16);
 
60
    }
 
61
    
 
62
    /**
 
63
     * Removes any contained headers.
 
64
     */
 
65
    public void clear() {
 
66
        headers.clear();
 
67
    }
 
68
    
 
69
    /**
 
70
     * Adds the given header to the group.  The order in which this header was
 
71
     * added is preserved.
 
72
     * 
 
73
     * @param header the header to add
 
74
     */
 
75
    public void addHeader(Header header) {
 
76
        if (header == null) {
 
77
            return;
 
78
        }
 
79
        headers.add(header);
 
80
    }
 
81
    
 
82
    /**
 
83
     * Removes the given header.
 
84
     *
 
85
     * @param header the header to remove
 
86
     */
 
87
    public void removeHeader(Header header) {
 
88
        if (header == null) {
 
89
            return;
 
90
        }
 
91
        headers.remove(header);
 
92
    }
 
93
 
 
94
    /**
 
95
     * Replaces the first occurence of the header with the same name. If no header with 
 
96
     * the same name is found the given header is added to the end of the list.
 
97
     * 
 
98
     * @param header the new header that should replace the first header with the same 
 
99
     * name if present in the list.
 
100
     */
 
101
    public void updateHeader(Header header) {
 
102
        if (header == null) {
 
103
            return;
 
104
        }
 
105
        for (int i = 0; i < this.headers.size(); i++) {
 
106
            Header current = (Header) this.headers.get(i);
 
107
            if (current.getName().equalsIgnoreCase(header.getName())) {
 
108
                this.headers.set(i, header);
 
109
                return;
 
110
            }
 
111
        }
 
112
        this.headers.add(header);
 
113
    }
 
114
 
 
115
    /**
 
116
     * Sets all of the headers contained within this group overriding any
 
117
     * existing headers. The headers are added in the order in which they appear
 
118
     * in the array.
 
119
     * 
 
120
     * @param headers the headers to set
 
121
     */
 
122
    public void setHeaders(Header[] headers) {
 
123
        clear();
 
124
        if (headers == null) {
 
125
            return;
 
126
        }
 
127
        for (int i = 0; i < headers.length; i++) {
 
128
            this.headers.add(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 headers[0];
 
150
        } else {
 
151
            CharArrayBuffer valueBuffer = new CharArrayBuffer(128);
 
152
            valueBuffer.append(headers[0].getValue());
 
153
            for (int i = 1; i < headers.length; i++) {
 
154
                valueBuffer.append(", ");
 
155
                valueBuffer.append(headers[i].getValue());
 
156
            }
 
157
            
 
158
            return new BasicHeader(name.toLowerCase(Locale.ENGLISH), 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 (int i = 0; i < headers.size(); i++) {
 
176
            Header header = (Header) headers.get(i);
 
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 (int i = 0; i < headers.size(); i++) {
 
195
            Header header = (Header) headers.get(i);
 
196
            if (header.getName().equalsIgnoreCase(name)) {
 
197
                return header;
 
198
            }
 
199
        }
 
200
        return null;                
 
201
    }
 
202
    
 
203
    /**
 
204
     * Gets the last header with the given name.
 
205
     *
 
206
     * <p>Header name comparison is case insensitive.
 
207
     *
 
208
     * @param name the name of the header to get
 
209
     * @return the last header or <code>null</code>
 
210
     */
 
211
    public Header getLastHeader(String name) {
 
212
        // start at the end of the list and work backwards
 
213
        for (int i = headers.size() - 1; i >= 0; i--) {
 
214
            Header header = (Header) headers.get(i);
 
215
            if (header.getName().equalsIgnoreCase(name)) {
 
216
                return header;
 
217
            }            
 
218
        }
 
219
        
 
220
        return null;        
 
221
    }
 
222
    
 
223
    /**
 
224
     * Gets all of the headers contained within this group.
 
225
     * 
 
226
     * @return an array of length >= 0
 
227
     */
 
228
    public Header[] getAllHeaders() {
 
229
        return (Header[]) headers.toArray(new Header[headers.size()]);
 
230
    }
 
231
    
 
232
    /**
 
233
     * Tests if headers with the given name are contained within this group.
 
234
     * 
 
235
     * <p>Header name comparison is case insensitive.
 
236
     * 
 
237
     * @param name the header name to test for
 
238
     * @return <code>true</code> if at least one header with the name is
 
239
     * contained, <code>false</code> otherwise
 
240
     */
 
241
    public boolean containsHeader(String name) {
 
242
        for (int i = 0; i < headers.size(); i++) {
 
243
            Header header = (Header) headers.get(i);
 
244
            if (header.getName().equalsIgnoreCase(name)) {
 
245
                return true;
 
246
            }
 
247
        }
 
248
        
 
249
        return false;
 
250
    }
 
251
 
 
252
    /**
 
253
     * Returns an iterator over this group of headers.
 
254
     * 
 
255
     * @return iterator over this group of headers.
 
256
     * 
 
257
     * @since 4.0
 
258
     */
 
259
    public HeaderIterator iterator() {
 
260
        return new BasicListHeaderIterator(this.headers, null); 
 
261
    }
 
262
 
 
263
    /**
 
264
     * Returns an iterator over the headers with a given name in this group.
 
265
     *
 
266
     * @param name      the name of the headers over which to iterate, or
 
267
     *                  <code>null</code> for all headers
 
268
     *
 
269
     * @return iterator over some headers in this group.
 
270
     * 
 
271
     * @since 4.0
 
272
     */
 
273
    public HeaderIterator iterator(final String name) {
 
274
        return new BasicListHeaderIterator(this.headers, name);
 
275
    }
 
276
    
 
277
    /**
 
278
     * Returns a copy of this object
 
279
     * 
 
280
     * @return copy of this object
 
281
     */
 
282
    public HeaderGroup copy() {
 
283
        HeaderGroup clone = new HeaderGroup();
 
284
        clone.headers.addAll(this.headers);
 
285
        return clone;
 
286
    }
 
287
    
 
288
    public Object clone() throws CloneNotSupportedException {
 
289
        HeaderGroup clone = (HeaderGroup) super.clone();
 
290
        clone.headers = new ArrayList(this.headers);
 
291
        return clone;
 
292
    }
 
293
    
 
294
}