forked from OpenFeign/feign
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpressions.java
More file actions
159 lines (136 loc) · 4.92 KB
/
Expressions.java
File metadata and controls
159 lines (136 loc) · 4.92 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
/**
* Copyright 2012-2020 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 java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import feign.Util;
public final class Expressions {
private static Map<Pattern, Class<? extends Expression>> expressions;
static {
expressions = new LinkedHashMap<>();
/*
* basic pattern for variable names. this is compliant with RFC 6570 Simple Expressions ONLY
* with the following additional values allowed without required pct-encoding:
*
* - brackets - dashes
*
* see https://tools.ietf.org/html/rfc6570#section-2.3 for more information.
*/
expressions.put(Pattern.compile("^([+#./;?&]?)(.*)$"),
SimpleExpression.class);
}
public static Expression create(final String value) {
/* remove the start and end braces */
final String expression = stripBraces(value);
if (expression == null || expression.isEmpty()) {
throw new IllegalArgumentException("an expression is required.");
}
Optional<Entry<Pattern, Class<? extends Expression>>> matchedExpressionEntry =
expressions.entrySet()
.stream()
.filter(entry -> entry.getKey().matcher(expression).matches())
.findFirst();
if (!matchedExpressionEntry.isPresent()) {
/* not a valid expression */
return null;
}
Entry<Pattern, Class<? extends Expression>> matchedExpression = matchedExpressionEntry.get();
Pattern expressionPattern = matchedExpression.getKey();
/* create a new regular expression matcher for the expression */
String variableName = null;
String variablePattern = null;
Matcher matcher = expressionPattern.matcher(expression);
if (matcher.matches()) {
/* we have a valid variable expression, extract the name from the first group */
variableName = matcher.group(2).trim();
if (variableName.contains(":")) {
/* split on the colon */
String[] parts = variableName.split(":");
variableName = parts[0];
variablePattern = parts[1];
}
/* look for nested expressions */
if (variableName.contains("{")) {
/* nested, literal */
return null;
}
}
return new SimpleExpression(variableName, variablePattern);
}
private static String stripBraces(String expression) {
if (expression == null) {
return null;
}
if (expression.startsWith("{") && expression.endsWith("}")) {
return expression.substring(1, expression.length() - 1);
}
return expression;
}
/**
* Expression that adheres to Simple String Expansion as outlined in <a
* href="https://tools.ietf.org/html/rfc6570#section-3.2.2>Simple String Expansion (Level 1)</a>
*/
static class SimpleExpression extends Expression {
SimpleExpression(String expression, String pattern) {
super(expression, pattern);
}
String encode(Object value) {
return UriUtils.encode(value.toString(), Util.UTF_8);
}
@Override
String expand(Object variable, boolean encode) {
StringBuilder expanded = new StringBuilder();
if (Iterable.class.isAssignableFrom(variable.getClass())) {
expanded.append(this.expandIterable((Iterable<?>) variable));
} else {
expanded.append((encode) ? encode(variable) : variable);
}
/* return the string value of the variable */
String result = expanded.toString();
if (!this.matches(result)) {
throw new IllegalArgumentException("Value " + expanded
+ " does not match the expression pattern: " + this.getPattern());
}
return result;
}
private String expandIterable(Iterable<?> values) {
StringBuilder result = new StringBuilder();
for (Object value : values) {
if (value == null) {
/* skip */
continue;
}
/* expand the value */
String expanded = this.encode(value);
if (expanded.isEmpty()) {
/* always append the separator */
result.append(",");
} else {
if (result.length() != 0) {
if (!result.toString().equalsIgnoreCase(",")) {
result.append(",");
}
}
result.append(expanded);
}
}
/* return the expanded value */
return result.toString();
}
}
}