incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1369828 - in /sling/trunk/bundles/servlets/post/src: main/java/org/apache/sling/servlets/post/impl/SlingPostServlet.java test/java/org/apache/sling/servlets/post/impl/SlingPostServletTest.java
Date Mon, 06 Aug 2012 13:06:56 GMT
Author: bdelacretaz
Date: Mon Aug  6 13:06:55 2012
New Revision: 1369828

URL: http://svn.apache.org/viewvc?rev=1369828&view=rev
Log:
SLING-2543 - call HttpServletResponse.encodeRedirectURL when redirecting. Slightly modified
patch from James Phillpotts, thanks!

Modified:
    sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/SlingPostServlet.java
    sling/trunk/bundles/servlets/post/src/test/java/org/apache/sling/servlets/post/impl/SlingPostServletTest.java

Modified: sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/SlingPostServlet.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/SlingPostServlet.java?rev=1369828&r1=1369827&r2=1369828&view=diff
==============================================================================
--- sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/SlingPostServlet.java
(original)
+++ sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/SlingPostServlet.java
Mon Aug  6 13:06:55 2012
@@ -214,10 +214,8 @@ public class SlingPostServlet extends Sl
 
         // check for redirect URL if processing succeeded
         if (htmlResponse.isSuccessful()) {
-            String redirect = getRedirectUrl(request, htmlResponse);
-            if (redirect != null) {
-                response.sendRedirect(redirect);
-                return;
+            if (redirectIfNeeded(getRedirectUrl(request, htmlResponse), response)) {
+            	return;
             }
         }
 
@@ -225,6 +223,23 @@ public class SlingPostServlet extends Sl
         htmlResponse.send(response, isSetStatus(request));
     }
 
+	/**
+	 * Redirects the HttpServletResponse, if redirectURL is not empty
+	 * @param redirectURL The computed redirect URL
+	 * @param response The HttpServletResponse to use for redirection 
+	 * @return Whether a redirect was requested
+	 * @throws IOException
+	 */
+	boolean redirectIfNeeded(String redirectURL,
+			SlingHttpServletResponse response)
+			throws IOException {
+		if (redirectURL != null) {
+		    response.sendRedirect(response.encodeRedirectURL(redirectURL));
+		    return true;
+		}
+		return false;
+	}
+
     /**
      * Creates an instance of a PostResponse.
      * @param req The request being serviced

Modified: sling/trunk/bundles/servlets/post/src/test/java/org/apache/sling/servlets/post/impl/SlingPostServletTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/post/src/test/java/org/apache/sling/servlets/post/impl/SlingPostServletTest.java?rev=1369828&r1=1369827&r2=1369828&view=diff
==============================================================================
--- sling/trunk/bundles/servlets/post/src/test/java/org/apache/sling/servlets/post/impl/SlingPostServletTest.java
(original)
+++ sling/trunk/bundles/servlets/post/src/test/java/org/apache/sling/servlets/post/impl/SlingPostServletTest.java
Mon Aug  6 13:06:55 2012
@@ -18,9 +18,14 @@
  */
 package org.apache.sling.servlets.post.impl;
 
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+
 import junit.framework.TestCase;
 
 import org.apache.sling.commons.testing.sling.MockSlingHttpServletRequest;
+import org.apache.sling.commons.testing.sling.MockSlingHttpServletResponse;
 import org.apache.sling.servlets.post.JSONResponse;
 import org.apache.sling.servlets.post.PostResponse;
 import org.apache.sling.servlets.post.SlingPostConstants;
@@ -71,6 +76,39 @@ public class SlingPostServletTest extend
         PostResponse result = servlet.createPostResponse(req);
         assertTrue(result instanceof JSONResponse);
     }
+    
+    public void testRedirection() throws Exception {
+    	final String[] redirectLocation = new String[1];
+    	MockSlingHttpServletResponse resp = new MockSlingHttpServletResponse() {
+    		@Override
+    		public String encodeRedirectURL(String s) {
+    			try {
+					return URLEncoder.encode(s, "UTF-8");
+				} catch (UnsupportedEncodingException e) {
+					fail("Should have UTF-8?? " + e);
+					return null;
+				}
+    		}
+    		
+    		@Override
+    		public void sendRedirect(String s) throws IOException {
+    			redirectLocation[0] = s;
+    		}
+    	};
+    	
+    	SlingPostServlet servlet = new SlingPostServlet();
+
+    	assertTrue(servlet.redirectIfNeeded("\u0414\u0440\u0443\u0433\u0430.html", resp));
+    	assertEquals("Should encode UTF-8", "%D0%94%D1%80%D1%83%D0%B3%D0%B0.html", redirectLocation[0]);
+
+    	redirectLocation[0] = null;
+    	assertTrue(servlet.redirectIfNeeded("fred.html", resp));
+    	assertEquals("Plain old ASCII passes through", "fred.html", redirectLocation[0]);
+
+    	redirectLocation[0] = null;
+    	assertFalse(servlet.redirectIfNeeded(null, resp));
+    	assertNull("Shouldn't have encoded anything", redirectLocation[0]);
+    }
 
     private static class StatusParamSlingHttpServletRequest extends
             MockSlingHttpServletRequest {



Mime
View raw message