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

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

commit 62cb954c6d71809ca961ca04c4aa4b37c0819056
Author: Julian Reschke <[email protected]>
AuthorDate: Mon Nov 23 10:23:38 2026 +0100

    SLING-13288: logging of 'invalid' vanity paths
---
 .../impl/mapping/VanityPathHandler.java            | 52 +++++++++++++++++++---
 .../impl/mapping/VanityPathMapEntriesTest.java     | 41 ++++++++++++++---
 2 files changed, 81 insertions(+), 12 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/resourceresolver/impl/mapping/VanityPathHandler.java
 
b/src/main/java/org/apache/sling/resourceresolver/impl/mapping/VanityPathHandler.java
index 892cac91..7023e1f0 100644
--- 
a/src/main/java/org/apache/sling/resourceresolver/impl/mapping/VanityPathHandler.java
+++ 
b/src/main/java/org/apache/sling/resourceresolver/impl/mapping/VanityPathHandler.java
@@ -44,6 +44,7 @@ import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.resource.ResourceUtil;
 import org.apache.sling.api.resource.ValueMap;
 import org.apache.sling.api.resource.path.Path;
+import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -80,6 +81,9 @@ public class VanityPathHandler {
 
     private final Logger log = 
LoggerFactory.getLogger(VanityPathHandler.class);
 
+    // keep track of some dubious vanityPaths for diagnostics (thus 
size-limited)
+    private static final int MAX_REPORT_DUBIOUS_VANITY_PATHS = 50;
+
     private final MapConfigurationProvider factory;
     private byte[] vanityBloomFilter;
 
