-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathHeaderSet.java
More file actions
126 lines (111 loc) · 4.13 KB
/
HeaderSet.java
File metadata and controls
126 lines (111 loc) · 4.13 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/****************************************************************************/
/* File: HeaderHelper.java */
/* Author: F. Georges - fgeorges.org */
/* Date: 2009-02-21 */
/* Tags: */
/* Copyright (c) 2009 Florent Georges (see end of file.) */
/* ------------------------------------------------------------------------ */
package org.expath.httpclient;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.message.BasicHeader;
/**
* TODO: Doc...
*
* TODO: Change this class to a real wrapper around a {@link Header[]} or a
* {@link Collection}<Header>.
*
* @author Florent Georges
*/
public class HeaderSet
implements Iterable<Header>
{
/**
* Build a new object with no header.
*/
public HeaderSet()
{
myHeaders = new ArrayList<Header>();
}
/**
* Build a new object by *copying* its parameter.
*/
public HeaderSet(Header[] headers)
throws HttpClientException
{
if ( headers == null ) {
throw new HttpClientException("Headers array is null");
}
myHeaders = new ArrayList<Header>(headers.length);
myHeaders.addAll(Arrays.asList(headers));
}
/**
* Build a new object by *copying* its parameter.
*/
public HeaderSet(Collection<Header> headers)
throws HttpClientException
{
if ( headers == null ) {
throw new HttpClientException("Headers list is null");
}
myHeaders = new ArrayList<Header>(headers);
}
public Iterator<Header> iterator()
{
return myHeaders.iterator();
}
public Header[] toArray()
{
return myHeaders.toArray(new Header[0]);
}
public boolean isEmpty()
{
return myHeaders.isEmpty();
}
public Header add(Header h)
{
myHeaders.add(h);
return h;
}
public Header add(String name, String value)
{
Header h = new BasicHeader(name, value);
myHeaders.add(h);
return h;
}
public Header getFirstHeader(String name)
throws HttpClientException
{
for ( Header h : myHeaders ) {
if ( name.equalsIgnoreCase(h.getName()) ) {
return h;
}
}
return null;
}
private List<Header> myHeaders;
}
/* ------------------------------------------------------------------------ */
/* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS COMMENT. */
/* */
/* The contents of this file are subject to the Mozilla Public License */
/* Version 1.0 (the "License"); you may not use this file except in */
/* compliance with the License. You may obtain a copy of the License at */
/* http://www.mozilla.org/MPL/. */
/* */
/* Software distributed under the License is distributed on an "AS IS" */
/* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See */
/* the License for the specific language governing rights and limitations */
/* under the License. */
/* */
/* The Original Code is: all this file. */
/* */
/* The Initial Developer of the Original Code is Florent Georges. */
/* */
/* Contributor(s): none. */
/* ------------------------------------------------------------------------ */