You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2025/07/11 09:45:54 UTC
(sling-org-apache-sling-servlets-resolver) 01/01: SLING-12739 - Use more specific ResourcePredicate interface
This is an automated email from the ASF dual-hosted git repository.
bdelacretaz pushed a commit to branch issues/SLING-12739-ResourcePredicate
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-servlets-resolver.git
commit fc6a30cd39cda09a69ec6e6f6199f869ed8edc1a
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Fri Jul 11 11:43:45 2025 +0200
SLING-12739 - Use more specific ResourcePredicate interface
---
.../servlets/resolver/api/ResourcePredicate.java | 26 ++++++++++
.../sling/servlets/resolver/api/package-info.java | 21 ++++++++
.../resolver/internal/SlingServletResolver.java | 7 ++-
.../internal/AbsoluteResourceTypeTest.java | 4 +-
.../internal/resourcehiding/ServletHidingTest.java | 6 +--
.../it/resourcehiding/BadPredicateNameIT.java | 59 ----------------------
.../it/resourcehiding/BasicResourceHidingIT.java | 2 +-
.../it/resourcehiding/HiddenServletFallbackIT.java | 2 +-
.../it/resourcehiding/ResourceHidingITBase.java | 23 +++------
9 files changed, 64 insertions(+), 86 deletions(-)
diff --git a/src/main/java/org/apache/sling/servlets/resolver/api/ResourcePredicate.java b/src/main/java/org/apache/sling/servlets/resolver/api/ResourcePredicate.java
new file mode 100644
index 0000000..9bc4db6
--- /dev/null
+++ b/src/main/java/org/apache/sling/servlets/resolver/api/ResourcePredicate.java
@@ -0,0 +1,26 @@
+/*
+ * 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;
+
+public interface ResourcePredicate 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..7d74c6d 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.ResourcePredicate;
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 ResourcePredicate resourceHidingPredicate;
/**
* The allowed execution paths.
@@ -455,7 +454,7 @@ public class SlingServletResolver
/** @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());
+ final boolean result = r != null && resourceHidingPredicate != null && resourceHidingPredicate.test(r);
if(result && LOGGER.isDebugEnabled()) {
LOGGER.debug("Resource hidden by resource hiding predicate: {}", r.getPath());
}
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..6fe873e 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.ResourcePredicate;
import org.apache.sling.servlets.resolver.internal.resource.MockServletResource;
public class AbsoluteResourceTypeTest extends SlingServletResolverTestBase {
@@ -114,7 +114,7 @@ public class AbsoluteResourceTypeTest extends SlingServletResolverTestBase {
@Test
public void testAbsolutePathHiddenByPredicate() throws Exception {
- final Predicate<String> hideAbsolutePath = path -> absolutePath.equals(path);
+ final ResourcePredicate hideAbsolutePath = r -> r.getPath().equals(absolutePath);
final Field f = servletResolver.getClass().getDeclaredField("resourceHidingPredicate");
f.setAccessible(true);
f.set(servletResolver, hideAbsolutePath);
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..afb6a78 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.ResourcePredicate;
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,7 +58,7 @@ public class ServletHidingTest extends SlingServletResolverTestBase {
}
}
- private void setServletHidingFilter(Predicate<String> predicate) throws Exception {
+ private void setServletHidingFilter(ResourcePredicate predicate) throws Exception {
final Field predicateField = servletResolver.getClass().getDeclaredField("resourceHidingPredicate");
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 ResourcePredicate 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..8124125 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.ResourcePredicate;
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(ResourcePredicate p) {
+ final ResourcePredicate wrappedPredicate = new ResourcePredicate() {
@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(ResourcePredicate.class.getName(), wrappedPredicate, null);
}
}
\ No newline at end of file