Search in sources :

Example 26 with FilterMap

use of org.apache.tomcat.util.descriptor.web.FilterMap 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 27 with FilterMap

use of org.apache.tomcat.util.descriptor.web.FilterMap 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 28 with FilterMap

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

the class ContextMBean method findFilterMaps.

/**
 * Return the set of filter mappings for this Context.
 * @return a string array with a representation of all the filter mappings
 * @throws MBeanException propagated from the managed resource access
 */
public String[] findFilterMaps() throws MBeanException {
    Context context = doGetManagedResource();
    FilterMap[] maps = context.findFilterMaps();
    String[] stringMaps = new String[maps.length];
    for (int counter = 0; counter < maps.length; counter++) {
        stringMaps[counter] = maps[counter].toString();
    }
    return stringMaps;
}
Also used : Context(org.apache.catalina.Context) FilterMap(org.apache.tomcat.util.descriptor.web.FilterMap) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint)

Example 29 with FilterMap

use of org.apache.tomcat.util.descriptor.web.FilterMap 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

FilterMap (org.apache.tomcat.util.descriptor.web.FilterMap)29 FilterDef (org.apache.tomcat.util.descriptor.web.FilterDef)22 Context (org.apache.catalina.Context)12 Tomcat (org.apache.catalina.startup.Tomcat)10 HashMap (java.util.HashMap)8 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)6 List (java.util.List)5 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)5 Test (org.junit.Test)5 File (java.io.File)4 SecurityConstraint (org.apache.tomcat.util.descriptor.web.SecurityConstraint)4 DispatcherType (jakarta.servlet.DispatcherType)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 Map (java.util.Map)3 Container (org.apache.catalina.Container)3 IOException (java.io.IOException)2 LinkedHashMap (java.util.LinkedHashMap)2 StringTokenizer (java.util.StringTokenizer)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2