Example usage for javax.servlet ServletContext addFilter

List of usage examples for javax.servlet ServletContext addFilter

Introduction

In this page you can find the example usage for javax.servlet ServletContext addFilter.

Prototype

public FilterRegistration.Dynamic addFilter(String filterName, Class<? extends Filter> filterClass);

Source Link

Document

Adds the filter with the given name and class type to this servlet context.

Usage

From source file:org.jblogcms.core.config.MainConfig.java

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.register(MainContext.class);

    ServletRegistration.Dynamic dispatcher = servletContext.addServlet(DISPATCHER_SERVLET_NAME,
            new DispatcherServlet(rootContext));
    dispatcher.setLoadOnStartup(1);//ww  w  .ja v a2 s.c o  m
    dispatcher.addMapping(DISPATCHER_SERVLET_MAPPING);
    EnumSet<DispatcherType> dispatcherTypes = EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD);

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

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

    FilterRegistration.Dynamic security = servletContext.addFilter("springSecurityFilterChain",
            new DelegatingFilterProxy());
    security.addMappingForUrlPatterns(dispatcherTypes, true, "/*");

    servletContext.addListener(new ContextLoaderListener(rootContext));

}

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

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

    ConfiguracionLogs.agregarLlave();/* w w  w.ja v a  2s  .  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:org.homiefund.init.WebAppBoostrapper.java

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.register(ApplicationConfiguration.class, SecurityConfiguration.class);

    servletContext.addListener(new ContextLoaderListener(rootContext));
    servletContext.addListener(new RequestContextListener());

    AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
    dispatcherContext.register(MvcConfiguration.class);

    ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher",
            new DispatcherServlet(dispatcherContext));
    dispatcher.setLoadOnStartup(1);//w  w w .  j  a va 2 s . c om
    dispatcher.setAsyncSupported(true);
    dispatcher.addMapping("/");

    servletContext.addFilter("encodingFilter", new CharacterEncodingFilter("UTF-8", true))
            .addMappingForUrlPatterns(null, false, "/*");

    servletContext.addFilter("httpMethodFilter", new HiddenHttpMethodFilter()).addMappingForUrlPatterns(null,
            true, "/*");
}

From source file:com.github.sshw.config.RootApplicationConfig.java

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.register(RootApplicationContext.class);

    ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher",
            new DispatcherServlet(rootContext));
    dispatcher.setLoadOnStartup(1);/*ww w. j a  v  a 2 s .c om*/
    dispatcher.setAsyncSupported(true);
    dispatcher.addMapping("/");

    EnumSet<DispatcherType> dispatcherTypes = EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD);

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

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

    FilterRegistration.Dynamic security = servletContext.addFilter("springSecurityFilterChain",
            new DelegatingFilterProxy());
    security.addMappingForUrlPatterns(dispatcherTypes, true, "/*");

    servletContext.addListener(new ContextLoaderListener(rootContext));
}

From source file:com.test.config.BackendConsoleWebConfig.java

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext webCtx = new AnnotationConfigWebApplicationContext();
    webCtx.register(BackendConsoleMVCConfig.class);
    webCtx.register(BackendConsoleConfig.class);

    servletContext.addListener(new ContextLoaderListener(webCtx));

    /* Spring Delegating Dispatcher Servlet */
    Servlet dispatcherServlet = new DispatcherServlet(webCtx);
    ServletRegistration.Dynamic dispatcherServletReg = servletContext.addServlet("dispatcherServlet",
            dispatcherServlet);/*from  w  w w.  j  a  v a 2  s.  co m*/
    dispatcherServletReg.setLoadOnStartup(1);
    dispatcherServletReg.setInitParameter("contextConfigLocation", "");
    dispatcherServletReg.addMapping("/");

    /* Spring Security Delegating Filter */
    FilterRegistration springSecurityFilterChainReg = servletContext.addFilter("springSecurityFilterChain",
            DelegatingFilterProxy.class);
    springSecurityFilterChainReg.addMappingForServletNames(EnumSet.of(DispatcherType.REQUEST,
            DispatcherType.FORWARD, DispatcherType.ERROR, DispatcherType.ASYNC), false,
            dispatcherServletReg.getName());
}

From source file:rashjz.info.com.az.config.MyWebInitializer.java

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
    appContext.register(SpringWebConfig.class);

    DispatcherServlet dispatcherServlet = new DispatcherServlet(appContext);
    dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
    ServletRegistration.Dynamic dispatcher = servletContext.addServlet("SpringDispatcher", dispatcherServlet);
    dispatcher.setLoadOnStartup(1);/* www .  j  a  va2  s  . co  m*/
    dispatcher.addMapping("/");

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

    EnumSet<DispatcherType> dispatcherTypes = EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD);
    FilterRegistration.Dynamic characterEncoding = servletContext.addFilter("characterEncoding",
            characterEncodingFilter);
    characterEncoding.addMappingForUrlPatterns(dispatcherTypes, true, "/*");
}

From source file:org.kew.rmf.reconciliation.config.WebAppInitializer.java

