-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
HeaderTemplate.java
183 lines (159 loc) · 5.53 KB
/
HeaderTemplate.java
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
179
180
181
182
183
/*
* Copyright 2012-2023 The Feign Authors
*
* Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package feign.template;
import feign.Util;
import feign.template.Template.EncodingOptions;
import feign.template.Template.ExpansionOptions;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
/**
* Template for HTTP Headers. Variables that are unresolved are ignored and Literals are not
* encoded.
*/
public final class HeaderTemplate {
private final String name;
private final List<Template> values = new CopyOnWriteArrayList<>();
public static HeaderTemplate create(String name, Iterable<String> values) {
if (name == null || name.isEmpty()) {
throw new IllegalArgumentException("name is required.");
}
if (values == null) {
throw new IllegalArgumentException("values are required");
}
return new HeaderTemplate(name, values, Util.UTF_8);
}
public static HeaderTemplate literal(String name, Iterable<String> values) {
if (name == null || name.isEmpty()) {
throw new IllegalArgumentException("name is required.");
}
if (values == null) {
throw new IllegalArgumentException("values are required");
}
return new HeaderTemplate(name, values, Util.UTF_8, true);
}
/**
* Append values to a Header Template.
*
* @param headerTemplate to append to.
* @param values to append.
* @return a new Header Template with the values added.
*/
public static HeaderTemplate append(HeaderTemplate headerTemplate, Iterable<String> values) {
LinkedHashSet<String> headerValues = new LinkedHashSet<>(headerTemplate.getValues());
headerValues.addAll(StreamSupport.stream(values.spliterator(), false)
.filter(Util::isNotBlank)
.collect(Collectors.toCollection(LinkedHashSet::new)));
return create(headerTemplate.getName(), headerValues);
}
/**
* Append values to a Header Template, as literals
*
* @param headerTemplate to append to.
* @param values to append.
* @return a new Header Template with the values added.
*/
public static HeaderTemplate appendLiteral(HeaderTemplate headerTemplate,
Iterable<String> values) {
LinkedHashSet<String> headerValues = new LinkedHashSet<>(headerTemplate.getValues());
headerValues.addAll(StreamSupport.stream(values.spliterator(), false)
.filter(Util::isNotBlank)
.collect(Collectors.toCollection(LinkedHashSet::new)));
return literal(headerTemplate.getName(), headerValues);
}
/**
* Create a new Header Template.
*
* @param name of the Header.
* @param values for the Header.
* @param charset to use when encoding the values.
*/
private HeaderTemplate(String name, Iterable<String> values, Charset charset) {
this(name, values, charset, false);
}
/**
* Create a new Header Template.
*
* @param name of the header
* @param values of the header
* @param charset for the header
* @param literal indicator. Will treat all values as literals instead of possible expressions.
*/
private HeaderTemplate(String name, Iterable<String> values, Charset charset, boolean literal) {
this.name = name;
for (String value : values) {
if (value == null || value.isEmpty()) {
/* skip */
continue;
}
if (literal) {
this.values.add(
new Template(
ExpansionOptions.ALLOW_UNRESOLVED,
EncodingOptions.NOT_REQUIRED,
false,
charset,
Collections.singletonList(Literal.create(value))));
} else {
this.values.add(
new Template(
value,
ExpansionOptions.REQUIRED,
EncodingOptions.NOT_REQUIRED,
false,
charset));
}
}
}
public Collection<String> getValues() {
return Collections.unmodifiableList(this.values.stream()
.map(Template::toString)
.collect(Collectors.toList()));
}
public List<String> getVariables() {
List<String> variables = new ArrayList<>();
for (Template template : this.values) {
variables.addAll(template.getVariables());
}
return Collections.unmodifiableList(variables);
}
public String getName() {
return this.name;
}
public String expand(Map<String, ?> variables) {
List<String> expanded = new ArrayList<>();
if (!this.values.isEmpty()) {
for (Template template : this.values) {
String result = template.expand(variables);
if (result == null) {
/* ignore unresolved values */
continue;
}
expanded.add(result);
}
}
StringBuilder result = new StringBuilder();
if (!expanded.isEmpty()) {
result.append(String.join(", ", expanded));
}
return result.toString();
}
}