Search in sources :

Example 11 with RequestLogHandler

use of org.eclipse.jetty.server.handler.RequestLogHandler in project airlift by airlift.

the class HttpServer method createLogHandler.

private static RequestLogHandler createLogHandler(HttpServerConfig config, TraceTokenManager tokenManager, EventClient eventClient) throws IOException {
    // TODO: use custom (more easily-parseable) format
    // TODO: make retention & rotation configurable
    RequestLogHandler logHandler = new RequestLogHandler();
    File logFile = new File(config.getLogPath());
    if (logFile.exists() && !logFile.isFile()) {
        throw new IOException(format("Log path %s exists but is not a file", logFile.getAbsolutePath()));
    }
    File logPath = logFile.getParentFile();
    if (!logPath.mkdirs() && !logPath.exists()) {
        throw new IOException(format("Cannot create %s and path does not already exist", logPath.getAbsolutePath()));
    }
    RequestLog requestLog = new DelimitedRequestLog(config.getLogPath(), config.getLogHistory(), config.getLogQueueSize(), config.getLogMaxFileSize().toBytes(), tokenManager, eventClient, config.isLogCompressionEnabled());
    logHandler.setRequestLog(requestLog);
    return logHandler;
}
Also used : RequestLog(org.eclipse.jetty.server.RequestLog) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) IOException(java.io.IOException) File(java.io.File)

Example 12 with RequestLogHandler

use of org.eclipse.jetty.server.handler.RequestLogHandler in project athenz by yahoo.

the class AthenzJettyContainerTest method testRequestLogHandler.

@Test
public void testRequestLogHandler() {
    System.setProperty(AthenzConsts.ATHENZ_PROP_ACCESS_LOG_RETAIN_DAYS, "3");
    System.setProperty(AthenzConsts.ATHENZ_PROP_MAX_THREADS, "100");
    AthenzJettyContainer container = new AthenzJettyContainer();
    container.createServer(100);
    container.addRequestLogHandler();
    // now retrieve the request log handler
    Handler[] handlers = container.getHandlers().getHandlers();
    RequestLogHandler logHandler = null;
    for (Handler handler : handlers) {
        if (handler instanceof RequestLogHandler) {
            logHandler = (RequestLogHandler) handler;
            break;
        }
    }
    assertNotNull(logHandler);
    RequestLog reqLog = logHandler.getRequestLog();
    assertNotNull(reqLog);
    assertEquals(reqLog.getClass(), AthenzRequestLog.class);
}
Also used : RequestLog(org.eclipse.jetty.server.RequestLog) AthenzRequestLog(com.yahoo.athenz.container.log.AthenzRequestLog) Slf4jRequestLog(org.eclipse.jetty.server.Slf4jRequestLog) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Handler(org.eclipse.jetty.server.Handler) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) Test(org.testng.annotations.Test)

Example 13 with RequestLogHandler

use of org.eclipse.jetty.server.handler.RequestLogHandler in project knox by apache.

the class GatewayServer method createHandlers.

private static HandlerCollection createHandlers(final GatewayConfig config, final GatewayServices services, final ContextHandlerCollection contexts, final Map<String, Integer> topologyPortMap) {
    HandlerCollection handlers = new HandlerCollection();
    RequestLogHandler logHandler = new RequestLogHandler();
    logHandler.setRequestLog(new AccessHandler());
    TraceHandler traceHandler = new TraceHandler();
    traceHandler.setHandler(contexts);
    traceHandler.setTracedBodyFilter(System.getProperty("org.apache.knox.gateway.trace.body.status.filter"));
    CorrelationHandler correlationHandler = new CorrelationHandler();
    correlationHandler.setHandler(traceHandler);
    /* KNOX-732: Handler for GZip compression */
    GzipHandler gzipHandler = new GzipHandler();
    String[] mimeTypes = {};
    if (config.getMimeTypesToCompress() != null && !config.getMimeTypesToCompress().isEmpty()) {
        mimeTypes = (String[]) config.getMimeTypesToCompress().toArray();
    }
    gzipHandler.addIncludedMimeTypes(mimeTypes);
    gzipHandler.setHandler(correlationHandler);
    // Used to correct the {target} part of request with Topology Port Mapping feature
    final PortMappingHelperHandler portMappingHandler = new PortMappingHelperHandler(config);
    portMappingHandler.setHandler(gzipHandler);
    if (config.isGatewayPortMappingEnabled()) {
        for (final Map.Entry<String, Integer> entry : topologyPortMap.entrySet()) {
            log.createJettyHandler(entry.getKey());
            final ContextHandler topologyContextHandler = new ContextHandler();
            final RequestUpdateHandler updateHandler = new RequestUpdateHandler(config, entry.getKey(), services);
            topologyContextHandler.setHandler(updateHandler);
            topologyContextHandler.setVirtualHosts(new String[] { "@" + entry.getKey().toLowerCase() });
            handlers.addHandler(topologyContextHandler);
        }
    }
    handlers.addHandler(logHandler);
    if (config.isWebsocketEnabled()) {
        final GatewayWebsocketHandler websocketHandler = new GatewayWebsocketHandler(config, services);
        websocketHandler.setHandler(portMappingHandler);
        handlers.addHandler(websocketHandler);
    } else {
        handlers.addHandler(portMappingHandler);
    }
    return handlers;
}
Also used : CorrelationHandler(org.apache.knox.gateway.filter.CorrelationHandler) RequestUpdateHandler(org.apache.knox.gateway.filter.RequestUpdateHandler) TraceHandler(org.apache.knox.gateway.trace.TraceHandler) PortMappingHelperHandler(org.apache.knox.gateway.filter.PortMappingHelperHandler) AccessHandler(org.apache.knox.gateway.trace.AccessHandler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) GatewayWebsocketHandler(org.apache.knox.gateway.websockets.GatewayWebsocketHandler) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) GzipHandler(org.eclipse.jetty.servlets.gzip.GzipHandler) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 14 with RequestLogHandler

use of org.eclipse.jetty.server.handler.RequestLogHandler 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 15 with RequestLogHandler

use of org.eclipse.jetty.server.handler.RequestLogHandler 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)

Aggregations

RequestLogHandler (org.eclipse.jetty.server.handler.RequestLogHandler)45 HandlerCollection (org.eclipse.jetty.server.handler.HandlerCollection)30 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)25 ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)22 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)18 Handler (org.eclipse.jetty.server.Handler)14 Slf4jRequestLog (org.eclipse.jetty.server.Slf4jRequestLog)13 Server (org.eclipse.jetty.server.Server)12 ServerConnector (org.eclipse.jetty.server.ServerConnector)12 StatisticsHandler (org.eclipse.jetty.server.handler.StatisticsHandler)10 RequestLog (org.eclipse.jetty.server.RequestLog)8 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)8 File (java.io.File)7 IOException (java.io.IOException)7 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)7 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)7 NCSARequestLog (org.eclipse.jetty.server.NCSARequestLog)6 JacksonJsonProvider (com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider)5 Test (org.junit.Test)5 HttpURLConnection (java.net.HttpURLConnection)4