@Override
public void onStartup(ServletContext servletContext) {
    WebApplicationContext rootContext = createRootContext(servletContext);

    configureSpringMvc(servletContext, rootContext);

    // Add Perf4J graphing servlet
    ServletRegistration.Dynamic servletRegistration = servletContext.addServlet("perf4j",
            org.perf4j.logback.servlet.GraphingServlet.class);
    servletRegistration.setInitParameter("graphNames", "graphOtherTimes,graphQueryTimes,graphQueriesPerSecond");
    servletRegistration.addMapping("/perf4j");

    String TAG_SWAP_FILTER_CLASS = "org.kew.servlet.filter.TagSwapFilter";
    try {//from  ww  w  .  j a  v  a  2  s.c om
        Class.forName(TAG_SWAP_FILTER_CLASS, false, this.getClass().getClassLoader());
        String[] urlPatterns = { "/", "/about/*", "/admin", "/filematch/*", "/help" };

        FilterRegistration.Dynamic cssLinkFilter = servletContext.addFilter("CssLinkFilter",
                TAG_SWAP_FILTER_CLASS);
        cssLinkFilter.setInitParameter("include_file_name", "/var/lib/science-apps/web-resources/head.chunk");
        cssLinkFilter.setInitParameter("tag_name", "[KEWCSS]");
        cssLinkFilter.addMappingForUrlPatterns(null, true, urlPatterns);

        FilterRegistration.Dynamic headerFilter = servletContext.addFilter("HeaderFilter",
                TAG_SWAP_FILTER_CLASS);
        headerFilter.setInitParameter("include_file_name", "/var/lib/science-apps/web-resources/bodytop.chunk");
        headerFilter.setInitParameter("tag_name", "[KEWHEADER]");
        headerFilter.addMappingForUrlPatterns(null, true, urlPatterns);

        FilterRegistration.Dynamic footerFilter = servletContext.addFilter("FooterFilter",
                TAG_SWAP_FILTER_CLASS);
        footerFilter.setInitParameter("include_file_name",
                "/var/lib/science-apps/web-resources/bodybottom.chunk");
        footerFilter.setInitParameter("tag_name", "[KEWFOOTER]");
        footerFilter.addMappingForUrlPatterns(null, true, urlPatterns);
    } catch (ClassNotFoundException e) {
        log.error("Kew servlet filters not in use, class {} not on classpath", TAG_SWAP_FILTER_CLASS);
    }
}

From source file:com.rakesh.rp3599.config.WebAppInitializer.java

public void onStartup(ServletContext container) throws ServletException {

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

    /** Dispatcher Servlet **/
    ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher",
            new DispatcherServlet(rootContext));
    dispatcher.setLoadOnStartup(1);/*from   w ww  . j  av a 2s .c  om*/
    dispatcher.setAsyncSupported(true);
    dispatcher.addMapping("/");

    /** Dispatcher Types Enum **/
    EnumSet<DispatcherType> dispatcherTypes = EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD);

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

    /** UTF-8 Filter **/
    FilterRegistration.Dynamic characterEncoding = container.addFilter("characterEncoding",
            characterEncodingFilter);
    characterEncoding.addMappingForUrlPatterns(dispatcherTypes, true, "/*");

    /** Context Listener **/
    container.addListener(new ContextLoaderListener(rootContext));

}

From source file:eu.enhan.timelord.web.init.TimelordWebInit.java

/**
 * @see org.springframework.web.WebApplicationInitializer#onStartup(javax.servlet.ServletContext)
 *//*from   w  w w .j  av a 2 s  .  c  o  m*/
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext rootCtx = new AnnotationConfigWebApplicationContext();
    rootCtx.register(AppConfig.class);
    servletContext.addListener(new ContextLoaderListener(rootCtx));

    AnnotationConfigWebApplicationContext webAppCtx = new AnnotationConfigWebApplicationContext();
    webAppCtx.setParent(rootCtx);
    webAppCtx.register(WebConfig.class);

    XmlWebApplicationContext securityCtx = new XmlWebApplicationContext();
    securityCtx.setServletContext(servletContext);
    securityCtx.setParent(rootCtx);
    securityCtx.setConfigLocation("classpath:/spring/security.xml");

    ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher",
            new DispatcherServlet(webAppCtx));
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/");

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

}

From source file:io.gravitee.management.war.WebAppInitializer.java

@Override
public void onStartup(ServletContext context) throws ServletException {
    // initialize
    initialize();/*  ww  w.  java2 s  .  co  m*/
    Properties prop = propertiesLoader.load();

    // REST configuration
    ServletRegistration.Dynamic servletRegistration = context.addServlet("REST",
            ServletContainer.class.getName());
    servletRegistration.addMapping("/management/*");
    servletRegistration.setLoadOnStartup(1);
    servletRegistration.setInitParameter("javax.ws.rs.Application", GraviteeApplication.class.getName());

    // Spring configuration
    System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME,
            prop.getProperty("security.type", "basic-auth"));
    context.addListener(new ContextLoaderListener());
    context.setInitParameter("contextClass", AnnotationConfigWebApplicationContext.class.getName());
    context.setInitParameter("contextConfigLocation", RestConfiguration.class.getName());

    // Spring Security filter
    context.addFilter("springSecurityFilterChain", DelegatingFilterProxy.class)
            .addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), false, "/*");
}