Commits

jcarreira  committed a2c9bb7

WW-241: ServletRedirectResult should redirect to contextPath + location -> if the redirect location does not begin with "/" the context path is appended.

git-svn-id: http://svn.opensymphony.com/svn/webwork/trunk@9573baa09-0c28-0410-bef9-dab3c582ae83

  • Participants
  • Parent commits 3f855f0

Comments (0)

Files changed (2)

File src/java/com/opensymphony/webwork/dispatcher/ServletRedirectResult.java

 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
+import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
 
     }
 
     public void execute(ActionInvocation invocation) throws Exception {
+        HttpServletRequest request = ServletActionContext.getRequest();
         HttpServletResponse response = ServletActionContext.getResponse();
 
         if (parse) {
             log.debug("Redirecting to location " + location);
         }
 
-        response.sendRedirect(location);
+        if (location.startsWith("/")) {
+            response.sendRedirect(location);
+        } else {
+            response.sendRedirect(request.getContextPath() + location);
+        }
     }
 }

File src/test/com/opensymphony/webwork/dispatcher/ServletRedirectResultTest.java

  * @version $Revision$
  */
 public class ServletRedirectResultTest extends TestCase implements WebWorkStatics {
-    //~ Methods ////////////////////////////////////////////////////////////////
+    //~ Instance fields ////////////////////////////////////////////////////////
 
-    public void testSimple() {
-        ServletRedirectResult view = new ServletRedirectResult();
-        view.setLocation("foo.jsp");
+    protected ServletRedirectResult view;
+    private Mock requestMock;
+    private Mock responseMock;
 
-        Mock responseMock = new Mock(HttpServletResponse.class);
-        responseMock.expectVoid("sendRedirect", C.args(C.eq("foo.jsp")));
+    //~ Methods ////////////////////////////////////////////////////////////////
 
-        Mock requestMock = new Mock(HttpServletRequest.class);
+    public void testAbsoluteRedirect() {
+        view.setLocation("/bar/foo.jsp");
+        responseMock.expect("sendRedirect", C.args(C.eq("/bar/foo.jsp")));
 
-        ActionContext ac = new ActionContext(Ognl.createDefaultContext(null));
-        ActionContext.setContext(ac);
-        ServletActionContext.setResponse((HttpServletResponse) responseMock.proxy());
-        ServletActionContext.setRequest((HttpServletRequest) requestMock.proxy());
+        try {
+            view.execute(null);
+        } catch (Exception e) {
+            e.printStackTrace();
+            fail();
+        }
+    }
+
+    public void testRelativeRedirect() {
+        view.setLocation("foo.jsp");
+        responseMock.expect("sendRedirect", C.args(C.eq("/context/foo.jsp")));
 
         try {
             view.execute(null);
             e.printStackTrace();
             fail();
         }
+    }
+
+    protected void setUp() {
+        view = new ServletRedirectResult();
+
+        responseMock = new Mock(HttpServletResponse.class);
+
+        requestMock = new Mock(HttpServletRequest.class);
+        requestMock.matchAndReturn("getContextPath", "/context/");
+
+        ActionContext ac = new ActionContext(Ognl.createDefaultContext(null));
+        ActionContext.setContext(ac);
+        ServletActionContext.setResponse((HttpServletResponse) responseMock.proxy());
+        ServletActionContext.setRequest((HttpServletRequest) requestMock.proxy());
+    }
 
+    protected void tearDown() {
         requestMock.verify();
         responseMock.verify();
     }