You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by en...@apache.org on 2018/09/04 19:48:10 UTC
[sling-org-apache-sling-resourceresolver] branch master updated:
SLING-7881 Resource resolver may calculate incorrect map path for an
address that is a selector on the root resource
This is an automated email from the ASF dual-hosted git repository.
enorman pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-resourceresolver.git
The following commit(s) were added to refs/heads/master by this push:
new 34f301b SLING-7881 Resource resolver may calculate incorrect map path for an address that is a selector on the root resource
34f301b is described below
commit 34f301b18227cbca24a8f5e636c405fc844077da
Author: Eric Norman <en...@apache.org>
AuthorDate: Tue Sep 4 12:47:36 2018 -0700
SLING-7881 Resource resolver may calculate incorrect map path for an
address that is a selector on the root resource
---
.../resourceresolver/impl/mapping/MapEntry.java | 20 +++++++-
.../impl/mapping/MapEntryTest.java | 60 ++++++++++++++++++++++
2 files changed, 78 insertions(+), 2 deletions(-)
diff --git a/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntry.java b/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntry.java
index 0790032..2fc2b56 100644
--- a/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntry.java
+++ b/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntry.java
@@ -275,8 +275,24 @@ public class MapEntry implements Comparable<MapEntry> {
final String[] redirects = getRedirect();
final String[] results = new String[redirects.length];
for (int i = 0; i < redirects.length; i++) {
- try{
- results[i] = m.replaceFirst(redirects[i]);
+ try {
+ String redirect = redirects[i];
+ // SLING-7881 - if the value is a selector on the root resource then the
+ // result will need to remove the trailing slash from the path
+ if (redirect.length() > 1 && redirect.endsWith("/")) {
+ if (value.length() > m.end()) {
+ if ('.' == value.charAt(m.end())) {
+ //the suffix starts with a dot and the redirect prefix ends with
+ // a slash so we need to remove the trailing slash from the prefix
+ // value to make a valid path when they are combined.
+ //
+ // for example: http/localhost.8080/.2.json should become /content.2.json
+ // instead of /content/.2.json
+ redirect = redirect.substring(0, redirect.length() - 1);
+ }
+ }
+ }
+ results[i] = m.replaceFirst(redirect);
} catch (final StringIndexOutOfBoundsException siob){
log.debug("Exception while replacing, ignoring entry {} ", redirects[i], siob);
} catch (final IllegalArgumentException iae){
diff --git a/src/test/java/org/apache/sling/resourceresolver/impl/mapping/MapEntryTest.java b/src/test/java/org/apache/sling/resourceresolver/impl/mapping/MapEntryTest.java
index d7d9225..aa78ce5 100644
--- a/src/test/java/org/apache/sling/resourceresolver/impl/mapping/MapEntryTest.java
+++ b/src/test/java/org/apache/sling/resourceresolver/impl/mapping/MapEntryTest.java
@@ -20,6 +20,7 @@ package org.apache.sling.resourceresolver.impl.mapping;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@@ -195,6 +196,65 @@ public class MapEntryTest {
assertTrue(b.compareTo(b) == 0);
}
+ /**
+ * SLING-7881 - Test that an address for for root and no selectors resolves
+ */
+ @Test public void test_replace_for_root() {
+ //no trailing slash, so no match to the pattern.
+ MapEntry mapEntry = new MapEntry("^[^/]+/[^/]+/", 200, false, 5, "/", "/content/");
+ String[] redirects = mapEntry.replace("http/localhost.8080");
+ assertNull(redirects);
+ }
+
+ /**
+ * SLING-7881 - Test that an address for a selector on the root resolves to valid replacement
+ * candidate paths.
+ */
+ @Test public void test_replace_for_root_selector() {
+ MapEntry mapEntry = new MapEntry("^[^/]+/[^/]+/", 200, false, 5, "/", "/content/");
+ String[] redirects = mapEntry.replace("http/localhost.8080/.2.json");
+ assertEquals(2, redirects.length);
+ assertEquals("/.2.json", redirects[0]);
+ assertEquals("/content.2.json", redirects[1]);
+
+ // or no selectors and just the extension
+ redirects = mapEntry.replace("http/localhost.8080/.json");
+ assertEquals(2, redirects.length);
+ assertEquals("/.json", redirects[0]);
+ assertEquals("/content.json", redirects[1]);
+ }
+
+ /**
+ * SLING-7881 - Test that an address for something other than root with no selectors resolves to
+ * valid replacement candidate paths.
+ */
+ @Test public void test_replace_for_nonroot() {
+ MapEntry mapEntry = new MapEntry("^[^/]+/[^/]+/", 200, false, 5, "/", "/content/");
+ String[] redirects = mapEntry.replace("http/localhost.8080/foo");
+ assertEquals(2, redirects.length);
+ assertEquals("/foo", redirects[0]);
+ assertEquals("/content/foo", redirects[1]);
+ }
+
+ /**
+ * SLING-7881 - Test that an address for a selector on something other than root resolves to
+ * valid replacement candidate paths.
+ */
+ @Test public void test_replace_for_nonroot_selector() {
+ MapEntry mapEntry = new MapEntry("^[^/]+/[^/]+/", 200, false, 5, "/", "/content/");
+ String[] redirects = mapEntry.replace("http/localhost.8080/foo.2.json");
+ assertEquals(2, redirects.length);
+ assertEquals("/foo.2.json", redirects[0]);
+ assertEquals("/content/foo.2.json", redirects[1]);
+
+ // or no selectors and just the extension
+ redirects = mapEntry.replace("http/localhost.8080/foo.json");
+ assertEquals(2, redirects.length);
+ assertEquals("/foo.json", redirects[0]);
+ assertEquals("/content/foo.json", redirects[1]);
+ }
+
+
private void assertEqualUri(String expected, String uriPath) {
String uri = MapEntry.toURI(uriPath);
assertNotNull("Failed converting " + uriPath, uri);