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

reschke 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 4907a785 SLING-12759: ResourceResolver: push 'non-optimized' alias 
handling from ResourceResolverImpl into AliasHandler (#177)
4907a785 is described below

commit 4907a7853db8f90134ee6f13f4bdeb07970e32e9
Author: Julian Reschke <[email protected]>
AuthorDate: Mon May 19 14:38:33 2025 +0200

    SLING-12759: ResourceResolver: push 'non-optimized' alias handling from 
ResourceResolverImpl into AliasHandler (#177)
    
    * SLING-12759: ResourceResolver: push 'non-optimized' alias handling from 
ResourceResolverImpl into AliasHandler
    
    * SLING-12759: ResourceResolver: push 'non-optimized' alias handling from 
ResourceResolverImpl into AliasHandler - allow passing a Resource intead of a 
path string
---
 .../impl/ResourceResolverImpl.java                 | 65 ++++++----------------
 .../impl/mapping/AliasHandler.java                 | 11 +++-
 .../resourceresolver/impl/mapping/MapEntries.java  |  7 ++-
 .../impl/mapping/MapEntriesHandler.java            |  9 +++
 4 files changed, 41 insertions(+), 51 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 a123f83c..0913240e 100644
--- 
a/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverImpl.java
+++ 
b/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverImpl.java
@@ -927,55 +927,26 @@ public class ResourceResolverImpl extends SlingAdaptable 
implements ResourceReso
 
         // we do not have a child with the exact name, so we look for
         // a child, whose alias matches the childName
-        if (factory.getMapEntries().isOptimizeAliasResolutionEnabled()) {
-            final String parentPath = parent.getPath();
-            logger.debug(
-                    "getChildInternal: Optimize Alias Resolution is Enabled, 
looking up {} in {}",
-                    childName,
-                    parentPath);
-
-            // optimized alias resolution: aliases are cached by MapEntries
-            final Optional<String> aliasedResourceName =
-                    
factory.getMapEntries().getAliasMap(parentPath).entrySet().stream()
-                            .filter(e -> e.getValue().contains(childName))
-                            .findFirst()
-                            .map(Map.Entry::getKey);
-            if (aliasedResourceName.isPresent()) {
-                // we know that MapEntries already has checked for valid 
aliases
-                final String aliasPath = parentPath + '/' + 
aliasedResourceName.get();
-                final Resource aliasedChild =
-                        getAbsoluteResourceInternal(parent, 
ResourceUtil.normalize(aliasPath), EMPTY_PARAMETERS, true);
-                logger.debug("getChildInternal: Found Resource {} with alias 
{} to use", aliasedChild, childName);
-                return aliasedChild;
-            }
+        final String parentPath = parent.getPath();
+        logger.debug("getChildInternal: looking up {} in {}", childName, 
parentPath);
+
+        final Optional<String> aliasedResourceName = 
factory.getMapEntries().getAliasMap(parent).entrySet().stream()
+                .filter(e -> e.getValue().contains(childName))
+                .findFirst()
+                .map(Map.Entry::getKey);
+
+        if (aliasedResourceName.isPresent()) {
+            // we know that MapEntries already has checked for valid aliases
+            final String aliasPath = parentPath + '/' + 
aliasedResourceName.get();
+            final Resource aliasedChild =
+                    getAbsoluteResourceInternal(parent, 
ResourceUtil.normalize(aliasPath), EMPTY_PARAMETERS, true);
+            logger.debug("getChildInternal: Found Resource {} with alias {} to 
use", aliasedChild, childName);
+            return aliasedChild;
         } else {
-            if (this.factory.isOptimizeAliasResolutionEnabled()) {
-                this.factory.getMapEntries().logDisableAliasOptimization();
-            }
-            logger.debug("getChildInternal: Optimize Alias Resolution is 
Disabled");
-            final Iterator<Resource> children = listChildren(parent);
-            while (children.hasNext()) {
-                child = children.next();
-                if (!child.getPath().endsWith(JCR_CONTENT_LEAF)) {
-                    final String[] aliases = 
ResourceResolverControl.getProperty(child, PROP_ALIAS, String[].class);
-                    if (aliases != null) {
-                        for (final String alias : aliases) {
-                            if (childName.equals(alias)) {
-                                logger.debug(
-                                        "getChildInternal: Found Resource {} 
with alias {} to use", child, childName);
-                                final Resource aliasedChild = 
getAbsoluteResourceInternal(
-                                        parent, 
ResourceUtil.normalize(child.getPath()), EMPTY_PARAMETERS, true);
-                                return aliasedChild;
-                            }
-                        }
-                    }
-                }
-            }
+            // no match for the childName found
+            logger.debug("getChildInternal: Resource {} has no child {}", 
parent, childName);
+            return null;
         }
-
-        // no match for the childName found
-        logger.debug("getChildInternal: Resource {} has no child {}", parent, 
childName);
-        return null;
     }
 
     /**
diff --git 
a/src/main/java/org/apache/sling/resourceresolver/impl/mapping/AliasHandler.java
 
b/src/main/java/org/apache/sling/resourceresolver/impl/mapping/AliasHandler.java
index af5cb29f..da158284 100644
--- 
a/src/main/java/org/apache/sling/resourceresolver/impl/mapping/AliasHandler.java
+++ 
b/src/main/java/org/apache/sling/resourceresolver/impl/mapping/AliasHandler.java
@@ -327,11 +327,17 @@ class AliasHandler {
         return result != null ? result : Collections.emptyMap();
     }
 
+    public @NotNull Map<String, Collection<String>> getAliasMap(@NotNull 
Resource parent) {
+        Map<String, Collection<String>> result = this.aliasMapsMap != 
UNITIALIZED_MAP
+                ? getAliasMapFromCache(parent.getPath())
+                : getAliasMapFromRepo(parent);
+        return result != null ? result : Collections.emptyMap();
+    }
+
     private @Nullable Map<String, Collection<String>> 
getAliasMapFromCache(@Nullable String parentPath) {
         return aliasMapsMap.get(parentPath);
     }
 
-    // TODO: there's an opportunity for optimization when the caller already 
has a Resource
     private @Nullable Map<String, Collection<String>> 
getAliasMapFromRepo(@Nullable String parentPath) {
 
         if (parentPath == null) {
@@ -340,8 +346,7 @@ class AliasHandler {
             try (ResourceResolver resolver =
                     
factory.getServiceResourceResolver(factory.getServiceUserAuthenticationInfo(SERVICE_USER)))
 {
 
-                Resource parent = resolver.getResource(parentPath);
-                return getAliasMapFromRepo(parent);
+                return getAliasMapFromRepo(resolver.getResource(parentPath));
             } catch (LoginException ex) {
                 log.error("Could not obtain resolver to resolve any aliases 
from repository", ex);
                 return null;
diff --git 
a/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java 
b/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java
index ab9f8fc5..c8e47673 100644
--- 
a/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java
+++ 
b/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java
@@ -367,10 +367,15 @@ public class MapEntries implements MapEntriesHandler, 
ResourceChangeListener, Ex
     }
 
     @Override
-    public @NotNull Map<String, Collection<String>> getAliasMap(final @NotNull 
String parentPath) {
+    public @NotNull Map<String, Collection<String>> getAliasMap(@NotNull 
String parentPath) {
         return ah.getAliasMap(parentPath);
     }
 
+    @Override
+    public @NotNull Map<String, Collection<String>> getAliasMap(@NotNull 
Resource parent) {
+        return ah.getAliasMap(parent);
+    }
+
     /**
      * Refresh the resource resolver if not already done
      * @param resolverRefreshed Boolean flag containing the state if the 
resolver
diff --git 
a/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntriesHandler.java
 
b/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntriesHandler.java
index 12bbf893..64ac5733 100644
--- 
a/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntriesHandler.java
+++ 
b/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntriesHandler.java
@@ -24,6 +24,7 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
+import org.apache.sling.api.resource.Resource;
 import org.jetbrains.annotations.NotNull;
 
 /**
@@ -61,6 +62,11 @@ public interface MapEntriesHandler {
             return Collections.emptyMap();
         }
 
+        @Override
+        public @NotNull Map<String, Collection<String>> getAliasMap(@NotNull 
Resource parent) {
+            return Collections.emptyMap();
+        }
+
         @Override
         public Map<String, List<String>> getVanityPathMappings() {
             return Collections.emptyMap();
@@ -100,6 +106,9 @@ public interface MapEntriesHandler {
     @NotNull
     Map<String, Collection<String>> getAliasMap(@NotNull String parentPath);
 
+    @NotNull
+    Map<String, Collection<String>> getAliasMap(@NotNull Resource parent);
+
     /**
      * Creates an iterator over the possibly applicable mapping entries for 
resolving a resource
      *

Reply via email to