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

markt pushed a commit to branch 9.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/9.0.x by this push:
     new 2ded0810d5 Fix BZ 69633 - Add context root mapping support got filters
2ded0810d5 is described below

commit 2ded0810d55afc1c31dc62a91290f8a35a72c6c5
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Thu Apr 10 10:24:02 2025 +0100

    Fix BZ 69633 - Add context root mapping support got filters
    
    https://bz.apache.org/bugzilla/show_bug.cgi?id=69633
---
 java/org/apache/catalina/util/FilterUtil.java     |  12 ++-
 test/org/apache/catalina/util/TestFilterUtil.java | 121 ++++++++++++++++++++++
 webapps/docs/changelog.xml                        |   4 +
 3 files changed, 136 insertions(+), 1 deletion(-)

diff --git a/java/org/apache/catalina/util/FilterUtil.java 
b/java/org/apache/catalina/util/FilterUtil.java
index 9d5833d015..58f119078c 100644
--- a/java/org/apache/catalina/util/FilterUtil.java
+++ b/java/org/apache/catalina/util/FilterUtil.java
@@ -79,6 +79,11 @@ public class FilterUtil {
             return false;
         }
 
+        /*
+         * Note: Order does not matter here in terms of specification 
compliance because this is Filter mapping. If any
+         * rule matches then this method returns true. Order would matter if 
this was Servlet mapping.
+         */
+
         // Case 1 - Exact Match
         if (testPath.equals(requestPath)) {
             return true;
@@ -109,7 +114,12 @@ public class FilterUtil {
             }
         }
 
-        // Case 4 - "Default" Match
+        // Case 4 - Context Root
+        if (testPath.isEmpty() && requestPath.equals("/")) {
+            return true;
+        }
+
+        // Case 5 - "Default" Match
         return false; // NOTE - Not relevant for selecting filters
     }
 
diff --git a/test/org/apache/catalina/util/TestFilterUtil.java 
b/test/org/apache/catalina/util/TestFilterUtil.java
new file mode 100644
index 0000000000..3af9c409a6
--- /dev/null
+++ b/test/org/apache/catalina/util/TestFilterUtil.java
@@ -0,0 +1,121 @@
+/*
+ * 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.catalina.util;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import jakarta.servlet.FilterChain;
+import jakarta.servlet.GenericFilter;
+import jakarta.servlet.GenericServlet;
+import jakarta.servlet.ServletException;
+import jakarta.servlet.ServletRequest;
+import jakarta.servlet.ServletResponse;
+import jakarta.servlet.http.HttpServletResponse;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import org.apache.catalina.Context;
+import org.apache.catalina.Wrapper;
+import org.apache.catalina.startup.Tomcat;
+import org.apache.catalina.startup.TomcatBaseTest;
+import org.apache.tomcat.util.buf.ByteChunk;
+import org.apache.tomcat.util.descriptor.web.FilterDef;
+import org.apache.tomcat.util.descriptor.web.FilterMap;
+
+public class TestFilterUtil extends TomcatBaseTest {
+
+    @Test
+    public void testContextRootMappedFilter() throws Exception {
+        // Setup Tomcat instance
+        Tomcat tomcat = getTomcatInstance();
+
+        // No file system docBase required
+        Context ctx = getProgrammaticRootContext();
+
+        CountingFilter countingFilter = new CountingFilter();
+
+        FilterDef fd = new FilterDef();
+        fd.setFilter(countingFilter);
+        fd.setFilterName("CountingFilter");
+
+        FilterMap fm = new FilterMap();
+        fm.setFilterName(fd.getFilterName());
+        fm.addURLPattern("");
+
+        ctx.addFilterDef(fd);
+        ctx.addFilterMap(fm);
+
+        Wrapper w = tomcat.addServlet("", "Default", new DefaultServlet());
+        w.addMapping("/");
+
+        tomcat.start();
+
+        ByteChunk bc = new ByteChunk();
+        int rc;
+
+        Assert.assertEquals(0, countingFilter.getCount());
+
+        rc = getUrl("http://localhost:"; + getPort(), bc, false);
+        Assert.assertEquals(HttpServletResponse.SC_OK, rc);
+        Assert.assertEquals(1, countingFilter.getCount());
+
+        rc = getUrl("http://localhost:"; + getPort() + "/", bc, false);
+        Assert.assertEquals(HttpServletResponse.SC_OK, rc);
+        Assert.assertEquals(2, countingFilter.getCount());
+
+        rc = getUrl("http://localhost:"; + getPort() + "/not-a-context-root", 
bc, false);
+        Assert.assertEquals(HttpServletResponse.SC_OK, rc);
+        Assert.assertEquals(2, countingFilter.getCount());
+    }
+
+
+    public static class CountingFilter extends GenericFilter {
+
+        private static final long serialVersionUID = 1L;
+
+        private AtomicInteger count = new AtomicInteger(0);
+
+
+        public int getCount() {
+            return count.get();
+        }
+
+
+        @Override
+        public void doFilter(ServletRequest request, ServletResponse response, 
FilterChain chain)
+                throws IOException, ServletException {
+            count.incrementAndGet();
+            chain.doFilter(request, response);
+        }
+    }
+
+
+    public static class DefaultServlet extends GenericServlet {
+
+        private static final long serialVersionUID = 1L;
+
+        @Override
+        public void service(ServletRequest req, ServletResponse res) throws 
ServletException, IOException {
+            res.setContentType("text/plain");
+            res.setCharacterEncoding(StandardCharsets.UTF_8);
+            res.getWriter().print("OK");
+        }
+    }
+}
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 414e31d869..eb49ce56aa 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -128,6 +128,10 @@
         Process possible path parameters rewrite production in the rewrite
         valve. (remm)
       </fix>
+      <fix>
+        <bug>69633</bug>: Add support for Filters using context root mappings.
+        (markt)
+      </fix>
       <fix>
         <bug>69643</bug>: Optimize directory listing for large amount of files.
         Patch submitted by  Loic de l'Eprevier. (remm)


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to