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 2022/06/11 22:44:49 UTC
[sling-org-apache-sling-api] branch master updated: SLING-11347 SlingUriBuilder preserve mapped resource path (#41)
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-api.git
The following commit(s) were added to refs/heads/master by this push:
new 2c82030 SLING-11347 SlingUriBuilder preserve mapped resource path (#41)
2c82030 is described below
commit 2c820300ee67eaa43e925a646dd4398dcda7a2be
Author: Eric Norman <en...@apache.org>
AuthorDate: Sat Jun 11 15:44:44 2022 -0700
SLING-11347 SlingUriBuilder preserve mapped resource path (#41)
---
.../org/apache/sling/api/uri/SlingUriBuilder.java | 22 +++++++-
.../org/apache/sling/api/uri/package-info.java | 2 +-
.../apache/sling/api/uri/SlingUriBuilderTest.java | 65 ++++++++++++++++++++++
3 files changed, 86 insertions(+), 3 deletions(-)
diff --git a/src/main/java/org/apache/sling/api/uri/SlingUriBuilder.java b/src/main/java/org/apache/sling/api/uri/SlingUriBuilder.java
index f5cb76d..916b282 100644
--- a/src/main/java/org/apache/sling/api/uri/SlingUriBuilder.java
+++ b/src/main/java/org/apache/sling/api/uri/SlingUriBuilder.java
@@ -160,12 +160,30 @@ public class SlingUriBuilder {
*/
@NotNull
public static SlingUriBuilder createFrom(@NotNull SlingHttpServletRequest request) {
- return createFrom(request.getRequestPathInfo())
- .setResourceResolver(request.getResourceResolver())
+ @NotNull
+ ResourceResolver resourceResolver = request.getResourceResolver();
+ @NotNull
+ SlingUriBuilder uriBuilder = createFrom(request.getRequestPathInfo())
+ .setResourceResolver(resourceResolver)
.setScheme(request.getScheme())
.setHost(request.getServerName())
.setPort(request.getServerPort())
.setQuery(request.getQueryString());
+
+ // SLING-11347 - check if the original request was using a mapped path
+ @Nullable
+ String resourcePath = uriBuilder.getResourcePath();
+ if (resourcePath != null) {
+ @NotNull
+ String mappedResourcePath = resourceResolver.map(request, resourcePath);
+ if (!resourcePath.equals(mappedResourcePath) &&
+ request.getPathInfo().startsWith(mappedResourcePath)) {
+ // mapped path is different from the resource path and
+ // the request path was the mapped path, so switch to it
+ uriBuilder.setResourcePath(mappedResourcePath);
+ }
+ }
+ return uriBuilder;
}
/**
diff --git a/src/main/java/org/apache/sling/api/uri/package-info.java b/src/main/java/org/apache/sling/api/uri/package-info.java
index 36befcc..8c68894 100644
--- a/src/main/java/org/apache/sling/api/uri/package-info.java
+++ b/src/main/java/org/apache/sling/api/uri/package-info.java
@@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
-@Version("1.3.0")
+@Version("1.3.1")
package org.apache.sling.api.uri;
import org.osgi.annotation.versioning.Version;
diff --git a/src/test/java/org/apache/sling/api/uri/SlingUriBuilderTest.java b/src/test/java/org/apache/sling/api/uri/SlingUriBuilderTest.java
index 2f4bc92..8e3cee8 100644
--- a/src/test/java/org/apache/sling/api/uri/SlingUriBuilderTest.java
+++ b/src/test/java/org/apache/sling/api/uri/SlingUriBuilderTest.java
@@ -32,6 +32,7 @@ import java.util.Map;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.request.RequestPathInfo;
import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -50,6 +51,9 @@ public class SlingUriBuilderTest {
@Mock
Resource resource;
+ @Mock
+ ResourceResolver resourceResolver;
+
@Before
public void before() {
when(request.getRequestPathInfo()).thenReturn(requestPathInfo);
@@ -87,6 +91,10 @@ public class SlingUriBuilderTest {
@Test
public void testCreateFromRequest() {
+ // to satisfy that the return of this call is @NotNull
+ when(request.getResourceResolver()).thenReturn(resourceResolver);
+ when(resourceResolver.map(request, "/test/to/path")).thenReturn("/test/to/path");
+
when(request.getScheme()).thenReturn("https");
when(request.getServerName()).thenReturn("example.com");
when(request.getServerPort()).thenReturn(443);
@@ -101,6 +109,59 @@ public class SlingUriBuilderTest {
assertEquals("https://example.com/test/to/path.sel1.sel2.html/suffix/path?par1=val1", testUri.toString());
}
+ /**
+ * SLING-11347 verify that mapped path remains mapped in the new SlingUri
+ */
+ @Test
+ public void testCreateFromRequestWithMappedPath() {
+
+ when(request.getResourceResolver()).thenReturn(resourceResolver);
+ // simulate the resourcePath not already under /content so ResourceResolver#resolve would
+ // change the result
+ when(request.getPathInfo()).thenReturn("/test/to/path");
+ when(resourceResolver.map(request, "/content/test/to/path")).thenReturn("/test/to/path");
+
+ when(request.getScheme()).thenReturn("https");
+ when(request.getServerName()).thenReturn("example.com");
+ when(request.getServerPort()).thenReturn(443);
+ when(request.getQueryString()).thenReturn("par1=val1");
+ // simulate the ResourceResolver#resolve switching to a resource under /content
+ when(requestPathInfo.getResourcePath()).thenReturn("/content/test/to/path");
+ when(requestPathInfo.getSelectors()).thenReturn(new String[] { "sel1", "sel2" });
+ when(requestPathInfo.getExtension()).thenReturn("html");
+ when(requestPathInfo.getSuffix()).thenReturn("/suffix/path");
+
+ SlingUri testUri = SlingUriBuilder.createFrom(request).build();
+
+ assertEquals("https://example.com/test/to/path.sel1.sel2.html/suffix/path?par1=val1", testUri.toString());
+ }
+
+ /**
+ * SLING-11347 verify that not-mapped path remains not-mapped in the new SlingUri
+ */
+ @Test
+ public void testCreateFromRequestWithNotMappedPath() {
+
+ when(request.getResourceResolver()).thenReturn(resourceResolver);
+ // simulate the resourcePath already under /content so ResourceResolver#resolve would
+ // not change the result
+ when(request.getPathInfo()).thenReturn("/content/test/to/path");
+ when(resourceResolver.map(request, "/content/test/to/path")).thenReturn("/test/to/path");
+
+ when(request.getScheme()).thenReturn("https");
+ when(request.getServerName()).thenReturn("example.com");
+ when(request.getServerPort()).thenReturn(443);
+ when(request.getQueryString()).thenReturn("par1=val1");
+ when(requestPathInfo.getResourcePath()).thenReturn("/content/test/to/path");
+ when(requestPathInfo.getSelectors()).thenReturn(new String[] { "sel1", "sel2" });
+ when(requestPathInfo.getExtension()).thenReturn("html");
+ when(requestPathInfo.getSuffix()).thenReturn("/suffix/path");
+
+ SlingUri testUri = SlingUriBuilder.createFrom(request).build();
+
+ assertEquals("https://example.com/content/test/to/path.sel1.sel2.html/suffix/path?par1=val1", testUri.toString());
+ }
+
@Test
public void testCreateFromResource() {
@@ -128,6 +189,10 @@ public class SlingUriBuilderTest {
@Test
public void testCreateFromPath() {
+ // to satisfy that the return of this call is @NotNull
+ when(request.getResourceResolver()).thenReturn(resourceResolver);
+ when(resourceResolver.map(request, "/test/to/path")).thenReturn("/test/to/path");
+
when(request.getScheme()).thenReturn("https");
when(request.getServerName()).thenReturn("example.com");
when(request.getServerPort()).thenReturn(443);