1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.cubby.internal.routing.impl; |
17 | |
|
18 | |
import static org.seasar.cubby.CubbyConstants.ATTR_ROUTING; |
19 | |
import static org.seasar.cubby.internal.util.LogMessages.format; |
20 | |
|
21 | |
import java.util.Collections; |
22 | |
import java.util.List; |
23 | |
import java.util.Map; |
24 | |
import java.util.regex.Matcher; |
25 | |
import java.util.regex.Pattern; |
26 | |
|
27 | |
import javax.servlet.http.HttpServletRequest; |
28 | |
import javax.servlet.http.HttpServletResponse; |
29 | |
|
30 | |
import org.seasar.cubby.internal.routing.Router; |
31 | |
import org.seasar.cubby.internal.util.RequestUtils; |
32 | |
import org.seasar.cubby.routing.PathInfo; |
33 | |
import org.seasar.cubby.routing.PathResolver; |
34 | |
import org.seasar.cubby.routing.Routing; |
35 | |
import org.seasar.cubby.spi.PathResolverProvider; |
36 | |
import org.seasar.cubby.spi.ProviderFactory; |
37 | |
import org.slf4j.Logger; |
38 | |
import org.slf4j.LoggerFactory; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | 9 | public class RouterImpl implements Router { |
47 | |
|
48 | |
|
49 | 3 | private static final Logger logger = LoggerFactory |
50 | |
.getLogger(RouterImpl.class); |
51 | |
|
52 | |
|
53 | 3 | private static final List<Pattern> EMPTY_IGNORE_PATH_PATTERNS = Collections |
54 | |
.emptyList(); |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
public PathInfo routing(final HttpServletRequest request, |
60 | |
final HttpServletResponse response) { |
61 | 6 | return routing(request, response, EMPTY_IGNORE_PATH_PATTERNS); |
62 | |
} |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
public PathInfo routing(final HttpServletRequest request, |
68 | |
final HttpServletResponse response, |
69 | |
final List<Pattern> ignorePathPatterns) { |
70 | 9 | final String path = RequestUtils.getPath(request); |
71 | 9 | if (logger.isDebugEnabled()) { |
72 | 9 | logger.debug(format("DCUB0006", path)); |
73 | |
} |
74 | |
|
75 | 9 | final Routing routing = RequestUtils |
76 | |
.getAttribute(request, ATTR_ROUTING); |
77 | 9 | if (routing != null) { |
78 | 3 | request.removeAttribute(ATTR_ROUTING); |
79 | 3 | final PathInfo pathInfo = new ForwardFromActionPathInfo(routing); |
80 | 3 | return pathInfo; |
81 | |
} |
82 | |
|
83 | 6 | if (isIgnorePath(path, ignorePathPatterns)) { |
84 | 3 | return null; |
85 | |
} |
86 | |
|
87 | 3 | final PathResolverProvider pathResolverProvider = ProviderFactory |
88 | |
.get(PathResolverProvider.class); |
89 | 3 | final PathResolver pathResolver = pathResolverProvider |
90 | |
.getPathResolver(); |
91 | 3 | final PathInfo pathInfo = pathResolver.getPathInfo(path, request |
92 | |
.getMethod(), request.getCharacterEncoding()); |
93 | 3 | return pathInfo; |
94 | |
} |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
private boolean isIgnorePath(final String path, |
107 | |
final List<Pattern> ignorePathPatterns) { |
108 | 6 | for (final Pattern pattern : ignorePathPatterns) { |
109 | 3 | final Matcher matcher = pattern.matcher(path); |
110 | 3 | if (matcher.matches()) { |
111 | 3 | return true; |
112 | |
} |
113 | 0 | } |
114 | 3 | return false; |
115 | |
} |
116 | |
|
117 | 9 | static class ForwardFromActionPathInfo implements PathInfo { |
118 | |
|
119 | |
private final Routing routing; |
120 | |
|
121 | 3 | private final Map<String, String[]> uriParameters = Collections |
122 | |
.emptyMap(); |
123 | |
|
124 | 3 | public ForwardFromActionPathInfo(final Routing routing) { |
125 | 3 | this.routing = routing; |
126 | 3 | } |
127 | |
|
128 | |
public Map<String, String[]> getURIParameters() { |
129 | 0 | return uriParameters; |
130 | |
} |
131 | |
|
132 | |
public Routing dispatch(final Map<String, Object[]> parameterMap) { |
133 | 3 | return routing; |
134 | |
} |
135 | |
|
136 | |
} |
137 | |
|
138 | |
} |