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

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


The following commit(s) were added to refs/heads/master by this push:
     new 8eaec9f  SLING-10110: support regexes in  ignoreUrlParams (#26)
8eaec9f is described below

commit 8eaec9fd263889f7753493a58c9d8181af1133fa
Author: Andreas Bannasch <[email protected]>
AuthorDate: Mon Dec 12 16:12:56 2022 +0100

    SLING-10110: support regexes in  ignoreUrlParams (#26)
    
    * SLING-10110: added a functionality for resolving Star-Wildcards in 
ignoreUrlParams, added 5 Unit-Tests to make sure existing and new logic for 
that work properly
    * SLING-10110: reworked the code to not do collection modification and made 
it more readable
    * SLING-10110: removed unused imports
    * SLING-10110: removed the unneeded predicate (wanted to use it for checks 
but it turned out to not be that helpful after all)
    * SLING-10110: added breaks to the loop when the first match is made (saves 
resources and has better performance)
    * SLING-10110: set indents of IncludeTagFilter back to original Code-Style
    * SLING-10110: fix sonar code smells
    * SLING-10110: use existing import order for IncludeTagFilter
    * SLING-10110: enabled ignoreUrlParams to have full support for 
Regex-Patterns, added two new tests specifically for another regex case, 
updated README.md
    * SLING-10110: added the suggested improvements from the pull-request
    * SLING-10110: make parameterMap in RequestHelperUtilTest a class-member 
and reset before each test
    * SLING-10110: rename a method to fit the new logic better
---
 README.md                                          |   2 +-
 .../sling/dynamicinclude/IncludeTagFilter.java     |  16 +--
 .../dynamicinclude/util/RequestHelperUtil.java     |  55 +++++++++
 .../dynamicinclude/util/RequestHelperUtilTest.java | 134 +++++++++++++++++++++
 4 files changed, 192 insertions(+), 15 deletions(-)

diff --git a/README.md b/README.md
index f59f5c1..60bbd4e 100644
--- a/README.md
+++ b/README.md
@@ -42,7 +42,7 @@ Filter is delivered as a standard OSGi bundle. SDI is 
configured via the configu
 * **Component TTL** - time to live in seconds, set for rendered component 
(require Dispatcher 4.1.11+)
 * **Required header** - SDI will be enabled only if the configured header is 
present in the request. By default it's `Server-Agent=Communique-Dispatcher` 
header, added by the AEM dispatcher. You may enter just the header name only or 
the name and the value split with `=`.
 * **Disable Ignore URL params check** - SDI will process all requests and 
discard ignore URL params check including requests with GET params.
-* **Ignore URL params** - SDI normally skips all requests containing any GET 
parameters. This option allows to set a list of parameters that should be 
ignored in the test. See the [Ignoring URL 
parameters](https://docs.adobe.com/docs/en/dispatcher/disp-config.html#Ignoring%20URL%20Parameters)
 section in the dispatcher documentation.
+* **Ignore URL params** - SDI normally skips all requests containing any GET 
parameters. This option allows to set a list of parameters that should be 
ignored in the test. (Supports Java Regex Pattern e.g. "**tracking-(.*)**" or 
"**param-[0-9]**") See the [Ignoring URL 
parameters](https://docs.adobe.com/docs/en/dispatcher/disp-config.html#Ignoring%20URL%20Parameters)
 section in the dispatcher documentation.
 * **Include path rewriting** -- enable rewriting link (according to sling 
mappings) that is used for dynamic content including.
 * **Append suffix** -- ensures that the suffix of the parent request is 
included with the dynamic include.
 
diff --git 
a/src/main/java/org/apache/sling/dynamicinclude/IncludeTagFilter.java 
b/src/main/java/org/apache/sling/dynamicinclude/IncludeTagFilter.java
index 7bdddd3..b872caa 100644
--- a/src/main/java/org/apache/sling/dynamicinclude/IncludeTagFilter.java
+++ b/src/main/java/org/apache/sling/dynamicinclude/IncludeTagFilter.java
@@ -23,8 +23,6 @@ import java.io.IOException;
 import java.io.PrintWriter;
 import java.net.URI;
 import java.net.URISyntaxException;
-import java.util.Collection;
-import java.util.Enumeration;
 
 import javax.servlet.Filter;
 import javax.servlet.FilterChain;
@@ -41,6 +39,7 @@ import org.apache.sling.api.resource.ResourceUtil;
 import org.apache.sling.dynamicinclude.api.IncludeGenerator;
 import org.apache.sling.dynamicinclude.generator.IncludeGeneratorWhiteboard;
 import org.apache.sling.dynamicinclude.impl.UrlBuilder;
+import org.apache.sling.dynamicinclude.util.RequestHelperUtil;
 import org.apache.sling.servlets.annotations.SlingServletFilter;
 import org.apache.sling.servlets.annotations.SlingServletFilterScope;
 import org.osgi.framework.Constants;
@@ -106,24 +105,13 @@ public class IncludeTagFilter implements Filter {
 
     private boolean shouldWriteIncludes(Configuration config, 
SlingHttpServletRequest request) {
         // Do not skip GET requests when DisableIgnoreUrlParams set to true.
-        if (!config.isDisableIgnoreUrlParams() && 
requestHasParameters(config.getIgnoreUrlParams(), request)) {
+        if (!config.isDisableIgnoreUrlParams() && 
RequestHelperUtil.requestHasNonIgnoredParameters(config.getIgnoreUrlParams(), 
request)) {
             return false;
         }
         final String requiredHeader = config.getRequiredHeader();
         return StringUtils.isBlank(requiredHeader) || 
containsHeader(requiredHeader, request);
     }
 
-    private boolean requestHasParameters(Collection<String> ignoreUrlParams, 
SlingHttpServletRequest request) {
-        final Enumeration<?> paramNames = request.getParameterNames();
-        while (paramNames.hasMoreElements()) {
-            final String paramName = (String) paramNames.nextElement();
-            if (!ignoreUrlParams.contains(paramName)) {
-                return true;
-            }
-        }
-        return false;
-    }
-
     private boolean containsHeader(String requiredHeader, 
SlingHttpServletRequest request) {
         final String name, expectedValue;
         if (StringUtils.contains(requiredHeader, '=')) {
diff --git 
a/src/main/java/org/apache/sling/dynamicinclude/util/RequestHelperUtil.java 
b/src/main/java/org/apache/sling/dynamicinclude/util/RequestHelperUtil.java
new file mode 100644
index 0000000..d697e00
--- /dev/null
+++ b/src/main/java/org/apache/sling/dynamicinclude/util/RequestHelperUtil.java
@@ -0,0 +1,55 @@
+/*-
+ * 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.dynamicinclude.util;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+
+import java.util.Collection;
+
+public class RequestHelperUtil {
+
+       private RequestHelperUtil() {
+               // private constructor to prevent instance creation of util 
classes
+       }
+
+       /**
+        * Checks if a request contains any parameters that are not defined in 
the ignoreUrlParams.
+        * Wildcards as they are possible to configure on Apache-Dispatcher, 
are also properly checked.
+        *
+        * @param ignoreUrlParams The list of configured ignoreUrlParams 
(regular expressions)
+        * @param request         The slingRequest whose parameters we want to 
check
+        * @return true if there was any parameter that is not defined on the 
ignoreUrlsParams-Collection, otherwise false
+        */
+       public static boolean requestHasNonIgnoredParameters(Collection<String> 
ignoreUrlParams, SlingHttpServletRequest request) {
+               return request.getParameterMap().keySet().stream()
+                               .anyMatch(urlParameter -> 
!matchesRegularExpressionIgnoreUrlParameter(ignoreUrlParams, urlParameter));
+       }
+
+       private static boolean 
matchesRegularExpressionIgnoreUrlParameter(Collection<String> 
ignoreUrlParameters, String requestParameter) {
+               for (String ignoreUrlParameter : ignoreUrlParameters) {
+                       if (requestParameter.matches(ignoreUrlParameter)) {
+                               return true;
+                       }
+               }
+
+               return false;
+       }
+
+}
diff --git 
a/src/test/java/org/apache/sling/dynamicinclude/util/RequestHelperUtilTest.java 
b/src/test/java/org/apache/sling/dynamicinclude/util/RequestHelperUtilTest.java
new file mode 100644
index 0000000..9088425
--- /dev/null
+++ 
b/src/test/java/org/apache/sling/dynamicinclude/util/RequestHelperUtilTest.java
@@ -0,0 +1,134 @@
+/*-
+ * 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.dynamicinclude.util;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mockito;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+@RunWith(MockitoJUnitRunner.class)
+public class RequestHelperUtilTest {
+
+       private static final String TEST_PARAM_NAME = "test-param";
+       private static final String IGNORE_PARAM_REGEX_STAR_WILDCARD = 
"test-(.*)";
+
+       private SlingHttpServletRequest slingHttpServletRequest;
+       private Collection<String> ignoreUrlParams;
+       private Map<String, String[]> parameterMap;
+
+       @Before
+       public void prepareTestData() {
+               slingHttpServletRequest = 
Mockito.mock(SlingHttpServletRequest.class);
+
+               ignoreUrlParams = new ArrayList<>();
+               parameterMap = new HashMap<>();
+       }
+
+       @Test
+       public void requestHasParameters_noParametersAtAll() {
+               
Mockito.when(slingHttpServletRequest.getParameterMap()).thenReturn(parameterMap);
+
+               
Assert.assertFalse(RequestHelperUtil.requestHasNonIgnoredParameters(ignoreUrlParams,
 slingHttpServletRequest));
+       }
+
+       @Test
+       public void 
requestHasParameters_onlyIgnoredParameters_withoutWildcards() {
+               ignoreUrlParams.add(TEST_PARAM_NAME);
+
+               parameterMap.put(TEST_PARAM_NAME, new String[] {});
+
+               
Mockito.when(slingHttpServletRequest.getParameterMap()).thenReturn(parameterMap);
+
+               
Assert.assertFalse(RequestHelperUtil.requestHasNonIgnoredParameters(ignoreUrlParams,
 slingHttpServletRequest));
+       }
+
+       @Test
+       public void requestHasParameters_onlyIgnoredParameters_withWildcards() {
+               ignoreUrlParams.add(IGNORE_PARAM_REGEX_STAR_WILDCARD);
+               ignoreUrlParams.add("hello");
+
+               parameterMap.put(TEST_PARAM_NAME, new String[] {});
+               parameterMap.put("hello", new String[] {});
+
+               
Mockito.when(slingHttpServletRequest.getParameterMap()).thenReturn(parameterMap);
+
+               
Assert.assertFalse(RequestHelperUtil.requestHasNonIgnoredParameters(ignoreUrlParams,
 slingHttpServletRequest));
+       }
+
+       @Test
+       public void 
requestHasParameters_hasParametersThatAreNotIgnored_withoutWildcards() {
+               ignoreUrlParams.add(TEST_PARAM_NAME);
+
+               parameterMap.put(TEST_PARAM_NAME, new String[] {});
+               parameterMap.put("some-other-param", new String[] {});
+
+               
Mockito.when(slingHttpServletRequest.getParameterMap()).thenReturn(parameterMap);
+
+               
Assert.assertTrue(RequestHelperUtil.requestHasNonIgnoredParameters(ignoreUrlParams,
 slingHttpServletRequest));
+       }
+
+       @Test
+       public void 
requestHasParameters_hasParametersThatAreNotIgnored_withWildcards() {
+               ignoreUrlParams.add(IGNORE_PARAM_REGEX_STAR_WILDCARD);
+
+               parameterMap.put(TEST_PARAM_NAME, new String[] {});
+               parameterMap.put("some-other-param", new String[] {});
+
+               
Mockito.when(slingHttpServletRequest.getParameterMap()).thenReturn(parameterMap);
+
+               
Assert.assertTrue(RequestHelperUtil.requestHasNonIgnoredParameters(ignoreUrlParams,
 slingHttpServletRequest));
+       }
+
+       @Test
+       public void requestHasParameters_specificRegex_hasIgnoredParameters() {
+               ignoreUrlParams.add("hello-[0-9]-world");
+
+               parameterMap.put(TEST_PARAM_NAME, new String[] {});
+               parameterMap.put("hello-1-world", new String[] {});
+               parameterMap.put("hello-2-world", new String[] {});
+
+               
Mockito.when(slingHttpServletRequest.getParameterMap()).thenReturn(parameterMap);
+
+               
Assert.assertTrue(RequestHelperUtil.requestHasNonIgnoredParameters(ignoreUrlParams,
 slingHttpServletRequest));
+       }
+
+       @Test
+       public void requestHasParameters_specificRegex_hasNoIgnoredParameters() 
{
+               ignoreUrlParams.add("hello-[0-9]-world");
+
+               parameterMap.put("hello-1-world", new String[] {});
+               parameterMap.put("hello-2-world", new String[] {});
+               parameterMap.put("hello-3-world", new String[] {});
+
+               
Mockito.when(slingHttpServletRequest.getParameterMap()).thenReturn(parameterMap);
+
+               
Assert.assertFalse(RequestHelperUtil.requestHasNonIgnoredParameters(ignoreUrlParams,
 slingHttpServletRequest));
+       }
+
+}

Reply via email to