Search in sources :

Example 41 with FilterDef

use of org.apache.tomcat.util.descriptor.web.FilterDef in project tomcat by apache.

the class TestApplicationFilterConfig method testBug54170.

@Test
public void testBug54170() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    Tomcat.addServlet(ctx, "HelloWorld", new HelloWorldServlet());
    ctx.addServletMappingDecoded("/", "HelloWorld");
    // Add a filter with a name that should be escaped if used in a JMX
    // object name
    FilterDef filterDef = new FilterDef();
    filterDef.setFilterClass(AddDefaultCharsetFilter.class.getName());
    filterDef.setFilterName("bug54170*");
    ctx.addFilterDef(filterDef);
    tomcat.start();
    final MBeanServer mbeanServer = Registry.getRegistry(null, null).getMBeanServer();
    // There should be one Servlet MBean registered
    Set<ObjectName> servlets = mbeanServer.queryNames(new ObjectName("Tomcat:j2eeType=Servlet,*"), null);
    Assert.assertEquals(1, servlets.size());
    // There should be one Filter MBean registered
    Set<ObjectName> filters = mbeanServer.queryNames(new ObjectName("Tomcat:j2eeType=Filter,*"), null);
    Assert.assertEquals(1, filters.size());
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) FilterDef(org.apache.tomcat.util.descriptor.web.FilterDef) AddDefaultCharsetFilter(org.apache.catalina.filters.AddDefaultCharsetFilter) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 42 with FilterDef

use of org.apache.tomcat.util.descriptor.web.FilterDef in project tomcat by apache.

the class TestExpiresFilter method validate.

protected void validate(HttpServlet servlet, Integer expectedMaxAgeInSeconds, int expectedResponseStatusCode) throws Exception {
    // SETUP
    Tomcat tomcat = getTomcatInstance();
    Context root = tomcat.addContext("", TEMP_DIR);
    FilterDef filterDef = new FilterDef();
    filterDef.addInitParameter("ExpiresDefault", "access plus 1 minute");
    filterDef.addInitParameter("ExpiresByType text/xml;charset=utf-8", "access plus 3 minutes");
    filterDef.addInitParameter("ExpiresByType text/xml", "access plus 5 minutes");
    filterDef.addInitParameter("ExpiresByType text", "access plus 7 minutes");
    filterDef.addInitParameter("ExpiresExcludedResponseStatusCodes", "304, 503");
    filterDef.setFilterClass(ExpiresFilter.class.getName());
    filterDef.setFilterName(ExpiresFilter.class.getName());
    root.addFilterDef(filterDef);
    FilterMap filterMap = new FilterMap();
    filterMap.setFilterName(ExpiresFilter.class.getName());
    filterMap.addURLPatternDecoded("*");
    root.addFilterMap(filterMap);
    Tomcat.addServlet(root, servlet.getClass().getName(), servlet);
    root.addServletMappingDecoded("/test", servlet.getClass().getName());
    tomcat.start();
    try {
        Calendar.getInstance(TimeZone.getTimeZone("GMT"));
        long timeBeforeInMillis = System.currentTimeMillis();
        // TEST
        ByteChunk bc = new ByteChunk();
        Map<String, List<String>> responseHeaders = new HashMap<>();
        int rc = getUrl("http://localhost:" + getPort() + "/test", bc, responseHeaders);
        // VALIDATE
        Assert.assertEquals(expectedResponseStatusCode, rc);
        StringBuilder msg = new StringBuilder();
        for (Entry<String, List<String>> field : responseHeaders.entrySet()) {
            for (String value : field.getValue()) {
                msg.append((field.getKey() == null ? "" : field.getKey() + ": ") + value + "\n");
            }
        }
        System.out.println(msg);
        Integer actualMaxAgeInSeconds;
        String cacheControlHeader = getSingleHeader("Cache-Control", responseHeaders);
        if (cacheControlHeader == null) {
            actualMaxAgeInSeconds = null;
        } else {
            actualMaxAgeInSeconds = null;
            StringTokenizer cacheControlTokenizer = new StringTokenizer(cacheControlHeader, ",");
            while (cacheControlTokenizer.hasMoreTokens() && actualMaxAgeInSeconds == null) {
                String cacheDirective = cacheControlTokenizer.nextToken();
                StringTokenizer cacheDirectiveTokenizer = new StringTokenizer(cacheDirective, "=");
                if (cacheDirectiveTokenizer.countTokens() == 2) {
                    String key = cacheDirectiveTokenizer.nextToken().trim();
                    String value = cacheDirectiveTokenizer.nextToken().trim();
                    if (key.equalsIgnoreCase("max-age")) {
                        actualMaxAgeInSeconds = Integer.valueOf(value);
                    }
                }
            }
        }
        if (expectedMaxAgeInSeconds == null) {
            Assert.assertNull("actualMaxAgeInSeconds '" + actualMaxAgeInSeconds + "' should be null", actualMaxAgeInSeconds);
            return;
        }
        Assert.assertNotNull(actualMaxAgeInSeconds);
        String contentType = getSingleHeader("Content-Type", responseHeaders);
        int deltaInSeconds = Math.abs(actualMaxAgeInSeconds.intValue() - expectedMaxAgeInSeconds.intValue());
        Assert.assertTrue("actualMaxAgeInSeconds: " + actualMaxAgeInSeconds + ", expectedMaxAgeInSeconds: " + expectedMaxAgeInSeconds + ", request time: " + timeBeforeInMillis + " for content type " + contentType, deltaInSeconds < 3);
    } finally {
        tomcat.stop();
    }
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) FilterDef(org.apache.tomcat.util.descriptor.web.FilterDef) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) HashMap(java.util.HashMap) FilterMap(org.apache.tomcat.util.descriptor.web.FilterMap) StartingPoint(org.apache.catalina.filters.ExpiresFilter.StartingPoint) StringTokenizer(java.util.StringTokenizer) ArrayList(java.util.ArrayList) List(java.util.List)

