You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by re...@apache.org on 2025/01/14 10:35:28 UTC

(sling-org-apache-sling-resourceresolver) branch SLING-12615 updated: Resource Resolver: refactor MapEntries - extract vanity path handling - isValidVanityPath

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

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


The following commit(s) were added to refs/heads/SLING-12615 by this push:
     new 9ec27bb  Resource Resolver: refactor MapEntries - extract vanity path handling - isValidVanityPath
9ec27bb is described below

commit 9ec27bb8aebae2673ae56c0cff998fbe188e8134
Author: Julian Reschke <re...@apache.org>
AuthorDate: Tue Jan 14 11:35:19 2025 +0100

    Resource Resolver: refactor MapEntries - extract vanity path handling - isValidVanityPath
---
 .../resourceresolver/impl/mapping/MapEntries.java  | 38 +-----------
 .../impl/mapping/PagedQueryIterator.java           |  2 +-
 .../impl/mapping/VanityPathHandler.java            | 46 +++++++++++++-
 .../impl/mapping/MapEntriesTest.java               | 10 ---
 .../impl/mapping/VanityPathHandlerTest.java        | 72 ++++++++++++++++++++++
 5 files changed, 119 insertions(+), 49 deletions(-)

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 a20c9c1..39b860a 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
@@ -32,7 +32,6 @@ import org.apache.sling.api.resource.observation.ResourceChangeListener;
 import org.apache.sling.api.resource.path.Path;
 import org.apache.sling.resourceresolver.impl.ResourceResolverImpl;
 import org.apache.sling.resourceresolver.impl.ResourceResolverMetrics;
