Search in sources :

Example 16 with FilterMap

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

the class TestExpiresFilter method testConfiguration.

@Test
public void testConfiguration() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    Context root = tomcat.addContext("", TEMP_DIR);
    FilterDef filterDef = new FilterDef();
    filterDef.addInitParameter("ExpiresDefault", "access plus 1 month");
    filterDef.addInitParameter("ExpiresByType text/html", "access plus 1 month 15 days 2 hours");
    filterDef.addInitParameter("ExpiresByType image/gif", "modification plus 5 hours 3 minutes");
    filterDef.addInitParameter("ExpiresByType image/jpg", "A10000");
    filterDef.addInitParameter("ExpiresByType video/mpeg", "M20000");
    filterDef.addInitParameter("ExpiresExcludedResponseStatusCodes", "304, 503");
    ExpiresFilter expiresFilter = new ExpiresFilter();
    filterDef.setFilter(expiresFilter);
    filterDef.setFilterClass(ExpiresFilter.class.getName());
    filterDef.setFilterName(ExpiresFilter.class.getName());
    root.addFilterDef(filterDef);
    FilterMap filterMap = new FilterMap();
    filterMap.setFilterName(ExpiresFilter.class.getName());
    filterMap.addURLPatternDecoded("*");
    tomcat.start();
    try {
        // VERIFY EXCLUDED RESPONSE STATUS CODES
        int[] excludedResponseStatusCodes = expiresFilter.getExcludedResponseStatusCodesAsInts();
        Assert.assertEquals(2, excludedResponseStatusCodes.length);
        Assert.assertEquals(304, excludedResponseStatusCodes[0]);
        Assert.assertEquals(503, excludedResponseStatusCodes[1]);
        // VERIFY DEFAULT CONFIGURATION
        ExpiresConfiguration expiresConfigurationDefault = expiresFilter.getDefaultExpiresConfiguration();
        Assert.assertEquals(StartingPoint.ACCESS_TIME, expiresConfigurationDefault.getStartingPoint());
        Assert.assertEquals(1, expiresConfigurationDefault.getDurations().size());
        Assert.assertEquals(DurationUnit.MONTH, expiresConfigurationDefault.getDurations().get(0).getUnit());
        Assert.assertEquals(1, expiresConfigurationDefault.getDurations().get(0).getAmount());
        // VERIFY TEXT/HTML
        ExpiresConfiguration expiresConfigurationTextHtml = expiresFilter.getExpiresConfigurationByContentType().get("text/html");
        Assert.assertEquals(StartingPoint.ACCESS_TIME, expiresConfigurationTextHtml.getStartingPoint());
        Assert.assertEquals(3, expiresConfigurationTextHtml.getDurations().size());
        Duration oneMonth = expiresConfigurationTextHtml.getDurations().get(0);
        Assert.assertEquals(DurationUnit.MONTH, oneMonth.getUnit());
        Assert.assertEquals(1, oneMonth.getAmount());
        Duration fifteenDays = expiresConfigurationTextHtml.getDurations().get(1);
        Assert.assertEquals(DurationUnit.DAY, fifteenDays.getUnit());
        Assert.assertEquals(15, fifteenDays.getAmount());
        Duration twoHours = expiresConfigurationTextHtml.getDurations().get(2);
        Assert.assertEquals(DurationUnit.HOUR, twoHours.getUnit());
        Assert.assertEquals(2, twoHours.getAmount());
        // VERIFY IMAGE/GIF
        ExpiresConfiguration expiresConfigurationImageGif = expiresFilter.getExpiresConfigurationByContentType().get("image/gif");
        Assert.assertEquals(StartingPoint.LAST_MODIFICATION_TIME, expiresConfigurationImageGif.getStartingPoint());
        Assert.assertEquals(2, expiresConfigurationImageGif.getDurations().size());
        Duration fiveHours = expiresConfigurationImageGif.getDurations().get(0);
        Assert.assertEquals(DurationUnit.HOUR, fiveHours.getUnit());
        Assert.assertEquals(5, fiveHours.getAmount());
        Duration threeMinutes = expiresConfigurationImageGif.getDurations().get(1);
        Assert.assertEquals(DurationUnit.MINUTE, threeMinutes.getUnit());
        Assert.assertEquals(3, threeMinutes.getAmount());
        // VERIFY IMAGE/JPG
        ExpiresConfiguration expiresConfigurationImageJpg = expiresFilter.getExpiresConfigurationByContentType().get("image/jpg");
        Assert.assertEquals(StartingPoint.ACCESS_TIME, expiresConfigurationImageJpg.getStartingPoint());
        Assert.assertEquals(1, expiresConfigurationImageJpg.getDurations().size());
        Duration tenThousandSeconds = expiresConfigurationImageJpg.getDurations().get(0);
        Assert.assertEquals(DurationUnit.SECOND, tenThousandSeconds.getUnit());
        Assert.assertEquals(10000, tenThousandSeconds.getAmount());
        // VERIFY VIDEO/MPEG
        ExpiresConfiguration expiresConfiguration = expiresFilter.getExpiresConfigurationByContentType().get("video/mpeg");
        Assert.assertEquals(StartingPoint.LAST_MODIFICATION_TIME, expiresConfiguration.getStartingPoint());
        Assert.assertEquals(1, expiresConfiguration.getDurations().size());
        Duration twentyThousandSeconds = expiresConfiguration.getDurations().get(0);
        Assert.assertEquals(DurationUnit.SECOND, twentyThousandSeconds.getUnit());
        Assert.assertEquals(20000, twentyThousandSeconds.getAmount());
    } finally {
        tomcat.stop();
    }
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) FilterDef(org.apache.tomcat.util.descriptor.web.FilterDef) ExpiresConfiguration(org.apache.catalina.filters.ExpiresFilter.ExpiresConfiguration) Duration(org.apache.catalina.filters.ExpiresFilter.Duration) FilterMap(org.apache.tomcat.util.descriptor.web.FilterMap) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 17 with FilterMap

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

