You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2020/04/22 05:43:51 UTC

[sling-org-apache-sling-engine] branch master updated: SLING-9388 : Add new properties for filtering based on resource and request path

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

cziegeler pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-engine.git


The following commit(s) were added to refs/heads/master by this push:
     new bba773a  SLING-9388 : Add new properties for filtering based on resource and request path
bba773a is described below

commit bba773a85e4eec22043e636b4cfde5eeb0cb4404
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Wed Apr 22 07:43:38 2020 +0200

    SLING-9388 : Add new properties for filtering based on resource and request path
---
 pom.xml                                            |  4 +--
 .../org/apache/sling/engine/EngineConstants.java   | 23 +++++++++++--
 .../sling/engine/impl/filter/FilterPredicate.java  | 39 ++++++++++++++--------
 .../java/org/apache/sling/engine/package-info.java |  2 +-
 .../engine/impl/filter/AbstractFilterTest.java     | 38 ++++++++++++++++++---
 .../engine/impl/filter/FilterPredicateTest.java    | 22 ++++++++++--
 6 files changed, 101 insertions(+), 27 deletions(-)

diff --git a/pom.xml b/pom.xml
index 1e0133c..4015c0a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -23,12 +23,12 @@
     <parent>
         <groupId>org.apache.sling</groupId>
         <artifactId>sling-bundle-parent</artifactId>
-        <version>35</version>
+        <version>38</version>
         <relativePath />
     </parent>
 
     <artifactId>org.apache.sling.engine</artifactId>
-    <version>2.6.23-SNAPSHOT</version>
+    <version>2.7.0</version>
 
     <name>Apache Sling Engine Implementation</name>
     <description>
