Search in sources :

Example 26 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 27 with FilterDef

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

the class TestContextConfigAnnotation method testFilterMapping.

@Test
public void testFilterMapping() throws Exception {
    WebXml webxml = new WebXml();
    Map<String, JavaClassCacheEntry> javaClassCache = new HashMap<>();
    ContextConfig config = new ContextConfig();
    File sFile = paramClassResource("org/apache/catalina/startup/ParamServlet");
    config.processAnnotationsFile(sFile, webxml, false, javaClassCache);
    File fFile = paramClassResource("org/apache/catalina/startup/ParamFilter");
    config.processAnnotationsFile(fFile, webxml, false, javaClassCache);
    FilterDef fdef = webxml.getFilters().get("paramFilter");
    assertNotNull(fdef);
    assertEquals("Servlet says: ", fdef.getParameterMap().get("message"));
}
Also used : WebXml(org.apache.tomcat.util.descriptor.web.WebXml) FilterDef(org.apache.tomcat.util.descriptor.web.FilterDef) HashMap(java.util.HashMap) JavaClassCacheEntry(org.apache.catalina.startup.ContextConfig.JavaClassCacheEntry) File(java.io.File) Test(org.junit.Test)

Example 28 with FilterDef

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

the class TestContextConfigAnnotation method testDuplicateFilterMapping.

@Test
public void testDuplicateFilterMapping() throws Exception {
    WebXml webxml = new WebXml();
    Map<String, JavaClassCacheEntry> javaClassCache = new HashMap<>();
    ContextConfig config = new ContextConfig();
    File pFile = paramClassResource("org/apache/catalina/startup/DuplicateMappingParamFilter");
    assertTrue(pFile.exists());
    try {
        config.processAnnotationsFile(pFile, webxml, false, javaClassCache);
        fail();
    } catch (IllegalArgumentException ex) {
    // ignore
    }
    FilterDef filterDef = webxml.getFilters().get("paramD");
    assertNull(filterDef);
}
Also used : WebXml(org.apache.tomcat.util.descriptor.web.WebXml) FilterDef(org.apache.tomcat.util.descriptor.web.FilterDef) HashMap(java.util.HashMap) JavaClassCacheEntry(org.apache.catalina.startup.ContextConfig.JavaClassCacheEntry) File(java.io.File) Test(org.junit.Test)

Example 29 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 30 with FilterDef

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

Aggregations

FilterDef (org.apache.tomcat.util.descriptor.web.FilterDef)32 Test (org.junit.Test)16 FilterMap (org.apache.tomcat.util.descriptor.web.FilterMap)14 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)13 HttpServletRequest (javax.servlet.http.HttpServletRequest)11 Context (org.apache.catalina.Context)8 HashMap (java.util.HashMap)7 Tomcat (org.apache.catalina.startup.Tomcat)5 SecurityConstraint (org.apache.tomcat.util.descriptor.web.SecurityConstraint)4 File (java.io.File)3 Container (org.apache.catalina.Container)3 JavaClassCacheEntry (org.apache.catalina.startup.ContextConfig.JavaClassCacheEntry)3 IOException (java.io.IOException)2 HttpURLConnection (java.net.HttpURLConnection)2 URL (java.net.URL)2 List (java.util.List)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Wrapper (org.apache.catalina.Wrapper)2 ErrorPage (org.apache.tomcat.util.descriptor.web.ErrorPage)2 WebXml (org.apache.tomcat.util.descriptor.web.WebXml)2