Example usage for javax.servlet ServletContext addServlet

List of usage examples for javax.servlet ServletContext addServlet

Introduction

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

Prototype

public ServletRegistration.Dynamic addServlet(String servletName, Class<? extends Servlet> servletClass);

Source Link

Document

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

Usage

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);// w  ww. j a  va 2  s .  co  m
    dispatcher.addMapping("*.htm");
}

From source file:com.haulmont.cuba.web.sys.singleapp.SingleAppWebContextLoader.java

protected void registerIdpServlet(ServletContext servletContext) {
    String serviceProvidersUrls = AppContext.getProperty(IDP_SERVICE_PROVIDERS_URLS);
    if (StringUtils.isEmpty(serviceProvidersUrls)) {
        log.debug("No service providers were found. IDP Servlet will not be started");
        return;//from www.  ja v  a  2 s. c  o m
    }

    CubaIdpServlet idpServlet = new SingleAppIdpServlet(dependencyJars);
    try {
        idpServlet.init(new CubaServletConfig("idp", servletContext));
    } catch (ServletException e) {
        throw new RuntimeException("An error occurred while initializing idp servlet", e);
    }

    ServletRegistration.Dynamic idpServletRegistration = servletContext.addServlet("idp", idpServlet);
    idpServletRegistration.setLoadOnStartup(4);
    idpServletRegistration.addMapping("/idp/*");

    DelegatingFilterProxy idpSpringSecurityFilterChain = new DelegatingFilterProxy();
    idpSpringSecurityFilterChain
            .setContextAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.idp");
    idpSpringSecurityFilterChain.setTargetBeanName("springSecurityFilterChain");

    FilterRegistration.Dynamic idpSpringSecurityFilterChainReg = servletContext
            .addFilter("idpSpringSecurityFilterChain", idpSpringSecurityFilterChain);

    idpSpringSecurityFilterChainReg.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true,
            "/idp/*");
}

From source file:org.obiba.mica.config.WebConfiguration.java

/**
 * Initializes Metrics./*from  www. ja  v a  2  s.  c o  m*/
 */
private void initMetrics(ServletContext servletContext, EnumSet<DispatcherType> disps) {
    log.debug("Initializing Metrics registries");
    servletContext.setAttribute(InstrumentedFilter.REGISTRY_ATTRIBUTE, metricRegistry);
    servletContext.setAttribute(MetricsServlet.METRICS_REGISTRY, metricRegistry);

    log.debug("Registering Metrics Filter");
    FilterRegistration.Dynamic metricsFilter = servletContext.addFilter("webappMetricsFilter",
            new InstrumentedFilter());

    metricsFilter.addMappingForUrlPatterns(disps, true, "/*");
    metricsFilter.setAsyncSupported(true);

    log.debug("Registering Metrics Servlet");
    ServletRegistration.Dynamic metricsAdminServlet = servletContext.addServlet("metricsServlet",
            new MetricsServlet());

    metricsAdminServlet.addMapping("/jvm/*");
    metricsAdminServlet.setAsyncSupported(true);
    metricsAdminServlet.setLoadOnStartup(2);
}

From source file:org.bitcoinrt.atmosphere.config.BitcointWebAppInitializer.java

public void onStartup(ServletContext servletContext) throws ServletException {

    AnnotationConfigWebApplicationContext webAppContext = new AnnotationConfigWebApplicationContext();
    webAppContext.register(WebConfig.class);

    final DispatcherServlet dispatcherServlet = new DispatcherServlet(webAppContext);

    @SuppressWarnings("serial")
    MeteorServlet meteorServlet = new MeteorServlet() {
        @Override/*www . ja  va 2s  . co m*/
        public void init(ServletConfig sc) throws ServletException {
            super.init(sc);

            // MeteorServlet only support init parameters but in Java config it's easier
            // to register instances. So we re-register the default Atmosphere handler.

            BroadcasterFactory.getDefault().remove("/*");
            framework.addAtmosphereHandler("/*", new ReflectorServletProcessor(dispatcherServlet));
            framework.initAtmosphereHandler(sc);
        }
    };

    servletContext.addServlet("meteor", meteorServlet).addMapping("/");
}

From source file:br.com.joaops.smt.SmtApplicationInitializer.java

@Override
public void onStartup(ServletContext sc) throws ServletException {
    WebApplicationContext context = getContext();
    sc.addListener(new ContextLoaderListener(context));
    sc.addFilter("CharacterEncodingFilter", getCharacterEncodingFilter()).addMappingForUrlPatterns(null, true,
            MAPPING_URL);//from  w  w w .  j av a 2s .  c  om
    sc.addFilter("RequestContextFilter", getRequestContextFilter()).addMappingForUrlPatterns(null, true,
            MAPPING_URL);
    sc.addFilter("OpenEntityManagerInViewFilter", getOpenEntityManagerInViewFilter())
            .addMappingForUrlPatterns(null, true, MAPPING_URL);
    sc.addFilter("securityFilter", getDelegatingFilterProxy()).addMappingForUrlPatterns(null, false,
            MAPPING_URL); //Deixar como false, para que no d erro no multi tenant! Onde o mtodo SecurityContextHolder.getContext().getAuthentication() sempre retornava nulo depois do login
    ServletRegistration.Dynamic dispatcher = sc.addServlet("LoversBookServlet", new DispatcherServlet(context));
    dispatcher.setLoadOnStartup(1);
    dispatcher.setAsyncSupported(Boolean.TRUE);
    dispatcher.addMapping(MAPPING_URL);
}

