Author: fmeschbe
Date: Wed Feb 2 08:37:55 2011
New Revision: 1066367
URL: http://svn.apache.org/viewvc?rev=1066367&view=rev
Log:
SLING-1965 Enhance integration test to also include a test for absolute URL
redirects and fix use cases involving request selectors, extension, and suffix
besides query string.
Modified:
sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/RedirectServlet.java
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/RedirectTest.java
Modified:
sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/RedirectServlet.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/RedirectServlet.java?rev=1066367&r1=1066366&r2=1066367&view=diff
==============================================================================
---
sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/RedirectServlet.java
(original)
+++
sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/RedirectServlet.java
Wed Feb 2 08:37:55 2011
@@ -147,11 +147,10 @@ public class RedirectServlet extends Sli
// make path relative and append selectors, extension etc.
// this is an absolute URI suitable for the Location header
targetPath = toRedirectPath(targetPath, request);
- }
-
- // append current querystring
- if (request.getQueryString() != null) {
- targetPath += "?" + request.getQueryString();
+ } else {
+ // just append any selectors, extension, suffix and query
string
+ targetPath = appendSelectorsExtensionSuffixQuery(request,
+ new StringBuilder(targetPath)).toString();
}
final int status = getStatus(valueMap);
@@ -207,9 +206,6 @@ public class RedirectServlet extends Sli
static String toRedirectPath(String targetPath,
SlingHttpServletRequest request) {
- // if the target path is an URL, do nothing and return it unmodified
- final RequestPathInfo rpi = request.getRequestPathInfo();
-
// make sure the target path is absolute
final String rawAbsPath;
if (targetPath.startsWith("/")) {
@@ -228,7 +224,35 @@ public class RedirectServlet extends Sli
target.append(absPath);
}
+ appendSelectorsExtensionSuffixQuery(request, target);
+
+ // return the mapped full path and return if already an absolute URI
+ final String finalTarget = request.getResourceResolver().map(request,
target.toString());
+ if (isUrl(finalTarget)) {
+ return finalTarget;
+ }
+
+ // otherwise prepend the current request's information
+ return toAbsoluteUri(request.getScheme(), request.getServerName(),
+ request.getServerPort(), finalTarget);
+ }
+
+ /**
+ * Appends optional request selectors, extension, suffix and query string
to
+ * the URL to be prepared in the target string builder and returns the
+ * string builder.
+ *
+ * @param request The Sling HTTP Servlet Request providing access to the
+ * data to be appended
+ * @param target The String builder to append the data to. This must not be
+ * null.
+ * @return The <code>target</code> string builder.
+ * @throws NullPointerException if request or target is <code>null</code>.
+ */
+ private static StringBuilder appendSelectorsExtensionSuffixQuery(
+ SlingHttpServletRequest request, StringBuilder target) {
// append current selectors, extension and suffix
+ final RequestPathInfo rpi = request.getRequestPathInfo();
if (rpi.getExtension() != null) {
if (rpi.getSelectorString() != null) {
@@ -242,15 +266,12 @@ public class RedirectServlet extends Sli
}
}
- // return the mapped full path and return if already an absolute URI
- final String finalTarget = request.getResourceResolver().map(request,
target.toString());
- if (isUrl(finalTarget)) {
- return finalTarget;
+ // append current querystring
+ if (request.getQueryString() != null) {
+ target.append('?').append(request.getQueryString());
}
- // otherwise prepend the current request's information
- return toAbsoluteUri(request.getScheme(), request.getServerName(),
- request.getServerPort(), finalTarget);
+ return target;
}
/**
Modified:
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/RedirectTest.java
URL:
http://svn.apache.org/viewvc/sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/RedirectTest.java?rev=1066367&r1=1066366&r2=1066367&view=diff
==============================================================================
---
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/RedirectTest.java
(original)
+++
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/RedirectTest.java
Wed Feb 2 08:37:55 2011
@@ -91,6 +91,55 @@ public class RedirectTest extends HttpTe
assertTrue(location.endsWith("/index.html?param=value"));
}
+ /** test 302 response with existing sling:target */
+ public void testRedirect302_absolute() throws IOException {
+
+ // create a node redirecting to /index
+ Map<String, String> props = new HashMap<String, String>();
+ props.put("sling:resourceType", "sling:redirect");
+ props.put("sling:target", "http://some.host.none/index.html");
+ String redirNodeUrl = testClient.createNode(postUrl, props);
+
+ // get the created node without following redirects
+ GetMethod get = new GetMethod(redirNodeUrl);
+ get.setFollowRedirects(false);
+ int status = httpClient.executeMethod(get);
+
+ // expect temporary redirect ...
+ assertEquals(302, status);
+
+ // ... to */index.html
+ String location = get.getResponseHeader("Location").getValue();
+ assertNotNull(location);
+ assertTrue(location.equals("http://some.host.none/index.html"));
+
+ // get the created node without following redirects
+ get = new GetMethod(redirNodeUrl + ".html");
+ get.setFollowRedirects(false);
+ status = httpClient.executeMethod(get);
+
+ // expect temporary redirect ...
+ assertEquals(302, status);
+
+ // ... to */index.html
+ location = get.getResponseHeader("Location").getValue();
+ assertNotNull(location);
+ assertTrue(location.equals("http://some.host.none/index.html.html"));
+
+ // get the created node without following redirects
+ get = new GetMethod(redirNodeUrl + "?param=value");
+ get.setFollowRedirects(false);
+ status = httpClient.executeMethod(get);
+
+ // expect temporary redirect ...
+ assertEquals(302, status);
+
+ // ... to */index.html
+ location = get.getResponseHeader("Location").getValue();
+ assertNotNull(location);
+
assertTrue(location.equals("http://some.host.none/index.html?param=value"));
+ }
+
/** test 404 response when sling:target is missing */
public void testRedirect404() throws IOException {
// create a sling:redirect node without a target