diff --git a/src/main/java/org/apache/sling/engine/EngineConstants.java b/src/main/java/org/apache/sling/engine/EngineConstants.java
index 3c0aa67..66899fd 100644
--- a/src/main/java/org/apache/sling/engine/EngineConstants.java
+++ b/src/main/java/org/apache/sling/engine/EngineConstants.java
@@ -154,13 +154,30 @@ public class EngineConstants {
      * @since 2.1, Sling Engine 2.2
      */
     public static final String SLING_FILTER_SCOPE = "sling.filter.scope";
-    
+
     /**
-     * Regular expression pattern for enabling a filter on a matching request path
-     *@since 2.2, Sling Engine 2.4
+     * Regular expression pattern for enabling a filter on a matching path
+     * This refers to both, the request path and the resolved resource path excluding selectors,
+     * extension and suffix.
      */
     public static final String SLING_FILTER_PATTERN = "sling.filter.pattern";
 
+    /**
+     * Regular expression pattern for enabling a filter on a matching request path
+     * This refers to the resolved resource path excluding selectors,
+     * extension and suffix.
+     * @since 2.4.0, Sling Engine 2.7.0
+     */
+    public static final String SLING_FILTER_RESOURCE_PATTERN = "sling.filter.resource.pattern";
+
+    /**
+     * Regular expression pattern for enabling a filter on a matching request path
+     * This refers to the request path excluding selectors,
+     * extension and suffix.
+     * @since 2.4.0, Sling Engine 2.7.0
+     */
+    public static final String SLING_FILTER_REQUEST_PATTERN = "sling.filter.request.pattern";
+
 
     /**
      * Regular expression pattern for enabling a filter on a matching request suffix
diff --git a/src/main/java/org/apache/sling/engine/impl/filter/FilterPredicate.java b/src/main/java/org/apache/sling/engine/impl/filter/FilterPredicate.java
index 61afd96..440044e 100644
--- a/src/main/java/org/apache/sling/engine/impl/filter/FilterPredicate.java
+++ b/src/main/java/org/apache/sling/engine/impl/filter/FilterPredicate.java
@@ -18,6 +18,22 @@
  */
 package org.apache.sling.engine.impl.filter;
 
+import static java.util.Arrays.asList;
+import static org.apache.sling.engine.EngineConstants.SLING_FILTER_EXTENSIONS;
+import static org.apache.sling.engine.EngineConstants.SLING_FILTER_METHODS;
+import static org.apache.sling.engine.EngineConstants.SLING_FILTER_PATTERN;
+import static org.apache.sling.engine.EngineConstants.SLING_FILTER_REQUEST_PATTERN;
+import static org.apache.sling.engine.EngineConstants.SLING_FILTER_RESOURCETYPES;
+import static org.apache.sling.engine.EngineConstants.SLING_FILTER_RESOURCE_PATTERN;
+import static org.apache.sling.engine.EngineConstants.SLING_FILTER_SELECTORS;
+import static org.apache.sling.engine.EngineConstants.SLING_FILTER_SUFFIX_PATTERN;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.regex.Pattern;
+
+import javax.servlet.Filter;
+
 import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.api.request.RequestPathInfo;
 import org.apache.sling.api.resource.Resource;
@@ -26,19 +42,6 @@ import org.osgi.framework.ServiceReference;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import javax.servlet.Filter;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.regex.Pattern;
-
-import static java.util.Arrays.asList;
-import static org.apache.sling.engine.EngineConstants.SLING_FILTER_EXTENSIONS;
-import static org.apache.sling.engine.EngineConstants.SLING_FILTER_METHODS;
-import static org.apache.sling.engine.EngineConstants.SLING_FILTER_PATTERN;
-import static org.apache.sling.engine.EngineConstants.SLING_FILTER_SUFFIX_PATTERN;
-import static org.apache.sling.engine.EngineConstants.SLING_FILTER_RESOURCETYPES;
-import static org.apache.sling.engine.EngineConstants.SLING_FILTER_SELECTORS;
-
 /**
  * Contains a set of predicates that helps testing whether to enable a filter for a request or not
  * it can only be constructed from a filter service reference, from whose properties it builds its
@@ -53,6 +56,8 @@ public class FilterPredicate {
     Collection<String> extensions;
     Collection<String> resourceTypes;
     Pattern pathRegex;
+    Pattern resourcePathRegex;
+    Pattern requestPathRegex;
     Pattern suffixRegex;
 
     /*
@@ -64,6 +69,8 @@ public class FilterPredicate {
         resourceTypes = asCollection(reference, SLING_FILTER_RESOURCETYPES);
         methods = asCollection(reference, SLING_FILTER_METHODS);
         pathRegex = asPattern(reference, SLING_FILTER_PATTERN);
+        resourcePathRegex = asPattern(reference, SLING_FILTER_RESOURCE_PATTERN);
+        requestPathRegex = asPattern(reference, SLING_FILTER_REQUEST_PATTERN);
         suffixRegex = asPattern(reference, SLING_FILTER_SUFFIX_PATTERN);
     }
 
@@ -99,7 +106,7 @@ public class FilterPredicate {
     /**
      * @param resourceTypes configured resourceTypes
      * @param request request that is being tested
-     * @return true if the request's resource is of one of the types, or if not or miscongfigured
+     * @return true if the request's resource is of one of the types, or if not or misconfigured
      */
     private boolean anyResourceTypeMatches(final Collection<String> resourceTypes, final SlingHttpServletRequest request) {
         if (resourceTypes == null) {
@@ -138,6 +145,8 @@ public class FilterPredicate {
                 && anyResourceTypeMatches(resourceTypes, req)
                 && (patternMatches(pathRegex, path == null || path.isEmpty() ? "/" : path)
                 || patternMatches(pathRegex, uri == null || uri.isEmpty() ? "/" : uri))
+                && (patternMatches(requestPathRegex,  uri == null || uri.isEmpty() ? "/" : uri))
+                && (patternMatches(resourcePathRegex,  path == null || path.isEmpty() ? "/" : path))
                 && patternMatches(suffixRegex, requestPathInfo.getSuffix());
         LOG.debug("selection of {} returned {}", this, select);
         return select;
@@ -148,6 +157,8 @@ public class FilterPredicate {
         return "FilterPredicate{" +
                 "methods='" + methods + '\'' +
                 ", pathRegex=" + pathRegex +
+                ", requestPathRegex=" + requestPathRegex +
+                ", resourcePathRegex=" + resourcePathRegex +
                 ", suffixRegex=" + suffixRegex +
                 ", selectors='" + selectors + '\'' +
                 ", extensions='" + extensions + '\'' +
diff --git a/src/main/java/org/apache/sling/engine/package-info.java b/src/main/java/org/apache/sling/engine/package-info.java
index b3aee91..c4c2df0 100644
--- a/src/main/java/org/apache/sling/engine/package-info.java
+++ b/src/main/java/org/apache/sling/engine/package-info.java
@@ -17,6 +17,6 @@
  * under the License.
  */
 
-@org.osgi.annotation.versioning.Version("2.3.0")
+@org.osgi.annotation.versioning.Version("2.4.0")
 package org.apache.sling.engine;
 
diff --git a/src/test/java/org/apache/sling/engine/impl/filter/AbstractFilterTest.java b/src/test/java/org/apache/sling/engine/impl/filter/AbstractFilterTest.java
index fe83fec..1cf2cb8 100644
--- a/src/test/java/org/apache/sling/engine/impl/filter/AbstractFilterTest.java
+++ b/src/test/java/org/apache/sling/engine/impl/filter/AbstractFilterTest.java
@@ -18,6 +18,11 @@
  */
 package org.apache.sling.engine.impl.filter;
 
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.servlet.Filter;
+
 import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.api.request.RequestPathInfo;
 import org.jmock.Expectations;
@@ -28,10 +33,6 @@ import org.junit.runner.RunWith;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.ServiceReference;
 
-import javax.servlet.Filter;
-import java.util.HashMap;
-import java.util.Map;
-
 @RunWith(JMock.class)
 public abstract class AbstractFilterTest {
     protected final Mockery context = new JUnit4Mockery();
@@ -75,6 +76,7 @@ public abstract class AbstractFilterTest {
         };
         return ref;
     }
+
     protected SlingHttpServletRequest mockRequest(final String path,
                                                   final String extension,
                                                   final String[] selectors,
@@ -104,6 +106,34 @@ public abstract class AbstractFilterTest {
         }});
         return req;
     }
+
+    protected SlingHttpServletRequest mockRequest(final String resourcePath,
+            final String requestPath,
+            final String extension) {
+        final RequestPathInfo info = context.mock(RequestPathInfo.class, "info " + resourcePath + requestPath + extension);
+        context.checking(new Expectations() {{
+            allowing(info).getExtension();
+            will(returnValue(extension));
+            allowing(info).getSuffix();
+            will(returnValue(null));
+            allowing(info).getSelectors();
+            will(returnValue(new String[0]));
+            allowing(info).getResourcePath();
+            will(returnValue(resourcePath   ));
+        }});
+
+        final SlingHttpServletRequest req = context.mock(SlingHttpServletRequest.class, "req " + resourcePath + requestPath + extension);
+        context.checking(new Expectations() {{
+            allowing(req).getRequestPathInfo();
+            will(returnValue(info));
+            allowing(req).getMethod();
+            will(returnValue(null));
+            allowing(req).getPathInfo();
+            will(returnValue(requestPath));
+        }});
+        return req;
+    }
+
     protected FilterPredicate predicate(Object... args){
         FilterPredicate predicate = new FilterPredicate(mockService(args));
         return predicate;
diff --git a/src/test/java/org/apache/sling/engine/impl/filter/FilterPredicateTest.java b/src/test/java/org/apache/sling/engine/impl/filter/FilterPredicateTest.java
index 6b3c97b..62e6e3a 100644
--- a/src/test/java/org/apache/sling/engine/impl/filter/FilterPredicateTest.java
+++ b/src/test/java/org/apache/sling/engine/impl/filter/FilterPredicateTest.java
@@ -18,16 +18,18 @@
  */
 package org.apache.sling.engine.impl.filter;
 
-import org.junit.Test;
-
 import static org.apache.sling.engine.EngineConstants.SLING_FILTER_EXTENSIONS;
 import static org.apache.sling.engine.EngineConstants.SLING_FILTER_METHODS;
 import static org.apache.sling.engine.EngineConstants.SLING_FILTER_PATTERN;
-import static org.apache.sling.engine.EngineConstants.SLING_FILTER_SUFFIX_PATTERN;
+import static org.apache.sling.engine.EngineConstants.SLING_FILTER_REQUEST_PATTERN;
+import static org.apache.sling.engine.EngineConstants.SLING_FILTER_RESOURCE_PATTERN;
 import static org.apache.sling.engine.EngineConstants.SLING_FILTER_SELECTORS;
+import static org.apache.sling.engine.EngineConstants.SLING_FILTER_SUFFIX_PATTERN;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
+import org.junit.Test;
+
 public class FilterPredicateTest extends AbstractFilterTest {
 
     /**
@@ -46,6 +48,20 @@ public class FilterPredicateTest extends AbstractFilterTest {
     }
 
     @Test
+    public void testRequestPathPattern() {
+        FilterPredicate predicate = predicate(SLING_FILTER_REQUEST_PATTERN,"/content/test/.*");
+        assertTrue("/content/test/foo should be selected", predicate.test(mockRequest("/content/bar/foo", "/content/test/foo", "json")));
+        assertFalse("/content/bar/foo should not be selected", predicate.test(mockRequest("/content/bar/foo","/content/bar/foo", "json")));
+    }
+
+    @Test
+    public void testResourcePathPattern() {
+        FilterPredicate predicate = predicate(SLING_FILTER_RESOURCE_PATTERN,"/content/test/.*");
+        assertTrue("/content/test/foo should be selected", predicate.test(mockRequest("/content/test/foo", "/content/bar/foo", "json")));
+        assertFalse("/content/bar/foo should not be selected", predicate.test(mockRequest("/content/bar/foo","/content/bar/foo", "json")));
+    }
+
+    @Test
     public void testExtensions() {
         FilterPredicate predicate = predicate(SLING_FILTER_PATTERN,
                 "/content/test/.*",