Example usage for javax.servlet FilterRegistration.Dynamic addMappingForUrlPatterns

List of usage examples for javax.servlet FilterRegistration.Dynamic addMappingForUrlPatterns

Introduction

In this page you can find the example usage for javax.servlet FilterRegistration.Dynamic addMappingForUrlPatterns.

Prototype

public void addMappingForUrlPatterns(EnumSet<DispatcherType> dispatcherTypes, boolean isMatchAfter,
        String... urlPatterns);

Source Link

Document

Adds a filter mapping with the given url patterns and dispatcher types for the Filter represented by this FilterRegistration.

Usage

From source file:de.pksoftware.springstrap.core.util.ConfigUtils.java

/**
 * Creates the filter chain.//from w w w.  j  a va  2s  .c o m
 * @param sContext
 */
public static void createFilterChain(ServletContext sContext) {
    // EnumSet<DispatcherType> dispatcherTypes =
    // EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD);

    CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
    characterEncodingFilter.setEncoding("UTF-8");
    characterEncodingFilter.setForceEncoding(true);

    FilterRegistration.Dynamic characterEncoding = sContext.addFilter("characterEncoding",
            characterEncodingFilter);
    characterEncoding.addMappingForUrlPatterns(null, true, "/*");

    DelegatingFilterProxy filter = new DelegatingFilterProxy("springSecurityFilterChain");
    //TODO Why is this needed?
    filter.setContextAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher");

    sContext.addFilter("springSecurityFilterChain", filter).addMappingForUrlPatterns(null, false, "/*");

    // FilterRegistration.Dynamic security =
    // sContext.addFilter("springSecurityFilterChain", new
    // DelegatingFilterProxy());
    // security.addMappingForUrlPatterns(null, true, "/*");
}

From source file:io.lavagna.config.DispatcherServletInitializer.java

private static void addFilter(ServletContext context, String filterName, Class<? extends Filter> filterClass,
        String... urlPatterns) {/*from  w  ww . j av  a 2s. com*/
    javax.servlet.FilterRegistration.Dynamic hstsFilter = context.addFilter(filterName, filterClass);
    hstsFilter.setAsyncSupported(true);
    hstsFilter.addMappingForUrlPatterns(null, false, urlPatterns);
}

From source file:com.sg.rest.spring.WebApplicationInitializer.java

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    FilterRegistration.Dynamic corsFilter = servletContext.addFilter("corsFilter", CorsFilter.class);
    corsFilter.addMappingForUrlPatterns(null, true, "/*");
    super.onStartup(servletContext);
}

From source file:io.appform.nautilus.funnel.FunnelServerApp.java

private void configureCors(Environment environment) {
    FilterRegistration.Dynamic filter = environment.servlets().addFilter("CORS", CrossOriginFilter.class);
    filter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");
    filter.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM, "GET,PUT,POST,DELETE,OPTIONS");
    filter.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, "*");
    filter.setInitParameter(CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER, "*");
    filter.setInitParameter("allowedHeaders",
            "Content-Type,Authorization,X-Requested-With,Content-Length,Accept,Origin");
    filter.setInitParameter("allowCredentials", "true");
}

From source file:com.gantzgulch.sharing.configuration.WebInitializer.java

@Override
public void onStartup(ServletContext servletContext) throws ServletException {

    AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
    appContext.getEnvironment().setActiveProfiles("production");
    appContext.scan("com.gantzgulch.sharing");

    ContextLoaderListener contextListener = new ContextLoaderListener(appContext);
    servletContext.addListener(contextListener);

    DelegatingFilterProxy filterProxy = new DelegatingFilterProxy("springSecurityFilterChain");
    FilterRegistration.Dynamic filter = servletContext.addFilter("securityFilter", filterProxy);
    filter.addMappingForUrlPatterns(null, false, "/*");

    AnnotationConfigWebApplicationContext webContext = new AnnotationConfigWebApplicationContext();
    webContext.register(MvcAppConfig.class);

    ServletRegistration.Dynamic dispatcher = servletContext.addServlet("sharing",
            new DispatcherServlet(webContext));
    dispatcher.setLoadOnStartup(1);//from   w ww . j  a v  a  2 s . co m
    dispatcher.addMapping("*.htm");
}

From source file:com.azirar.requester.ws.config.WebAppInitializer.java

public void onStartup(ServletContext servletContext) throws ServletException {

    AnnotationConfigWebApplicationContext dispatcherServlet = new AnnotationConfigWebApplicationContext();

    dispatcherServlet.register(AppConf.class);

    dispatcherServlet.setServletContext(servletContext);

    //        //Added filter dynamically
    javax.servlet.FilterRegistration.Dynamic corsFilter = servletContext.addFilter("corsFilter",
            CorsFilter.class);
    corsFilter.addMappingForUrlPatterns(null, true, "/*");

    Dynamic dynamic = servletContext.addServlet("dispatcher", new DispatcherServlet(dispatcherServlet));
    dynamic.addMapping("/");
    dynamic.setLoadOnStartup(1);//from  w  w  w  . j a  va 2s .c  om
}