-import org.apache.sling.resourceresolver.impl.mapping.MapConfigurationProvider.VanityPathConfig;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 import org.osgi.framework.BundleContext;
@@ -417,7 +416,7 @@ public class MapEntries implements
     }
 
     private boolean updateResource(final String path, final AtomicBoolean resolverRefreshed) {
-        final boolean isValidVanityPath =  this.isValidVanityPath(path);
+        final boolean isValidVanityPath =  this.vanityPathHandler.isValidVanityPath(path);
         if ( this.useOptimizeAliasResolution || isValidVanityPath) {
             this.initializing.lock();
 
@@ -1002,39 +1001,6 @@ public class MapEntries implements
         return entryMap;
     }
 
-    /**
-     * Check if the path is a valid vanity path
-     * @param path The resource path to check
-     * @return {@code true} if this is valid, {@code false} otherwise
-     */
-    private boolean isValidVanityPath(final String path){
-        if (path == null) {
-            throw new IllegalArgumentException("Unexpected null path");
-        }
-
-        // ignore system tree
-        if (path.startsWith(JCR_SYSTEM_PREFIX)) {
-            log.debug("isValidVanityPath: not valid {}", path);
-            return false;
-        }
-
-        // check allow/deny list
-        if ( this.factory.getVanityPathConfig() != null ) {
-            boolean allowed = false;
-            for(final VanityPathConfig config : this.factory.getVanityPathConfig()) {
-                if ( path.startsWith(config.prefix) ) {
-                    allowed = !config.isExclude;
-                    break;
-                }
-            }
-            if ( !allowed ) {
-                log.debug("isValidVanityPath: not valid as not in allow list {}", path);
-                return false;
-            }
-        }
-        return true;
-    }
-
     private String getActualContentPath(final String path){
         final String checkPath;
         if ( path.endsWith(JCR_CONTENT_SUFFIX) ) {
@@ -1418,7 +1384,7 @@ public class MapEntries implements
      */
     private String loadVanityPath(final Resource resource, final Map<String, List<MapEntry>> entryMap, final Map <String, List<String>> targetPaths, boolean addToCache) {
 
-        if (!isValidVanityPath(resource.getPath())) {
+        if (!this.vanityPathHandler.isValidVanityPath(resource.getPath())) {
             return null;
         }
 
diff --git a/src/main/java/org/apache/sling/resourceresolver/impl/mapping/PagedQueryIterator.java b/src/main/java/org/apache/sling/resourceresolver/impl/mapping/PagedQueryIterator.java
index 466d16c..e7b07bd 100644
--- a/src/main/java/org/apache/sling/resourceresolver/impl/mapping/PagedQueryIterator.java
+++ b/src/main/java/org/apache/sling/resourceresolver/impl/mapping/PagedQueryIterator.java
@@ -33,7 +33,7 @@ import java.util.concurrent.TimeUnit;
  */
 public class PagedQueryIterator implements Iterator<Resource> {
 
-    /** default log, using MapEntries for backwards compatibility */
+    /** default log */
     private final Logger log = LoggerFactory.getLogger(PagedQueryIterator.class);
 
     private final String subject;
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 4b6150a..5284102 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
@@ -18,17 +18,27 @@
  */
 package org.apache.sling.resourceresolver.impl.mapping;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Objects;
+
 /**
  * Handles loading and caching of vanity paths.
  */
 public class VanityPathHandler {
 
+    /** default log */
+    private final Logger log = LoggerFactory.getLogger(VanityPathHandler.class);
+
     private static final int VANITY_BLOOM_FILTER_MAX_ENTRIES = 10000000;
 
     private volatile MapConfigurationProvider factory;
 
     private final byte[] bloomFilter;
 
+    private static final String JCR_SYSTEM_PREFIX = "/jcr:system/";
+
     /**
      * @param factory {@link MapConfigurationProvider}
      */
@@ -38,11 +48,43 @@ public class VanityPathHandler {
                 this.factory.getVanityBloomFilterMaxBytes());
     }
 
-    public boolean cacheProbablyContains(String path) {
+    /**
+     * Check if the path is a valid vanity path
+     * @param path The resource path to check
+     * @return {@code true} if this is valid, {@code false} otherwise
+     */
+    boolean isValidVanityPath(final String path){
+
+        Objects.requireNonNull(path, "Unexpected null path");
+
+        // ignore system tree
+        if (path.startsWith(JCR_SYSTEM_PREFIX)) {
+            log.debug("isValidVanityPath: not valid {}", path);
+            return false;
+        }
+
+        // check allow/deny list
+        if (this.factory.getVanityPathConfig() != null) {
+            boolean allowed = false;
+            for (final MapConfigurationProvider.VanityPathConfig config : this.factory.getVanityPathConfig()) {
+                if (path.startsWith(config.prefix)) {
+                    allowed = !config.isExclude;
+                    break;
+                }
+            }
+            if (!allowed) {
+                log.debug("isValidVanityPath: not valid as not in allow list {}", path);
+                return false;
+            }
+        }
+        return true;
+    }
+
+    boolean cacheProbablyContains(String path) {
         return BloomFilterUtils.probablyContains(this.bloomFilter, path);
     }
 
-    public void cacheWillProbablyContain(String path) {
+    void cacheWillProbablyContain(String path) {
         BloomFilterUtils.add(this.bloomFilter, path);
     }
  }
diff --git a/src/test/java/org/apache/sling/resourceresolver/impl/mapping/MapEntriesTest.java b/src/test/java/org/apache/sling/resourceresolver/impl/mapping/MapEntriesTest.java
index 75b5e50..0526c16 100644
--- a/src/test/java/org/apache/sling/resourceresolver/impl/mapping/MapEntriesTest.java
+++ b/src/test/java/org/apache/sling/resourceresolver/impl/mapping/MapEntriesTest.java
@@ -1892,16 +1892,6 @@ public class MapEntriesTest extends AbstractMappingMapEntriesTest {
         assertEquals(Collections.emptyMap(), aliasMapEntry);
     }
 
-    @Test
-    public void test_isValidVanityPath() throws Exception {
-        Method method = MapEntries.class.getDeclaredMethod("isValidVanityPath", String.class);
-        method.setAccessible(true);
-
-        assertFalse((Boolean)method.invoke(mapEntries, "/jcr:system/node"));
-
-        assertTrue((Boolean)method.invoke(mapEntries, "/justVanityPath"));
-    }
-
     @Test
     //SLING-4847
     public void test_doNodeAdded1() throws Exception {
diff --git a/src/test/java/org/apache/sling/resourceresolver/impl/mapping/VanityPathHandlerTest.java b/src/test/java/org/apache/sling/resourceresolver/impl/mapping/VanityPathHandlerTest.java
new file mode 100644
index 0000000..b57e087
--- /dev/null
+++ b/src/test/java/org/apache/sling/resourceresolver/impl/mapping/VanityPathHandlerTest.java
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.resourceresolver.impl.mapping;
+
+import org.apache.sling.api.resource.path.Path;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.when;
+
+public class VanityPathHandlerTest extends AbstractMappingMapEntriesTest {
+
+    @Mock
+    private MapConfigurationProvider resourceResolverFactory;
+
+    @Mock
+    private BundleContext bundleContext;
+
+    @Mock
+    private Bundle bundle;
+
+    @SuppressWarnings("unchecked")
+    @Override
+    @Before
+    public void setup() throws Exception {
+        MockitoAnnotations.openMocks(this).close();
+
+        final List<MapConfigurationProvider.VanityPathConfig> configs = List.of
+                (new MapConfigurationProvider.VanityPathConfig("/justVanityPath", false));
+
+        when(bundle.getSymbolicName()).thenReturn("TESTBUNDLE");
+        when(bundleContext.getBundle()).thenReturn(bundle);
+        when(resourceResolverFactory.getServiceResourceResolver(any(Map.class))).thenReturn(resourceResolver);
+        when(resourceResolverFactory.getObservationPaths()).thenReturn(new Path[] { new Path("/") });
+        when(resourceResolverFactory.getMapRoot()).thenReturn(MapEntries.DEFAULT_MAP_ROOT);
+        when(resourceResolverFactory.getVanityPathConfig()).thenReturn(configs);
+    }
+
+    @Test
+    public void test_isValidVanityPath() {
+        VanityPathHandler vph = new VanityPathHandler(resourceResolverFactory);
+        assertFalse(vph.isValidVanityPath("/jcr:system/node"));
+        assertTrue(vph.isValidVanityPath("/justVanityPath"));
+    }
+}