Example 43 with FilterDef

use of org.apache.tomcat.util.descriptor.web.FilterDef in project tomcat by apache.

the class TestExpiresFilter method testBug63909.

/*
     * Tests Expires filter with:
     * - per content type expires
     * - no default
     * - Default servlet returning 304s (without content-type)
     */
@Test
public void testBug63909() throws Exception {
    Tomcat tomcat = getTomcatInstanceTestWebapp(false, false);
    Context ctxt = (Context) tomcat.getHost().findChild("/test");
    FilterDef filterDef = new FilterDef();
    filterDef.addInitParameter("ExpiresByType text/xml;charset=utf-8", "access plus 3 minutes");
    filterDef.addInitParameter("ExpiresByType text/xml", "access plus 5 minutes");
    filterDef.addInitParameter("ExpiresByType text", "access plus 7 minutes");
    filterDef.addInitParameter("ExpiresExcludedResponseStatusCodes", "");
    filterDef.setFilterClass(ExpiresFilter.class.getName());
    filterDef.setFilterName(ExpiresFilter.class.getName());
    ctxt.addFilterDef(filterDef);
    FilterMap filterMap = new FilterMap();
    filterMap.setFilterName(ExpiresFilter.class.getName());
    filterMap.addURLPatternDecoded("*");
    ctxt.addFilterMap(filterMap);
    tomcat.start();
    ByteChunk bc = new ByteChunk();
    Map<String, List<String>> requestHeaders = new CaseInsensitiveKeyMap<>();
    List<String> ifModifiedSinceValues = new ArrayList<>();
    ifModifiedSinceValues.add(FastHttpDateFormat.getCurrentDate());
    requestHeaders.put("If-Modified-Since", ifModifiedSinceValues);
    Map<String, List<String>> responseHeaders = new CaseInsensitiveKeyMap<>();
    int rc = getUrl("http://localhost:" + getPort() + "/test/bug6nnnn/bug69303.txt", bc, requestHeaders, responseHeaders);
    Assert.assertEquals(HttpServletResponse.SC_NOT_MODIFIED, rc);
    StringBuilder msg = new StringBuilder();
    for (Entry<String, List<String>> field : responseHeaders.entrySet()) {
        for (String value : field.getValue()) {
            msg.append((field.getKey() == null ? "" : field.getKey() + ": ") + value + "\n");
        }
    }
    System.out.println(msg);
    Integer actualMaxAgeInSeconds;
    String cacheControlHeader = getSingleHeader("Cache-Control", responseHeaders);
    if (cacheControlHeader == null) {
        actualMaxAgeInSeconds = null;
    } else {
        actualMaxAgeInSeconds = null;
        StringTokenizer cacheControlTokenizer = new StringTokenizer(cacheControlHeader, ",");
        while (cacheControlTokenizer.hasMoreTokens() && actualMaxAgeInSeconds == null) {
            String cacheDirective = cacheControlTokenizer.nextToken();
            StringTokenizer cacheDirectiveTokenizer = new StringTokenizer(cacheDirective, "=");
            if (cacheDirectiveTokenizer.countTokens() == 2) {
                String key = cacheDirectiveTokenizer.nextToken().trim();
                String value = cacheDirectiveTokenizer.nextToken().trim();
                if (key.equalsIgnoreCase("max-age")) {
                    actualMaxAgeInSeconds = Integer.valueOf(value);
                }
            }
        }
    }
    Assert.assertNotNull(actualMaxAgeInSeconds);
    Assert.assertTrue(Math.abs(actualMaxAgeInSeconds.intValue() - 420) < 3);
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) FilterDef(org.apache.tomcat.util.descriptor.web.FilterDef) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) ArrayList(java.util.ArrayList) FilterMap(org.apache.tomcat.util.descriptor.web.FilterMap) StartingPoint(org.apache.catalina.filters.ExpiresFilter.StartingPoint) StringTokenizer(java.util.StringTokenizer) ArrayList(java.util.ArrayList) List(java.util.List) CaseInsensitiveKeyMap(org.apache.tomcat.util.collections.CaseInsensitiveKeyMap) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 44 with FilterDef

