Search in sources :

Example 1 with Dynamic

use of javax.servlet.FilterRegistration.Dynamic in project tutorials by eugenp.

the class MainWebAppInitializer method onStartup.

// 
/**
 * Register and configure all Servlet container components necessary to power the web application.
 */
@Override
public void onStartup(final ServletContext sc) throws ServletException {
    System.out.println("MyWebAppInitializer.onStartup()");
    // Create the 'root' Spring application context
    final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
    root.scan("org.baeldung.config.parent");
    // root.getEnvironment().setDefaultProfiles("embedded");
    // Manages the lifecycle of the root application context
    sc.addListener(new ContextLoaderListener(root));
    // Handles requests into the application
    final AnnotationConfigWebApplicationContext childWebApplicationContext = new AnnotationConfigWebApplicationContext();
    childWebApplicationContext.scan("org.baeldung.config.child");
    final ServletRegistration.Dynamic appServlet = sc.addServlet("api", new DispatcherServlet(childWebApplicationContext));
    appServlet.setLoadOnStartup(1);
    final Set<String> mappingConflicts = appServlet.addMapping("/");
    if (!mappingConflicts.isEmpty()) {
        throw new IllegalStateException("'appServlet' could not be mapped to '/' due " + "to an existing mapping. This is a known issue under Tomcat versions " + "<= 7.0.14; see https://issues.apache.org/bugzilla/show_bug.cgi?id=51278");
    }
    // spring security filter
    final DelegatingFilterProxy springSecurityFilterChain = new DelegatingFilterProxy("springSecurityFilterChain");
    final Dynamic addedFilter = sc.addFilter("springSecurityFilterChain", springSecurityFilterChain);
    addedFilter.addMappingForUrlPatterns(null, false, "/*");
}
Also used : ServletRegistration(javax.servlet.ServletRegistration) Dynamic(javax.servlet.FilterRegistration.Dynamic) DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) ContextLoaderListener(org.springframework.web.context.ContextLoaderListener) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) DelegatingFilterProxy(org.springframework.web.filter.DelegatingFilterProxy)

Example 2 with Dynamic

use of javax.servlet.FilterRegistration.Dynamic in project wildfly-swarm by wildfly-swarm.

the class OpenTracingInitializer method contextInitialized.

@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
    ServletContext servletContext = servletContextEvent.getServletContext();
    String skipPatternAttribute = servletContext.getInitParameter(TracingFilter.SKIP_PATTERN);
    if (null != skipPatternAttribute && !skipPatternAttribute.isEmpty()) {
        servletContext.setAttribute(TracingFilter.SKIP_PATTERN, Pattern.compile(skipPatternAttribute));
    }
    logger.info("Registering Tracing Filter");
    Dynamic filterRegistration = servletContext.addFilter("tracingFilter", new TracingFilter());
    filterRegistration.setAsyncSupported(true);
    filterRegistration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "*");
    String skipParameter = servletContext.getInitParameter("skipOpenTracingResolver");
    if (skipParameter != null && Boolean.parseBoolean(skipParameter)) {
        logger.info("OpenTracing automatic resolution is being explicitly skipped.");
        return;
    }
    if (GlobalTracer.isRegistered()) {
        logger.info("A Tracer is already registered at the GlobalTracer. Skipping resolution via TraceResolver.");
        return;
    }
    Tracer tracer = TracerResolver.resolveTracer();
    if (null == tracer) {
        logger.info("Could not get a valid OpenTracing Tracer from the classpath. Skipping.");
        return;
    }
    logger.info(String.format("Registering %s as the OpenTracing Tracer", tracer.getClass().getName()));
    GlobalTracer.register(tracer);
}
Also used : TracingFilter(io.opentracing.contrib.web.servlet.filter.TracingFilter) Dynamic(javax.servlet.FilterRegistration.Dynamic) Tracer(io.opentracing.Tracer) GlobalTracer(io.opentracing.util.GlobalTracer) ServletContext(javax.servlet.ServletContext)

Example 3 with Dynamic

use of javax.servlet.FilterRegistration.Dynamic in project molgenis by molgenis.

the class MolgenisWebAppInitializer method onStartup.

/**
 * A Molgenis common web application initializer
 */
