Repository navigation
Expand file tree
/
Copy pathHeaders.java
More file actions
178 lines (153 loc) · 4.89 KB
/
Copy pathHeaders.java
File metadata and controls
178 lines (153 loc) · 4.89 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
* The MIT License
*
* Copyright for portions of unirest-java are held by Kong Inc (c) 2013.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package kong.unirest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.function.Supplier;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
public class Headers {
private static final long serialVersionUID = 71310341388734766L;
private List<Header> headers = new ArrayList<>();
public Headers() {
}
public Headers(Collection<Entry> entries) {
entries.forEach(e -> add(e.name, e.value));
}
/**
* Add a header element
* @param name the name of the header
* @param value the value for the header
*/
public void add(String name, String value) {
add(name, () -> value);
}
/**
* Add a header element with a supplier which will be evaluated on request
* @param name the name of the header
* @param value the value for the header
*/
public void add(String name, Supplier<String> value) {
if (Objects.nonNull(name)) {
headers.add(new Entry(name, value));
}
}
/**
* Replace a header value. If there are multiple instances it will overwrite all of them
* @param name the name of the header
* @param value the value for the header
*/
public void replace(String name, String value) {
remove(name);
add(name, value);
}
private void remove(String name) {
headers.removeIf(h -> isName(h, name));
}
/**
* Get the number of header keys.
* @return the size of the header keys
*/
public int size() {
return headers.stream().map(Header::getName).collect(toSet()).size();
}
/**
* Get all the values for a header name
* @param name name of the header element
* @return a list of values
*/
public List<String> get(String name) {
return headers.stream()
.filter(h -> isName(h, name))
.map(Header::getValue)
.collect(toList());
}
/**
* Add a bunch of headers at once
* @param header a header
*/
public void putAll(Headers header) {
this.headers.addAll(header.headers);
}
/**
* Check if a header is present
* @param name a header
* @return if the headers contain this name.
*/
public boolean containsKey(String name) {
return this.headers.stream().anyMatch(h -> isName(h, name));
}
/**
* Clear the headers!
*/
public void clear() {
this.headers.clear();
}
/**
* Get the first header value for a name
* @param key the name of the header
* @return the first value
*/
public String getFirst(String key) {
return headers
.stream()
.filter(h -> isName(h, key))
.findFirst()
.map(Header::getValue)
.orElse("");
}
/**
* Get all of the headers
* @return all the headers, in order
*/
public List<Header> all() {
return this.headers;
}
private boolean isName(Header h, String name) {
return Util.nullToEmpty(name).equalsIgnoreCase(h.getName());
}
static class Entry implements Header {
private final String name;
private final Supplier<String> value;
public Entry(String name, String value) {
this.name = name;
this.value = () -> value;
}
public Entry(String name, Supplier<String> value) {
this.name = name;
this.value = value;
}
@Override
public String getName() {
return name;
}
@Override
public String getValue() {
return value.get();
}
}
}