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

bdelacretaz pushed a commit to branch resolver-2.x
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-servlets-resolver.git


The following commit(s) were added to refs/heads/resolver-2.x by this push:
     new f519229  SLING-12739 - Use more specific 
IgnoredServletResourcePredicate interface (#60)
f519229 is described below

commit f5192292b7be4d0adaabb6e39abbc0e1425588ec
Author: Bertrand Delacretaz <[email protected]>
AuthorDate: Fri Jul 11 12:34:12 2025 +0200

    SLING-12739 - Use more specific IgnoredServletResourcePredicate interface 
(#60)
---
 .../api/IgnoredServletResourcePredicate.java       | 36 +++++++++++++
 .../sling/servlets/resolver/api/package-info.java  | 21 ++++++++
 .../resolver/internal/SlingServletResolver.java    | 17 +++----
 .../internal/AbsoluteResourceTypeTest.java         |  6 +--
 .../internal/resourcehiding/ServletHidingTest.java |  8 +--
 .../it/resourcehiding/BadPredicateNameIT.java      | 59 ----------------------
 .../it/resourcehiding/BasicResourceHidingIT.java   |  2 +-
 .../it/resourcehiding/HiddenServletFallbackIT.java |  2 +-
 .../it/resourcehiding/ResourceHidingITBase.java    | 23 +++------
 9 files changed, 81 insertions(+), 93 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/servlets/resolver/api/IgnoredServletResourcePredicate.java
 
b/src/main/java/org/apache/sling/servlets/resolver/api/IgnoredServletResourcePredicate.java
new file mode 100644
index 0000000..41550ab
--- /dev/null
+++ 
b/src/main/java/org/apache/sling/servlets/resolver/api/IgnoredServletResourcePredicate.java
@@ -0,0 +1,36 @@
+/*
+ * 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.servlets.resolver.api;
+
+import java.util.function.Predicate;
+
+import org.apache.sling.api.resource.Resource;
+import org.osgi.annotation.versioning.ConsumerType;
+
+/** Created for https://issues.apache.org/jira/browse/SLING-12739
+ *  to hide specific scripts or servlets from the resolution
+ *  mechanism. Can be used for "soft deprecation" of scripts and
+ *  servlets for example.
+ *
+ *  @returns true if the supplied Resource must be ignored by
+ *  the servlets resolver.
+ */
+@ConsumerType
+public interface IgnoredServletResourcePredicate extends Predicate<Resource> {
+}
diff --git 
a/src/main/java/org/apache/sling/servlets/resolver/api/package-info.java 
b/src/main/java/org/apache/sling/servlets/resolver/api/package-info.java
new file mode 100644
index 0000000..e520d89
--- /dev/null
+++ b/src/main/java/org/apache/sling/servlets/resolver/api/package-info.java
@@ -0,0 +1,21 @@
+/*
+ * 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.
+ */
+@Version("1.0.0")
+package org.apache.sling.servlets.resolver.api;
+import org.osgi.annotation.versioning.Version;
diff --git 
a/src/main/java/org/apache/sling/servlets/resolver/internal/SlingServletResolver.java
 
b/src/main/java/org/apache/sling/servlets/resolver/internal/SlingServletResolver.java
index a54d8bd..12b3b68 100644
--- 
a/src/main/java/org/apache/sling/servlets/resolver/internal/SlingServletResolver.java
+++ 
b/src/main/java/org/apache/sling/servlets/resolver/internal/SlingServletResolver.java
@@ -28,7 +28,6 @@ import java.io.IOException;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.concurrent.atomic.AtomicReference;
-import java.util.function.Predicate;
 
 import javax.servlet.Servlet;
 import javax.servlet.ServletContext;
@@ -55,6 +54,7 @@ import org.apache.sling.api.servlets.OptingServlet;
 import org.apache.sling.api.servlets.ServletResolver;
 import org.apache.sling.api.servlets.ServletResolverConstants;
 import org.apache.sling.serviceusermapping.ServiceUserMapped;
+import org.apache.sling.servlets.resolver.api.IgnoredServletResourcePredicate;
 import 
org.apache.sling.servlets.resolver.internal.defaults.DefaultErrorHandlerServlet;
 import org.apache.sling.servlets.resolver.internal.defaults.DefaultServlet;
 import 
org.apache.sling.servlets.resolver.internal.helper.AbstractResourceCollector;
@@ -134,11 +134,10 @@ public class SlingServletResolver
     private final ThreadLocal<ResourceResolver> perThreadScriptResolver = new 
ThreadLocal<>();
 
     @Reference(
-        target = "(name=sling.servlet.resolver.resource.hiding)",
         policy = ReferencePolicy.DYNAMIC,
         cardinality = ReferenceCardinality.OPTIONAL
     )
-    private volatile Predicate<String> resourceHidingPredicate;
+    private volatile IgnoredServletResourcePredicate ignoredResourcePredicate;
 
     /**
      * The allowed execution paths.
@@ -453,11 +452,11 @@ public class SlingServletResolver
         return res;
     }
 
-    /** @return true if the given Resource is hidden by our 
resourceHidingPredicate */
-    private boolean isHidden(@NotNull Resource r) {
-        final boolean result = r != null && resourceHidingPredicate != null && 
resourceHidingPredicate.test(r.getPath());
+    /** @return true if the IgnoredResourcePredicate is set and returns true 
for the supplied Resource */
+    private boolean ignoreResource(@NotNull Resource r) {
+        final boolean result = r != null && ignoredResourcePredicate != null 
&& ignoredResourcePredicate.test(r);
         if(result && LOGGER.isDebugEnabled()) {
-            LOGGER.debug("Resource hidden by resource hiding predicate: {}", 
r.getPath());
+            LOGGER.debug("IgnoredResourcePredicate causes Resource to be 
ignored: {}", r.getPath());
         }
         return result;
     }
@@ -488,7 +487,7 @@ public class SlingServletResolver
             final String scriptPath = 
ResourceUtil.normalize(scriptNameOrResourceType);
             if (scriptPath != null &&  isPathAllowed(scriptPath, 
this.executionPaths.get()) ) {
                 final Resource res = 
AbstractResourceCollector.getResourceOrNull(resolver,scriptPath,useResourceCaching);
-                servlet = isHidden(res) ? null : this.getServlet(res);
+                servlet = ignoreResource(res) ? null : this.getServlet(res);
                 if (servlet != null && 
!pathBasedServletAcceptor.accept(request, servlet)) {
                     if(LOGGER.isDebugEnabled()) {
                         LOGGER.debug("Servlet {} rejected by {} returning 
FORBIDDEN status", RequestUtil.getServletName(servlet),
@@ -556,7 +555,7 @@ public class SlingServletResolver
         }
 
         final Collection<Resource> candidates = 
locationUtil.getServlets(resolver, localCache.getScriptEngineExtensions());
-        candidates.removeIf(r -> isHidden(r));
+        candidates.removeIf(r -> ignoreResource(r));
 
         if (LOGGER.isDebugEnabled()) {
             if (candidates.isEmpty()) {
diff --git 
a/src/test/java/org/apache/sling/servlets/resolver/internal/AbsoluteResourceTypeTest.java
 
b/src/test/java/org/apache/sling/servlets/resolver/internal/AbsoluteResourceTypeTest.java
index 2c22511..1315bf7 100644
--- 
a/src/test/java/org/apache/sling/servlets/resolver/internal/AbsoluteResourceTypeTest.java
+++ 
b/src/test/java/org/apache/sling/servlets/resolver/internal/AbsoluteResourceTypeTest.java
@@ -34,12 +34,12 @@ import java.lang.reflect.Method;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.concurrent.atomic.AtomicReference;
-import java.util.function.Predicate;
 
 import javax.servlet.Servlet;
 
 import org.apache.sling.api.resource.PersistenceException;
 import org.osgi.framework.Bundle;
+import org.apache.sling.servlets.resolver.api.IgnoredServletResourcePredicate;
 import 
org.apache.sling.servlets.resolver.internal.resource.MockServletResource;
 
 public class AbsoluteResourceTypeTest extends SlingServletResolverTestBase {
@@ -114,8 +114,8 @@ public class AbsoluteResourceTypeTest extends 
SlingServletResolverTestBase {
 
   @Test
   public void testAbsolutePathHiddenByPredicate() throws Exception {
-    final Predicate<String> hideAbsolutePath = path -> 
absolutePath.equals(path);
-    final Field f = 
servletResolver.getClass().getDeclaredField("resourceHidingPredicate");
+    final IgnoredServletResourcePredicate hideAbsolutePath = r -> 
r.getPath().equals(absolutePath);
+    final Field f = 
servletResolver.getClass().getDeclaredField("ignoredResourcePredicate");
     f.setAccessible(true);
     f.set(servletResolver, hideAbsolutePath);
     try {
diff --git 
a/src/test/java/org/apache/sling/servlets/resolver/internal/resourcehiding/ServletHidingTest.java
 
b/src/test/java/org/apache/sling/servlets/resolver/internal/resourcehiding/ServletHidingTest.java
index 55577da..9a6b285 100644
--- 
a/src/test/java/org/apache/sling/servlets/resolver/internal/resourcehiding/ServletHidingTest.java
+++ 
b/src/test/java/org/apache/sling/servlets/resolver/internal/resourcehiding/ServletHidingTest.java
@@ -28,7 +28,6 @@ import java.util.HashMap;
 import java.util.Map;
 import java.util.UUID;
 import java.util.concurrent.atomic.AtomicBoolean;
-import java.util.function.Predicate;
 
 import javax.servlet.Servlet;
 
@@ -36,6 +35,7 @@ import org.apache.sling.api.resource.PersistenceException;
 import org.apache.sling.api.resource.ResourceUtil;
 import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
 import org.apache.sling.commons.testing.sling.MockSlingHttpServletRequest;
+import org.apache.sling.servlets.resolver.api.IgnoredServletResourcePredicate;
 import 
org.apache.sling.servlets.resolver.internal.SlingServletResolverTestBase;
 import org.apache.sling.servlets.resolver.internal.helper.HelperTestBase;
 import 
org.apache.sling.servlets.resolver.internal.resource.MockServletResource;
@@ -58,8 +58,8 @@ public class ServletHidingTest extends 
SlingServletResolverTestBase {
         }
     }
 
-    private void setServletHidingFilter(Predicate<String> predicate) throws 
Exception {
-        final Field predicateField = 
servletResolver.getClass().getDeclaredField("resourceHidingPredicate");
+    private void setServletHidingFilter(IgnoredServletResourcePredicate 
predicate) throws Exception {
+        final Field predicateField = 
servletResolver.getClass().getDeclaredField("ignoredResourcePredicate");
         predicateField.setAccessible(true);
         predicateField.set(servletResolver, predicate);
     }
@@ -101,7 +101,7 @@ public class ServletHidingTest extends 
SlingServletResolverTestBase {
     @Test
     public void testHideAndSeek() throws Exception {
         final AtomicBoolean hide = new AtomicBoolean();
-        final Predicate<String> pred = (ignoredPath) -> hide.get();
+        final IgnoredServletResourcePredicate pred = r -> hide.get();
 
         // No filtering
         setServletHidingFilter(null);
diff --git 
a/src/test/java/org/apache/sling/servlets/resolver/it/resourcehiding/BadPredicateNameIT.java
 
b/src/test/java/org/apache/sling/servlets/resolver/it/resourcehiding/BadPredicateNameIT.java
deleted file mode 100644
index d8e8f7b..0000000
--- 
a/src/test/java/org/apache/sling/servlets/resolver/it/resourcehiding/BadPredicateNameIT.java
+++ /dev/null
@@ -1,59 +0,0 @@
-/*
- * 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.servlets.resolver.it.resourcehiding;
-
-import static org.junit.Assert.assertEquals;
-
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.ops4j.pax.exam.junit.PaxExam;
-import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
-import org.ops4j.pax.exam.spi.reactors.PerClass;
-
-@RunWith(PaxExam.class)
-@ExamReactorStrategy(PerClass.class)
-public class BadPredicateNameIT extends ResourceHidingITBase {
-
-    @Before
-    public void setupPredicate() {
-        registerPredicate(path -> true, 
"invalid.name.that.causes.the.predicate.to.be.ignored");
-    }
-
-    @After
-    public void checkNothingHidden() {
-        assertEquals(0, hiddenResourcesCount);
-    }
-
-    @Test
-    public void testExtApresent() throws Exception {
-        assertTestServlet("/." + EXT_A, EXT_A);
-    }
-
-    @Test
-    public void testExtBpresent() throws Exception {
-        assertTestServlet("/." + EXT_B, EXT_B);
-    }
-
-    @Test
-    public void testSelApresent() throws Exception {
-        assertTestServlet("/." + SEL_A + "." + EXT_A, SEL_A);
-    }
-}
diff --git 
a/src/test/java/org/apache/sling/servlets/resolver/it/resourcehiding/BasicResourceHidingIT.java
 
b/src/test/java/org/apache/sling/servlets/resolver/it/resourcehiding/BasicResourceHidingIT.java
index 10afb48..bb51114 100644
--- 
a/src/test/java/org/apache/sling/servlets/resolver/it/resourcehiding/BasicResourceHidingIT.java
+++ 
b/src/test/java/org/apache/sling/servlets/resolver/it/resourcehiding/BasicResourceHidingIT.java
@@ -35,7 +35,7 @@ public class BasicResourceHidingIT extends 
ResourceHidingITBase {
 
     @Before
     public void setupPredicate() {
-        registerPredicate((path) -> path.contains(EXT_B));
+        registerPredicate(r -> r.getPath().contains(EXT_B));
     }
 
     @Test
diff --git 
a/src/test/java/org/apache/sling/servlets/resolver/it/resourcehiding/HiddenServletFallbackIT.java
 
b/src/test/java/org/apache/sling/servlets/resolver/it/resourcehiding/HiddenServletFallbackIT.java
index c891e7c..73c85ba 100644
--- 
a/src/test/java/org/apache/sling/servlets/resolver/it/resourcehiding/HiddenServletFallbackIT.java
+++ 
b/src/test/java/org/apache/sling/servlets/resolver/it/resourcehiding/HiddenServletFallbackIT.java
@@ -33,7 +33,7 @@ public class HiddenServletFallbackIT extends 
ResourceHidingITBase {
 
     @Before
     public void setupPredicate() {
-        registerPredicate((path) -> path.contains(SEL_A));
+        registerPredicate(r -> r.getPath().contains(SEL_A));
     }
 
     @Test
diff --git 
a/src/test/java/org/apache/sling/servlets/resolver/it/resourcehiding/ResourceHidingITBase.java
 
b/src/test/java/org/apache/sling/servlets/resolver/it/resourcehiding/ResourceHidingITBase.java
index 5d8631c..6221ea3 100644
--- 
a/src/test/java/org/apache/sling/servlets/resolver/it/resourcehiding/ResourceHidingITBase.java
+++ 
b/src/test/java/org/apache/sling/servlets/resolver/it/resourcehiding/ResourceHidingITBase.java
@@ -18,13 +18,12 @@
  */
 package org.apache.sling.servlets.resolver.it.resourcehiding;
 
-import java.util.Hashtable;
 import java.util.UUID;
-import java.util.function.Predicate;
 
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.servlets.resolver.api.IgnoredServletResourcePredicate;
 import org.apache.sling.servlets.resolver.it.ServletResolverTestSupport;
 import org.apache.sling.servlets.resolver.it.TestServlet;
-import org.jetbrains.annotations.Nullable;
 import org.junit.Before;
 
 /** Base for all our hiding tests, so that they all use the same set of 
servlets  */
@@ -35,8 +34,6 @@ public class ResourceHidingITBase extends 
ServletResolverTestSupport {
     protected final static String SEL_A = "SEL_A" + UUID.randomUUID();
     protected int hiddenResourcesCount = 0;
 
-    public final static String PREDICATE_NAME = 
"sling.servlet.resolver.resource.hiding";
-
     @Before
     public void reset() {
         hiddenResourcesCount = 0;
@@ -67,23 +64,17 @@ public class ResourceHidingITBase extends 
ServletResolverTestSupport {
         .register(bundleContext);
     }
 
-    protected void registerPredicate(Predicate<String> p) {
-        registerPredicate(p, null);
-    }
-
-    protected void registerPredicate(Predicate<String> p, @Nullable String 
name) {
-        final Predicate<String> wrappedPredicate = new Predicate<String>() {
+    protected void registerPredicate(IgnoredServletResourcePredicate p) {
+        final IgnoredServletResourcePredicate wrappedPredicate = new 
IgnoredServletResourcePredicate() {
             @Override
-            public boolean test(String path) {
-                final boolean result = p.test(path);
+            public boolean test(Resource r) {
+                final boolean result = p.test(r);
                 if(result) {
                     hiddenResourcesCount++;
                 }
                 return result;
             }
         };
-        final Hashtable<String,String> props = new Hashtable<>();
-        props.put("name", name != null ? name : PREDICATE_NAME);
-        bundleContext.registerService(Predicate.class.getName(), 
wrappedPredicate, props);
+        
bundleContext.registerService(IgnoredServletResourcePredicate.class.getName(), 
wrappedPredicate, null);
     }
 }
\ No newline at end of file

Reply via email to