|
| 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 | +} |
0 commit comments