This is an automated email from the ASF dual-hosted git repository.

reschke pushed a commit to branch 1.x
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-resourceresolver.git


The following commit(s) were added to refs/heads/1.x by this push:
     new 5bd4683b Revert "SLING-12787: ResourceResolver: alias refactoring - 
use Resource instead of Path when getting aliases (#178)"
5bd4683b is described below

commit 5bd4683b48d4f77ee662b403fed89a11877c3066
Author: Julian Reschke <[email protected]>
AuthorDate: Fri May 30 16:05:24 2025 +0100

    Revert "SLING-12787: ResourceResolver: alias refactoring - use Resource 
instead of Path when getting aliases (#178)"
    
    This reverts commit 2457aaa4c0b97ed3d1100b50463bcc9aa0b89f92.
---
 .../impl/ResourceResolverImpl.java                 |  6 +++
 .../impl/mapping/ResourceMapperImpl.java           | 45 +++++++++-------------
 .../impl/MockedResourceResolverImplTest.java       | 12 +-----
 .../impl/ResourceDecorationTest.java               |  2 +-
 4 files changed, 26 insertions(+), 39 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverImpl.java
 
b/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverImpl.java
index 5056d89a..2c196b2c 100644
--- 
a/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverImpl.java
+++ 
b/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverImpl.java
@@ -84,6 +84,12 @@ public class ResourceResolverImpl extends SlingAdaptable 
implements ResourceReso
 
     public static final String PROP_ALIAS = "sling:alias";
 
+    // The suffix of a resource being a content node of some parent
+    // such as nt:file. The slash is included to prevent false
+    // positives for the String.endsWith check for names like
+    // "xyzjcr:content"
+    public static final String JCR_CONTENT_LEAF = "/jcr:content";
+
     protected static final String PARENT_RT_CACHEKEY = 
ResourceResolverImpl.class.getName() + ".PARENT_RT";
 
     /** The factory which created this resource resolver. */
diff --git 
a/src/main/java/org/apache/sling/resourceresolver/impl/mapping/ResourceMapperImpl.java
 
b/src/main/java/org/apache/sling/resourceresolver/impl/mapping/ResourceMapperImpl.java
index ec6bfc75..6a7576e6 100644
--- 
a/src/main/java/org/apache/sling/resourceresolver/impl/mapping/ResourceMapperImpl.java
+++ 
b/src/main/java/org/apache/sling/resourceresolver/impl/mapping/ResourceMapperImpl.java
@@ -28,6 +28,7 @@ import java.util.List;
 import java.util.function.UnaryOperator;
 
 import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceUtil;
 import org.apache.sling.api.resource.mapping.ResourceMapper;
 import org.apache.sling.resourceresolver.impl.JcrNamespaceMangler;
 import org.apache.sling.resourceresolver.impl.ResourceResolverImpl;
@@ -35,7 +36,6 @@ import 
org.apache.sling.resourceresolver.impl.helper.ResourceDecoratorTracker;
 import org.apache.sling.resourceresolver.impl.helper.URI;
 import org.apache.sling.resourceresolver.impl.helper.URIException;
 import org.apache.sling.resourceresolver.impl.params.ParsedParameters;
-import org.jetbrains.annotations.NotNull;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -211,44 +211,35 @@ public class ResourceMapperImpl implements ResourceMapper 
{
         return mappedPaths;
     }
 
-    /*
-     * Populate a {@linkplain PathGenerator} based on the aliases of this 
resource, plus all ancestors
-     * @param resource the resource from which to start
-     * @param pathGenerator path generator to populate
-     */
-    private void resolveAliases(@NotNull Resource resource, @NotNull 
PathGenerator pathGenerator) {
-        Resource current = resource;
-
-        while (current != null) {
-            String name = current.getName();
-
-            // read aliases only if it's not a jcr:content resource
-            Collection<String> aliases = name.equals("jcr:content") ? 
Collections.emptyList() : readAliases(current);
+    private void resolveAliases(Resource res, PathGenerator pathBuilder) {
+        String path = res.getPath();
 
+        while (path != null) {
+            Collection<String> aliases = Collections.emptyList();
+            // read alias only if we can read the resources and it's not a 
jcr:content leaf
+            if (!path.endsWith(ResourceResolverImpl.JCR_CONTENT_LEAF)) {
+                aliases = readAliases(path);
+            }
             // build the path from the name segments or aliases
-            pathGenerator.insertSegment(aliases, name);
-
-            // traverse up
-            current = current.getParent();
-
-            // reached the root? -> stop traversing up
-            if (current != null && current.getParent() == null) {
-                current = null;
+            pathBuilder.insertSegment(aliases, ResourceUtil.getName(path));
+            path = ResourceUtil.getParent(path);
+            if ("/".equals(path)) {
+                path = null;
             }
         }
     }
 
     /**
      * Resolve the aliases for the given resource by a lookup in MapEntries
-     * @param resource resource for which to lookup aliases
+     * @param path path for which to lookup aliases
      * @return collection of aliases for that resource
      */
-    private @NotNull Collection<String> readAliases(@NotNull Resource 
resource) {
-        Resource parent = resource.getParent();
-        if (parent == null) {
+    private Collection<String> readAliases(String path) {
+        String parentPath = ResourceUtil.getParent(path);
+        if (parentPath == null) {
             return Collections.emptyList();
         } else {
-            return 
mapEntries.getAliasMap(parent).getOrDefault(resource.getName(), 
Collections.emptyList());
+            return 
mapEntries.getAliasMap(parentPath).getOrDefault(ResourceUtil.getName(path), 
Collections.emptyList());
         }
     }
 
diff --git 
a/src/test/java/org/apache/sling/resourceresolver/impl/MockedResourceResolverImplTest.java
 
b/src/test/java/org/apache/sling/resourceresolver/impl/MockedResourceResolverImplTest.java
index 57aad76e..0f9dfc6f 100644
--- 
a/src/test/java/org/apache/sling/resourceresolver/impl/MockedResourceResolverImplTest.java
+++ 
b/src/test/java/org/apache/sling/resourceresolver/impl/MockedResourceResolverImplTest.java
@@ -40,7 +40,6 @@ import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceMetadata;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.resource.ResourceResolverFactory;
-import org.apache.sling.api.resource.ResourceUtil;
 import org.apache.sling.api.resource.ValueMap;
 import org.apache.sling.api.wrappers.ValueMapDecorator;
 import org.apache.sling.resourceresolver.impl.mapping.MapEntries;
@@ -500,7 +499,7 @@ public class MockedResourceResolverImplTest {
     }
 
     /**
-     * Build a resource with parent, path, children and resource resolver.
+     * Build a resource with path, children and resource resolver.
      * @param fullpath
      * @param children
      * @param resourceResolver
@@ -513,18 +512,9 @@ public class MockedResourceResolverImplTest {
             ResourceResolver resourceResolver,
             ResourceProvider<?> provider,
             String... properties) {
-
-        // build a mocked parent resource so that getParent() can return 
something meaningful (it is null when we are
-        // already at root level)
-        Resource parentResource = fullpath == null || "/".equals(fullpath)
-                ? null
-                : buildResource(
-                        ResourceUtil.getParent(fullpath), 
Collections.emptyList(), resourceResolver, provider, null);
-
         Resource resource = mock(Resource.class);
         Mockito.when(resource.getName()).thenReturn(getResourceName(fullpath));
         Mockito.when(resource.getPath()).thenReturn(fullpath);
-        Mockito.when(resource.getParent()).thenReturn(parentResource);
         ResourceMetadata resourceMetadata = new ResourceMetadata();
         
Mockito.when(resource.getResourceMetadata()).thenReturn(resourceMetadata);
         Mockito.when(resource.listChildren()).thenReturn(children.iterator());
diff --git 
a/src/test/java/org/apache/sling/resourceresolver/impl/ResourceDecorationTest.java
 
b/src/test/java/org/apache/sling/resourceresolver/impl/ResourceDecorationTest.java
index 11b7f660..c85ba4f5 100644
--- 
a/src/test/java/org/apache/sling/resourceresolver/impl/ResourceDecorationTest.java
+++ 
b/src/test/java/org/apache/sling/resourceresolver/impl/ResourceDecorationTest.java
@@ -33,7 +33,7 @@ import static org.junit.Assert.assertTrue;
 public class ResourceDecorationTest extends ResourceDecoratorTestBase {
 
     private static final String DECORATED_NAME = "decorated";
-    private static final String DECORATED_PATH = "/decorated";
+    private static final String DECORATED_PATH = "/decoratedPath";
 
     /** Wrap any resource so that its name is DECORATED_NAME */
     protected Resource wrapResourceForTest(Resource resource) {

Reply via email to