@@ -194,10 +198,12 @@ public class VanityPathHandler {
             try (ResourceResolver resolver =
                     
factory.getServiceResourceResolver(factory.getServiceUserAuthenticationInfo("mapping")))
 {
 
+                List<String> dubiousPaths = new ArrayList<>();
+
                 StopWatch sw = StopWatch.createStarted();
                 log.debug("vanity path initialization - start");
 
-                vanityTargets = loadVanityPaths(resolver);
+                vanityTargets = loadVanityPaths(resolver, dubiousPaths);
 
                 // process pending events
                 VanityPathHandler.this.drain.accept("draining vanity path 
event queue (during cache initialization)");
@@ -229,7 +235,7 @@ public class VanityPathHandler {
 
         boolean updateTheCache = isAllVanityPathEntriesCached()
                 || vanityCounter.longValue() < 
this.factory.getMaxCachedVanityPathEntries();
-        return null != loadVanityPath(resource, resolveMapsMap, vanityTargets, 
updateTheCache, true);
+        return null != loadVanityPath(resource, resolveMapsMap, vanityTargets, 
updateTheCache, true, new ArrayList<>());
     }
 
     private String getMapEntryRedirect(final MapEntry mapEntry) {
@@ -411,11 +417,11 @@ public class VanityPathHandler {
                             && 
(this.factory.isMaxCachedVanityPathEntriesStartup()
                                     || this.isAllVanityPathEntriesCached()
                                     || vanityCounter.longValue() < 
this.factory.getMaxCachedVanityPathEntries())) {
-                        loadVanityPath(resource, resolveMapsMap, 
vanityTargets, true, true);
+                        loadVanityPath(resource, resolveMapsMap, 
vanityTargets, true, true, new ArrayList<>());
                         entryMap = resolveMapsMap;
                     } else {
                         final Map<String, List<String>> targetPaths = new 
HashMap<>();
-                        loadVanityPath(resource, entryMap, targetPaths, true, 
false);
+                        loadVanityPath(resource, entryMap, targetPaths, true, 
false, new ArrayList<>());
                     }
                 }
             }
@@ -462,11 +468,27 @@ public class VanityPathHandler {
         return true;
     }
 
+    private String diagnoseDubiousVanityPath(String resource, String path) {
+        String reason = "";
+        if (path == null) {
+            reason = "null path";
+        } else if (!path.startsWith("/")) {
+            reason = "not an absolute path";
+        } else if (path.contains("//")) {
+            reason = "empty path segment";
+        }
+
+        return reason.isEmpty() ? "" : String.format("'%s', value '%s': %s", 
resource, path, reason);
+    }
+
     /**
      * Load vanity paths - search for all nodes (except under /jcr:system)
      * having a sling:vanityPath property
+     * <p>
+     * Also collect diagnostics about vanity paths that appear to be broken.
      */
-    private Map<String, List<String>> loadVanityPaths(ResourceResolver 
resolver) {
+    private Map<String, List<String>> loadVanityPaths(
+            @NotNull ResourceResolver resolver, @NotNull List<String> 
dubiousVanityPaths) {
         final Map<String, List<String>> targetPaths = new 
ConcurrentHashMap<>();
         final String baseQueryString =
                 "SELECT [sling:vanityPath], [sling:redirect], 
[sling:redirectStatus]" + " FROM [nt:base]" + " WHERE "
@@ -497,7 +519,7 @@ public class VanityPathHandler {
                 countInScope += 1;
                 final boolean addToCache = isAllVanityPathEntriesCached()
                         || vanityCounter.longValue() < 
this.factory.getMaxCachedVanityPathEntries();
-                loadVanityPath(resource, resolveMapsMap, targetPaths, 
addToCache, true);
+                loadVanityPath(resource, resolveMapsMap, targetPaths, 
addToCache, true, dubiousVanityPaths);
             }
         }
 
@@ -522,6 +544,14 @@ public class VanityPathHandler {
             }
         }
 
+        // warn about dubious vanity paths
+
+        if (dubiousVanityPaths.size() >= MAX_REPORT_DUBIOUS_VANITY_PATHS) {
+            log.warn("There are {} dubious vanity paths; excerpt: {}", 
dubiousVanityPaths.size(), dubiousVanityPaths);
+        } else if (!dubiousVanityPaths.isEmpty()) {
+            log.warn("There are {} dubious vanity paths: {}", 
dubiousVanityPaths.size(), dubiousVanityPaths);
+        }
+
         this.vanityResourcesOnStartup.set(count);
 
         return targetPaths;
@@ -544,7 +574,8 @@ public class VanityPathHandler {
             final Map<String, List<MapEntry>> entryMap,
             final Map<String, List<String>> targetPaths,
             boolean addToCache,
-            boolean updateCounter) {
+            boolean updateCounter,
+            final List<String> dubiousVanityPaths) {
 
         if (!isValidVanityPath(resource.getPath())) {
             return null;
@@ -562,6 +593,13 @@ public class VanityPathHandler {
         }
 
         for (final String pVanityPath : pVanityPaths) {
+
+            // collect vanity path values that seem to be broken
+            final String diagnostics = 
diagnoseDubiousVanityPath(resource.getPath(), pVanityPath);
+            if (!diagnostics.isEmpty() && dubiousVanityPaths.size() <= 
MAX_REPORT_DUBIOUS_VANITY_PATHS) {
+                dubiousVanityPaths.add(diagnostics);
+            }
+
             final String[] result = 
this.getVanityPathDefinition(resource.getPath(), pVanityPath);
             if (result != null) {
                 // redirect target is the node providing the sling:vanityPath
diff --git 
a/src/test/java/org/apache/sling/resourceresolver/impl/mapping/VanityPathMapEntriesTest.java
 
b/src/test/java/org/apache/sling/resourceresolver/impl/mapping/VanityPathMapEntriesTest.java
index 67cdcaf0..25b788f8 100644
--- 
a/src/test/java/org/apache/sling/resourceresolver/impl/mapping/VanityPathMapEntriesTest.java
+++ 
b/src/test/java/org/apache/sling/resourceresolver/impl/mapping/VanityPathMapEntriesTest.java
@@ -237,11 +237,12 @@ public class VanityPathMapEntriesTest extends 
AbstractMappingMapEntriesTest {
         return (Boolean) method.invoke(mapEntries, new 
MapEntries.ChangeContext(path, false, true), bool);
     }
 
-    private static void loadVanityPaths(MapEntries mapEntries, 
ResourceResolver resourceResolver)
+    private static void loadVanityPaths(MapEntries mapEntries, 
ResourceResolver resourceResolver, List<String> dubious)
             throws IllegalAccessException, NoSuchMethodException, 
InvocationTargetException {
-        Method method = 
VanityPathHandler.class.getDeclaredMethod("loadVanityPaths", 
ResourceResolver.class);
+        Method method =
+                VanityPathHandler.class.getDeclaredMethod("loadVanityPaths", 
ResourceResolver.class, List.class);
         method.setAccessible(true);
-        method.invoke(mapEntries.vph, resourceResolver);
+        method.invoke(mapEntries.vph, resourceResolver, dubious);
     }
 
     @Override
@@ -335,6 +336,36 @@ public class VanityPathMapEntriesTest extends 
AbstractMappingMapEntriesTest {
                 });
     }
 
+    @Test
+    public void test_simple_dubious_vanity_path() {
+        // a dubious vanity path
+        String vanityPath = "empty//path//segment";
+
+        String containerName = "foo";
+        String childName = "child";
+        String oneMore = "one-more";
+        prepareMapEntriesForVanityPath(false, false, containerName, childName, 
oneMore, vanityPath);
+
+        try (TestLogger logger = TestLogger.create(VanityPathHandler.class)
+                .contains("dubious vanity paths")
+                .start()) {
+            initializeVanityPaths();
+            List<String> logs = logger.stopAndGetLogs();
+            // 1 because both resources share the same broken vanity path
+            assertEquals(1, logs.size());
+            // dubious vanity path results in resource name in root with "-" 
stripped??
+            assertTrue(logs.get(0).contains("onemore"));
+        }
+
+        Map<String, List<String>> vanityMap = 
mapEntries.getVanityPathMappings();
+        assertNotNull(vanityMap);
+
+        List<String> paths = vanityMap.get("/" + containerName + "/" + 
oneMore);
+        assertEquals(1, paths.size());
+        // dubious vanity path results in resource name in root with "-" 
stripped??
+        assertEquals("/" + vanityPath + "/onemore", paths.get(0));
+    }
+
     // see SLING-12620
     @Test
     public void test_simple_vanity_path_support_with_null_parent() {
@@ -1121,7 +1152,7 @@ public class VanityPathMapEntriesTest extends 
AbstractMappingMapEntriesTest {
                     }
                 });
 
-        loadVanityPaths(mapEntries, resourceResolver);
+        loadVanityPaths(mapEntries, resourceResolver, new ArrayList<>());
 
         assertEquals(2, getVanityCounter(mapEntries).longValue());
     }
@@ -1143,7 +1174,7 @@ public class VanityPathMapEntriesTest extends 
AbstractMappingMapEntriesTest {
                     }
                 });
 
-        loadVanityPaths(mapEntries, resourceResolver);
+        loadVanityPaths(mapEntries, resourceResolver, new ArrayList<>());
 
         assertEquals(2, getVanityCounter(mapEntries).longValue());
     }

Reply via email to