1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.hc.core5.http.message;
29
30 import java.io.Serializable;
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.Iterator;
34 import java.util.List;
35 import java.util.Locale;
36
37 import org.apache.hc.core5.http.Header;
38 import org.apache.hc.core5.http.MessageHeaders;
39 import org.apache.hc.core5.http.ProtocolException;
40 import org.apache.hc.core5.util.CharArrayBuffer;
41 import org.apache.hc.core5.util.LangUtils;
42
43
44
45
46
47
48
49 public class HeaderGroup implements MessageHeaders, Serializable {
50
51 private static final long serialVersionUID = 2608834160639271617L;
52
53 private static final Headerp/Header.html#Header">Header[] EMPTY = new Header[] {};
54
55
56 private final List<Header> headers;
57
58
59
60
61
62 public HeaderGroup() {
63 this.headers = new ArrayList<>(16);
64 }
65
66
67
68
69 public void clear() {
70 headers.clear();
71 }
72
73
74
75
76
77
78
79 public void addHeader(final Header header) {
80 if (header == null) {
81 return;
82 }
83 headers.add(header);
84 }
85
86
87
88
89
90
91
92 public boolean removeHeader(final Header header) {
93 if (header == null) {
94 return false;
95 }
96 for (int i = 0; i < this.headers.size(); i++) {
97 final Header current = this.headers.get(i);
98 if (headerEquals(header, current)) {
99 this.headers.remove(current);
100 return true;
101 }
102 }
103 return false;
104 }
105
106 private boolean headerEquals(final Header/Header.html#Header">Header header1, final Header header2) {
107 return header2 == header1 || header2.getName().equalsIgnoreCase(header1.getName())
108 && LangUtils.equals(header1.getValue(), header2.getValue());
109 }
110
111
112
113
114
115
116
117
118
119 public boolean removeHeaders(final Header header) {
120 if (header == null) {
121 return false;
122 }
123 boolean removed = false;
124 for (final Iterator<Header> iterator = headerIterator(); iterator.hasNext();) {
125 final Header current = iterator.next();
126 if (headerEquals(header, current)) {
127 iterator.remove();
128 removed = true;
129 }
130 }
131 return removed;
132 }
133
134
135
136
137
138
139
140
141
142
143 public void setHeader(final Header header) {
144 if (header == null) {
145 return;
146 }
147 for (int i = 0; i < this.headers.size(); i++) {
148 final Header current = this.headers.get(i);
149 if (current.getName().equalsIgnoreCase(header.getName())) {
150 this.headers.set(i, header);
151 return;
152 }
153 }
154 this.headers.add(header);
155 }
156
157
158
159
160
161
162
163
164 public void setHeaders(final Header... headers) {
165 clear();
166 if (headers == null) {
167 return;
168 }
169 Collections.addAll(this.headers, headers);
170 }
171
172
173
174
175
176
177
178
179
180
181
182
183 public Header getCondensedHeader(final String name) {
184 final Header[] hdrs = getHeaders(name);
185
186 if (hdrs.length == 0) {
187 return null;
188 } else if (hdrs.length == 1) {
189 return hdrs[0];
190 } else {
191 final CharArrayBufferffer.html#CharArrayBuffer">CharArrayBuffer valueBuffer = new CharArrayBuffer(128);
192 valueBuffer.append(hdrs[0].getValue());
193 for (int i = 1; i < hdrs.length; i++) {
194 valueBuffer.append(", ");
195 valueBuffer.append(hdrs[i].getValue());
196 }
197
198 return new BasicHeader(name.toLowerCase(Locale.ROOT), valueBuffer.toString());
199 }
200 }
201
202
203
204
205
206
207
208
209
210
211
212 @Override
213 public Header[] getHeaders(final String name) {
214 List<Header> headersFound = null;
215 for (int i = 0; i < this.headers.size(); i++) {
216 final Header header = this.headers.get(i);
217 if (header.getName().equalsIgnoreCase(name)) {
218 if (headersFound == null) {
219 headersFound = new ArrayList<>();
220 }
221 headersFound.add(header);
222 }
223 }
224 return headersFound != null ? headersFound.toArray(EMPTY) : EMPTY;
225 }
226
227
228
229
230
231
232
233
234
235 @Override
236 public Header getFirstHeader(final String name) {
237 for (int i = 0; i < this.headers.size(); i++) {
238 final Header header = this.headers.get(i);
239 if (header.getName().equalsIgnoreCase(name)) {
240 return header;
241 }
242 }
243 return null;
244 }
245
246
247
248
249
250
251
252
253
254
255 @Override
256 public Header getHeader(final String name) throws ProtocolException {
257 int count = 0;
258 Header singleHeader = null;
259 for (int i = 0; i < this.headers.size(); i++) {
260 final Header header = this.headers.get(i);
261 if (header.getName().equalsIgnoreCase(name)) {
262 singleHeader = header;
263 count++;
264 }
265 }
266 if (count > 1) {
267 throw new ProtocolException("multiple '%s' headers found", name);
268 }
269 return singleHeader;
270 }
271
272
273
274
275
276
277
278
279
280 @Override
281 public Header getLastHeader(final String name) {
282
283 for (int i = headers.size() - 1; i >= 0; i--) {
284 final Header header = headers.get(i);
285 if (header.getName().equalsIgnoreCase(name)) {
286 return header;
287 }
288 }
289
290 return null;
291 }
292
293
294
295
296
297
298 @Override
299 public Header[] getHeaders() {
300 return headers.toArray(EMPTY);
301 }
302
303
304
305
306
307
308
309
310
311
312 @Override
313 public boolean containsHeader(final String name) {
314 for (int i = 0; i < this.headers.size(); i++) {
315 final Header header = this.headers.get(i);
316 if (header.getName().equalsIgnoreCase(name)) {
317 return true;
318 }
319 }
320
321 return false;
322 }
323
324
325
326
327
328
329
330
331 @Override
332 public int countHeaders(final String name) {
333 int count = 0;
334 for (int i = 0; i < this.headers.size(); i++) {
335 final Header header = this.headers.get(i);
336 if (header.getName().equalsIgnoreCase(name)) {
337 count++;
338 }
339 }
340 return count;
341 }
342
343
344
345
346
347
348
349
350 @Override
351 public Iterator<Header> headerIterator() {
352 return new BasicListHeaderIterator(this.headers, null);
353 }
354
355
356
357
358
359
360
361
362
363
364
365 @Override
366 public Iterator<Header> headerIterator(final String name) {
367 return new BasicListHeaderIterator(this.headers, name);
368 }
369
370
371
372
373
374
375
376
377
378 public boolean removeHeaders(final String name) {
379 if (name == null) {
380 return false;
381 }
382 boolean removed = false;
383 for (final Iterator<Header> iterator = headerIterator(); iterator.hasNext(); ) {
384 final Header header = iterator.next();
385 if (header.getName().equalsIgnoreCase(name)) {
386 iterator.remove();
387 removed = true;
388 }
389 }
390 return removed;
391 }
392
393 @Override
394 public String toString() {
395 return this.headers.toString();
396 }
397
398 }