Search in sources :

Example 1 with ServletContainer

use of org.glassfish.jersey.servlet.ServletContainer in project hbase by apache.

the class HBaseRESTTestingUtility method startServletContainer.

public void startServletContainer(Configuration conf) throws Exception {
    if (server != null) {
        LOG.error("ServletContainer already running");
        return;
    }
    // Inject the conf for the test by being first to make singleton
    RESTServlet.getInstance(conf, UserProvider.instantiate(conf));
    // set up the Jersey servlet container for Jetty
    ResourceConfig app = new ResourceConfig().packages("org.apache.hadoop.hbase.rest").register(Jackson1Feature.class);
    ServletHolder sh = new ServletHolder(new ServletContainer(app));
    // set up Jetty and run the embedded server
    server = new Server(0);
    LOG.info("configured " + ServletContainer.class.getName());
    HttpConfiguration httpConfig = new HttpConfiguration();
    httpConfig.setSendDateHeader(false);
    httpConfig.setSendServerVersion(false);
    ServerConnector serverConnector = new ServerConnector(server, new HttpConnectionFactory(httpConfig));
    serverConnector.setPort(testServletPort);
    server.addConnector(serverConnector);
    // set up context
    ServletContextHandler ctxHandler = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
    ctxHandler.addServlet(sh, "/*");
    // Load filters specified from configuration.
    String[] filterClasses = conf.getStrings(Constants.FILTER_CLASSES, ArrayUtils.EMPTY_STRING_ARRAY);
    for (String filter : filterClasses) {
        filter = filter.trim();
        ctxHandler.addFilter(filter, "/*", EnumSet.of(DispatcherType.REQUEST));
    }
    LOG.info("Loaded filter classes :" + filterClasses);
    conf.set(RESTServer.REST_CSRF_BROWSER_USERAGENTS_REGEX_KEY, ".*");
    RESTServer.addCSRFFilter(ctxHandler, conf);
    HttpServerUtil.constrainHttpMethods(ctxHandler);
    // start the server
    server.start();
    // get the port
    testServletPort = ((ServerConnector) server.getConnectors()[0]).getLocalPort();
    LOG.info("started " + server.getClass().getName() + " on port " + testServletPort);
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ServletContainer(org.glassfish.jersey.servlet.ServletContainer) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 2 with ServletContainer

use of org.glassfish.jersey.servlet.ServletContainer in project kafka by apache.

the class RestServer method start.

public void start(Herder herder) {
    log.info("Starting REST server");
    ResourceConfig resourceConfig = new ResourceConfig();
    resourceConfig.register(new JacksonJsonProvider());
    resourceConfig.register(RootResource.class);
    resourceConfig.register(new ConnectorsResource(herder));
    resourceConfig.register(new ConnectorPluginsResource(herder));
    resourceConfig.register(ConnectExceptionMapper.class);
    ServletContainer servletContainer = new ServletContainer(resourceConfig);
    ServletHolder servletHolder = new ServletHolder(servletContainer);
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    context.addServlet(servletHolder, "/*");
    String allowedOrigins = config.getString(WorkerConfig.ACCESS_CONTROL_ALLOW_ORIGIN_CONFIG);
    if (allowedOrigins != null && !allowedOrigins.trim().isEmpty()) {
        FilterHolder filterHolder = new FilterHolder(new CrossOriginFilter());
        filterHolder.setName("cross-origin");
        filterHolder.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, allowedOrigins);
        String allowedMethods = config.getString(WorkerConfig.ACCESS_CONTROL_ALLOW_METHODS_CONFIG);
        if (allowedMethods != null && !allowedOrigins.trim().isEmpty()) {
            filterHolder.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM, allowedMethods);
        }
        context.addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST));
    }
    RequestLogHandler requestLogHandler = new RequestLogHandler();
    Slf4jRequestLog requestLog = new Slf4jRequestLog();
    requestLog.setLoggerName(RestServer.class.getCanonicalName());
    requestLog.setLogLatency(true);
    requestLogHandler.setRequestLog(requestLog);
    HandlerCollection handlers = new HandlerCollection();
    handlers.setHandlers(new Handler[] { context, new DefaultHandler(), requestLogHandler });
    /* Needed for graceful shutdown as per `setStopTimeout` documentation */
    StatisticsHandler statsHandler = new StatisticsHandler();
    statsHandler.setHandler(handlers);
    jettyServer.setHandler(statsHandler);
    jettyServer.setStopTimeout(GRACEFUL_SHUTDOWN_TIMEOUT_MS);
    jettyServer.setStopAtShutdown(true);
    try {
        jettyServer.start();
    } catch (Exception e) {
        throw new ConnectException("Unable to start REST server", e);
    }
    log.info("REST server listening at " + jettyServer.getURI() + ", advertising URL " + advertisedUrl());
}
Also used : Slf4jRequestLog(org.eclipse.jetty.server.Slf4jRequestLog) ConnectorsResource(org.apache.kafka.connect.runtime.rest.resources.ConnectorsResource) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) JacksonJsonProvider(com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider) ConnectorPluginsResource(org.apache.kafka.connect.runtime.rest.resources.ConnectorPluginsResource) CrossOriginFilter(org.eclipse.jetty.servlets.CrossOriginFilter) ConnectRestException(org.apache.kafka.connect.runtime.rest.errors.ConnectRestException) IOException(java.io.IOException) ConnectException(org.apache.kafka.connect.errors.ConnectException) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) ServletContainer(org.glassfish.jersey.servlet.ServletContainer) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) StatisticsHandler(org.eclipse.jetty.server.handler.StatisticsHandler) ConnectException(org.apache.kafka.connect.errors.ConnectException)

