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_ACTION; |
19 | |
import static org.seasar.cubby.CubbyConstants.ATTR_ACTION_CONTEXT; |
20 | |
import static org.seasar.cubby.CubbyConstants.ATTR_ERRORS; |
21 | |
import static org.seasar.cubby.CubbyConstants.ATTR_FLASH; |
22 | |
import static org.seasar.cubby.internal.util.LogMessages.format; |
23 | |
|
24 | |
import java.lang.reflect.Method; |
25 | |
import java.util.Iterator; |
26 | |
import java.util.Map; |
27 | |
|
28 | |
import javax.servlet.http.HttpServletRequest; |
29 | |
import javax.servlet.http.HttpServletResponse; |
30 | |
|
31 | |
import org.seasar.cubby.action.ActionContext; |
32 | |
import org.seasar.cubby.action.ActionErrors; |
33 | |
import org.seasar.cubby.action.ActionException; |
34 | |
import org.seasar.cubby.action.ActionResult; |
35 | |
import org.seasar.cubby.internal.action.impl.ActionContextImpl; |
36 | |
import org.seasar.cubby.internal.action.impl.ActionErrorsImpl; |
37 | |
import org.seasar.cubby.internal.controller.ActionProcessor; |
38 | |
import org.seasar.cubby.internal.controller.ActionResultWrapper; |
39 | |
import org.seasar.cubby.plugin.ActionInvocation; |
40 | |
import org.seasar.cubby.plugin.Plugin; |
41 | |
import org.seasar.cubby.plugin.PluginRegistry; |
42 | |
import org.seasar.cubby.routing.Routing; |
43 | |
import org.seasar.cubby.spi.ContainerProvider; |
44 | |
import org.seasar.cubby.spi.ProviderFactory; |
45 | |
import org.seasar.cubby.spi.container.Container; |
46 | |
import org.slf4j.Logger; |
47 | |
import org.slf4j.LoggerFactory; |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | 2 | public class ActionProcessorImpl implements ActionProcessor { |
56 | |
|
57 | |
|
58 | 1 | private static final Logger logger = LoggerFactory |
59 | |
.getLogger(ActionProcessorImpl.class); |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
public ActionResultWrapper process(final HttpServletRequest request, |
65 | |
final HttpServletResponse response, final Routing routing) |
66 | |
throws Exception { |
67 | |
|
68 | 1 | final Method actionMethod = routing.getActionMethod(); |
69 | 1 | if (logger.isDebugEnabled()) { |
70 | 1 | logger.debug(format("DCUB0004", request.getRequestURI())); |
71 | 1 | logger.debug(format("DCUB0005", actionMethod)); |
72 | |
} |
73 | |
|
74 | 1 | final Class<?> actionClass = routing.getActionClass(); |
75 | |
|
76 | 1 | final Container container = ProviderFactory |
77 | |
.get(ContainerProvider.class).getContainer(); |
78 | |
|
79 | 1 | final ActionErrors actionErrors = new ActionErrorsImpl(); |
80 | 1 | request.setAttribute(ATTR_ERRORS, actionErrors); |
81 | |
|
82 | 1 | final Map<String, Object> flashMap = new FlashMap<String, Object>( |
83 | |
request); |
84 | 1 | request.setAttribute(ATTR_FLASH, flashMap); |
85 | |
|
86 | 1 | final Object action = container.lookup(actionClass); |
87 | 1 | request.setAttribute(ATTR_ACTION, action); |
88 | |
|
89 | 1 | final ActionContext actionContext = new ActionContextImpl(action, |
90 | |
actionClass, actionMethod, actionErrors, flashMap); |
91 | 1 | request.setAttribute(ATTR_ACTION_CONTEXT, actionContext); |
92 | |
|
93 | 1 | actionContext.invokeInitializeMethod(); |
94 | |
|
95 | 1 | final ActionInvocation actionInvocation = new ActionInvocationImpl( |
96 | |
request, response, actionContext); |
97 | 1 | final ActionResult actionResult = actionInvocation.proceed(); |
98 | 1 | if (actionResult == null) { |
99 | 0 | throw new ActionException(format("ECUB0101", actionMethod)); |
100 | |
} |
101 | |
|
102 | 1 | final ActionResultWrapper actionResultWrapper = new ActionResultWrapperImpl( |
103 | |
actionResult, actionContext); |
104 | 1 | return actionResultWrapper; |
105 | |
} |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | 2 | static class ActionInvocationImpl implements ActionInvocation { |
113 | |
|
114 | |
|
115 | |
private final HttpServletRequest request; |
116 | |
|
117 | |
|
118 | |
private final HttpServletResponse response; |
119 | |
|
120 | |
|
121 | |
private final ActionContext actionContext; |
122 | |
|
123 | |
|
124 | |
private final Iterator<Plugin> pluginsIterator; |
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
public ActionInvocationImpl(final HttpServletRequest request, |
137 | |
final HttpServletResponse response, |
138 | 1 | final ActionContext actionContext) { |
139 | 1 | this.request = request; |
140 | 1 | this.response = response; |
141 | 1 | this.actionContext = actionContext; |
142 | |
|
143 | 1 | final PluginRegistry pluginRegistry = PluginRegistry.getInstance(); |
144 | 1 | this.pluginsIterator = pluginRegistry.getPlugins().iterator(); |
145 | 1 | } |
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
public ActionResult proceed() throws Exception { |
151 | |
final ActionResult actionResult; |
152 | 2 | if (pluginsIterator.hasNext()) { |
153 | 1 | final Plugin plugin = pluginsIterator.next(); |
154 | 1 | actionResult = plugin.invokeAction(this); |
155 | 1 | } else { |
156 | 1 | final ActionContext actionContext = getActionContext(); |
157 | 1 | final Object action = actionContext.getAction(); |
158 | 1 | final Method actionMethod = actionContext.getActionMethod(); |
159 | 1 | actionResult = (ActionResult) actionMethod.invoke(action); |
160 | |
} |
161 | 2 | return actionResult; |
162 | |
} |
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
public HttpServletRequest getRequest() { |
168 | 0 | return request; |
169 | |
} |
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
public HttpServletResponse getResponse() { |
175 | 0 | return response; |
176 | |
} |
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
public ActionContext getActionContext() { |
182 | 1 | return actionContext; |
183 | |
} |
184 | |
|
185 | |
} |
186 | |
|
187 | |
} |