forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRouteTableRouterBuilder.java
More file actions
179 lines (168 loc) · 4.91 KB
/
RouteTableRouterBuilder.java
File metadata and controls
179 lines (168 loc) · 4.91 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package act.route;
/*-
* #%L
* ACT Framework
* %%
* Copyright (C) 2014 - 2017 ActFramework
* %%
* 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.
* #L%
*/
import act.Act;
import act.app.App;
import act.app.event.SysEventId;
import org.osgl.http.H;
import org.osgl.http.util.Path;
import org.osgl.util.C;
import org.osgl.util.E;
import org.osgl.util.S;
import org.osgl.util.Unsafe;
import java.util.Iterator;
import java.util.List;
/**
* {@code RouteTableRouterBuilder} take a list of route map definition line and
* build the router. The line of a route map definition should look like:
* <p/>
* <pre>
* [http-method] [url-path] [action-definition]
* </pre>
* <p/>
* Where http-method could be one of the following:
* <ul>
* <li>GET</li>
* <li>POST</li>
* <li>PUT</li>
* <li>DELETE</li>
* </ul>
* <p/>
* url-path defines the incoming URL path, and it could
* be either static or dynamic. For example,
* <p/>
* <pre>
* # home
* /
*
* # order list
* /order
*
* # (dynamic) access to a certain order by ID
* /order/{id}
*
* # (dynamic) access to a user by ID with regex spec
* /user/{<[1-9]{5}>id}
* </pre>
* <p/>
* action-definition could be in either built-in action
* or controller action method.
* <p/>
* <p>Built-in action definition should be in a format of
* <code>[directive]: [payload]</code>, for example
* </p>
* <p/>
* <ul>
* <li>
* Echo - write back a text message directly
* <pre>
* echo: hello world!
* </pre>
* </li>
* <li>
* Redirect - send permanent redirect in the response
* <pre>
* redirect: http://www.google.com
* </pre>
* </li>
* <li>
* Static file directory handler - fetch files in a local directory
* <pre>
* staticDir: /public
* </pre>
* </li>
* <li>
* Static file locator - fetch specified file on request
* <pre>
* staticFile: /public/js/jquery.js
* </pre>
* </li>
* </ul>
*/
public class RouteTableRouterBuilder implements RouterBuilder {
public static final String ROUTES_FILE = "routes.conf";
private List<String> lines;
public RouteTableRouterBuilder(List<String> lines) {
E.NPE(lines);
this.lines = lines;
}
public RouteTableRouterBuilder(String... lines) {
E.illegalArgumentIf(lines.length == 0, "Empty route configuration file lines");
this.lines = C.listOf(lines);
}
@Override
public void build(Router router) {
int lineNo = lines.size();
for (int i = 0; i < lineNo; ++i) {
String line = lines.get(i).trim();
if (line.startsWith("#")) continue;
if (S.blank(line)) continue;
try {
process(line, router);
} catch (final RuntimeException e) {
if (Act.isDev()) {
final App app = router.app();
app.jobManager().on(SysEventId.PRE_START, new Runnable() {
@Override
public void run() {
app.setBlockIssue(e);
}
});
} else {
throw e;
}
}
}
}
private void process(String line, Router router) {
Iterator<String> itr = Path.tokenizer(Unsafe.bufOf(line), 0, ' ', '\u0000');
final String UNKNOWN = S.fmt("route configuration not recognized: %s", line);
String method = null, path = null;
S.Buffer action = S.newBuffer();
if (itr.hasNext()) {
method = itr.next();
} else {
E.illegalArgumentIf(true, UNKNOWN);
}
if (itr.hasNext()) {
path = itr.next();
} else {
E.illegalArgumentIf(true, UNKNOWN);
}
E.illegalArgumentIf(!itr.hasNext(), UNKNOWN);
action.append(itr.next());
while (itr.hasNext()) {
action.append(" ").append(itr.next());
}
if ("*".contentEquals(method)) {
for (H.Method m : Router.supportedHttpMethods()) {
router.addMapping(m, path, action.toString(), RouteSource.ROUTE_TABLE);
}
} else {
String s = method;
if ("context".equalsIgnoreCase(s) || "ctx".equalsIgnoreCase(s)) {
router.addContext(action.toString(), path);
} else {
H.Method m = H.Method.valueOfIgnoreCase(s);
router.addMapping(m, path, action.toString(), RouteSource.ROUTE_TABLE);
}
}
}
}