From source file:com.gailo22.atmosphere.config.AtmosphereWebAppInitializer.java

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

    final AnnotationConfigWebApplicationContext webAppContext = new AnnotationConfigWebApplicationContext();
    webAppContext.register(WebConfig.class);

    final DispatcherServlet dispatcherServlet = new DispatcherServlet(webAppContext);

    @SuppressWarnings("serial")
    final MeteorServlet meteorServlet = new MeteorServlet() {
        @Override/*from  ww w.  j  av  a2 s.c  om*/
        public void init(final ServletConfig sc) throws ServletException {
            super.init(sc);

            // MeteorServlet only support init parameters but in Java config it's easier
            // to register instances. So we re-register the default Atmosphere handler.

            BroadcasterFactory.getDefault().remove("/*");
            this.framework.addAtmosphereHandler("/*", new ReflectorServletProcessor(dispatcherServlet));
            this.framework.initAtmosphereHandler(sc);
        }
    };

    servletContext.addServlet("meteor", meteorServlet).addMapping("/");
}

From source file:com.cfitzarl.cfjwed.Main.java

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    // Add spring security filter chain
    servletContext.addFilter("springSecurityFilterChain", DelegatingFilterProxy.class)
            .addMappingForUrlPatterns(null, false, "/*");

    // Add Hibernate session binder
    servletContext.addFilter("jpaTransactionFilter", OpenEntityManagerInViewFilter.class)
            .addMappingForUrlPatterns(null, false, "/*");

    // Define application context
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.register(WebApplicationMvcConfigurer.class);
    rootContext.scan("com.cfitzarl.cfjwed");

    // Create a dispatcher servlet that loads the application context
    DispatcherServlet dispatcherServlet = new DispatcherServlet();
    dispatcherServlet.setApplicationContext(rootContext);

    // Add the dispatcher servlet to the servlet context and map it to /
    ServletRegistration.Dynamic servletRegister = servletContext.addServlet("dispatcher", dispatcherServlet);
    servletRegister.setLoadOnStartup(1);
    servletRegister.addMapping("/");

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

From source file:com.sonicle.webtop.core.app.ContextLoader.java

