Author: justin
Date: Tue Oct 30 16:36:04 2012
New Revision: 1403772
URL: http://svn.apache.org/viewvc?rev=1403772&view=rev
Log:
SLING-2639 - adding check for extension in resource names with vanity paths
Modified:
sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/VanityPathTest.java
Modified:
sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java?rev=1403772&r1=1403771&r2=1403772&view=diff
==============================================================================
---
sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java
(original)
+++
sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java
Tue Oct 30 16:36:04 2012
@@ -560,12 +560,14 @@ public class MapEntries implements Event
// sling:vanityPath
// property (or its parent if the node is called
// jcr:content)
- final String redirect;
+ final Resource redirectTarget;
if (resource.getName().equals("jcr:content")) {
- redirect = resource.getParent().getPath();
+ redirectTarget = resource.getParent();
} else {
- redirect = resource.getPath();
+ redirectTarget = resource;
}
+ final String redirect = redirectTarget.getPath();
+ final String redirectName = redirectTarget.getName();
// whether the target is attained by a 302/FOUND or by
an
// internal redirect is defined by the sling:redirect
@@ -575,12 +577,23 @@ public class MapEntries implements Event
: -1;
final String checkPath = result[1];
- // 1. entry with exact match
- this.addEntry(entryMap, checkPath, new MapEntry(url +
"$", status, false, redirect + ".html"));
- // 2. entry with match supporting selectors and
extension
- this.addEntry(entryMap, checkPath, new MapEntry(url +
"(\\..*)", status, false, redirect + "$1"));
+ if (redirectName.indexOf('.') > -1) {
+ // 1. entry with exact match
+ this.addEntry(entryMap, checkPath, new
MapEntry(url + "$", status, false, redirect));
+ final int idx = redirectName.lastIndexOf('.');
+ final String extension =
redirectName.substring(idx + 1);
+
+ // 2. entry with extension
+ this.addEntry(entryMap, checkPath, new
MapEntry(url + "\\." + extension, status, false, redirect));
+ } else {
+ // 1. entry with exact match
+ this.addEntry(entryMap, checkPath, new
MapEntry(url + "$", status, false, redirect + ".html"));
+
+ // 2. entry with match supporting selectors and
extension
+ this.addEntry(entryMap, checkPath, new
MapEntry(url + "(\\..*)", status, false, redirect + "$1"));
+ }
// 3. keep the path to return
targetPaths.add(redirect);
}
Modified:
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/VanityPathTest.java
URL:
http://svn.apache.org/viewvc/sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/VanityPathTest.java?rev=1403772&r1=1403771&r2=1403772&view=diff
==============================================================================
---
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/VanityPathTest.java
(original)
+++
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/VanityPathTest.java
Tue Oct 30 16:36:04 2012
@@ -17,9 +17,13 @@
package org.apache.sling.launchpad.webapp.integrationtest;
import java.io.IOException;
+import java.util.Arrays;
+import java.util.Collections;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
+import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.sling.commons.testing.integration.HttpTestBase;
import org.apache.sling.servlets.post.SlingPostConstants;
@@ -145,7 +149,39 @@ public class VanityPathTest extends Http
assertNotNull(location);
assertEquals(removeHttpBase(createdNodeUrl) + ".test.json", location);
}
-
+
+ /** test vanity path on a path with an extension with a redirect */
+ public void testRedirectOnPathWithExtension() throws IOException {
+ // create a node with a vanity path
+ Map<String, String> props = new HashMap<String, String>();
+ props.put("jcr:mixinTypes", "sling:VanityPath");
+ props.put("sling:vanityPath", vanityPath);
+ props.put("sling:redirect", "true");
+ String createdNodeUrl = testClient.createNode(postUrl, props);
+
+ String pathWithExtension = removeHttpBase(createdNodeUrl) + ".ext";
+ List<NameValuePair> moveParams = Arrays.asList(
+ new NameValuePair(":dest", pathWithExtension),
+ new NameValuePair(":operation", "move"));
+ assertPostStatus(createdNodeUrl, 201, moveParams, "Could not move
created node from " + createdNodeUrl);
+ createdNodeUrl = createdNodeUrl + ".ext";
+
+ waitForMapReload();
+
+ // get the created node's vanity path without following redirects
+ GetMethod get = new GetMethod(vanityUrl);
+ get.setFollowRedirects(false);
+ int status = httpClient.executeMethod(get);
+
+ // expect temporary redirect ...
+ assertEquals(302, status);
+
+ // ... to the created node
+ String location = get.getResponseHeader("Location").getValue();
+ assertNotNull(location);
+ assertEquals(pathWithExtension, location);
+ }
+
private String removeHttpBase(String url) {
return url.startsWith(HTTP_BASE_URL) ?
url.substring(HTTP_BASE_URL.length()) : url;
}