1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.cubby.internal.controller.impl; |
17 | |
|
18 | |
import static org.seasar.cubby.CubbyConstants.ATTR_PARAMS; |
19 | |
|
20 | |
import java.util.Iterator; |
21 | |
import java.util.Map; |
22 | |
|
23 | |
import javax.servlet.http.HttpServletRequest; |
24 | |
import javax.servlet.http.HttpServletResponse; |
25 | |
|
26 | |
import org.seasar.cubby.internal.controller.ActionProcessor; |
27 | |
import org.seasar.cubby.internal.controller.ActionResultWrapper; |
28 | |
import org.seasar.cubby.internal.controller.RequestProcessor; |
29 | |
import org.seasar.cubby.internal.controller.ThreadContext; |
30 | |
import org.seasar.cubby.internal.controller.ThreadContext.Command; |
31 | |
import org.seasar.cubby.plugin.Plugin; |
32 | |
import org.seasar.cubby.plugin.PluginRegistry; |
33 | |
import org.seasar.cubby.plugin.RequestProcessingInvocation; |
34 | |
import org.seasar.cubby.routing.PathInfo; |
35 | |
import org.seasar.cubby.routing.Routing; |
36 | |
import org.seasar.cubby.spi.ProviderFactory; |
37 | |
import org.seasar.cubby.spi.RequestParserProvider; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | 1 | public class RequestProcessorImpl implements RequestProcessor { |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
public void process(final HttpServletRequest request, |
51 | |
final HttpServletResponse response, final PathInfo pathInfo) |
52 | |
throws Exception { |
53 | 1 | final HttpServletRequest wrappedRequest = new CubbyHttpServletRequestWrapper( |
54 | |
request, pathInfo.getURIParameters()); |
55 | 1 | final RequestProcessingInvocation invocation = new RequestProcessingInvocationImpl( |
56 | |
wrappedRequest, response, pathInfo); |
57 | 1 | invocation.proceed(); |
58 | 1 | } |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | 3 | static class RequestProcessingInvocationImpl implements |
66 | |
RequestProcessingInvocation { |
67 | |
|
68 | |
|
69 | 1 | private final ActionProcessor actionProcessor = new ActionProcessorImpl(); |
70 | |
|
71 | |
|
72 | |
private final HttpServletRequest request; |
73 | |
|
74 | |
|
75 | |
private final HttpServletResponse response; |
76 | |
|
77 | |
|
78 | |
private final PathInfo pathInfo; |
79 | |
|
80 | |
|
81 | |
private final Iterator<Plugin> pluginsIterator; |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
public RequestProcessingInvocationImpl( |
94 | |
final HttpServletRequest request, |
95 | 1 | final HttpServletResponse response, final PathInfo pathInfo) { |
96 | 1 | this.request = request; |
97 | 1 | this.response = response; |
98 | 1 | this.pathInfo = pathInfo; |
99 | |
|
100 | 1 | final PluginRegistry pluginRegistry = PluginRegistry.getInstance(); |
101 | 1 | this.pluginsIterator = pluginRegistry.getPlugins().iterator(); |
102 | 1 | } |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
public Void proceed() throws Exception { |
108 | 2 | if (pluginsIterator.hasNext()) { |
109 | 1 | final Plugin plugin = pluginsIterator.next(); |
110 | 1 | plugin.invokeRequestProcessing(this); |
111 | 1 | } else { |
112 | 1 | final HttpServletRequest request = getRequest(); |
113 | 1 | final RequestParserProvider requestParserProvider = ProviderFactory |
114 | |
.get(RequestParserProvider.class); |
115 | 1 | final Map<String, Object[]> parameterMap = requestParserProvider |
116 | |
.getParameterMap(request); |
117 | 1 | request.setAttribute(ATTR_PARAMS, parameterMap); |
118 | 1 | final Routing routing = pathInfo.dispatch(parameterMap); |
119 | 1 | final Command command = new Command() { |
120 | |
|
121 | |
public void execute(final HttpServletRequest request, |
122 | |
final HttpServletResponse response) |
123 | |
throws Exception { |
124 | 1 | final ActionResultWrapper actionResultWrapper = actionProcessor |
125 | |
.process(request, response, routing); |
126 | 1 | actionResultWrapper.execute(request, response); |
127 | 1 | } |
128 | |
|
129 | |
}; |
130 | 1 | ThreadContext.runInContext(request, response, command); |
131 | |
} |
132 | 2 | return null; |
133 | |
} |
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
public HttpServletRequest getRequest() { |
139 | 1 | return request; |
140 | |
} |
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
public HttpServletResponse getResponse() { |
146 | 0 | return response; |
147 | |
} |
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
public PathInfo getPathInfo() { |
153 | 0 | return pathInfo; |
154 | |
} |
155 | |
|
156 | |
} |
157 | |
|
158 | |
} |