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
122 lines (105 loc) · 4.11 KB
/
Expressions.java
File metadata and controls
122 lines (105 loc) · 4.11 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
/**
* Copyright 2012-2019 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.UriUtils.FragmentType;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public final class Expressions {
private static Map<Pattern, Class<? extends Expression>> expressions;
static {
expressions = new LinkedHashMap<>();
expressions.put(Pattern.compile("(\\w[-\\w.]*[ ]*)(:(.+))?"), SimpleExpression.class);
}
public static Expression create(final String value, final FragmentType type) {
/* 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(1).trim();
if (matcher.group(2) != null && matcher.group(3) != null) {
/* this variable contains an optional pattern */
variablePattern = matcher.group(3);
}
}
return new SimpleExpression(variableName, variablePattern, type);
}
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 {
private final FragmentType type;
SimpleExpression(String expression, String pattern, FragmentType type) {
super(expression, pattern);
this.type = type;
}
String encode(Object value) {
return UriUtils.encodeReserved(value.toString(), type, Util.UTF_8);
}
@Override
String expand(Object variable, boolean encode) {
StringBuilder expanded = new StringBuilder();
if (Iterable.class.isAssignableFrom(variable.getClass())) {
List<String> items = new ArrayList<>();
for (Object item : ((Iterable) variable)) {
items.add((encode) ? encode(item) : item.toString());
}
expanded.append(String.join(",", items));
} 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;
}
}
}