Search in sources :

Example 6 with ServletContainer

use of org.glassfish.jersey.servlet.ServletContainer in project apache-kafka-on-k8s by banzaicloud.

the class JsonRestServer method start.

/**
 * Start the JsonRestServer.
 *
 * @param resources         The path handling resources to register.
 */
public void start(Object... resources) {
    log.info("Starting REST server");
    ResourceConfig resourceConfig = new ResourceConfig();
    resourceConfig.register(new JacksonJsonProvider(JsonUtil.JSON_SERDE));
    for (Object resource : resources) {
        resourceConfig.register(resource);
        log.info("Registered resource {}", resource);
    }
    resourceConfig.register(RestExceptionMapper.class);
    ServletContainer servletContainer = new ServletContainer(resourceConfig);
    ServletHolder servletHolder = new ServletHolder(servletContainer);
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    context.addServlet(servletHolder, "/*");
    RequestLogHandler requestLogHandler = new RequestLogHandler();
    Slf4jRequestLog requestLog = new Slf4jRequestLog();
    requestLog.setLoggerName(JsonRestServer.class.getCanonicalName());
    requestLog.setLogLatency(true);
    requestLogHandler.setRequestLog(requestLog);
    HandlerCollection handlers = new HandlerCollection();
    handlers.setHandlers(new Handler[] { context, new DefaultHandler(), requestLogHandler });
    StatisticsHandler statsHandler = new StatisticsHandler();
    statsHandler.setHandler(handlers);
    jettyServer.setHandler(statsHandler);
    /* Needed for graceful shutdown as per `setStopTimeout` documentation */
    jettyServer.setStopTimeout(GRACEFUL_SHUTDOWN_TIMEOUT_MS);
    jettyServer.setStopAtShutdown(true);
    try {
        jettyServer.start();
    } catch (Exception e) {
        throw new RuntimeException("Unable to start REST server", e);
    }
    log.info("REST server listening at " + jettyServer.getURI());
}
Also used : Slf4jRequestLog(org.eclipse.jetty.server.Slf4jRequestLog) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) JacksonJsonProvider(com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider) IOException(java.io.IOException) 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)

Example 7 with ServletContainer

use of org.glassfish.jersey.servlet.ServletContainer in project apache-kafka-on-k8s by banzaicloud.

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(new RootResource(herder));
    resourceConfig.register(new ConnectorsResource(herder, config));
    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 : RootResource(org.apache.kafka.connect.runtime.rest.resources.RootResource) 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) ConfigException(org.apache.kafka.common.config.ConfigException) 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 8 with ServletContainer

use of org.glassfish.jersey.servlet.ServletContainer in project spring-roo by spring-projects.

the class JAXRSApplication method addHttpService.

/**
 * Set the HTTP service in which register the servlet container that will
 * host the JAX-RS resources.
 * <p/>
 * After get an HTTP service, start the resource tracker to track
 * for REST services.
 *
 * @param service
 * @return
 * @throws ServletException
 * @throws NamespaceException
 */
public HttpService addHttpService(HttpService service) throws ServletException, NamespaceException {
    // 1st registered HttpService will win
    if (this.httpService == null) {
        this.servletContainer = new ServletContainer(this.resourceConfig);
        this.httpService = service;
        this.httpService.registerServlet(this.contextPath, servletContainer, null, null);
        // After get an HTTP service, start the resource tracker to track
        // for REST resources
        this.resourceTracker = new ResourceTracker(this.bc, this);
        this.resourceTracker.open();
        return service;
    } else {
        return this.httpService;
    }
}
Also used : ServletContainer(org.glassfish.jersey.servlet.ServletContainer)

Example 9 with ServletContainer

use of org.glassfish.jersey.servlet.ServletContainer in project incubator-pulsar by apache.

the class ProxyServer method addRestResources.

public void addRestResources(String basePath, String javaPackages, String attribute, Object attributeValue) {
    JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider();
    provider.setMapper(ObjectMapperFactory.create());
    ResourceConfig config = new ResourceConfig();
    config.packages("jersey.config.server.provider.packages", javaPackages);
    config.register(provider);
    ServletHolder servletHolder = new ServletHolder(new ServletContainer(config));
    servletHolder.setAsyncSupported(true);
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath(basePath);
    context.addServlet(servletHolder, "/*");
    context.setAttribute(attribute, attributeValue);
    handlers.add(context);
}
Also used : ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ServletContainer(org.glassfish.jersey.servlet.ServletContainer) JacksonJaxbJsonProvider(com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 10 with ServletContainer

use of org.glassfish.jersey.servlet.ServletContainer in project incubator-pulsar by apache.

the class WebService method addRestResources.

public void addRestResources(String basePath, String javaPackages, boolean requiresAuthentication, Map<String, Object> attributeMap) {
    JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider();
    provider.setMapper(ObjectMapperFactory.create());
    ResourceConfig config = new ResourceConfig();
    config.packages("jersey.config.server.provider.packages", javaPackages);
    config.register(provider);
    config.register(MultiPartFeature.class);
    ServletHolder servletHolder = new ServletHolder(new ServletContainer(config));
    servletHolder.setAsyncSupported(true);
    addServlet(basePath, servletHolder, requiresAuthentication, attributeMap);
}
Also used : ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ServletContainer(org.glassfish.jersey.servlet.ServletContainer) JacksonJaxbJsonProvider(com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider) ResourceConfig(org.glassfish.jersey.server.ResourceConfig)

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