From 5bf084fbe4e80f7be4ef9d16721d8c66fd5c31b4 Mon Sep 17 00:00:00 2001 From: Keith Donald Date: Wed, 23 Apr 2008 19:28:04 +0000 Subject: [PATCH] redirect prefixes --- .../mvc/servlet/FlowHandlerAdapter.java | 5 +- .../mvc/servlet/FlowHandlerAdapterTests.java | 124 ++++++++++++++++++ 2 files changed, 127 insertions(+), 2 deletions(-) diff --git a/spring-webflow/src/main/java/org/springframework/webflow/mvc/servlet/FlowHandlerAdapter.java b/spring-webflow/src/main/java/org/springframework/webflow/mvc/servlet/FlowHandlerAdapter.java index 9b63fef6..d212723d 100644 --- a/spring-webflow/src/main/java/org/springframework/webflow/mvc/servlet/FlowHandlerAdapter.java +++ b/spring-webflow/src/main/java/org/springframework/webflow/mvc/servlet/FlowHandlerAdapter.java @@ -356,7 +356,7 @@ public class FlowHandlerAdapter extends WebApplicationObjectSupport implements H IllegalArgumentException iae = new IllegalArgumentException("The redirect url '" + url + "' is invalid; specify a fully qualified URL when using the '" + URL_LOCATION_PREFIX + "' location prefix"); - e.initCause(e); + iae.initCause(e); throw iae; } } else { @@ -366,7 +366,8 @@ public class FlowHandlerAdapter extends WebApplicationObjectSupport implements H private void sendServletRelativeRedirect(String location, HttpServletRequest request, HttpServletResponse response) throws IOException { - StringBuffer url = new StringBuffer(request.getServletPath()); + StringBuffer url = new StringBuffer(request.getContextPath()); + url.append(request.getServletPath()); if (!location.startsWith("/")) { url.append('/'); } diff --git a/spring-webflow/src/test/java/org/springframework/webflow/mvc/servlet/FlowHandlerAdapterTests.java b/spring-webflow/src/test/java/org/springframework/webflow/mvc/servlet/FlowHandlerAdapterTests.java index c5579e37..820ef6d9 100644 --- a/spring-webflow/src/test/java/org/springframework/webflow/mvc/servlet/FlowHandlerAdapterTests.java +++ b/spring-webflow/src/test/java/org/springframework/webflow/mvc/servlet/FlowHandlerAdapterTests.java @@ -213,6 +213,130 @@ public class FlowHandlerAdapterTests extends TestCase { EasyMock.verify(new Object[] { flowExecutor }); } + public void testLaunchFlowWithExternalRedirectServletRelative() throws Exception { + request.setContextPath("/springtravel"); + request.setServletPath("/app"); + request.setPathInfo("/foo"); + request.setRequestURI("/springtravel/app/foo"); + request.setMethod("GET"); + context.requestExternalRedirect("servletRelative:bar"); + flowExecutor.launchExecution("foo", flowInput, context); + FlowExecutionResult result = FlowExecutionResult.createPausedResult("foo", "12345"); + EasyMock.expectLastCall().andReturn(result); + EasyMock.replay(new Object[] { flowExecutor }); + flowHandlerAdapter.handle(request, response, flowHandler); + EasyMock.verify(new Object[] { flowExecutor }); + assertEquals("/springtravel/app/bar", response.getRedirectedUrl()); + EasyMock.verify(new Object[] { flowExecutor }); + } + + public void testLaunchFlowWithExternalRedirectServletRelativeWithSlash() throws Exception { + request.setContextPath("/springtravel"); + request.setServletPath("/app"); + request.setPathInfo("/foo"); + request.setRequestURI("/springtravel/app/foo"); + request.setMethod("GET"); + context.requestExternalRedirect("servletRelative:/bar"); + flowExecutor.launchExecution("foo", flowInput, context); + FlowExecutionResult result = FlowExecutionResult.createPausedResult("foo", "12345"); + EasyMock.expectLastCall().andReturn(result); + EasyMock.replay(new Object[] { flowExecutor }); + flowHandlerAdapter.handle(request, response, flowHandler); + EasyMock.verify(new Object[] { flowExecutor }); + assertEquals("/springtravel/app/bar", response.getRedirectedUrl()); + EasyMock.verify(new Object[] { flowExecutor }); + } + + public void testLaunchFlowWithExternalRedirectContextRelative() throws Exception { + request.setContextPath("/springtravel"); + request.setServletPath("/app"); + request.setPathInfo("/foo"); + request.setRequestURI("/springtravel/app/foo"); + request.setMethod("GET"); + context.requestExternalRedirect("contextRelative:bar"); + flowExecutor.launchExecution("foo", flowInput, context); + FlowExecutionResult result = FlowExecutionResult.createPausedResult("foo", "12345"); + EasyMock.expectLastCall().andReturn(result); + EasyMock.replay(new Object[] { flowExecutor }); + flowHandlerAdapter.handle(request, response, flowHandler); + EasyMock.verify(new Object[] { flowExecutor }); + assertEquals("/springtravel/bar", response.getRedirectedUrl()); + EasyMock.verify(new Object[] { flowExecutor }); + } + + public void testLaunchFlowWithExternalRedirectContextRelativeWithSlash() throws Exception { + request.setContextPath("/springtravel"); + request.setServletPath("/app"); + request.setPathInfo("/foo"); + request.setRequestURI("/springtravel/app/foo"); + request.setMethod("GET"); + context.requestExternalRedirect("contextRelative:/bar"); + flowExecutor.launchExecution("foo", flowInput, context); + FlowExecutionResult result = FlowExecutionResult.createPausedResult("foo", "12345"); + EasyMock.expectLastCall().andReturn(result); + EasyMock.replay(new Object[] { flowExecutor }); + flowHandlerAdapter.handle(request, response, flowHandler); + EasyMock.verify(new Object[] { flowExecutor }); + assertEquals("/springtravel/bar", response.getRedirectedUrl()); + EasyMock.verify(new Object[] { flowExecutor }); + } + + public void testLaunchFlowWithExternalRedirectServerRelative() throws Exception { + request.setContextPath("/springtravel"); + request.setServletPath("/app"); + request.setPathInfo("/foo"); + request.setRequestURI("/springtravel/app/foo"); + request.setMethod("GET"); + context.requestExternalRedirect("serverRelative:bar"); + flowExecutor.launchExecution("foo", flowInput, context); + FlowExecutionResult result = FlowExecutionResult.createPausedResult("foo", "12345"); + EasyMock.expectLastCall().andReturn(result); + EasyMock.replay(new Object[] { flowExecutor }); + flowHandlerAdapter.handle(request, response, flowHandler); + EasyMock.verify(new Object[] { flowExecutor }); + assertEquals("/bar", response.getRedirectedUrl()); + EasyMock.verify(new Object[] { flowExecutor }); + } + + public void testLaunchFlowWithExternalRedirectServerRelativeWithSlash() throws Exception { + request.setContextPath("/springtravel"); + request.setServletPath("/app"); + request.setPathInfo("/foo"); + request.setRequestURI("/springtravel/app/foo"); + request.setMethod("GET"); + context.requestExternalRedirect("serverRelative:/bar"); + flowExecutor.launchExecution("foo", flowInput, context); + FlowExecutionResult result = FlowExecutionResult.createPausedResult("foo", "12345"); + EasyMock.expectLastCall().andReturn(result); + EasyMock.replay(new Object[] { flowExecutor }); + flowHandlerAdapter.handle(request, response, flowHandler); + EasyMock.verify(new Object[] { flowExecutor }); + assertEquals("/bar", response.getRedirectedUrl()); + EasyMock.verify(new Object[] { flowExecutor }); + } + + public void testLaunchFlowWithExternalRedirectMalformedUrl() throws Exception { + request.setContextPath("/springtravel"); + request.setServletPath("/app"); + request.setPathInfo("/foo"); + request.setRequestURI("/springtravel/app/foo"); + request.setMethod("GET"); + context.requestExternalRedirect("url:/bogus"); + flowExecutor.launchExecution("foo", flowInput, context); + FlowExecutionResult result = FlowExecutionResult.createPausedResult("foo", "12345"); + EasyMock.expectLastCall().andReturn(result); + EasyMock.replay(new Object[] { flowExecutor }); + try { + flowHandlerAdapter.handle(request, response, flowHandler); + fail("Should have failed iae"); + } catch (IllegalArgumentException e) { + + } + EasyMock.verify(new Object[] { flowExecutor }); + assertEquals(null, response.getRedirectedUrl()); + EasyMock.verify(new Object[] { flowExecutor }); + } + public void testDefaultHandleFlowException() throws Exception { request.setContextPath("/springtravel"); request.setServletPath("/app");