Search in sources :

Example 1 with FilterMap

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

the class TestAddCharSetFilter method doTest.

private void doTest(String encoding, String expected, int mode) throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    // Add the Servlet
    CharsetServlet servlet = new CharsetServlet(mode);
    Tomcat.addServlet(ctx, "servlet", servlet);
    ctx.addServletMappingDecoded("/", "servlet");
    // Add the Filter
    FilterDef filterDef = new FilterDef();
    filterDef.setFilterClass(AddDefaultCharsetFilter.class.getName());
    filterDef.setFilterName("filter");
    if (encoding != null) {
        filterDef.addInitParameter("encoding", encoding);
    }
    ctx.addFilterDef(filterDef);
    FilterMap filterMap = new FilterMap();
    filterMap.setFilterName("filter");
    filterMap.addServletName("servlet");
    ctx.addFilterMap(filterMap);
    tomcat.start();
    Map<String, List<String>> headers = new HashMap<>();
    getUrl("http://localhost:" + getPort() + "/", new ByteChunk(), headers);
    List<String> ctHeaders = headers.get("Content-Type");
    assertEquals(1, ctHeaders.size());
    String ct = ctHeaders.get(0);
    assertEquals("text/plain;charset=" + expected, ct);
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) FilterDef(org.apache.tomcat.util.descriptor.web.FilterDef) HashMap(java.util.HashMap) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) List(java.util.List) FilterMap(org.apache.tomcat.util.descriptor.web.FilterMap)

Example 2 with FilterMap

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

the class TomcatStarter method addFilter.

private void addFilter(WebFilterFace filterFace) {
    if (filterFace == null || filterFace.getFilter() == null) {
        return;
    }
    log.debugf("[NutzBoot] add filter name=%s pathSpec=%s", filterFace.getName(), filterFace.getPathSpec());
    FilterDef filterDef = new FilterDef();
    filterDef.setFilter(filterFace.getFilter());
    filterDef.setFilterName(filterFace.getName());
    filterDef.setFilterClass(filterFace.getFilter().getClass().getName());
    if (filterFace.getInitParameters() != null && !filterFace.getInitParameters().isEmpty()) {
        for (Map.Entry<String, String> entry : filterFace.getInitParameters().entrySet()) {
            filterDef.addInitParameter(entry.getKey(), entry.getValue());
        }
    }
    this.tomcatContext.addFilterDef(filterDef);
    FilterMap filterMap = new FilterMap();
    filterMap.addURLPatternDecoded(filterFace.getPathSpec());
    filterMap.setFilterName(filterFace.getName());
    this.tomcatContext.addFilterMap(filterMap);
}
Also used : FilterDef(org.apache.tomcat.util.descriptor.web.FilterDef) FilterMap(org.apache.tomcat.util.descriptor.web.FilterMap) Map(java.util.Map) FilterMap(org.apache.tomcat.util.descriptor.web.FilterMap)

Example 3 with FilterMap

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

the class CatalinaContainer method prepareContext.

private StandardContext prepareContext(Host host, ContainerConfig.Configuration configuration, ComponentConfig.WebappInfo appInfo, Property clusterProp) throws ContainerException {
    StandardContext context = new StandardContext();
    Tomcat.initWebappDefaults(context);
    String location = getWebappRootLocation(appInfo);
    boolean contextIsDistributable = isContextDistributable(configuration, location);
    context.setParent(host);
    context.setDocBase(location);
    context.setPath(getWebappMountPoint(appInfo));
    context.addLifecycleListener(new ContextConfig());
    context.setJ2EEApplication("OFBiz");
    context.setJ2EEServer("OFBiz Container");
    context.setLoader(new WebappLoader(Thread.currentThread().getContextClassLoader()));
    context.setDisplayName(appInfo.name);
    context.setDocBase(location);
    context.setReloadable(ContainerConfig.getPropertyValue(configuration, "apps-context-reloadable", false));
    context.setDistributable(contextIsDistributable);
    context.setCrossContext(ContainerConfig.getPropertyValue(configuration, "apps-cross-context", true));
    context.setPrivileged(appInfo.privileged);
    context.getServletContext().setAttribute("_serverId", appInfo.server);
    context.getServletContext().setAttribute("componentName", appInfo.componentConfig.getComponentName());
    if (clusterProp != null && contextIsDistributable) {
        context.setManager(prepareClusterManager(clusterProp));
    }
    StandardRoot resources = new StandardRoot(context);
    resources.setAllowLinking(true);
    context.setResources(resources);
    JarScanner jarScanner = context.getJarScanner();
    if (jarScanner instanceof StandardJarScanner) {
        StandardJarScanner standardJarScanner = (StandardJarScanner) jarScanner;
        standardJarScanner.setJarScanFilter(new FilterJars());
        standardJarScanner.setScanClassPath(true);
    }
    Map<String, String> initParameters = appInfo.getInitParameters();
    // request dumper filter
    if ("true".equals(initParameters.get("enableRequestDump"))) {
        FilterDef requestDumperFilterDef = new FilterDef();
        requestDumperFilterDef.setFilterClass(RequestDumperFilter.class.getName());
        requestDumperFilterDef.setFilterName("RequestDumper");
        context.addFilterDef(requestDumperFilterDef);
        FilterMap requestDumperFilterMap = new FilterMap();
        requestDumperFilterMap.setFilterName("RequestDumper");
        requestDumperFilterMap.addURLPattern("*");
        context.addFilterMap(requestDumperFilterMap);
    }
    // set the init parameters
    initParameters.entrySet().forEach(entry -> context.addParameter(entry.getKey(), entry.getValue()));
    return context;
}
Also used : FilterDef(org.apache.tomcat.util.descriptor.web.FilterDef) RequestDumperFilter(org.apache.catalina.filters.RequestDumperFilter) StandardRoot(org.apache.catalina.webresources.StandardRoot) JarScanner(org.apache.tomcat.JarScanner) StandardJarScanner(org.apache.tomcat.util.scan.StandardJarScanner) FilterMap(org.apache.tomcat.util.descriptor.web.FilterMap) ContextConfig(org.apache.catalina.startup.ContextConfig) StandardContext(org.apache.catalina.core.StandardContext) StandardJarScanner(org.apache.tomcat.util.scan.StandardJarScanner) WebappLoader(org.apache.catalina.loader.WebappLoader)