Example 3 with ServletContainer

use of org.glassfish.jersey.servlet.ServletContainer in project jersey by jersey.

the class JerseyServletContainerInitializer method addServletWithExistingRegistration.

/**
     * Enhance existing servlet configuration.
     */
private static void addServletWithExistingRegistration(final ServletContext context, ServletRegistration registration, final Class<? extends Application> clazz, final Set<Class<?>> classes) throws ServletException {
    // create a new servlet container for a given app.
    final ResourceConfig resourceConfig = ResourceConfig.forApplicationClass(clazz, classes).addProperties(getInitParams(registration)).addProperties(Utils.getContextParams(context));
    if (registration.getClassName() != null) {
        // class name present - complete servlet registration from container point of view
        Utils.store(resourceConfig, context, registration.getName());
    } else {
        // no class name - no complete servlet registration from container point of view
        final ServletContainer servlet = new ServletContainer(resourceConfig);
        final ServletRegistration.Dynamic dynamicRegistration = context.addServlet(clazz.getName(), servlet);
        dynamicRegistration.setAsyncSupported(true);
        dynamicRegistration.setLoadOnStartup(1);
        registration = dynamicRegistration;
    }
    if (registration.getMappings().isEmpty()) {
        final ApplicationPath ap = clazz.getAnnotation(ApplicationPath.class);
        if (ap != null) {
            final String mapping = createMappingPath(ap);
            if (!mappingExists(context, mapping)) {
                registration.addMapping(mapping);
                LOGGER.log(Level.CONFIG, LocalizationMessages.JERSEY_APP_REGISTERED_MAPPING(clazz.getName(), mapping));
            } else {
                LOGGER.log(Level.WARNING, LocalizationMessages.JERSEY_APP_MAPPING_CONFLICT(clazz.getName(), mapping));
            }
        } else {
            // Error
            LOGGER.log(Level.WARNING, LocalizationMessages.JERSEY_APP_NO_MAPPING_OR_ANNOTATION(clazz.getName(), ApplicationPath.class.getSimpleName()));
        }
    } else {
        LOGGER.log(Level.CONFIG, LocalizationMessages.JERSEY_APP_REGISTERED_APPLICATION(clazz.getName()));
    }
}
Also used : ServletRegistration(javax.servlet.ServletRegistration) ServletContainer(org.glassfish.jersey.servlet.ServletContainer) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) ApplicationPath(javax.ws.rs.ApplicationPath)