the class ApplicationFilterRegistration method addMappingForServletNames.

@Override
public void addMappingForServletNames(EnumSet<DispatcherType> dispatcherTypes, boolean isMatchAfter, String... servletNames) {
    FilterMap filterMap = new FilterMap();
    filterMap.setFilterName(filterDef.getFilterName());
    if (dispatcherTypes != null) {
        for (DispatcherType dispatcherType : dispatcherTypes) {
            filterMap.setDispatcher(dispatcherType.name());
        }
    }
    if (servletNames != null) {
        for (String servletName : servletNames) {
            filterMap.addServletName(servletName);
        }
        if (isMatchAfter) {
            context.addFilterMap(filterMap);
        } else {
            context.addFilterMapBefore(filterMap);
        }
    }
// else error?
}
Also used : DispatcherType(javax.servlet.DispatcherType) FilterMap(org.apache.tomcat.util.descriptor.web.FilterMap)

Example 18 with FilterMap

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

the class ApplicationFilterRegistration method addMappingForUrlPatterns.

@Override
public void addMappingForUrlPatterns(EnumSet<DispatcherType> dispatcherTypes, boolean isMatchAfter, String... urlPatterns) {
    FilterMap filterMap = new FilterMap();
    filterMap.setFilterName(filterDef.getFilterName());
    if (dispatcherTypes != null) {
        for (DispatcherType dispatcherType : dispatcherTypes) {
            filterMap.setDispatcher(dispatcherType.name());
        }
    }
    if (urlPatterns != null) {
        // % decoded (if necessary) using UTF-8
        for (String urlPattern : urlPatterns) {
            filterMap.addURLPattern(urlPattern);
        }
        if (isMatchAfter) {
            context.addFilterMap(filterMap);
        } else {
            context.addFilterMapBefore(filterMap);
        }
    }
// else error?
}
Also used : DispatcherType(javax.servlet.DispatcherType) FilterMap(org.apache.tomcat.util.descriptor.web.FilterMap)

Example 19 with FilterMap

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

the class TestStandardContext method configureTest46243Context.

private static void configureTest46243Context(Context context, boolean fail) {
    // Add a test filter that fails
    FilterDef filterDef = new FilterDef();
    filterDef.setFilterClass(Bug46243Filter.class.getName());
    filterDef.setFilterName("Bug46243");
    filterDef.addInitParameter("fail", Boolean.toString(fail));
    context.addFilterDef(filterDef);
    FilterMap filterMap = new FilterMap();
    filterMap.setFilterName("Bug46243");
    filterMap.addURLPatternDecoded("*");
    context.addFilterMap(filterMap);
    // Add a test servlet so there is something to generate a response if
    // it works (although it shouldn't)
    Tomcat.addServlet(context, "Bug46243", new HelloWorldServlet());
    context.addServletMappingDecoded("/", "Bug46243");
}
Also used : FilterDef(org.apache.tomcat.util.descriptor.web.FilterDef) FilterMap(org.apache.tomcat.util.descriptor.web.FilterMap)

Example 20 with FilterMap

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