Example 4 with FilterMap

use of org.apache.tomcat.util.descriptor.web.FilterMap in project spring-framework by spring-projects.

the class TomcatWebSocketTestServer method deployConfig.

@Override
public void deployConfig(WebApplicationContext wac, Filter... filters) {
    Assert.state(this.port != -1, "setup() was never called.");
    this.context = this.tomcatServer.addContext("", System.getProperty("java.io.tmpdir"));
    this.context.addApplicationListener(WsContextListener.class.getName());
    Tomcat.addServlet(this.context, "dispatcherServlet", new DispatcherServlet(wac)).setAsyncSupported(true);
    this.context.addServletMappingDecoded("/", "dispatcherServlet");
    for (Filter filter : filters) {
        FilterDef filterDef = new FilterDef();
        filterDef.setFilterName(filter.getClass().getName());
        filterDef.setFilter(filter);
        filterDef.setAsyncSupported("true");
        this.context.addFilterDef(filterDef);
        FilterMap filterMap = new FilterMap();
        filterMap.setFilterName(filter.getClass().getName());
        filterMap.addURLPattern("/*");
        filterMap.setDispatcher("REQUEST,FORWARD,INCLUDE,ASYNC");
        this.context.addFilterMap(filterMap);
    }
}
Also used : WsContextListener(org.apache.tomcat.websocket.server.WsContextListener) FilterDef(org.apache.tomcat.util.descriptor.web.FilterDef) Filter(jakarta.servlet.Filter) DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) FilterMap(org.apache.tomcat.util.descriptor.web.FilterMap)

Example 5 with FilterMap

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

the class OpenEJBContextConfig method webConfig.

@Override
protected void webConfig() {
    TomcatHelper.configureJarScanner(context);
    // read the real config
    super.webConfig();
    if (IgnoredStandardContext.class.isInstance(context)) {
        // no need of jsf
        return;
    }
    if (AppFinder.findAppContextOrWeb(context.getLoader().getClassLoader(), AppFinder.WebBeansContextTransformer.INSTANCE) != null) {
        final FilterDef asyncOwbFilter = new FilterDef();
        asyncOwbFilter.setAsyncSupported("true");
        asyncOwbFilter.setDescription("OpenEJB CDI Filter - to propagate @RequestScoped in async tasks");
        asyncOwbFilter.setDisplayName("OpenEJB CDI");
        asyncOwbFilter.setFilterClass(EEFilter.class.getName());
        asyncOwbFilter.setFilterName(EEFilter.class.getName());
        context.addFilterDef(asyncOwbFilter);
        final FilterMap asyncOwbMapping = new FilterMap();
        asyncOwbMapping.setFilterName(asyncOwbFilter.getFilterName());
        asyncOwbMapping.addURLPattern("/*");
        context.addFilterMap(asyncOwbMapping);
    }
    if ("true".equalsIgnoreCase(SystemInstance.get().getProperty("tomee.jsp-development", "false"))) {
        for (final Container c : context.findChildren()) {
            if (Wrapper.class.isInstance(c)) {
                final Wrapper servlet = Wrapper.class.cast(c);
                if ("org.apache.jasper.servlet.JspServlet".equals(servlet.getServletClass())) {
                    servlet.addInitParameter("development", "true");
                }
            }
        }
    }
    final ClassLoader classLoader = context.getLoader().getClassLoader();
    // add myfaces auto-initializer if mojarra is not present
    try {
        classLoader.loadClass("com.sun.faces.context.SessionMap");
        return;
    } catch (final Throwable ignored) {
    // no-op
    }
    try {
        final Class<?> myfacesInitializer = Class.forName(MYFACES_TOMEEM_CONTAINER_INITIALIZER, true, classLoader);
        final ServletContainerInitializer instance = (ServletContainerInitializer) myfacesInitializer.newInstance();
        context.addServletContainerInitializer(instance, getJsfClasses(context));
        // cleanup listener
        context.addApplicationListener(TOMEE_MYFACES_CONTEXT_LISTENER);
    } catch (final Exception | NoClassDefFoundError ignored) {
    // no-op
    }
}
Also used : ServletContainerInitializer(javax.servlet.ServletContainerInitializer) Wrapper(org.apache.catalina.Wrapper) StandardWrapper(org.apache.catalina.core.StandardWrapper) Container(org.apache.catalina.Container) FilterDef(org.apache.tomcat.util.descriptor.web.FilterDef) EEFilter(org.apache.openejb.server.httpd.EEFilter) FilterMap(org.apache.tomcat.util.descriptor.web.FilterMap) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ClassFormatException(org.apache.tomcat.util.bcel.classfile.ClassFormatException)

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