From source file:io.github.microcks.config.WebConfiguration.java

/** */
private void initCORSFilter(ServletContext servletContext, EnumSet<DispatcherType> disps) {
    FilterRegistration.Dynamic corsFilter = servletContext.addFilter("corsFilter", new CorsFilter());
    corsFilter.addMappingForUrlPatterns(disps, true, "/api/*");
    corsFilter.setAsyncSupported(true);/*  w  w w  .  j a  v a  2s.c  o  m*/
}

From source file:gt.dakaik.config.AppConfig.java

@Override
public void onStartup(ServletContext servletContext) throws ServletException {

    ConfiguracionLogs.agregarLlave();/*from   w w w.jav  a2s.c o m*/

    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.register(RootContext.class);

    AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
    dispatcherContext.setServletContext(servletContext);
    dispatcherContext.setParent(rootContext);
    dispatcherContext.register(WebContext.class);

    ServletRegistration.Dynamic dispatcher = servletContext.addServlet(DISPATCHER_SERVLET_NAME,
            new DispatcherServlet(dispatcherContext));
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping(DISPATCHER_SERVLET_MAPPING);

    FilterRegistration.Dynamic FiltroLogs = servletContext.addFilter(FILTER_LOGGING, new FiltroLogs());
    FiltroLogs.addMappingForUrlPatterns(null, true, FILTER_LOGGING_MAPPING);

    servletContext.addListener(new ContextLoaderListener(rootContext));

}

From source file:com.econcept.init.MainWebAppplicationInitializer.java

@Override
public void onStartup(ServletContext pContainer) throws ServletException {
    // Create the 'root' Spring application context
    AnnotationConfigWebApplicationContext lRootContext = new AnnotationConfigWebApplicationContext();
    lRootContext.scan("com.econcept.init");

    // Manage the lifecycle of the root application context
    pContainer.addListener(new ContextLoaderListener(lRootContext));

    // Register and map the dispatcher servlet
    ServletRegistration.Dynamic lDispatcher = pContainer.addServlet("CFXServlet", CXFServlet.class);
    lDispatcher.addMapping("/rest/*");

    // Apply Spring OAuthSecurity to both forward and request dispatcher
    FilterRegistration.Dynamic lFilter = pContainer.addFilter("unicodeFilter", new UnicodeFilter());
    lFilter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), true, "/*");

    // Apply Spring OAuthSecurity to both forward and request dispatcher
    lFilter = pContainer.addFilter("securityFilter", new DelegatingFilterProxy("springSecurityFilterChain"));
    lFilter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), true, "/*");

    pContainer.addListener(AppHttpSessionListener.class);

}

From source file:com.redhat.developers.msa.namaste.NamasteApplication.java

@Override
public void run(NamasteConfiguration configuration, Environment environment) throws Exception {
    // Register Namaste REST service
    environment.jersey().register(new NamasteResource());

    // Enable CORS filter
    FilterRegistration.Dynamic filter = environment.servlets().addFilter("CORSFilter", CrossOriginFilter.class);
    filter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false,
            environment.getApplicationContext().getContextPath() + "*");

    // Register HystrixMetricsStreamServlet
    environment.getApplicationContext().addServlet(HystrixMetricsStreamServlet.class, "/hystrix.stream");

    // Register BraveServletFilter
    Brave brave = new Brave.Builder("namaste").spanCollector(HttpSpanCollector
            .create(System.getenv("ZIPKIN_SERVER_URL"), new EmptySpanCollectorMetricsHandler())).build();
    Filter braveFilter = new BraveServletFilter(brave.serverRequestInterceptor(),
            brave.serverResponseInterceptor(), new DefaultSpanNameProvider());
    Dynamic filterRegistration = environment.servlets().addFilter("BraveServletFilter", braveFilter);
    // Explicit mapping to avoid trace on readiness probe
    filterRegistration.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, "/api/namaste",
            "/api/namaste-chaining");

    // Configure Swagger
    environment.jersey().register(new ApiListingResource());
    environment.getObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
    BeanConfig beanConfig = new BeanConfig();
    beanConfig.setVersion("1.0.0");
    beanConfig.setSchemes(new String[] { "http" });
    beanConfig.setTitle("Namaste microservices REST API");
    beanConfig.setDescription("Operations that can be invoked in the namaste microservices");
    beanConfig.setResourcePackage("com.redhat.developers.msa.namaste");
    beanConfig.setLicense("Apache 2.0");
    beanConfig.setLicenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html");
    beanConfig.setContact("developer@redhat.com");
    beanConfig.setBasePath("/api");
    beanConfig.setPrettyPrint(true);//from  w w w . ja v a 2  s.  co  m
    beanConfig.setScan(true);
}