1 16 package org.springframework.webflow.executor.struts; 17 18 import java.util.HashMap ; 19 import java.util.Map ; 20 21 import javax.servlet.http.HttpServletRequest ; 22 import javax.servlet.http.HttpServletResponse ; 23 24 import org.apache.struts.action.ActionForm; 25 import org.apache.struts.action.ActionForward; 26 import org.apache.struts.action.ActionMapping; 27 import org.springframework.validation.Errors; 28 import org.springframework.web.context.WebApplicationContext; 29 import org.springframework.web.struts.ActionSupport; 30 import org.springframework.web.struts.DelegatingActionProxy; 31 import org.springframework.web.struts.SpringBindingActionForm; 32 import org.springframework.web.util.WebUtils; 33 import org.springframework.webflow.action.FormObjectAccessor; 34 import org.springframework.webflow.context.ExternalContext; 35 import org.springframework.webflow.execution.support.ApplicationView; 36 import org.springframework.webflow.execution.support.ExternalRedirect; 37 import org.springframework.webflow.execution.support.FlowDefinitionRedirect; 38 import org.springframework.webflow.executor.FlowExecutor; 39 import org.springframework.webflow.executor.ResponseInstruction; 40 import org.springframework.webflow.executor.support.FlowExecutorArgumentHandler; 41 import org.springframework.webflow.executor.support.FlowRequestHandler; 42 import org.springframework.webflow.executor.support.RequestParameterFlowExecutorArgumentHandler; 43 44 133 public class FlowAction extends ActionSupport { 134 135 139 protected static final String FLOW_EXECUTOR_BEAN_NAME = "flowExecutor"; 140 141 146 protected static final String FLOW_EXECUTOR_ARGUMENT_HANDLER_BEAN_NAME = "argumentHandler"; 147 148 152 private FlowExecutor flowExecutor; 153 154 157 private FlowExecutorArgumentHandler argumentHandler; 158 159 163 public FlowExecutor getFlowExecutor() { 164 return flowExecutor; 165 } 166 167 171 public void setFlowExecutor(FlowExecutor flowExecutor) { 172 this.flowExecutor = flowExecutor; 173 } 174 175 179 public FlowExecutorArgumentHandler getArgumentHandler() { 180 return argumentHandler; 181 } 182 183 187 public void setArgumentHandler(FlowExecutorArgumentHandler argumentHandler) { 188 this.argumentHandler = argumentHandler; 189 } 190 191 protected void onInit() { 192 WebApplicationContext context = getWebApplicationContext(); 193 if (getFlowExecutor() == null) { 194 if (context.containsBean(FLOW_EXECUTOR_BEAN_NAME)) { 195 setFlowExecutor((FlowExecutor)context.getBean(FLOW_EXECUTOR_BEAN_NAME, FlowExecutor.class)); 196 } 197 else { 198 throw new IllegalStateException ("No '" + FLOW_EXECUTOR_BEAN_NAME 199 + "' bean definition could be found; to use Spring Web Flow with Struts you must " 200 + "configure this FlowAction with a FlowExecutor"); 201 } 202 } 203 if (getArgumentHandler() == null) { 204 if (context.containsBean(FLOW_EXECUTOR_ARGUMENT_HANDLER_BEAN_NAME)) { 205 setArgumentHandler((FlowExecutorArgumentHandler)context.getBean( 206 FLOW_EXECUTOR_ARGUMENT_HANDLER_BEAN_NAME, FlowExecutorArgumentHandler.class)); 207 } 208 else { 209 argumentHandler = new RequestParameterFlowExecutorArgumentHandler(); 211 } 212 } 213 } 214 215 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, 216 HttpServletResponse response) throws Exception { 217 ExternalContext context = new StrutsExternalContext(mapping, form, getServletContext(), request, response); 218 ResponseInstruction responseInstruction = createRequestHandler().handleFlowRequest(context); 219 return toActionForward(responseInstruction, mapping, form, request, response, context); 220 } 221 222 227 protected FlowRequestHandler createRequestHandler() { 228 return new FlowRequestHandler(getFlowExecutor(), getArgumentHandler()); 229 } 230 231 235 protected ActionForward toActionForward(ResponseInstruction response, ActionMapping mapping, ActionForm form, 236 HttpServletRequest request, HttpServletResponse httpResponse, ExternalContext context) throws Exception { 237 if (response.isApplicationView()) { 238 ApplicationView forward = (ApplicationView)response.getViewSelection(); 240 Map model = new HashMap (forward.getModel()); 241 argumentHandler.exposeFlowExecutionContext( 242 response.getFlowExecutionKey(), response.getFlowExecutionContext(), model); 243 WebUtils.exposeRequestAttributes(request, model); 244 if (form instanceof SpringBindingActionForm) { 245 SpringBindingActionForm bindingForm = (SpringBindingActionForm)form; 246 Errors currentErrors = (Errors)model.get(FormObjectAccessor.getCurrentFormErrorsName()); 249 bindingForm.expose(currentErrors, request); 250 } 251 return findForward(forward, mapping); 252 253 } 254 else if (response.isFlowExecutionRedirect()) { 255 String flowExecutionUrl = argumentHandler.createFlowExecutionUrl( 257 response.getFlowExecutionKey(), response.getFlowExecutionContext(), context); 258 return createRedirectForward(flowExecutionUrl, httpResponse); 259 } 260 else if (response.isFlowDefinitionRedirect()) { 261 String flowUrl = argumentHandler.createFlowDefinitionUrl((FlowDefinitionRedirect)response.getViewSelection(), context); 263 return createRedirectForward(flowUrl, httpResponse); 264 } 265 else if (response.isExternalRedirect()) { 266 String externalUrl = argumentHandler.createExternalUrl((ExternalRedirect)response.getViewSelection(), 268 response.getFlowExecutionKey(), context); 269 return createRedirectForward(externalUrl, httpResponse); 270 } 271 else if (response.isNull()) { 272 return null; 274 } 275 else { 276 throw new IllegalArgumentException ("Don't know how to handle response instruction " + response); 277 } 278 } 279 280 289 protected ActionForward createRedirectForward(String url, HttpServletResponse response) throws Exception { 290 response.sendRedirect(url); 291 return null; 292 } 293 294 302 protected ActionForward findForward(ApplicationView forward, ActionMapping mapping) { 303 ActionForward actionForward = mapping.findForward(forward.getViewName()); 307 if (actionForward != null) { 308 actionForward = new ActionForward(actionForward.getName(), actionForward.getPath(), false); 311 } 312 else { 313 actionForward = new ActionForward(forward.getViewName(), false); 314 } 315 return actionForward; 316 } 317 } | Popular Tags |