public void initApp(ServletContext servletContext) throws IllegalStateException {
    String webappName = ContextUtils.getWebappFullName(servletContext, false);
    servletContext.setAttribute(WEBAPPNAME_ATTRIBUTE_KEY, webappName);
    if (servletContext.getAttribute(WEBTOPAPP_ATTRIBUTE_KEY) != null) {
        throw new IllegalStateException(
                "There is already a WebTop application associated with the current ServletContext.");
    }//from  w w  w .j av  a  2s.  c o m

    try {
        WebTopApp wta = new WebTopApp(servletContext);
        wta.boot();
        servletContext.setAttribute(WEBTOPAPP_ATTRIBUTE_KEY, wta);
        servletContext.setAttribute(JWTSignatureVerifier.SECRET_CONTEXT_ATTRIBUTE,
                wta.getDocumentServerSecretIn());

        Dynamic atmosphereServlet = servletContext.addServlet("AtmosphereServlet",
                com.sonicle.webtop.core.app.atmosphere.AtmosphereServlet.class);
        atmosphereServlet.setInitParameter("org.atmosphere.cpr.AtmosphereFramework.analytics", "false");
        atmosphereServlet.setInitParameter("org.atmosphere.cpr.broadcasterCacheClass",
                "com.sonicle.webtop.core.app.atmosphere.UUIDBroadcasterCache");
        atmosphereServlet.setInitParameter("org.atmosphere.cpr.broadcasterLifeCyclePolicy",
                "BroadcasterLifeCyclePolicy.EMPTY");
        atmosphereServlet.setInitParameter("org.atmosphere.cpr.asyncSupport",
                "org.atmosphere.container.JSR356AsyncSupport");
        //atmosphereServlet.setInitParameter("org.atmosphere.cpr.asyncSupport", "org.atmosphere.container.Tomcat7CometSupport");
        atmosphereServlet.setInitParameter("org.atmosphere.cpr.sessionSupport", "true");
        atmosphereServlet.setInitParameter("org.atmosphere.cpr.sessionCreate", "false");
        atmosphereServlet.setInitParameter("org.atmosphere.cpr.broadcaster.shareableThreadPool", "true");
        atmosphereServlet.setInitParameter("org.atmosphere.cpr.AtmosphereInterceptor",
                "org.atmosphere.interceptor.ShiroInterceptor");
        atmosphereServlet.setLoadOnStartup(1);
        atmosphereServlet.setAsyncSupported(true);
        atmosphereServlet.addMapping(com.sonicle.webtop.core.app.atmosphere.AtmosphereServlet.URL + "/*");

        Dynamic resourcesServlet = servletContext.addServlet("ResourcesServlet",
                com.sonicle.webtop.core.app.servlet.ResourceRequest.class);
        resourcesServlet.setLoadOnStartup(1);
        resourcesServlet.addMapping(com.sonicle.webtop.core.app.servlet.ResourceRequest.URL + "/*");

        Dynamic loginServlet = servletContext.addServlet("LoginServlet",
                com.sonicle.webtop.core.app.servlet.Login.class);
        loginServlet.setLoadOnStartup(1);
        loginServlet.addMapping(com.sonicle.webtop.core.app.servlet.Login.URL + "/*");

        Dynamic logoutServlet = servletContext.addServlet("LogoutServlet",
                com.sonicle.webtop.core.app.servlet.Logout.class);
        logoutServlet.setLoadOnStartup(1);
        logoutServlet.addMapping(com.sonicle.webtop.core.app.servlet.Logout.URL + "/*");

        Dynamic otpServlet = servletContext.addServlet("OtpServlet",
                com.sonicle.webtop.core.app.servlet.Otp.class);
        otpServlet.setLoadOnStartup(1);
        otpServlet.addMapping(com.sonicle.webtop.core.app.servlet.Otp.URL + "/*");

        Dynamic uiPrivateServlet = servletContext.addServlet("UIPrivateServlet",
                com.sonicle.webtop.core.app.servlet.UIPrivate.class);
        uiPrivateServlet.setLoadOnStartup(1);
        uiPrivateServlet.addMapping(com.sonicle.webtop.core.app.servlet.UIPrivate.URL + "/*");

        Dynamic privateRequestServlet = servletContext.addServlet("PrivateRequestServlet",
                com.sonicle.webtop.core.app.servlet.PrivateRequest.class);
        privateRequestServlet.setLoadOnStartup(1);
        privateRequestServlet.addMapping(com.sonicle.webtop.core.app.servlet.PrivateRequest.URL + "/*");
        privateRequestServlet.addMapping(com.sonicle.webtop.core.app.servlet.PrivateRequest.URL_LEGACY + "/*");

        Dynamic publicRequestServlet = servletContext.addServlet("PublicRequestServlet",
                com.sonicle.webtop.core.app.servlet.PublicRequest.class);
        publicRequestServlet.setLoadOnStartup(1);
        publicRequestServlet.addMapping(com.sonicle.webtop.core.app.servlet.PublicRequest.URL + "/*");

        Dynamic docEditorServlet = servletContext.addServlet("DocEditorServlet",
                com.sonicle.webtop.core.app.servlet.DocEditor.class);
        docEditorServlet.setLoadOnStartup(1);
        docEditorServlet.addMapping(com.sonicle.webtop.core.app.servlet.DocEditor.URL + "/*");

        // Adds RestApiServlets dynamically
        ServiceManager svcMgr = wta.getServiceManager();
        for (String serviceId : svcMgr.listRegisteredServices()) {
            ServiceDescriptor desc = svcMgr.getDescriptor(serviceId);

            if (desc.hasOpenApiDefinitions() || desc.hasRestApiEndpoints()) {
                addRestApiServlet(servletContext, desc);
            }
        }

    } catch (Throwable t) {
        servletContext.removeAttribute(WEBTOPAPP_ATTRIBUTE_KEY);
        logger.error("Error initializing WTA [{}]", webappName, t);
    }
}

From source file:com.nkapps.billing.configs.WebAppInitializer.java

@Override
public void onStartup(ServletContext container) {
    /*FilterRegistration.Dynamic encodingFilter = container.addFilter("encoding-filter", new CharacterEncodingFilter());
    encodingFilter.setInitParameter("encoding", "UTF-8");
    encodingFilter.setInitParameter("forceEncoding", "true");
    encodingFilter.addMappingForUrlPatterns(null, true, "/*");*/
    // Create the 'root' Spring application context
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.register(HibernateConfig.class, ServiceConfig.class, RabbitMQConfig.class);

    // Manage the lifecycle of the root application context
    container.addListener(new ContextLoaderListener(rootContext));

    // Create the dispatcher servlet's Spring application context
    AnnotationConfigWebApplicationContext dispatcherServlet = new AnnotationConfigWebApplicationContext();
    dispatcherServlet.register(MvcConfig.class);

    DispatcherServlet dp = new DispatcherServlet(dispatcherServlet);
    dp.setThrowExceptionIfNoHandlerFound(true);

    // Register and map the dispatcher servlet
    ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", dp);
    dispatcher.setLoadOnStartup(1);// w  w  w.j a  v a2  s.  c  om
    dispatcher.addMapping("/");

}