Search in sources :

Example 1 with ConnectorPluginsResource

use of org.apache.kafka.connect.runtime.rest.resources.ConnectorPluginsResource 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 2 with ConnectorPluginsResource

use of org.apache.kafka.connect.runtime.rest.resources.ConnectorPluginsResource 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 3 with ConnectorPluginsResource

use of org.apache.kafka.connect.runtime.rest.resources.ConnectorPluginsResource in project kafka by apache.

the class RestServer method initializeResources.

public void initializeResources(Herder herder) {
    log.info("Initializing REST resources");
    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);
    resourceConfig.property(ServerProperties.WADL_FEATURE_DISABLE, true);
    registerRestExtensions(herder, resourceConfig);
    List<String> adminListeners = config.getList(WorkerConfig.ADMIN_LISTENERS_CONFIG);
    ResourceConfig adminResourceConfig;
    if (adminListeners == null) {
        log.info("Adding admin resources to main listener");
        adminResourceConfig = resourceConfig;
        adminResourceConfig.register(new LoggingResource());
    } else if (adminListeners.size() > 0) {
        // TODO: we need to check if these listeners are same as 'listeners'
        // TODO: the following code assumes that they are different
        log.info("Adding admin resources to admin listener");
        adminResourceConfig = new ResourceConfig();
        adminResourceConfig.register(new JacksonJsonProvider());
        adminResourceConfig.register(new LoggingResource());
        adminResourceConfig.register(ConnectExceptionMapper.class);
    } else {
        log.info("Skipping adding admin resources");
        // set up adminResource but add no handlers to it
        adminResourceConfig = resourceConfig;
    }
    ServletContainer servletContainer = new ServletContainer(resourceConfig);
    ServletHolder servletHolder = new ServletHolder(servletContainer);
    List<Handler> contextHandlers = new ArrayList<>();
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    context.addServlet(servletHolder, "/*");
    contextHandlers.add(context);
    ServletContextHandler adminContext = null;
    if (adminResourceConfig != resourceConfig) {
        adminContext = new ServletContextHandler(ServletContextHandler.SESSIONS);
        ServletHolder adminServletHolder = new ServletHolder(new ServletContainer(adminResourceConfig));
        adminContext.setContextPath("/");
        adminContext.addServlet(adminServletHolder, "/*");
        adminContext.setVirtualHosts(new String[] { "@" + ADMIN_SERVER_CONNECTOR_NAME });
        contextHandlers.add(adminContext);
    }
    String allowedOrigins = config.getString(WorkerConfig.ACCESS_CONTROL_ALLOW_ORIGIN_CONFIG);
    if (!Utils.isBlank(allowedOrigins)) {
        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 (!Utils.isBlank(allowedMethods)) {
            filterHolder.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM, allowedMethods);
        }
        context.addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST));
    }
    String headerConfig = config.getString(WorkerConfig.RESPONSE_HTTP_HEADERS_CONFIG);
    if (!Utils.isBlank(headerConfig)) {
        configureHttpResponsHeaderFilter(context);
    }
    RequestLogHandler requestLogHandler = new RequestLogHandler();
    Slf4jRequestLogWriter slf4jRequestLogWriter = new Slf4jRequestLogWriter();
    slf4jRequestLogWriter.setLoggerName(RestServer.class.getCanonicalName());
    CustomRequestLog requestLog = new CustomRequestLog(slf4jRequestLogWriter, CustomRequestLog.EXTENDED_NCSA_FORMAT + " %{ms}T");
    requestLogHandler.setRequestLog(requestLog);
    contextHandlers.add(new DefaultHandler());
    contextHandlers.add(requestLogHandler);
    handlers.setHandlers(contextHandlers.toArray(new Handler[0]));
    try {
        context.start();
    } catch (Exception e) {
        throw new ConnectException("Unable to initialize REST resources", e);
    }
    if (adminResourceConfig != resourceConfig) {
        try {
            log.debug("Starting admin context");
            adminContext.start();
        } catch (Exception e) {
            throw new ConnectException("Unable to initialize Admin REST resources", e);
        }
    }
    log.info("REST resources initialized; server is started and ready to handle requests");
}
Also used : RootResource(org.apache.kafka.connect.runtime.rest.resources.RootResource) ConnectorsResource(org.apache.kafka.connect.runtime.rest.resources.ConnectorsResource) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) LoggingResource(org.apache.kafka.connect.runtime.rest.resources.LoggingResource) CustomRequestLog(org.eclipse.jetty.server.CustomRequestLog) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) JacksonJsonProvider(com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider) ArrayList(java.util.ArrayList) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) StatisticsHandler(org.eclipse.jetty.server.handler.StatisticsHandler) Handler(org.eclipse.jetty.server.Handler) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) ConnectorPluginsResource(org.apache.kafka.connect.runtime.rest.resources.ConnectorPluginsResource) ConnectExceptionMapper(org.apache.kafka.connect.runtime.rest.errors.ConnectExceptionMapper) CrossOriginFilter(org.eclipse.jetty.servlets.CrossOriginFilter) Slf4jRequestLogWriter(org.eclipse.jetty.server.Slf4jRequestLogWriter) IOException(java.io.IOException) 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) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) ConnectException(org.apache.kafka.connect.errors.ConnectException)

Aggregations

JacksonJsonProvider (com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider)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 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)3 RequestLogHandler (org.eclipse.jetty.server.handler.RequestLogHandler)3 StatisticsHandler (org.eclipse.jetty.server.handler.StatisticsHandler)3 FilterHolder (org.eclipse.jetty.servlet.FilterHolder)3 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)3 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)3 CrossOriginFilter (org.eclipse.jetty.servlets.CrossOriginFilter)3 ResourceConfig (org.glassfish.jersey.server.ResourceConfig)3 ServletContainer (org.glassfish.jersey.servlet.ServletContainer)3 IOException (java.io.IOException)2 ConfigException (org.apache.kafka.common.config.ConfigException)2 RootResource (org.apache.kafka.connect.runtime.rest.resources.RootResource)2 Slf4jRequestLog (org.eclipse.jetty.server.Slf4jRequestLog)2 HandlerCollection (org.eclipse.jetty.server.handler.HandlerCollection)2 ArrayList (java.util.ArrayList)1 ConnectExceptionMapper (org.apache.kafka.connect.runtime.rest.errors.ConnectExceptionMapper)1