the class TomcatRsRegistry method createRsHttpListener.

@Override
public AddressInfo createRsHttpListener(final String appId, final String webContext, final HttpListener listener, final ClassLoader classLoader, final String completePath, final String virtualHost, final String auth, final String realm) {
    String path = webContext;
    if (path == null) {
        throw new NullPointerException("contextRoot is null");
    }
    if (listener == null) {
        throw new NullPointerException("listener is null");
    }
    // find the existing host (we do not auto-create hosts)
    Container host;
    Context context = null;
    if (virtualHost == null) {
        host = hosts.getDefault();
    } else {
        host = hosts.get(virtualHost);
    }
    if (host == null) {
        for (final Host h : hosts) {
            context = findContext(h, appId, webContext);
            if (context != null) {
                host = h;
                if (classLoader != null && classLoader.equals(context.getLoader().getClassLoader())) {
                    break;
                }
            // else try next to find something better
            }
        }
        if (host == null) {
            throw new IllegalArgumentException("Invalid virtual host '" + virtualHost + "'.  Do you have a matching Host entry in the server.xml?");
        }
    } else {
        context = findContext(host, appId, webContext);
    }
    if (context == null) {
        throw new IllegalStateException("Invalid context '" + webContext + "'.  Cannot find context in host " + host.getName());
    }
    final CxfRsHttpListener cxfRsHttpListener = findCxfRsHttpListener(listener);
    final String description = "tomee-jaxrs-" + listener;
    String mapping = completePath;
    if (!completePath.endsWith("/*")) {
        // respect servlet spec (!= from our embedded listeners)
        if (completePath.endsWith("*")) {
            mapping = completePath.substring(0, completePath.length() - 1);
        }
        mapping = mapping + "/*";
    }
    final String urlPattern = removeWebContext(webContext, mapping);
    cxfRsHttpListener.setUrlPattern(urlPattern.substring(0, urlPattern.length() - 1));
    final FilterDef filterDef = new FilterDef();
    filterDef.setAsyncSupported("true");
    filterDef.setDescription(description);
    filterDef.setFilterName(description);
    filterDef.setDisplayName(description);
    filterDef.setFilter(new CXFJAXRSFilter(cxfRsHttpListener, context.findWelcomeFiles()));
    filterDef.setFilterClass(CXFJAXRSFilter.class.getName());
    // just keep base path
    filterDef.addInitParameter("mapping", urlPattern.substring(0, urlPattern.length() - "/*".length()));
    context.addFilterDef(filterDef);
    final FilterMap filterMap = new FilterMap();
    filterMap.addURLPattern(urlPattern);
    for (final DispatcherType type : DispatcherType.values()) {
        filterMap.setDispatcher(type.name());
    }
    filterMap.setFilterName(filterDef.getFilterName());
    context.addFilterMap(filterMap);
    Registrations.addFilterConfig(context, filterDef);
    path = address(connectors, host.getName(), webContext);
    final String key = address(connectors, host.getName(), completePath);
    listeners.put(new Key(appId, key), listener);
    return new AddressInfo(path, key);
}
Also used : Context(org.apache.catalina.Context) CxfRsHttpListener(org.apache.openejb.server.cxf.rs.CxfRsHttpListener) FilterDef(org.apache.tomcat.util.descriptor.web.FilterDef) Host(org.apache.catalina.Host) FilterMap(org.apache.tomcat.util.descriptor.web.FilterMap) Container(org.apache.catalina.Container) DispatcherType(javax.servlet.DispatcherType)

Aggregations

FilterMap (org.apache.tomcat.util.descriptor.web.FilterMap)21 FilterDef (org.apache.tomcat.util.descriptor.web.FilterDef)14 Context (org.apache.catalina.Context)7 Tomcat (org.apache.catalina.startup.Tomcat)5 HashMap (java.util.HashMap)4 DispatcherType (javax.servlet.DispatcherType)4 HashSet (java.util.HashSet)3 Container (org.apache.catalina.Container)3 SecurityConstraint (org.apache.tomcat.util.descriptor.web.SecurityConstraint)3 IOException (java.io.IOException)2 HttpURLConnection (java.net.HttpURLConnection)2 URL (java.net.URL)2 List (java.util.List)2 Wrapper (org.apache.catalina.Wrapper)2 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)2 TesterContext (org.apache.tomcat.unittest.TesterContext)2 ErrorPage (org.apache.tomcat.util.descriptor.web.ErrorPage)2 Test (org.junit.Test)2 File (java.io.File)1 MalformedURLException (java.net.MalformedURLException)1