/
RoutingHandler.java
295 lines (256 loc) · 11.2 KB
/
RoutingHandler.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
* JBoss, Home of Professional Open Source.
* Copyright 2014 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* 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 io.undertow.server;
import io.undertow.predicate.Predicate;
import io.undertow.server.handlers.ResponseCodeHandler;
import io.undertow.util.CopyOnWriteMap;
import io.undertow.util.HttpString;
import io.undertow.util.Methods;
import io.undertow.util.PathTemplate;
import io.undertow.util.PathTemplateMatch;
import io.undertow.util.PathTemplateMatcher;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* A Handler that handles the common case of routing via path template and method name.
*
* @author Stuart Douglas
*/
public class RoutingHandler implements HttpHandler {
// Matcher objects grouped by http methods.
private final Map<HttpString, PathTemplateMatcher<RoutingMatch>> matches = new CopyOnWriteMap<>();
// Matcher used to find if this instance contains matches for any http method for a path.
// This matcher is used to report if this instance can match a path for one of the http methods.
private final PathTemplateMatcher<RoutingMatch> allMethodsMatcher = new PathTemplateMatcher<>();
// Handler called when no match was found and invalid method handler can't be invoked.
private volatile HttpHandler fallbackHandler = ResponseCodeHandler.HANDLE_404;
// Handler called when this instance can not match the http method but can match another http method.
// For example: For an exchange the POST method is not matched by this instance but at least one http method is
// matched for the same exchange.
// If this handler is null the fallbackHandler will be used.
private volatile HttpHandler invalidMethodHandler = ResponseCodeHandler.HANDLE_405;
// If this is true then path matches will be added to the query parameters for easy access by later handlers.
private final boolean rewriteQueryParameters;
public RoutingHandler(boolean rewriteQueryParameters) {
this.rewriteQueryParameters = rewriteQueryParameters;
}
public RoutingHandler() {
this.rewriteQueryParameters = true;
}
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
PathTemplateMatcher<RoutingMatch> matcher = matches.get(exchange.getRequestMethod());
if (matcher == null) {
handleNoMatch(exchange);
return;
}
PathTemplateMatcher.PathMatchResult<RoutingMatch> match = matcher.match(exchange.getRelativePath());
if (match == null) {
handleNoMatch(exchange);
return;
}
exchange.putAttachment(PathTemplateMatch.ATTACHMENT_KEY, match);
if (rewriteQueryParameters) {
for (Map.Entry<String, String> entry : match.getParameters().entrySet()) {
exchange.addQueryParam(entry.getKey(), entry.getValue());
}
}
for (HandlerHolder handler : match.getValue().predicatedHandlers) {
if (handler.predicate.resolve(exchange)) {
handler.handler.handleRequest(exchange);
return;
}
}
if (match.getValue().defaultHandler != null) {
match.getValue().defaultHandler.handleRequest(exchange);
} else {
fallbackHandler.handleRequest(exchange);
}
}
/**
* Handles the case in with a match was not found for the http method but might exist for another http method.
* For example: POST not matched for a path but at least one match exists for same path.
*
* @param exchange The object for which its handled the "no match" case.
* @throws Exception
*/
private void handleNoMatch(final HttpServerExchange exchange) throws Exception {
// if invalidMethodHandler is null we fail fast without matching with allMethodsMatcher
if (invalidMethodHandler != null && allMethodsMatcher.match(exchange.getRelativePath()) != null) {
invalidMethodHandler.handleRequest(exchange);
return;
}
fallbackHandler.handleRequest(exchange);
}
public synchronized RoutingHandler add(final String method, final String template, HttpHandler handler) {
return add(new HttpString(method), template, handler);
}
public synchronized RoutingHandler add(HttpString method, String template, HttpHandler handler) {
PathTemplateMatcher<RoutingMatch> matcher = matches.get(method);
if (matcher == null) {
matches.put(method, matcher = new PathTemplateMatcher<>());
}
RoutingMatch res = matcher.get(template);
if (res == null) {
matcher.add(template, res = new RoutingMatch());
}
if (allMethodsMatcher.match(template) == null) {
allMethodsMatcher.add(template, res);
}
res.defaultHandler = handler;
return this;
}
public synchronized RoutingHandler get(final String template, HttpHandler handler) {
return add(Methods.GET, template, handler);
}
public synchronized RoutingHandler post(final String template, HttpHandler handler) {
return add(Methods.POST, template, handler);
}
public synchronized RoutingHandler put(final String template, HttpHandler handler) {
return add(Methods.PUT, template, handler);
}
public synchronized RoutingHandler delete(final String template, HttpHandler handler) {
return add(Methods.DELETE, template, handler);
}
public synchronized RoutingHandler add(final String method, final String template, Predicate predicate, HttpHandler handler) {
return add(new HttpString(method), template, predicate, handler);
}
public synchronized RoutingHandler add(HttpString method, String template, Predicate predicate, HttpHandler handler) {
PathTemplateMatcher<RoutingMatch> matcher = matches.get(method);
if (matcher == null) {
matches.put(method, matcher = new PathTemplateMatcher<>());
}
RoutingMatch res = matcher.get(template);
if (res == null) {
matcher.add(template, res = new RoutingMatch());
}
if (allMethodsMatcher.match(template) == null) {
allMethodsMatcher.add(template, res);
}
res.predicatedHandlers.add(new HandlerHolder(predicate, handler));
return this;
}
public synchronized RoutingHandler get(final String template, Predicate predicate, HttpHandler handler) {
return add(Methods.GET, template, predicate, handler);
}
public synchronized RoutingHandler post(final String template, Predicate predicate, HttpHandler handler) {
return add(Methods.POST, template, predicate, handler);
}
public synchronized RoutingHandler put(final String template, Predicate predicate, HttpHandler handler) {
return add(Methods.PUT, template, predicate, handler);
}
public synchronized RoutingHandler delete(final String template, Predicate predicate, HttpHandler handler) {
return add(Methods.DELETE, template, predicate, handler);
}
public synchronized RoutingHandler addAll(RoutingHandler routingHandler) {
for (Entry<HttpString, PathTemplateMatcher<RoutingMatch>> entry : routingHandler.getMatches().entrySet()) {
HttpString method = entry.getKey();
PathTemplateMatcher<RoutingMatch> matcher = matches.get(method);
if (matcher == null) {
matches.put(method, matcher = new PathTemplateMatcher<>());
}
matcher.addAll(entry.getValue());
// If we use allMethodsMatcher.addAll() we can have duplicate
// PathTemplates which we want to ignore here so it does not crash.
for (PathTemplate template : entry.getValue().getPathTemplates()) {
if (allMethodsMatcher.match(template.getTemplateString()) == null) {
allMethodsMatcher.add(template, new RoutingMatch());
}
}
}
return this;
}
/**
*
* Removes the specified route from the handler
*
* @param method The method to remove
* @param path the path tempate to remove
* @return this handler
*/
public RoutingHandler remove(HttpString method, String path) {
PathTemplateMatcher<RoutingMatch> handler = matches.get(method);
if(handler != null) {
handler.remove(path);
}
return this;
}
/**
*
* Removes the specified route from the handler
*
* @param path the path tempate to remove
* @return this handler
*/
public RoutingHandler remove(String path) {
allMethodsMatcher.remove(path);
return this;
}
Map<HttpString, PathTemplateMatcher<RoutingMatch>> getMatches() {
return matches;
}
/**
* @return Handler called when no match was found and invalid method handler can't be invoked.
*/
public HttpHandler getFallbackHandler() {
return fallbackHandler;
}
/**
* @param fallbackHandler Handler that will be called when no match was found and invalid method handler can't be
* invoked.
* @return This instance.
*/
public RoutingHandler setFallbackHandler(HttpHandler fallbackHandler) {
this.fallbackHandler = fallbackHandler;
return this;
}
/**
* @return Handler called when this instance can not match the http method but can match another http method.
*/
public HttpHandler getInvalidMethodHandler() {
return invalidMethodHandler;
}
/**
* Sets the handler called when this instance can not match the http method but can match another http method.
* For example: For an exchange the POST method is not matched by this instance but at least one http method matched
* for the exchange.
* If this handler is null the fallbackHandler will be used.
*
* @param invalidMethodHandler Handler that will be called when this instance can not match the http method but can
* match another http method.
* @return This instance.
*/
public RoutingHandler setInvalidMethodHandler(HttpHandler invalidMethodHandler) {
this.invalidMethodHandler = invalidMethodHandler;
return this;
}
private static class RoutingMatch {
final List<HandlerHolder> predicatedHandlers = new CopyOnWriteArrayList<>();
volatile HttpHandler defaultHandler;
}
private static class HandlerHolder {
final Predicate predicate;
final HttpHandler handler;
private HandlerHolder(Predicate predicate, HttpHandler handler) {
this.predicate = predicate;
this.handler = handler;
}
}
}