protected void onStartup(ServletContext servletContext, Class<?> appConfig, int maxFileSize) {
    // Create the 'root' Spring application context
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.register(appConfig);
    // Manage the lifecycle of the root application context
    servletContext.addListener(new ContextLoaderListener(rootContext));
    // Register and map the dispatcher servlet
    DispatcherServlet dispatcherServlet = new DispatcherServlet(rootContext);
    dispatcherServlet.setDispatchOptionsRequest(true);
    // instead of throwing a 404 when a handler is not found allow for custom handling
    dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
    ServletRegistration.Dynamic dispatcherServletRegistration = servletContext.addServlet("dispatcher", dispatcherServlet);
    if (dispatcherServletRegistration == null) {
        LOG.warn("ServletContext already contains a complete ServletRegistration for servlet 'dispatcher'");
    } else {
        final long maxSize = (long) maxFileSize * MB;
        dispatcherServletRegistration.addMapping("/");
        dispatcherServletRegistration.setMultipartConfig(new MultipartConfigElement(null, maxSize, maxSize, FILE_SIZE_THRESHOLD));
    }
    // add filters
    Dynamic browserDetectionFiler = servletContext.addFilter("browserDetectionFilter", BrowserDetectionFilter.class);
    browserDetectionFiler.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "*");
    Dynamic etagFilter = servletContext.addFilter("etagFilter", ShallowEtagHeaderFilter.class);
    etagFilter.addMappingForServletNames(EnumSet.of(DispatcherType.REQUEST), true, "dispatcher");
    Dynamic corsFilter = servletContext.addFilter("corsFilter", CorsFilter.class);
    corsFilter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/api/*");
    corsFilter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/fdp/*");
    // enable use of request scoped beans in FrontController
    servletContext.addListener(new RequestContextListener());
    servletContext.addListener(HttpSessionEventPublisher.class);
}
Also used : Dynamic(javax.servlet.FilterRegistration.Dynamic) RequestContextListener(org.springframework.web.context.request.RequestContextListener) DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) ContextLoaderListener(org.springframework.web.context.ContextLoaderListener) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext)

Example 4 with Dynamic

use of javax.servlet.FilterRegistration.Dynamic in project brave by openzipkin.

the class ITSpanCustomizingAsyncHandlerInterceptor method init.

@Override
public void init(ServletContextHandler handler) {
    AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext() {

        // overriding this allows us to register dependencies of TracingHandlerInterceptor
        // without passing static state to a configuration class.
        @Override
        protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) {
            beanFactory.registerSingleton("httpTracing", httpTracing);
            super.loadBeanDefinitions(beanFactory);
        }
    };
    // the test resource
    appContext.register(Servlet3TestController.class);
    // generic tracing setup
    appContext.register(TracingConfig.class);
    DispatcherServlet servlet = new DispatcherServlet(appContext);
    servlet.setDispatchOptionsRequest(true);
    ServletHolder servletHolder = new ServletHolder(servlet);
    servletHolder.setAsyncSupported(true);
    handler.addServlet(servletHolder, "/*");
    handler.addEventListener(new ContextLoaderListener(appContext));
    // add the trace filter, which lazy initializes a real tracing filter from the spring context
    Dynamic filterRegistration = handler.getServletContext().addFilter("tracingFilter", DelegatingTracingFilter.class);
    filterRegistration.setAsyncSupported(true);
    filterRegistration.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");
}
Also used : Dynamic(javax.servlet.FilterRegistration.Dynamic) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ContextLoaderListener(org.springframework.web.context.ContextLoaderListener) DispatcherType(javax.servlet.DispatcherType) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext)

Example 5 with Dynamic

use of javax.servlet.FilterRegistration.Dynamic in project brave by openzipkin.

the class ITTracingFilter method init.

@Override
public void init(ServletContextHandler handler) {
    handler.getServletContext().addFilter("tracingFilter", TracingFilter.create(httpTracing)).addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");
    Dynamic sparkFilter = handler.getServletContext().addFilter("sparkFilter", new SparkFilter());
    sparkFilter.setInitParameter("applicationClass", TestApplication.class.getName());
    sparkFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");
}
Also used : Dynamic(javax.servlet.FilterRegistration.Dynamic) SparkFilter(spark.servlet.SparkFilter) DispatcherType(javax.servlet.DispatcherType)

Aggregations

Dynamic (javax.servlet.FilterRegistration.Dynamic)10 DispatcherType (javax.servlet.DispatcherType)5 ContextLoaderListener (org.springframework.web.context.ContextLoaderListener)3 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)3 DispatcherServlet (org.springframework.web.servlet.DispatcherServlet)3 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)2 DefaultExceptionHandler (alfio.util.DefaultExceptionHandler)1 ITServletContainer (brave.test.http.ITServletContainer)1 Tracer (io.opentracing.Tracer)1 TracingFilter (io.opentracing.contrib.web.servlet.filter.TracingFilter)1 GlobalTracer (io.opentracing.util.GlobalTracer)1 ServletContext (javax.servlet.ServletContext)1 ServletRegistration (javax.servlet.ServletRegistration)1 ResourceConfig (org.glassfish.jersey.server.ResourceConfig)1 ServletContainer (org.glassfish.jersey.servlet.ServletContainer)1 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)1 MapSession (org.springframework.session.MapSession)1 MapSessionRepository (org.springframework.session.MapSessionRepository)1 Session (org.springframework.session.Session)1 SessionRepositoryFilter (org.springframework.session.web.http.SessionRepositoryFilter)1