-
Notifications
You must be signed in to change notification settings - Fork 888
/
RootNode.java
185 lines (173 loc) · 5.84 KB
/
RootNode.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
184
185
package org.jboss.resteasy.core.registry;
import org.jboss.resteasy.core.ResourceMethodInvoker;
import org.jboss.resteasy.spi.HttpRequest;
import org.jboss.resteasy.spi.ResourceInvoker;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import static org.jboss.resteasy.core.registry.SegmentNode.RESTEASY_CHOSEN_ACCEPT;
/**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
* @version $Revision: 1 $
*/
public class RootNode
{
protected SegmentNode root = new SegmentNode("");
protected int size = 0;
protected MultivaluedMap<String, MethodExpression> bounded = new MultivaluedHashMap<String, MethodExpression>();
protected ConcurrentHashMap<MatchCache.Key, MatchCache> cache = new ConcurrentHashMap<>();
public int getSize()
{
return size;
}
public MultivaluedMap<String, ResourceInvoker> getBounded()
{
MultivaluedHashMap<String, ResourceInvoker> rtn = new MultivaluedHashMap<String, ResourceInvoker>();
for (Map.Entry<String, List<MethodExpression>> entry : bounded.entrySet())
{
for (MethodExpression exp : entry.getValue())
{
rtn.add(entry.getKey(), exp.getInvoker());
}
}
return rtn;
}
private static boolean CACHE = true;
public ResourceInvoker match(HttpRequest request, int start)
{
if (!CACHE) {
return root.match(request, start).invoker;
}
MatchCache.Key key = new MatchCache.Key(request, start);
MatchCache match = cache.get(key);
if (match != null) {
//System.out.println("*** cache hit: " + key.method + " " + key.path);
request.setAttribute(RESTEASY_CHOSEN_ACCEPT, match.chosen);
} else {
match = root.match(request, start);
if (match.match != null && match.match.expression.getNumGroups() == 0 && match.invoker instanceof ResourceMethodInvoker) {
//System.out.println("*** caching: " + key.method + " " + key.path);
match.match = null;
cache.putIfAbsent(key, match);
}
}
return match.invoker;
}
public void removeBinding(String path, Method method)
{
List<MethodExpression> expressions = bounded.get(path);
if (expressions == null) return;
for (MethodExpression expression : expressions)
{
ResourceInvoker invoker = expression.getInvoker();
if (invoker.getMethod().equals(method))
{
expression.parent.targets.remove(expression);
expressions.remove(expression);
if (expressions.size() == 0) bounded.remove(path);
size--;
if (invoker instanceof ResourceMethodInvoker)
{
((ResourceMethodInvoker)invoker).cleanup();
}
return;
}
}
}
public void addInvoker(String path, ResourceInvoker invoker)
{
MethodExpression expression = addExpression(path, invoker);
size++;
bounded.add(path, expression);
}
protected MethodExpression addExpression(String path, ResourceInvoker invoker)
{
if (path.startsWith("/")) path = path.substring(1);
if (path.endsWith("/")) path = path.substring(0, path.length() - 1);
if ("".equals(path))
{
if (invoker instanceof ResourceMethodInvoker)
{
MethodExpression expression = new MethodExpression(root, "", invoker);
root.addExpression(expression);
return expression;
}
else
{
MethodExpression expression = new MethodExpression(root, "", invoker, "(.*)");
root.addExpression(expression);
return expression;
}
}
MethodExpression exp;
//Matcher param = PathHelper.URI_PARAM_PATTERN.matcher(path);
int expidx = path.indexOf('{');
if (expidx > -1)
{
int i =expidx;
while (i - 1 > -1)
{
if (path.charAt(i - 1) == '/')
{
break;
}
i--;
}
String staticPath = null;
if (i > 0) staticPath = path.substring(0, i - 1);
SegmentNode node = root;
if (staticPath != null)
{
String[] split = staticPath.split("/");
for (String segment : split)
{
SegmentNode tmp = node.children.get(segment);
if (tmp == null)
{
tmp = new SegmentNode(segment);
node.children.put(segment, tmp);
}
node = tmp;
}
}
if (invoker instanceof ResourceMethodInvoker)
{
exp = new MethodExpression(node, path, invoker);
}
else
{
exp = new MethodExpression(node, path, invoker, "(/.+)?");
}
node.addExpression(exp);
}
else
{
String[] split = path.split("/");
SegmentNode node = root;
for (String segment : split)
{
SegmentNode tmp = node.children.get(segment);
if (tmp == null)
{
tmp = new SegmentNode(segment);
node.children.put(segment, tmp);
}
node = tmp;
}
if (invoker instanceof ResourceMethodInvoker)
{
exp = new MethodExpression(node, path, invoker);
node.addExpression(exp);
}
else
{
exp = new MethodExpression(node, path, invoker, "(.*)");
node.addExpression(exp);
}
}
return exp;
}
}