Example 4 with ServletContainer

use of org.glassfish.jersey.servlet.ServletContainer in project jersey by jersey.

the class JerseyServletContainerInitializer method addServletWithApplication.

/**
     * Add new servlet according to {@link Application} subclass with {@link ApplicationPath} annotation or existing
     * {@code servlet-mapping}.
     */
private static void addServletWithApplication(final ServletContext context, final Class<? extends Application> clazz, final Set<Class<?>> defaultClasses) throws ServletException {
    final ApplicationPath ap = clazz.getAnnotation(ApplicationPath.class);
    if (ap != null) {
        // App is annotated with ApplicationPath
        final ResourceConfig resourceConfig = ResourceConfig.forApplicationClass(clazz, defaultClasses).addProperties(Utils.getContextParams(context));
        final ServletContainer s = new ServletContainer(resourceConfig);
        final ServletRegistration.Dynamic dsr = context.addServlet(clazz.getName(), s);
        dsr.setAsyncSupported(true);
        dsr.setLoadOnStartup(1);
        final String mapping = createMappingPath(ap);
        if (!mappingExists(context, mapping)) {
            dsr.addMapping(mapping);
            LOGGER.log(Level.CONFIG, LocalizationMessages.JERSEY_APP_REGISTERED_MAPPING(clazz.getName(), mapping));
        } else {
            LOGGER.log(Level.WARNING, LocalizationMessages.JERSEY_APP_MAPPING_CONFLICT(clazz.getName(), mapping));
        }
    }
}
Also used : ServletRegistration(javax.servlet.ServletRegistration) ServletContainer(org.glassfish.jersey.servlet.ServletContainer) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) ApplicationPath(javax.ws.rs.ApplicationPath)

Example 5 with ServletContainer

use of org.glassfish.jersey.servlet.ServletContainer in project uavstack by uavorg.

the class JerseyConfig method jerseyServlet.

@Bean
public ServletRegistrationBean jerseyServlet() {
    ServletRegistrationBean registration = new ServletRegistrationBean(new ServletContainer(), "/rest/*");
    // our rest resources will be available in the path /rest/*
    registration.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS, JerseyConfig.class.getName());
    return registration;
}
Also used : ServletRegistrationBean(org.springframework.boot.web.servlet.ServletRegistrationBean) ServletContainer(org.glassfish.jersey.servlet.ServletContainer) ServletRegistrationBean(org.springframework.boot.web.servlet.ServletRegistrationBean) Bean(org.springframework.context.annotation.Bean)

Aggregations

ServletContainer (org.glassfish.jersey.servlet.ServletContainer)49 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)30 ResourceConfig (org.glassfish.jersey.server.ResourceConfig)29 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)24 Server (org.eclipse.jetty.server.Server)10 IOException (java.io.IOException)7 DispatcherType (javax.servlet.DispatcherType)6 FilterHolder (org.eclipse.jetty.servlet.FilterHolder)6 JacksonJsonProvider (com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider)5 ServletRegistrationBean (org.springframework.boot.web.servlet.ServletRegistrationBean)5 Bean (org.springframework.context.annotation.Bean)5 JacksonJaxbJsonProvider (com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider)4 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)4 RequestLogHandler (org.eclipse.jetty.server.handler.RequestLogHandler)4 StatisticsHandler (org.eclipse.jetty.server.handler.StatisticsHandler)4 ServletRegistration (javax.servlet.ServletRegistration)3 ConnectException (org.apache.kafka.connect.errors.ConnectException)3 ConnectorPluginsResource (org.apache.kafka.connect.runtime.rest.resources.ConnectorPluginsResource)3 ConnectorsResource (org.apache.kafka.connect.runtime.rest.resources.ConnectorsResource)3 Slf4jRequestLog (org.eclipse.jetty.server.Slf4jRequestLog)3