1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.cubby.controller.impl; |
17 | |
|
18 | |
import static org.seasar.cubby.CubbyConstants.ATTR_ACTION; |
19 | |
import static org.seasar.cubby.CubbyConstants.ATTR_PARAMS; |
20 | |
import static org.seasar.cubby.CubbyConstants.ATTR_ROUTINGS; |
21 | |
|
22 | |
import java.lang.reflect.InvocationTargetException; |
23 | |
import java.lang.reflect.Method; |
24 | |
import java.util.Map; |
25 | |
|
26 | |
import javax.servlet.http.HttpServletRequest; |
27 | |
import javax.servlet.http.HttpServletResponse; |
28 | |
|
29 | |
import org.seasar.cubby.action.Action; |
30 | |
import org.seasar.cubby.action.ActionResult; |
31 | |
import org.seasar.cubby.controller.ActionProcessor; |
32 | |
import org.seasar.cubby.controller.ActionResultWrapper; |
33 | |
import org.seasar.cubby.controller.RequestParser; |
34 | |
import org.seasar.cubby.controller.RequestParserSelector; |
35 | |
import org.seasar.cubby.controller.RoutingsDispatcher; |
36 | |
import org.seasar.cubby.exception.ActionRuntimeException; |
37 | |
import org.seasar.cubby.routing.Routing; |
38 | |
import org.seasar.cubby.util.CubbyUtils; |
39 | |
import org.seasar.framework.container.S2Container; |
40 | |
import org.seasar.framework.container.factory.SingletonS2ContainerFactory; |
41 | |
import org.seasar.framework.log.Logger; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | 83 | public class ActionProcessorImpl implements ActionProcessor { |
50 | |
|
51 | |
|
52 | 1 | private static final Logger logger = Logger |
53 | |
.getLogger(ActionProcessorImpl.class); |
54 | |
|
55 | |
|
56 | 1 | private static final Object[] EMPTY_ARGS = new Object[0]; |
57 | |
|
58 | |
|
59 | |
private RoutingsDispatcher routingsDispatcher; |
60 | |
|
61 | |
|
62 | |
private RequestParserSelector requestParserSelector; |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
public void setRoutingsDispatcher( |
71 | |
final RoutingsDispatcher routingsDispatcher) { |
72 | 82 | this.routingsDispatcher = routingsDispatcher; |
73 | 82 | } |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
public void setRequestParserSelector( |
82 | |
final RequestParserSelector requestParserSelector) { |
83 | 82 | this.requestParserSelector = requestParserSelector; |
84 | 82 | } |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
public ActionResultWrapper process(final HttpServletRequest request, |
90 | |
final HttpServletResponse response) throws Exception { |
91 | 1 | final Map<String, Routing> routings = CubbyUtils.getAttribute(request, |
92 | |
ATTR_ROUTINGS); |
93 | 1 | if (routings == null) { |
94 | 0 | return null; |
95 | |
} |
96 | |
|
97 | 1 | request.removeAttribute(ATTR_ROUTINGS); |
98 | 1 | final Map<String, Object[]> parameterMap = parseRequest(request); |
99 | 1 | request.setAttribute(ATTR_PARAMS, parameterMap); |
100 | |
|
101 | 1 | final Routing routing = routingsDispatcher.dispatch(routings, |
102 | |
parameterMap); |
103 | 1 | if (routing == null) { |
104 | 0 | return null; |
105 | |
} |
106 | |
|
107 | 1 | final Class<? extends Action> actionClass = routing.getActionClass(); |
108 | 1 | final Method method = routing.getMethod(); |
109 | 1 | if (logger.isDebugEnabled()) { |
110 | 1 | logger.log("DCUB0004", new Object[] { request.getRequestURI() }); |
111 | 1 | logger.log("DCUB0005", new Object[] { method }); |
112 | |
} |
113 | 1 | final S2Container container = SingletonS2ContainerFactory |
114 | |
.getContainer(); |
115 | 1 | final Action action = (Action) container.getComponent(actionClass); |
116 | 1 | request.setAttribute(ATTR_ACTION, action); |
117 | 1 | final ActionResult actionResult = invoke(action, method); |
118 | 1 | if (actionResult == null) { |
119 | 0 | throw new ActionRuntimeException("ECUB0101", |
120 | |
new Object[] { method }); |
121 | |
} |
122 | 1 | final ActionResultWrapper actionResultWrapper = new ActionResultWrapperImpl( |
123 | |
actionResult, action, actionClass, method); |
124 | 1 | return actionResultWrapper; |
125 | |
} |
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
private ActionResult invoke(final Action action, final Method method) |
137 | |
throws Exception { |
138 | |
try { |
139 | 1 | final ActionResult result = (ActionResult) method.invoke(action, |
140 | |
EMPTY_ARGS); |
141 | 1 | return result; |
142 | 0 | } catch (final InvocationTargetException ex) { |
143 | 0 | logger.log(ex); |
144 | 0 | final Throwable target = ex.getTargetException(); |
145 | 0 | if (target instanceof Error) { |
146 | 0 | throw (Error) target; |
147 | 0 | } else if (target instanceof RuntimeException) { |
148 | 0 | throw (RuntimeException) target; |
149 | |
} else { |
150 | 0 | throw (Exception) target; |
151 | |
} |
152 | |
} |
153 | |
} |
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
private Map<String, Object[]> parseRequest(final HttpServletRequest request) { |
163 | 1 | final RequestParser requestParser = requestParserSelector |
164 | |
.select(request); |
165 | 1 | if (requestParser == null) { |
166 | 0 | throw new NullPointerException("requestParser"); |
167 | |
} |
168 | 1 | if (logger.isDebugEnabled()) { |
169 | 1 | logger.log("DCUB0016", new Object[] { requestParser }); |
170 | |
} |
171 | 1 | final Map<String, Object[]> parameterMap = requestParser |
172 | |
.getParameterMap(request); |
173 | 1 | return parameterMap; |
174 | |
} |
175 | |
|
176 | |
} |