diff --git a/spring-webflow/src/main/java/org/springframework/webflow/mvc/servlet/FlowHandlerMapping.java b/spring-webflow/src/main/java/org/springframework/webflow/mvc/servlet/FlowHandlerMapping.java index 93e6726c..1cc51a12 100644 --- a/spring-webflow/src/main/java/org/springframework/webflow/mvc/servlet/FlowHandlerMapping.java +++ b/spring-webflow/src/main/java/org/springframework/webflow/mvc/servlet/FlowHandlerMapping.java @@ -18,6 +18,9 @@ package org.springframework.webflow.mvc.servlet; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.util.Assert; import org.springframework.web.servlet.handler.AbstractHandlerMapping; import org.springframework.webflow.context.servlet.DefaultFlowUrlHandler; import org.springframework.webflow.context.servlet.FlowUrlHandler; @@ -39,10 +42,12 @@ import org.springframework.webflow.definition.registry.FlowDefinitionRegistry; */ public class FlowHandlerMapping extends AbstractHandlerMapping { - private FlowUrlHandler flowUrlHandler = new DefaultFlowUrlHandler(); + private static final Log logger = LogFactory.getLog(FlowHandlerMapping.class); private FlowDefinitionRegistry flowRegistry; + private FlowUrlHandler flowUrlHandler; + /** * Returns the registry of flows to query when this mapping is tested. * @return the flow definition registry @@ -77,9 +82,9 @@ public class FlowHandlerMapping extends AbstractHandlerMapping { } protected void initServletContext(ServletContext servletContext) { - if (flowRegistry == null) { - flowRegistry = (FlowDefinitionRegistry) getApplicationContext().getBean("flowRegistry", - FlowDefinitionRegistry.class); + Assert.notNull(flowRegistry, "The FlowRegistry to query when mapping requests is required"); + if (flowUrlHandler == null) { + flowUrlHandler = new DefaultFlowUrlHandler(); } } @@ -88,12 +93,23 @@ public class FlowHandlerMapping extends AbstractHandlerMapping { if (getApplicationContext().containsBean(flowId)) { Object handler = getApplicationContext().getBean(flowId); if (handler instanceof FlowHandler) { + if (logger.isDebugEnabled()) { + logger.debug("Mapping request with URI '" + request.getRequestURI() + "' to flow with id '" + + flowId + "'; custom FlowHandler " + handler + " will manage flow execution"); + } return handler; } } if (flowRegistry.containsFlowDefinition(flowId)) { + if (logger.isDebugEnabled()) { + logger.debug("Mapping request with URI '" + request.getRequestURI() + "' to flow with id '" + flowId + + "'"); + } return new DefaultFlowHandler(flowId); } + if (logger.isDebugEnabled()) { + logger.debug("No flow mapping found for request with URI '" + request.getRequestURI() + "'"); + } return null; } @@ -109,4 +125,4 @@ public class FlowHandlerMapping extends AbstractHandlerMapping { } } -} +} \ No newline at end of file diff --git a/spring-webflow/src/test/java/org/springframework/webflow/mvc/servlet/FlowHandlerMappingTests.java b/spring-webflow/src/test/java/org/springframework/webflow/mvc/servlet/FlowHandlerMappingTests.java new file mode 100644 index 00000000..c1bd90e3 --- /dev/null +++ b/spring-webflow/src/test/java/org/springframework/webflow/mvc/servlet/FlowHandlerMappingTests.java @@ -0,0 +1,158 @@ +package org.springframework.webflow.mvc.servlet; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import junit.framework.TestCase; + +import org.springframework.context.ApplicationContext; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockServletContext; +import org.springframework.web.context.support.StaticWebApplicationContext; +import org.springframework.web.servlet.HandlerExecutionChain; +import org.springframework.webflow.core.FlowException; +import org.springframework.webflow.core.collection.MutableAttributeMap; +import org.springframework.webflow.definition.FlowDefinition; +import org.springframework.webflow.definition.StateDefinition; +import org.springframework.webflow.definition.registry.FlowDefinitionRegistryImpl; +import org.springframework.webflow.execution.FlowExecutionOutcome; + +public class FlowHandlerMappingTests extends TestCase { + private FlowHandlerMapping mapping = new FlowHandlerMapping(); + + public void setUp() { + FlowDefinitionRegistryImpl registry = new FlowDefinitionRegistryImpl(); + registry.registerFlowDefinition(new FlowDefinitionImpl()); + registry.registerFlowDefinition(new FlowDefinitionImpl("foo/flow2")); + StaticWebApplicationContext context = new StaticWebApplicationContext(); + context.getBeanFactory().registerSingleton("foo/flow2", new CustomFlowHandler()); + mapping.setFlowRegistry(registry); + mapping.setServletContext(new MockServletContext()); + mapping.setApplicationContext(context); + } + + public void testGetHandler() throws Exception { + MockHttpServletRequest request = new MockHttpServletRequest(); + request.setContextPath("/springtravel"); + request.setServletPath("/app"); + request.setPathInfo("/flow"); + request.setRequestURI("/springtravel/app/flow"); + request.setMethod("GET"); + HandlerExecutionChain chain = mapping.getHandler(request); + FlowHandler handler = (FlowHandler) chain.getHandler(); + assertEquals("flow", handler.getFlowId()); + } + + public void testGetHandlerCustomFlowHandler() throws Exception { + MockHttpServletRequest request = new MockHttpServletRequest(); + request.setContextPath("/springtravel"); + request.setServletPath("/app"); + request.setPathInfo("/foo/flow2"); + request.setRequestURI("/springtravel/app/foo/flow2"); + request.setMethod("GET"); + HandlerExecutionChain chain = mapping.getHandler(request); + assertNotNull(chain); + FlowHandler handler = (FlowHandler) chain.getHandler(); + assertEquals("foo/flow2", handler.getFlowId()); + assertTrue(handler instanceof CustomFlowHandler); + } + + public void testGetHandlerNoHandler() throws Exception { + MockHttpServletRequest request = new MockHttpServletRequest(); + request.setContextPath("/springtravel"); + request.setServletPath("/app"); + request.setPathInfo("/bogus"); + request.setRequestURI("/springtravel/app/bogus"); + request.setMethod("GET"); + HandlerExecutionChain chain = mapping.getHandler(request); + assertNull(chain); + } + + private static class FlowDefinitionImpl implements FlowDefinition { + + private String flowId = "flow"; + + public FlowDefinitionImpl() { + + } + + public FlowDefinitionImpl(String flowId) { + super(); + this.flowId = flowId; + } + + public ApplicationContext getApplicationContext() { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Auto-generated method stub"); + } + + public ClassLoader getClassLoader() { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Auto-generated method stub"); + } + + public String getId() { + return flowId; + } + + public String[] getPossibleOutcomes() { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Auto-generated method stub"); + } + + public StateDefinition getStartState() { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Auto-generated method stub"); + } + + public StateDefinition getState(String id) throws IllegalArgumentException { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Auto-generated method stub"); + } + + public boolean inDevelopment() { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Auto-generated method stub"); + } + + public MutableAttributeMap getAttributes() { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Auto-generated method stub"); + } + + public String getCaption() { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Auto-generated method stub"); + } + + public String getDescription() { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Auto-generated method stub"); + } + + } + + public static class CustomFlowHandler implements FlowHandler { + + public MutableAttributeMap createExecutionInputMap(HttpServletRequest request) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Auto-generated method stub"); + } + + public String getFlowId() { + return "foo/flow2"; + } + + public String handleException(FlowException e, HttpServletRequest request, HttpServletResponse response) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Auto-generated method stub"); + } + + public String handleExecutionOutcome(FlowExecutionOutcome outcome, HttpServletRequest request, + HttpServletResponse response) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Auto-generated method stub"); + } + + } +}