1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.cubby.routing.impl; |
17 | |
|
18 | |
import static org.seasar.cubby.internal.util.LogMessages.format; |
19 | |
|
20 | |
import java.io.UnsupportedEncodingException; |
21 | |
import java.lang.reflect.Method; |
22 | |
import java.util.ArrayList; |
23 | |
import java.util.Collection; |
24 | |
import java.util.HashMap; |
25 | |
import java.util.Iterator; |
26 | |
import java.util.List; |
27 | |
import java.util.Map; |
28 | |
import java.util.TreeMap; |
29 | |
import java.util.Map.Entry; |
30 | |
import java.util.regex.Matcher; |
31 | |
import java.util.regex.Pattern; |
32 | |
|
33 | |
import org.seasar.cubby.action.Path; |
34 | |
import org.seasar.cubby.action.RequestMethod; |
35 | |
import org.seasar.cubby.internal.util.MetaUtils; |
36 | |
import org.seasar.cubby.internal.util.QueryStringBuilder; |
37 | |
import org.seasar.cubby.internal.util.URLBodyEncoder; |
38 | |
import org.seasar.cubby.routing.PathInfo; |
39 | |
import org.seasar.cubby.routing.PathResolver; |
40 | |
import org.seasar.cubby.routing.PathTemplateParser; |
41 | |
import org.seasar.cubby.routing.Routing; |
42 | |
import org.seasar.cubby.routing.RoutingException; |
43 | |
import org.seasar.cubby.util.ActionUtils; |
44 | |
import org.slf4j.Logger; |
45 | |
import org.slf4j.LoggerFactory; |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | 1203 | public class PathResolverImpl implements PathResolver { |
54 | |
|
55 | |
|
56 | 3 | private static final Logger logger = LoggerFactory |
57 | |
.getLogger(PathResolverImpl.class); |
58 | |
|
59 | |
|
60 | 171 | private final Map<RoutingKey, Routing> routings = new TreeMap<RoutingKey, Routing>(); |
61 | |
|
62 | |
|
63 | |
private PathTemplateParser pathTemplateParser; |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | 171 | public PathResolverImpl(final PathTemplateParser pathTemplateParser) { |
72 | 171 | this.pathTemplateParser = pathTemplateParser; |
73 | 171 | } |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
public Collection<Routing> getRoutings() { |
81 | 123 | return routings.values(); |
82 | |
} |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
public void add(final Class<?> actionClass) { |
88 | 4728 | for (final Method method : actionClass.getMethods()) { |
89 | 4497 | if (ActionUtils.isActionMethod(method)) { |
90 | 864 | final String actionPath = MetaUtils.getActionPath(actionClass, |
91 | |
method); |
92 | 864 | final RequestMethod[] acceptableRequestMethods = MetaUtils |
93 | |
.getAcceptableRequestMethods(actionClass, method); |
94 | 2502 | for (final RequestMethod requestMethod : acceptableRequestMethods) { |
95 | 1638 | final String onSubmit = MetaUtils.getOnSubmit(method); |
96 | 1638 | final int priority = MetaUtils.getPriority(method); |
97 | 1638 | this.add(actionPath, actionClass, method, requestMethod, |
98 | |
onSubmit, priority); |
99 | |
} |
100 | |
} |
101 | |
} |
102 | 231 | } |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
public void addAll(final Collection<Class<?>> actionClasses) { |
108 | 132 | for (final Class<?> actionClass : actionClasses) { |
109 | 222 | add(actionClass); |
110 | |
} |
111 | 132 | } |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
public void clear() { |
117 | 3 | routings.clear(); |
118 | 3 | } |
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
public void add(final String actionPath, final Class<?> actionClass, |
124 | |
final String methodName, final RequestMethod requestMethod, |
125 | |
final String onSubmit, final int priority) { |
126 | |
try { |
127 | 72 | final Method method = actionClass.getMethod(methodName); |
128 | 72 | this.add(actionPath, actionClass, method, requestMethod, onSubmit, |
129 | |
priority); |
130 | 0 | } catch (final NoSuchMethodException e) { |
131 | 0 | throw new RoutingException(e); |
132 | 72 | } |
133 | 72 | } |
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
private void add(final String actionPath, final Class<?> actionClass, |
152 | |
final Method method, final RequestMethod requestMethod, |
153 | |
final String onSubmit, final int priority) { |
154 | 1710 | if (!ActionUtils.isActionMethod(method)) { |
155 | 0 | throw new RoutingException(format("ECUB0003", method)); |
156 | |
} |
157 | |
|
158 | 1710 | final List<String> uriParameterNames = new ArrayList<String>(); |
159 | 1710 | final String uriRegex = pathTemplateParser.parse(actionPath, |
160 | 1710 | new PathTemplateParser.Handler() { |
161 | |
|
162 | |
public String handle(final String name, final String regex) { |
163 | 1155 | uriParameterNames.add(name); |
164 | 1155 | return regexGroup(regex); |
165 | |
} |
166 | |
|
167 | |
}); |
168 | 1710 | final Pattern pattern = Pattern.compile("^" + uriRegex + "$"); |
169 | |
|
170 | 1710 | final Routing routing = new RoutingImpl(actionClass, method, |
171 | |
actionPath, uriParameterNames, pattern, requestMethod, |
172 | |
onSubmit, priority); |
173 | 1710 | final RoutingKey key = new RoutingKey(routing); |
174 | |
|
175 | 1710 | if (logger.isDebugEnabled()) { |
176 | 1710 | logger.debug(format("DCUB0007", routing)); |
177 | |
} |
178 | 1710 | if (routings.containsKey(key)) { |
179 | 0 | final Routing duplication = routings.get(key); |
180 | 0 | throw new RoutingException(format("ECUB0001", routing, duplication)); |
181 | |
} |
182 | 1710 | routings.put(key, routing); |
183 | 1710 | } |
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
public PathInfo getPathInfo(final String path, final String requestMethod, |
189 | |
final String characterEncoding) { |
190 | 39 | final Iterator<Routing> iterator = getRoutings().iterator(); |
191 | 531 | while (iterator.hasNext()) { |
192 | 525 | final Routing routing = iterator.next(); |
193 | 525 | final Matcher matcher = routing.getPattern().matcher(path); |
194 | 525 | if (matcher.find()) { |
195 | 33 | if (routing.isAcceptable(requestMethod)) { |
196 | 33 | final Map<String, Routing> onSubmitRoutings = new HashMap<String, Routing>(); |
197 | 33 | onSubmitRoutings.put(routing.getOnSubmit(), routing); |
198 | 384 | while (iterator.hasNext()) { |
199 | 351 | final Routing anotherRouting = iterator.next(); |
200 | 351 | if (routing.getPattern().pattern().equals( |
201 | |
anotherRouting.getPattern().pattern()) |
202 | |
&& routing.getRequestMethod().equals( |
203 | |
anotherRouting.getRequestMethod())) { |
204 | 0 | onSubmitRoutings.put(anotherRouting.getOnSubmit(), |
205 | |
anotherRouting); |
206 | |
} |
207 | 351 | } |
208 | |
|
209 | 33 | final Map<String, String[]> uriParameters = new HashMap<String, String[]>(); |
210 | 57 | for (int i = 0; i < matcher.groupCount(); i++) { |
211 | 24 | final String name = routing.getUriParameterNames().get( |
212 | |
i); |
213 | 24 | final String value = matcher.group(i + 1); |
214 | 24 | uriParameters.put(name, new String[] { value }); |
215 | |
} |
216 | |
|
217 | 33 | final PathInfo pathInfo = new ResolvedPathInfo( |
218 | |
uriParameters, onSubmitRoutings); |
219 | |
|
220 | 33 | return pathInfo; |
221 | |
} |
222 | |
} |
223 | 492 | } |
224 | |
|
225 | 6 | return null; |
226 | |
} |
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
private static String regexGroup(final String regex) { |
236 | 1155 | return "(" + regex + ")"; |
237 | |
} |
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
public String reverseLookup(final Class<?> actionClass, |
243 | |
final String methodName, final Map<String, String[]> parameters, |
244 | |
final String characterEncoding) { |
245 | 72 | final Collection<Routing> routings = getRoutings(); |
246 | 72 | final Routing routing = findRouting(routings, actionClass, methodName); |
247 | 69 | final String actionPath = routing.getActionPath(); |
248 | 69 | final Map<String, String[]> copyOfParameters = new HashMap<String, String[]>( |
249 | |
parameters); |
250 | 69 | final StringBuilder path = new StringBuilder(100); |
251 | 69 | path.append(pathTemplateParser.parse(actionPath, |
252 | 69 | new PathTemplateParser.Handler() { |
253 | |
|
254 | |
public String handle(final String name, final String regex) { |
255 | 54 | if (!copyOfParameters.containsKey(name)) { |
256 | 3 | throw new RoutingException(format("ECUB0104", |
257 | |
actionPath, name)); |
258 | |
} |
259 | 51 | final String value = copyOfParameters.remove(name)[0]; |
260 | 51 | if (!value.matches(regex)) { |
261 | 3 | throw new RoutingException(format("ECUB0105", |
262 | |
actionPath, name, value, regex)); |
263 | |
} |
264 | 48 | return encode(value, characterEncoding); |
265 | |
} |
266 | |
|
267 | |
})); |
268 | |
|
269 | 63 | if (!copyOfParameters.isEmpty()) { |
270 | 24 | final QueryStringBuilder builder = new QueryStringBuilder(); |
271 | 24 | if (characterEncoding != null) { |
272 | 24 | builder.setEncode(characterEncoding); |
273 | |
} |
274 | 24 | for (final Entry<String, String[]> entry : copyOfParameters |
275 | |
.entrySet()) { |
276 | 66 | for (final String value : entry.getValue()) { |
277 | 33 | builder.addParam(entry.getKey(), value); |
278 | |
} |
279 | |
} |
280 | 24 | path.append('?'); |
281 | 24 | path.append(builder.toString()); |
282 | |
} |
283 | |
|
284 | 63 | return path.toString(); |
285 | |
} |
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
private static Routing findRouting(final Collection<Routing> routings, |
301 | |
final Class<?> actionClass, final String methodName) { |
302 | 72 | for (final Routing routing : routings) { |
303 | 219 | if (actionClass.getCanonicalName().equals( |
304 | |
routing.getActionClass().getCanonicalName())) { |
305 | 219 | if (methodName.equals(routing.getActionMethod().getName())) { |
306 | 69 | return routing; |
307 | |
} |
308 | |
} |
309 | |
} |
310 | 3 | throw new RoutingException(format("ECUB0103", actionClass, methodName)); |
311 | |
} |
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
private static String encode(final String str, |
323 | |
final String characterEncoding) { |
324 | 48 | if (characterEncoding == null) { |
325 | 0 | return str; |
326 | |
} |
327 | |
try { |
328 | 48 | return URLBodyEncoder.encode(str, characterEncoding); |
329 | 0 | } catch (final UnsupportedEncodingException e) { |
330 | 0 | throw new RoutingException(e); |
331 | |
} |
332 | |
} |
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | 10605 | static class RoutingKey implements Comparable<RoutingKey> { |
341 | |
|
342 | |
private final int priority; |
343 | |
|
344 | |
private final List<String> uriParameterNames; |
345 | |
|
346 | |
private final Pattern pattern; |
347 | |
|
348 | |
private final RequestMethod requestMethod; |
349 | |
|
350 | |
private final String onSubmit; |
351 | |
|
352 | 1758 | public RoutingKey(final Routing routing) { |
353 | 1758 | this.priority = routing.getPriority(); |
354 | 1758 | this.uriParameterNames = routing.getUriParameterNames(); |
355 | 1758 | this.pattern = routing.getPattern(); |
356 | 1758 | this.requestMethod = routing.getRequestMethod(); |
357 | 1758 | this.onSubmit = routing.getOnSubmit(); |
358 | 1758 | } |
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
|
364 | |
|
365 | |
|
366 | |
|
367 | |
|
368 | |
|
369 | |
|
370 | |
|
371 | |
|
372 | |
|
373 | |
|
374 | |
|
375 | |
|
376 | |
|
377 | |
|
378 | |
public int compareTo(final RoutingKey another) { |
379 | 10611 | int compare = this.priority - another.priority; |
380 | 10611 | if (compare != 0) { |
381 | 135 | return compare; |
382 | |
} |
383 | 10476 | compare = this.uriParameterNames.size() |
384 | |
- another.uriParameterNames.size(); |
385 | 10476 | if (compare != 0) { |
386 | 3558 | return compare; |
387 | |
} |
388 | 6918 | compare = this.pattern.pattern().compareTo( |
389 | |
another.pattern.pattern()); |
390 | 6918 | if (compare != 0) { |
391 | 4908 | return compare; |
392 | |
} |
393 | 2010 | compare = this.requestMethod.compareTo(another.requestMethod); |
394 | 2010 | if (compare != 0) { |
395 | 2004 | return compare; |
396 | |
} |
397 | 6 | if (this.onSubmit == another.onSubmit) { |
398 | 6 | compare = 0; |
399 | 0 | } else if (this.onSubmit == null) { |
400 | 0 | compare = -1; |
401 | 0 | } else if (another.onSubmit == null) { |
402 | 0 | compare = 1; |
403 | |
} else { |
404 | 0 | compare = this.onSubmit.compareTo(another.onSubmit); |
405 | |
} |
406 | 6 | return compare; |
407 | |
} |
408 | |
|
409 | |
|
410 | |
|
411 | |
|
412 | |
@Override |
413 | |
public int hashCode() { |
414 | 0 | final int prime = 31; |
415 | 0 | int result = 1; |
416 | 0 | result = prime * result |
417 | |
+ ((onSubmit == null) ? 0 : onSubmit.hashCode()); |
418 | 0 | result = prime |
419 | |
* result |
420 | |
+ ((pattern.pattern() == null) ? 0 : pattern.pattern() |
421 | |
.hashCode()); |
422 | 0 | result = prime * result + priority; |
423 | 0 | result = prime * result |
424 | |
+ ((requestMethod == null) ? 0 : requestMethod.hashCode()); |
425 | 0 | result = prime |
426 | |
* result |
427 | |
+ ((uriParameterNames == null) ? 0 : uriParameterNames |
428 | |
.hashCode()); |
429 | 0 | return result; |
430 | |
} |
431 | |
|
432 | |
|
433 | |
|
434 | |
|
435 | |
@Override |
436 | |
public boolean equals(final Object obj) { |
437 | 0 | if (this == obj) { |
438 | 0 | return true; |
439 | |
} |
440 | 0 | if (obj == null) { |
441 | 0 | return false; |
442 | |
} |
443 | 0 | if (getClass() != obj.getClass()) { |
444 | 0 | return false; |
445 | |
} |
446 | 0 | final RoutingKey other = (RoutingKey) obj; |
447 | 0 | if (onSubmit == null) { |
448 | 0 | if (other.onSubmit != null) { |
449 | 0 | return false; |
450 | |
} |
451 | 0 | } else if (!onSubmit.equals(other.onSubmit)) { |
452 | 0 | return false; |
453 | |
} |
454 | 0 | if (pattern == null) { |
455 | 0 | if (other.pattern != null) { |
456 | 0 | return false; |
457 | |
} |
458 | 0 | } else if (!pattern.pattern().equals(other.pattern.pattern())) { |
459 | 0 | return false; |
460 | |
} |
461 | 0 | if (priority != other.priority) { |
462 | 0 | return false; |
463 | |
} |
464 | 0 | if (requestMethod == null) { |
465 | 0 | if (other.requestMethod != null) { |
466 | 0 | return false; |
467 | |
} |
468 | 0 | } else if (!requestMethod.equals(other.requestMethod)) { |
469 | 0 | return false; |
470 | |
} |
471 | 0 | if (uriParameterNames == null) { |
472 | 0 | if (other.uriParameterNames != null) { |
473 | 0 | return false; |
474 | |
} |
475 | 0 | } else if (!uriParameterNames.equals(other.uriParameterNames)) { |
476 | 0 | return false; |
477 | |
} |
478 | 0 | return true; |
479 | |
} |
480 | |
|
481 | |
} |
482 | |
|
483 | |
|
484 | |
|
485 | |
|
486 | |
|
487 | |
|
488 | |
|
489 | |
static class ResolvedPathInfo implements PathInfo { |
490 | |
|
491 | |
|
492 | |
private final Map<String, String[]> uriParameters; |
493 | |
|
494 | |
|
495 | |
private final Map<String, Routing> routings; |
496 | |
|
497 | |
|
498 | |
|
499 | |
|
500 | |
|
501 | |
|
502 | |
|
503 | |
|
504 | |
|
505 | |
public ResolvedPathInfo(final Map<String, String[]> uriParameters, |
506 | 33 | final Map<String, Routing> routings) { |
507 | 33 | this.uriParameters = uriParameters; |
508 | 33 | this.routings = routings; |
509 | 33 | } |
510 | |
|
511 | |
|
512 | |
|
513 | |
|
514 | |
public Map<String, String[]> getURIParameters() { |
515 | 30 | return uriParameters; |
516 | |
} |
517 | |
|
518 | |
|
519 | |
|
520 | |
|
521 | |
public Routing dispatch(final Map<String, Object[]> parameterMap) { |
522 | 33 | for (final Entry<String, Routing> entry : routings.entrySet()) { |
523 | 33 | if (parameterMap.containsKey(entry.getKey())) { |
524 | 0 | return entry.getValue(); |
525 | |
} |
526 | |
} |
527 | 33 | return routings.get(null); |
528 | |
} |
529 | |
|
530 | |
} |
531 | |
|
532 | |
} |