forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRouterMatch.java
More file actions
109 lines (90 loc) · 2.39 KB
/
RouterMatch.java
File metadata and controls
109 lines (90 loc) · 2.39 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
/**
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby.internal;
import io.jooby.Context;
import io.jooby.MessageEncoder;
import io.jooby.Route;
import io.jooby.Router;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
public class RouterMatch implements Router.Match {
boolean matches;
private Route route;
Map vars = Collections.EMPTY_MAP;
private Route.Handler handler;
public RouterMatch() {
}
public void key(List<String> keys) {
for (int i = 0; i < Math.min(keys.size(), vars.size()); i++) {
vars.put(keys.get(i), vars.remove(i));
}
}
public void truncate(int size) {
while (size < vars.size()) {
vars.remove(size++);
}
}
public void value(String value) {
if (vars == Collections.EMPTY_MAP) {
vars = new LinkedHashMap();
}
vars.put(vars.size(), value);
}
public void pop() {
vars.remove(vars.size() - 1);
}
public void methodNotAllowed(Set<String> allow) {
String allowString = allow.stream().collect(Collectors.joining(","));
Route.Decorator decorator = next -> ctx -> {
ctx.setResponseHeader("Allow", allowString);
return next.apply(ctx);
};
handler = decorator.then(Route.METHOD_NOT_ALLOWED);
}
@Override public boolean matches() {
return matches;
}
@Override public Route route() {
return route;
}
@Override public Map<String, String> pathMap() {
return vars;
}
public RouterMatch found(Route route) {
this.route = route;
this.matches = true;
return this;
}
public void execute(Context context) {
context.setPathMap(vars);
context.setRoute(route);
try {
route.getPipeline().apply(context);
} catch (Throwable x) {
context.sendError(x);
} finally {
this.handler = null;
this.route = null;
this.vars = null;
}
}
public RouterMatch missing(String method, String path, MessageEncoder encoder) {
Route.Handler h;
if (this.handler == null) {
h = path.endsWith("/favicon.ico") ? Route.FAVICON : Route.NOT_FOUND;
} else {
h = this.handler;
}
this.route = new Route(method, path, h);
this.route.setEncoder(encoder);
this.route.setReturnType(Context.class);
return this;
}
}