Skip to content

Commit a05bb23

Browse files
committed
Fix BZ 69633 - Add context root mapping support got filters
https://bz.apache.org/bugzilla/show_bug.cgi?id=69633
1 parent c50e784 commit a05bb23

File tree

3 files changed

+136
-1
lines changed

3 files changed

+136
-1
lines changed

java/org/apache/catalina/util/FilterUtil.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ private static boolean matchFiltersURL(String testPath, String requestPath) {
7979
return false;
8080
}
8181

82+
/*
83+
* Note: Order does not matter here in terms of specification compliance because this is Filter mapping. If any
84+
* rule matches then this method returns true. Order would matter if this was Servlet mapping.
85+
*/
86+
8287
// Case 1 - Exact Match
8388
if (testPath.equals(requestPath)) {
8489
return true;
@@ -109,7 +114,12 @@ private static boolean matchFiltersURL(String testPath, String requestPath) {
109114
}
110115
}
111116

112-
// Case 4 - "Default" Match
117+
// Case 4 - Context Root
118+
if (testPath.isEmpty() && requestPath.equals("/")) {
119+
return true;
120+
}
121+
122+
// Case 5 - "Default" Match
113123
return false; // NOTE - Not relevant for selecting filters
114124
}
115125

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.catalina.util;
18+
19+
import java.io.IOException;
20+
import java.nio.charset.StandardCharsets;
21+
import java.util.concurrent.atomic.AtomicInteger;
22+
23+
import jakarta.servlet.FilterChain;
24+
import jakarta.servlet.GenericFilter;
25+
import jakarta.servlet.GenericServlet;
26+
import jakarta.servlet.ServletException;
27+
import jakarta.servlet.ServletRequest;
28+
import jakarta.servlet.ServletResponse;
29+
import jakarta.servlet.http.HttpServletResponse;
30+
31+
import org.junit.Assert;
32+
import org.junit.Test;
33+
34+
import org.apache.catalina.Context;
35+
import org.apache.catalina.Wrapper;
36+
import org.apache.catalina.startup.Tomcat;
37+
import org.apache.catalina.startup.TomcatBaseTest;
38+
import org.apache.tomcat.util.buf.ByteChunk;
39+
import org.apache.tomcat.util.descriptor.web.FilterDef;
40+
import org.apache.tomcat.util.descriptor.web.FilterMap;
41+
42+
public class TestFilterUtil extends TomcatBaseTest {
43+
44+
@Test
45+
public void testContextRootMappedFilter() throws Exception {
46+
// Setup Tomcat instance
47+
Tomcat tomcat = getTomcatInstance();
48+
49+
// No file system docBase required
50+
Context ctx = getProgrammaticRootContext();
51+
52+
CountingFilter countingFilter = new CountingFilter();
53+
54+
FilterDef fd = new FilterDef();
55+
fd.setFilter(countingFilter);
56+
fd.setFilterName("CountingFilter");
57+
58+
FilterMap fm = new FilterMap();
59+
fm.setFilterName(fd.getFilterName());
60+
fm.addURLPattern("");
61+
62+
ctx.addFilterDef(fd);
63+
ctx.addFilterMap(fm);
64+
65+
Wrapper w = tomcat.addServlet("", "Default", new DefaultServlet());
66+
w.addMapping("/");
67+
68+
tomcat.start();
69+
70+
ByteChunk bc = new ByteChunk();
71+
int rc;
72+
73+
Assert.assertEquals(0, countingFilter.getCount());
74+
75+
rc = getUrl("http://localhost:" + getPort(), bc, false);
76+
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
77+
Assert.assertEquals(1, countingFilter.getCount());
78+
79+
rc = getUrl("http://localhost:" + getPort() + "/", bc, false);
80+
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
81+
Assert.assertEquals(2, countingFilter.getCount());
82+
83+
rc = getUrl("http://localhost:" + getPort() + "/not-a-context-root", bc, false);
84+
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
85+
Assert.assertEquals(2, countingFilter.getCount());
86+
}
87+
88+
89+
public static class CountingFilter extends GenericFilter {
90+
91+
private static final long serialVersionUID = 1L;
92+
93+
private AtomicInteger count = new AtomicInteger(0);
94+
95+
96+
public int getCount() {
97+
return count.get();
98+
}
99+
100+
101+
@Override
102+
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
103+
throws IOException, ServletException {
104+
count.incrementAndGet();
105+
chain.doFilter(request, response);
106+
}
107+
}
108+
109+
110+
public static class DefaultServlet extends GenericServlet {
111+
112+
private static final long serialVersionUID = 1L;
113+
114+
@Override
115+
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
116+
res.setContentType("text/plain");
117+
res.setCharacterEncoding(StandardCharsets.UTF_8);
118+
res.getWriter().print("OK");
119+
}
120+
}
121+
}

webapps/docs/changelog.xml

+4
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@
161161
Process possible path parameters rewrite production in the rewrite
162162
valve. (remm)
163163
</fix>
164+
<fix>
165+
<bug>69633</bug>: Add support for Filters using context root mappings.
166+
(markt)
167+
</fix>
164168
<fix>
165169
<bug>69643</bug>: Optimize directory listing for large amount of files.
166170
Patch submitted by Loic de l'Eprevier. (remm)

0 commit comments

Comments
 (0)