Revision: 1367
http://stripes.svn.sourceforge.net/stripes/?rev=1367&view=rev
Author: bengunter
Date: 2010-12-03 16:49:24 +0000 (Fri, 03 Dec 2010)
Log Message:
-----------
Applied fix for STS-750 from trunk.
Modified Paths:
--------------
trunk/stripes/src/net/sourceforge/stripes/action/RedirectResolution.java
Added Paths:
-----------
trunk/tests/src/net/sourceforge/stripes/action/RedirectResolutionTest.java
Modified:
trunk/stripes/src/net/sourceforge/stripes/action/RedirectResolution.java
===================================================================
--- trunk/stripes/src/net/sourceforge/stripes/action/RedirectResolution.java
2010-12-03 16:46:53 UTC (rev 1366)
+++ trunk/stripes/src/net/sourceforge/stripes/action/RedirectResolution.java
2010-12-03 16:49:24 UTC (rev 1367)
@@ -21,6 +21,8 @@
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpServletResponseWrapper;
+
import java.io.IOException;
import java.util.Collection;
import java.util.HashSet;
@@ -40,6 +42,11 @@
* methods, and by invoking includeRequestParameters() which will cause all of
the current
* request parameters to be included into the URL.</p>
*
+ * <p>
+ * The redirect type can be switched from a 302 temporary redirect (default)
to a 301 permanent
+ * redirect using the setPermanent method.
+ * </p>
+ *
* @see ForwardResolution
* @author Tim Fennell
*/
@@ -48,6 +55,7 @@
private boolean prependContext = true;
private boolean includeRequestParameters;
private Collection<ActionBean> beans; // used to flash action beans
+ private boolean permanent = false;
/**
* Simple constructor that takes the URL to which to forward the user.
Defaults to
@@ -143,9 +151,24 @@
* @throws IOException thrown when the Servlet container encounters an
error
*/
@SuppressWarnings("unchecked")
- public void execute(HttpServletRequest request, HttpServletResponse
response)
- throws ServletException, IOException {
+ public void execute(HttpServletRequest request, HttpServletResponse
response)
+ throws ServletException, IOException {
+ if (permanent) {
+ response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
+ response = new HttpServletResponseWrapper(response) {
+
+ @Override
+ public void setStatus(int sc) {
+ }
+
+ @Override
+ public void sendRedirect(String location) throws IOException {
+ setHeader("Location", location);
+ }
+
+ };
+ }
if (this.includeRequestParameters) {
addParameters(request.getParameterMap());
}
@@ -177,4 +200,10 @@
response.sendRedirect(url);
}
+
+ /** Sets the redirect type to permanent (301) instead of temporary (302).
*/
+ public RedirectResolution setPermanent(boolean permanent) {
+ this.permanent = permanent;
+ return this;
+ }
}
Added:
trunk/tests/src/net/sourceforge/stripes/action/RedirectResolutionTest.java
===================================================================
--- trunk/tests/src/net/sourceforge/stripes/action/RedirectResolutionTest.java
(rev 0)
+++ trunk/tests/src/net/sourceforge/stripes/action/RedirectResolutionTest.java
2010-12-03 16:49:24 UTC (rev 1367)
@@ -0,0 +1,68 @@
+package net.sourceforge.stripes.action;
+
+import java.util.Locale;
+
+import javax.servlet.http.HttpServletResponse;
+
+import net.sourceforge.stripes.StripesTestFixture;
+import net.sourceforge.stripes.mock.MockHttpServletRequest;
+import net.sourceforge.stripes.mock.MockHttpServletResponse;
+
+import org.testng.Assert;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+public class RedirectResolutionTest {
+
+ @BeforeClass
+ public void setupServletContext(){
+ StripesTestFixture.getServletContext();
+ }
+ //helper method
+ private MockHttpServletRequest buildMockServletRequest(){
+ MockHttpServletRequest request = new
MockHttpServletRequest("/context", "/whatever");
+ request.addLocale(Locale.US);
+ return request;
+ }
+
+ @Test(groups = "fast")
+ public void testPermanantRedirect() throws Exception {
+ RedirectResolution resolution = new
RedirectResolution("http://www.stripesframework.org", false).setPermanent(true);
+ MockHttpServletResponse response = new MockHttpServletResponse();
+ resolution.execute(buildMockServletRequest(), response);
+
+ Assert.assertEquals(response.getStatus(),
HttpServletResponse.SC_MOVED_PERMANENTLY);
+
Assert.assertEquals(response.getHeaderMap().get("Location").iterator().next(),
"http://www.stripesframework.org");
+ }
+
+ @Test(groups = "fast")
+ public void testTemporaryRedirect() throws Exception {
+ RedirectResolution resolution = new
RedirectResolution("http://www.stripesframework.org", false);
+ MockHttpServletResponse response = new MockHttpServletResponse();
+ resolution.execute(buildMockServletRequest(), response);
+
+ Assert.assertEquals(response.getStatus(),
HttpServletResponse.SC_MOVED_TEMPORARILY);
+ Assert.assertEquals(response.getRedirectUrl(),
"http://www.stripesframework.org");
+ }
+
+ @Test(groups = "fast")
+ public void testPermanantRedirectWithParameters() throws Exception {
+ RedirectResolution resolution = new
RedirectResolution("http://www.stripesframework.org",
false).setPermanent(true).addParameter("test", "test");
+ MockHttpServletResponse response = new MockHttpServletResponse();
+ resolution.execute(buildMockServletRequest(), response);
+
+ Assert.assertEquals(response.getStatus(),
HttpServletResponse.SC_MOVED_PERMANENTLY);
+
Assert.assertEquals(response.getHeaderMap().get("Location").iterator().next(),
"http://www.stripesframework.org?test=test");
+ }
+
+ @Test(groups = "fast")
+ public void testTemporaryRedirectWithParameters() throws Exception {
+ RedirectResolution resolution = new
RedirectResolution("http://www.stripesframework.org",
false).addParameter("test", "test");
+ MockHttpServletResponse response = new MockHttpServletResponse();
+ resolution.execute(buildMockServletRequest(), response);
+
+ Assert.assertEquals(response.getStatus(),
HttpServletResponse.SC_MOVED_TEMPORARILY);
+ Assert.assertEquals(response.getRedirectUrl(),
"http://www.stripesframework.org?test=test");
+ }
+
+}
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
------------------------------------------------------------------------------
Increase Visibility of Your 3D Game App & Earn a Chance To Win $500!
Tap into the largest installed PC base & get more eyes on your game by
optimizing for Intel(R) Graphics Technology. Get started today with the
Intel(R) Software Partner Program. Five $500 cash prizes are up for grabs.
http://p.sf.net/sfu/intelisp-dev2dev
_______________________________________________
Stripes-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-development