use of org.apache.tomcat.util.descriptor.web.FilterDef in project tomcat by apache.

the class ApplicationContext method getFilterRegistrations.

@Override
public Map<String, ? extends FilterRegistration> getFilterRegistrations() {
    Map<String, ApplicationFilterRegistration> result = new HashMap<>();
    FilterDef[] filterDefs = context.findFilterDefs();
    for (FilterDef filterDef : filterDefs) {
        result.put(filterDef.getFilterName(), new ApplicationFilterRegistration(filterDef, context));
    }
    return result;
}
Also used : FilterDef(org.apache.tomcat.util.descriptor.web.FilterDef) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 45 with FilterDef

use of org.apache.tomcat.util.descriptor.web.FilterDef in project tomcat by apache.

the class TestRegExpCapture method testBug53387.

@Test
public void testBug53387() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    File appDir = new File("test/webapp");
    Context ctx = tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
    // SSI requires a privileged Context
    ctx.setPrivileged(true);
    FilterDef ssiFilter = new FilterDef();
    ssiFilter.setFilterName("ssiFilter");
    ssiFilter.setFilterClass(SSIFilter.class.getName());
    FilterMap ssiFilterMap = new FilterMap();
    ssiFilterMap.setFilterName("ssiFilter");
    ssiFilterMap.addURLPatternDecoded("*.shtml");
    ctx.addFilterDef(ssiFilter);
    ctx.addFilterMap(ssiFilterMap);
    ctx.addMimeMapping("shtml", "text/x-server-parsed-html");
    tomcat.start();
    ByteChunk body = new ByteChunk();
    int rc = getUrl("http://localhost:" + getPort() + "/test/bug5nnnn/bug53387.shtml" + queryString, body, null);
    Assert.assertEquals(200, rc);
    String text = body.toString();
    Assert.assertTrue(text, text.contains(expectedInBody));
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) FilterDef(org.apache.tomcat.util.descriptor.web.FilterDef) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) File(java.io.File) FilterMap(org.apache.tomcat.util.descriptor.web.FilterMap) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Aggregations

FilterDef (org.apache.tomcat.util.descriptor.web.FilterDef)45 Test (org.junit.Test)24 FilterMap (org.apache.tomcat.util.descriptor.web.FilterMap)22 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)21 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)16 Context (org.apache.catalina.Context)13 HashMap (java.util.HashMap)11 Tomcat (org.apache.catalina.startup.Tomcat)10 File (java.io.File)6 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)6 List (java.util.List)5 SecurityConstraint (org.apache.tomcat.util.descriptor.web.SecurityConstraint)5 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 Container (org.apache.catalina.Container)3 JavaClassCacheEntry (org.apache.catalina.startup.ContextConfig.JavaClassCacheEntry)3 IOException (java.io.IOException)2 LinkedHashMap (java.util.LinkedHashMap)2 StringTokenizer (java.util.StringTokenizer)2