Page 2 of 2 FirstFirst 12
Results 11 to 15 of 15

Thread: Tip: Accessing a flow execution from outside Spring Web Flow

  1. #11
    Join Date
    Nov 2005
    Location
    Iasi, Romania
    Posts
    104

    Default

    Seems that this works. I post it here maybe someone else will need it.

    Code:
    import java.util.Map;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.log4j.Logger;
    import org.springframework.validation.Errors;
    import org.springframework.webflow.action.FormObjectAccessor;
    import org.springframework.webflow.context.ExternalContext;
    import org.springframework.webflow.context.ExternalContextHolder;
    import org.springframework.webflow.context.servlet.ServletExternalContext;
    import org.springframework.webflow.execution.FlowExecution;
    import org.springframework.webflow.execution.repository.FlowExecutionKey;
    import org.springframework.webflow.execution.repository.FlowExecutionRepository;
    import org.springframework.webflow.executor.FlowExecutorImpl;
    import org.springframework.webflow.executor.mvc.FlowController;
    import org.springframework.webflow.executor.support.FlowExecutorArgumentHandler;
    import org.springmodules.xt.ajax.FormDataAccessor;
    
    /**
     * 
     * @author Elvis Ciocoiu
     *
     */
    public class SWFFormDataAccessor implements FormDataAccessor {
    	private static final Logger LOG = Logger.getLogger(SWFFormDataAccessor.class);
    	
    	public Object getCommandObject(HttpServletRequest request,
    			HttpServletResponse response, Object handler, Map model) {
    		
    		Object command = extract(request, response, (FlowController) handler, new SWFAccessorCallback() {
    
    			public Object extract(FlowExecution flowExecution) {
    				return flowExecution.getActiveSession().getScope().get(FormObjectAccessor.getCurrentFormObjectName());
    			}
    			
    		});
    		
    		return command;
    	}
    
    	public Errors getValidationErrors(HttpServletRequest request,
    			HttpServletResponse response, Object handler, Map model) {
    		Errors errors = (Errors) extract(request, response, (FlowController) handler, new SWFAccessorCallback() {
    
    			public Object extract(FlowExecution flowExecution) {
    				return (Errors) flowExecution.getActiveSession().getFlashMap().get(
    						FormObjectAccessor.getCurrentFormErrorsName(), Errors.class);
    			}
    			
    		});
    		
    		return errors;
    	}
    
    	private Object extract(HttpServletRequest request,
    			HttpServletResponse response, FlowController flowControllerflowControllertroller, SWFAccessorCallback callbak) {
    		FlowExecutionRepository repository = ((FlowExecutorImpl)flowControllerflowControllertroller.getFlowExecutor()).getExecutionRepository();
    		FlowExecutorArgumentHandler argumentHandler  = flowControllerflowControllertroller.getArgumentHandler();
    		
    		ExternalContext externalContext = new ServletExternalContext(request.getSession().getServletContext(), request, response);		
    		
    		if (argumentHandler.isFlowExecutionKeyPresent(externalContext)) {
    			try {
    				ExternalContextHolder.setExternalContext(externalContext);
    				FlowExecutionKey flowExecutionKey =
    					repository.parseFlowExecutionKey(argumentHandler.extractFlowExecutionKey(externalContext));
    				FlowExecution flowExecution = repository.getFlowExecution(flowExecutionKey);
    				
    				return callbak.extract(flowExecution);
    			}
    			catch (Exception e) {
    				e.printStackTrace();
    			}
    			finally {
    				ExternalContextHolder.setExternalContext(null);
    			}
    		}
    		
    		return null;
    	}
    
    	interface SWFAccessorCallback {
    		Object extract(FlowExecution flowExecution);
    	}
    }

  2. #12
    Join Date
    May 2005
    Posts
    17

    Default

    Hi, i'm working with SWF on Liferay portal.

    Each portlet has his own PortletFlowController instance and, AFAIK, his own WebApplicationContext.

    I would like to create a Servlet in my web app to be able to stream PDF file.

    But actually i'm not able to access to a flow execution contained by one of my portlets.

    Any ideas ?

    Thans

  3. #13
    Join Date
    Oct 2009
    Posts
    2

    Default

    Is there a webflow 2 version for this tip ?

    We are using this for a Spring MVC controller that returns "image/jpeg" content. The bytes of the image are stored on flowscope and so we need to access the flowscope from within our MVC controller.
    In webflow 1 this worked out just fine and we tried these changes to get it working in webflow 2.0.8:

    Code:
    ApplicationContext context = (ApplicationContext)getServletContext().getAttribute(DispatcherServlet.SERVLET_CONTEXT_PREFIX + "x");
    
    FlowController controller = (FlowController)context.getBean("flowController");
    FlowExecutionRepository repository = ((FlowExecutorImpl)controller.getFlowExecutor()).getExecutionRepository();
    
    ExternalContext externalContext = new ServletExternalContext(getServletContext(), request, response);
    ExternalContextHolder.setExternalContext(externalContext);
    FlowExecutionKey flowExecutionKey = repository.parseFlowExecutionKey(ServletRequestUtils.getRequiredStringParameter(request, "_flowExecutionKey"));
    
    FlowExecution flowExecution = repository.getFlowExecution(flowExecutionKey);
    
    MutableAttributeMap flowScope = flowExecution.getActiveSession().getScope();
    However, this does not seem work 100%. Sometimes we get "SnapshotNotFoundException" for some of our images. It isn't really clear and there is no rule as to when this exception occurs.

  4. #14

    Default

    please update this tip as per latest webflow api
    --

  5. #15
    Join Date
    Feb 2012
    Location
    Singapore
    Posts
    5

    Default Flow scope data outside SWF

    Is there any update on this ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •