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:br.com.joaops.awc.AwcApplicationInitializer.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);/*  ww  w  . j a v a  2 s. co m*/
    sc.addFilter("RequestContextFilter", getRequestContextFilter()).addMappingForUrlPatterns(null, true,
            MAPPING_URL);
    sc.addFilter("OpenEntityManagerInViewFilter", getOpenEntityManagerInViewFilter())
            .addMappingForUrlPatterns(null, true, MAPPING_URL);
    //sc.addFilter("securityFilter", getDelegatingFilterProxy()).addMappingForUrlPatterns(null, true, MAPPING_URL);
    ServletRegistration.Dynamic dispatcher = sc.addServlet("LoversBookServlet", new DispatcherServlet(context));
    dispatcher.setLoadOnStartup(1);
    dispatcher.setAsyncSupported(Boolean.TRUE);
    dispatcher.addMapping(MAPPING_URL);
}

From source file:com.kurento.kmf.content.internal.ContentApiWebApplicationInitializer.java

/**
 * RtpMedia initializator: this method search classes in the classpath using
 * the annotation {@link RtpContentService}, and it register a servlet for
 * each handler found./*from  w w  w .j a  v a 2  s  .c  o m*/
 * 
 * @param sc
 *            Servlet Context in which register servlets for each handler
 * @throws ServletException
 *             Exception raised when a reflection problem occurs, typically
 *             when a class has not been found in the classpath
 */
private void initializeRtpMediaServices(ServletContext sc) throws ServletException {
    for (String rh : findServices(RtpContentHandler.class, RtpContentService.class)) {
        try {
            RtpContentService mediaService = Class.forName(rh).getAnnotation(RtpContentService.class);
            if (mediaService != null) {
                String name = mediaService.name().isEmpty() ? rh : mediaService.name();
                String path = mediaService.path();
                log.debug("Registering RtpContentHandler with name " + name + " at path " + path);
                ServletRegistration.Dynamic sr = sc.addServlet(name, RtpMediaHandlerServlet.class);
                sr.addMapping(path);
                sr.setInitParameter(HANDLER_CLASS_PARAM_NAME, rh);
                sr.setAsyncSupported(true);
            }
        } catch (ClassNotFoundException e) {
            log.error("Error: could not find class " + rh + " in classpath", e);
            throw new ServletException(e);
        }
    }

}

From source file:com.kurento.kmf.content.internal.ContentApiWebApplicationInitializer.java

/**
 * Player initializator: this method search classes in the classpath using
 * the annotation {@link HttpPlayerService}, and it register a servlet for
 * each handler found.// w w  w .j a va 2s .c o m
 * 
 * @param sc
 *            Servlet Context in which register servlets for each handler
 * @throws ServletException
 *             Exception raised when a reflection problem occurs, typically
 *             when a class has not been found in the classpath
 */
private void initializePlayers(ServletContext sc) throws ServletException {
    for (String ph : findServices(HttpPlayerHandler.class, HttpPlayerService.class)) {
        try {
            HttpPlayerService playerService = Class.forName(ph).getAnnotation(HttpPlayerService.class);
            if (playerService != null) {
                String name = playerService.name().isEmpty() ? ph : playerService.name();
                String path = playerService.path();
                log.debug("Registering HttpPlayerHandler with name " + name + " at path " + path);
                ServletRegistration.Dynamic sr = sc.addServlet(name, PlayerHandlerServlet.class);
                if (sr == null) {
                    throw new ServletException("Duplicated handler named " + name
                            + " found. You must check your handlers' annotations to assert that no name duplications are declared.");
                }
                sr.addMapping(path);
                sr.setInitParameter(HANDLER_CLASS_PARAM_NAME, ph);
                sr.setAsyncSupported(true);
            }
        } catch (ClassNotFoundException e) {
            log.error("Error: could not find class " + ph + " in classpath", e);
            throw new ServletException(e);
        }
    }
}

From source file:com.kurento.kmf.content.internal.ContentApiWebApplicationInitializer.java

/**
 * WebRtc initializator: this method search classes in the classpath using
 * the annotation {@link WebRtcContentService}, and it register a servlet
 * for each handler found.//from ww  w .  j  a va 2 s  .co  m
 * 
 * @param sc
 *            Servlet Context in which register servlets for each handler
 * @throws ServletException
 *             Exception raised when a reflection problem occurs, typically
 *             when a class has not been found in the classpath
 */
private void initializeWebRtcMediaServices(ServletContext sc) throws ServletException {
    for (String wh : findServices(WebRtcContentHandler.class, WebRtcContentService.class)) {
        try {
            WebRtcContentService mediaService = Class.forName(wh).getAnnotation(WebRtcContentService.class);
            if (mediaService != null) {
                String name = mediaService.name().isEmpty() ? wh : mediaService.name();
                String path = mediaService.path();
                log.debug("Registering WebRtcContentHandler with name " + name + " at path " + path);
                ServletRegistration.Dynamic sr = sc.addServlet(name, WebRtcMediaHandlerServlet.class);
                sr.addMapping(path);
                sr.setInitParameter(HANDLER_CLASS_PARAM_NAME, wh);
                sr.setAsyncSupported(true);
            }
        } catch (ClassNotFoundException e) {
            log.error("Error: could not find class " + wh + " in classpath", e);
            throw new ServletException(e);
        }
    }
}

From source file:fi.helsinki.opintoni.config.WebConfigurer.java

/**
 * Initializes Metrics.//from   w  w  w  .ja va2 s  .  co  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("/metrics/metrics/*");
    metricsAdminServlet.setAsyncSupported(true);
    metricsAdminServlet.setLoadOnStartup(2);
}

From source file:com.kurento.kmf.content.internal.ContentApiWebApplicationInitializer.java

/**
 * Recorder initializator: this method search classes in the classpath using
 * the annotation {@link HttpRecorderService}, and it register a servlet for
 * each handler found./*from   ww w .  j av a 2s . c o  m*/
 * 
 * @param sc
 *            Servlet Context in which registering servlets for each handler
 * @throws ServletException
 *             Exception raised when a reflection problem occurs, typically
 *             when a class has not been found in the classpath
 */
private void initializeRecorders(ServletContext sc) throws ServletException {
    for (String rh : findServices(HttpRecorderHandler.class, HttpRecorderService.class)) {
        try {
            HttpRecorderService recorderService = Class.forName(rh).getAnnotation(HttpRecorderService.class);
            if (recorderService != null) {
                String name = recorderService.name().isEmpty() ? rh : recorderService.name();
                String path = recorderService.path();
                log.debug("Registering HttpRecorderHandler with name " + name + " at path " + path);
                ServletRegistration.Dynamic sr = sc.addServlet(name, RecorderHandlerServlet.class);
                sr.addMapping(path);
                sr.setInitParameter(HANDLER_CLASS_PARAM_NAME, rh);
                sr.setAsyncSupported(true);
            }
        } catch (ClassNotFoundException e) {
            log.error("Error: could not find class " + rh + " in classpath", e);
            throw new ServletException(e);
        }
    }
}

From source file:com.alliander.osgp.webdevicesimulator.application.config.WebDeviceSimulatorInitializer.java

@Override
public void onStartup(final ServletContext servletContext) throws ServletException {
    try {//w ww. j  a v a  2s .com
        // Force the timezone of application to UTC (required for
        // Hibernate/JDBC)
        TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

        final Context initialContext = new InitialContext();

        final String logLocation = (String) initialContext
                .lookup("java:comp/env/osp/webDeviceSimulator/log-config");
        LogbackConfigurer.initLogging(logLocation);
        InternalLoggerFactory.setDefaultFactory(new Slf4JLoggerFactory());

        final AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(ApplicationContext.class);

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

        servletContext.addListener(new ContextLoaderListener(rootContext));
    } catch (final NamingException e) {
        throw new ServletException("naming exception", e);
    } catch (final FileNotFoundException e) {
        throw new ServletException("Logging file not found", e);
    } catch (final JoranException e) {
        throw new ServletException("Logback exception", e);
    }
}

From source file:org.owasp.webgoat.application.WebGoatServletListener.java

private void loadServlets(ServletContextEvent sce) {
    final ServletContext servletContext = sce.getServletContext();
    ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(
            false);//from ww  w  .  jav  a 2 s . c  o  m
    provider.addIncludeFilter(new AnnotationTypeFilter(LessonServletMapping.class));
    Set<BeanDefinition> candidateComponents = provider.findCandidateComponents("org.owasp.webgoat");
    try {
        for (BeanDefinition beanDefinition : candidateComponents) {
            Class controllerClass = Class.forName(beanDefinition.getBeanClassName());
            LessonServletMapping pathAnnotation = (LessonServletMapping) controllerClass
                    .getAnnotation(LessonServletMapping.class);
            final ServletRegistration.Dynamic dynamic = servletContext
                    .addServlet(controllerClass.getSimpleName(), controllerClass);
            dynamic.addMapping(pathAnnotation.path());
        }
    } catch (Exception e) {
        logger.error("Error", e);
    }
}

From source file:net.rgielen.actionframeworks.springmvc.ApplicationInitializer.java

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.register(ApplicationConfig.class);
    servletContext.addListener(new ContextLoaderListener(context));

    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);//from   ww w .  j ava2s. com
    characterEncoding.addMappingForUrlPatterns(dispatcherTypes, false, "/*");

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

}

From source file:org.bonitasoft.web.designer.SpringWebApplicationInitializer.java

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

    for (String line : BANNER) {
        logger.info(line);//  www.j a  va2s.  co m
    }
    logger.info(Strings.repeat("=", 100));
    logger.info(String.format("UI-DESIGNER : %s edition v.%s", prop.getProperty("designer.edition"),
            prop.getProperty("designer.version")));
    logger.info(Strings.repeat("=", 100));

    // Create the root context Spring
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.register(new Class<?>[] { ApplicationConfig.class });

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

    // Register and map the dispatcher servlet
    ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher",
            new DispatcherServlet(rootContext));
    dispatcher.setLoadOnStartup(1);
    dispatcher.setMultipartConfig(new MultipartConfigElement(System.getProperty("java.io.tmpdir")));
    dispatcher.setAsyncSupported(true);
    dispatcher.addMapping("/");
}