
org.hdiv.webflow.executor.mvc.HDIVFlowController Maven / Gradle / Ivy
Show all versions of hdiv-webflow-1.0.5 Show documentation
/*
* Copyright 2004-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hdiv.webflow.executor.mvc;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hdiv.web.servlet.view.RedirectViewHDIV;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.webflow.context.ExternalContext;
import org.springframework.webflow.context.servlet.ServletExternalContext;
import org.springframework.webflow.execution.support.ApplicationView;
import org.springframework.webflow.execution.support.ExternalRedirect;
import org.springframework.webflow.execution.support.FlowDefinitionRedirect;
import org.springframework.webflow.execution.support.FlowExecutionRedirect;
import org.springframework.webflow.executor.FlowExecutor;
import org.springframework.webflow.executor.ResponseInstruction;
import org.springframework.webflow.executor.support.FlowExecutorArgumentHandler;
import org.springframework.webflow.executor.support.FlowRequestHandler;
import org.springframework.webflow.executor.support.RequestPathFlowExecutorArgumentHandler;
import org.springframework.webflow.executor.support.ResponseInstructionHandler;
/**
* Point of integration between Spring Web MVC and Spring Web Flow: a
* {@link Controller} that routes incoming requests to one or more managed flow
* executions.
*
* Requests into the web flow system are handled by a {@link FlowExecutor},
* which this class delegates to using a {@link FlowRequestHandler} helper.
* Consult the JavaDoc of that class for more information on how requests are
* processed.
*
* Note: a single FlowController
may execute all flows of your
* application.
*
* - By default, to have this controller launch a new flow execution
* (conversation), have the client send a
* {@link FlowExecutorArgumentHandler#getFlowIdArgumentName()} request parameter
* indicating the flow definition to launch.
*
- To have this controller participate in an existing flow execution
* (conversation), have the client send a
* {@link FlowExecutorArgumentHandler#getFlowExecutionKeyArgumentName()} request
* parameter identifying the conversation to participate in. See the
*
flow-launcher
sample application for examples of the various
* strategies for launching and resuming flow executions.
*
*
* Usage example:
*
*
* <!--
* Exposes flows for execution at a single request URL.
* The id of a flow to launch should be passed in by clients using
* the "_flowId" request parameter:
* e.g. /app.htm?_flowId=flow1
* -->
* <bean name="/app.htm" class="org.springframework.webflow.executor.mvc.FlowController">
* <property name="flowExecutor" ref="flowExecutor"/>
* </bean>
*
*
*
* It is also possible to customize the {@link FlowExecutorArgumentHandler}
* strategy to allow for different types of controller parameterization, for
* example perhaps in conjunction with a REST-style request mapper (see
* {@link RequestPathFlowExecutorArgumentHandler}).
*
* @see org.springframework.webflow.executor.FlowExecutor
* @see org.springframework.webflow.executor.support.FlowRequestHandler
* @see org.springframework.webflow.executor.support.FlowExecutorArgumentHandler
*
* @author Gorka Vicente
* @since HDIV 2.0.3
*/
public class HDIVFlowController extends org.springframework.webflow.executor.mvc.FlowController {
/**
* Create a new flow controller. Allows bean style usage.
*
* @see #setFlowExecutor(FlowExecutor)
* @see #setArgumentHandler(FlowExecutorArgumentHandler)
*/
public HDIVFlowController() {
super();
}
/**
* Create a ModelAndView object based on the information in the selected
* response instruction. Subclasses can override this to return a
* specialized ModelAndView or to do custom processing on it.
*
* @param responseInstruction the response instruction to convert
* @return a new ModelAndView object
*/
protected ModelAndView toModelAndView(final ResponseInstruction responseInstruction, final ExternalContext context) {
return (ModelAndView) new ResponseInstructionHandler() {
protected void handleApplicationView(ApplicationView view) throws Exception {
// forward to a view as part of an active conversation
Map model = new HashMap(view.getModel());
getArgumentHandler().exposeFlowExecutionContext(responseInstruction.getFlowExecutionKey(),
responseInstruction.getFlowExecutionContext(), model);
setResult(new ModelAndView(view.getViewName(), model));
}
protected void handleFlowDefinitionRedirect(FlowDefinitionRedirect redirect) throws Exception {
// restart the flow by redirecting to flow launch URL
String flowUrl = getArgumentHandler().createFlowDefinitionUrl(redirect, context);
setResult(new ModelAndView(new RedirectViewHDIV(flowUrl)));
}
protected void handleFlowExecutionRedirect(FlowExecutionRedirect redirect) throws Exception {
// redirect to active flow execution URL
String flowExecutionUrl = getArgumentHandler().createFlowExecutionUrl(
responseInstruction.getFlowExecutionKey(), responseInstruction.getFlowExecutionContext(),
context);
setResult(new ModelAndView(new RedirectViewHDIV(flowExecutionUrl)));
}
protected void handleExternalRedirect(ExternalRedirect redirect) throws Exception {
// redirect to external URL
String externalUrl = getArgumentHandler().createExternalUrl(redirect,
responseInstruction.getFlowExecutionKey(), context);
setResult(new ModelAndView(new RedirectViewHDIV(externalUrl)));
}
protected void handleNull() throws Exception {
// no response to issue
setResult(null);
}
}.handleQuietly(responseInstruction).getResult();
}
}