Java Code Examples for org.eclipse.jetty.server.handler.StatisticsHandler#setHandler()

The following examples show how to use org.eclipse.jetty.server.handler.StatisticsHandler#setHandler() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ApplicationServer.java    From rest-utils with Apache License 2.0 6 votes vote down vote up
private void finalizeHandlerCollection(HandlerCollection handlers, HandlerCollection wsHandlers) {
  /* DefaultHandler must come last eo ensure all contexts
   * have a chance to handle a request first */
  handlers.addHandler(new DefaultHandler());
  /* Needed for graceful shutdown as per `setStopTimeout` documentation */
  StatisticsHandler statsHandler = new StatisticsHandler();
  statsHandler.setHandler(handlers);

  ContextHandlerCollection contexts = new ContextHandlerCollection();
  contexts.setHandlers(new Handler[]{
      statsHandler,
      wsHandlers
  });

  super.setHandler(wrapWithGzipHandler(contexts));
}
 
Example 2
Source File: JettyStatisticsCollectorTest.java    From client_java with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
  server.addConnector(connector);
  HandlerCollection handlers = new HandlerCollection();

  ServletContextHandler context = new ServletContextHandler();
  context.setContextPath("/");
  handlers.setHandlers(new Handler[]{context});

  StatisticsHandler stats = new StatisticsHandler();
  stats.setHandler(handlers);
  server.setHandler(stats);

  // register collector
  new JettyStatisticsCollector(stats).register();

  server.setHandler(stats);
  server.start();
}
 
Example 3
Source File: RestServer.java    From DataLink with Apache License 2.0 4 votes vote down vote up
public void start(Keeper keeper) {
    log.info("Starting REST server");

    ResourceConfig resourceConfig = new ResourceConfig();
    resourceConfig.packages("com.ucar.datalink.worker.core.runtime.rest.resources");
    resourceConfig.register(new FastJsonProvider());
    resourceConfig.register(RootResource.class);
    resourceConfig.register(new TasksResource(keeper));
    resourceConfig.register(RestExceptionMapper.class);
    resourceConfig.register(FlushResource.class);
    resourceConfig.register(HBaseMetaResource.class);
    resourceConfig.register(WorkerResource.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 DatalinkException("Unable to start REST server", e);
    }

    log.info("REST server listening at " + jettyServer.getURI() + ", advertising URL " + advertisedUrl());
}
 
Example 4
Source File: RestServer.java    From DataLink with Apache License 2.0 4 votes vote down vote up
public void start() {
        log.info("Starting REST server");
        ResourceConfig resourceConfig = new ResourceConfig();
        resourceConfig.register(DataxResource.class);

        ServletContainer servletContainer = new ServletContainer(resourceConfig);
        ServletHolder servletHolder = new ServletHolder(servletContainer);

        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath("/");
        context.addServlet(servletHolder, "/*");

        String allowedOrigins = System.getProperty(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 = System.getProperty(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 RuntimeException("Unable to start REST server", e);
    }

    log.info("REST server listening at " + jettyServer.getURI() + ", advertising URL " + advertisedUrl());
}
 
Example 5
Source File: JettyHttpServer.java    From vespa with Apache License 2.0 4 votes vote down vote up
private HandlerCollection getHandlerCollection(
        ServerConfig serverConfig,
        ServletPathsConfig servletPathsConfig,
        List<JDiscServerConnector> connectors,
        ServletHolder jdiscServlet,
        ComponentRegistry<ServletHolder> servletHolders,
        FilterHolder jDiscFilterInvokerFilter) {

    ServletContextHandler servletContextHandler = createServletContextHandler();

    servletHolders.allComponentsById().forEach((id, servlet) -> {
        String path = getServletPath(servletPathsConfig, id);
        servletContextHandler.addServlet(servlet, path);
        servletContextHandler.addFilter(jDiscFilterInvokerFilter, path, EnumSet.allOf(DispatcherType.class));
    });

    servletContextHandler.addServlet(jdiscServlet, "/*");

    List<ConnectorConfig> connectorConfigs = connectors.stream().map(JDiscServerConnector::connectorConfig).collect(toList());
    var secureRedirectHandler = new SecuredRedirectHandler(connectorConfigs);
    secureRedirectHandler.setHandler(servletContextHandler);

    var proxyHandler = new HealthCheckProxyHandler(connectors);
    proxyHandler.setHandler(secureRedirectHandler);

    var authEnforcer = new TlsClientAuthenticationEnforcer(connectorConfigs);
    authEnforcer.setHandler(proxyHandler);

    GzipHandler gzipHandler = newGzipHandler(serverConfig);
    gzipHandler.setHandler(authEnforcer);

    HttpResponseStatisticsCollector statisticsCollector = new HttpResponseStatisticsCollector(serverConfig.metric().monitoringHandlerPaths());
    statisticsCollector.setHandler(gzipHandler);

    StatisticsHandler statisticsHandler = newStatisticsHandler();
    statisticsHandler.setHandler(statisticsCollector);

    HandlerCollection handlerCollection = new HandlerCollection();
    handlerCollection.setHandlers(new Handler[] { statisticsHandler });
    return handlerCollection;
}
 
Example 6
Source File: JettyServerFactory.java    From gravitee-management-rest-api with Apache License 2.0 4 votes vote down vote up
@Override
public Server getObject() throws Exception {

    // Setup ThreadPool
    QueuedThreadPool threadPool = new QueuedThreadPool(
            jettyConfiguration.getPoolMaxThreads(),
            jettyConfiguration.getPoolMinThreads(),
            jettyConfiguration.getPoolIdleTimeout(),
            new ArrayBlockingQueue<Runnable>(jettyConfiguration.getPoolQueueSize())
    );
    threadPool.setName("gravitee-listener");

    Server server = new Server(threadPool);

    // Extra options
    server.setDumpAfterStart(false);
    server.setDumpBeforeStop(false);
    server.setStopAtShutdown(true);

    // Setup JMX
    if (jettyConfiguration.isJmxEnabled()) {
        MBeanContainer mbContainer = new MBeanContainer(
                ManagementFactory.getPlatformMBeanServer());
        server.addBean(mbContainer);
    }

    // HTTP Configuration
    HttpConfiguration httpConfig = new HttpConfiguration();
    httpConfig.setOutputBufferSize(32768);
    httpConfig.setRequestHeaderSize(8192);
    httpConfig.setResponseHeaderSize(8192);
    httpConfig.setSendServerVersion(false);
    httpConfig.setSendDateHeader(false);

    // Setup Jetty HTTP or HTTPS Connector
    if (jettyConfiguration.isSecured()) {
        httpConfig.setSecureScheme("https");
        httpConfig.setSecurePort(jettyConfiguration.getHttpPort());

        // SSL Context Factory
        SslContextFactory sslContextFactory = new SslContextFactory.Server();

        if (jettyConfiguration.getKeyStorePath() != null) {
            sslContextFactory.setKeyStorePath(jettyConfiguration.getKeyStorePath());
            sslContextFactory.setKeyStorePassword(jettyConfiguration.getKeyStorePassword());

            if (KEYSTORE_TYPE_PKCS12.equalsIgnoreCase(jettyConfiguration.getKeyStoreType())) {
                sslContextFactory.setKeyStoreType(KEYSTORE_TYPE_PKCS12);
            }
        }

        if (jettyConfiguration.getTrustStorePath() != null) {
            sslContextFactory.setTrustStorePath(jettyConfiguration.getTrustStorePath());
            sslContextFactory.setTrustStorePassword(jettyConfiguration.getTrustStorePassword());

            if (KEYSTORE_TYPE_PKCS12.equalsIgnoreCase(jettyConfiguration.getTrustStoreType())) {
                sslContextFactory.setTrustStoreType(KEYSTORE_TYPE_PKCS12);
            }
        }

        HttpConfiguration httpsConfig = new HttpConfiguration(httpConfig);
        httpsConfig.addCustomizer(new SecureRequestCustomizer());

        ServerConnector https = new ServerConnector(server,
                new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()),
                new HttpConnectionFactory(httpsConfig));
        https.setHost(jettyConfiguration.getHttpHost());
        https.setPort(jettyConfiguration.getHttpPort());
        server.addConnector(https);
    } else {
        ServerConnector http = new ServerConnector(server,
                jettyConfiguration.getAcceptors(),
                jettyConfiguration.getSelectors(),
                new HttpConnectionFactory(httpConfig));
        http.setHost(jettyConfiguration.getHttpHost());
        http.setPort(jettyConfiguration.getHttpPort());
        http.setIdleTimeout(jettyConfiguration.getIdleTimeout());

        server.addConnector(http);
    }

    // Setup Jetty statistics
    if (jettyConfiguration.isStatisticsEnabled()) {
        StatisticsHandler stats = new StatisticsHandler();
        stats.setHandler(server.getHandler());
        server.setHandler(stats);
    }

    if (jettyConfiguration.isAccessLogEnabled()) {
        CustomRequestLog requestLog = new CustomRequestLog(
                new AsyncRequestLogWriter(jettyConfiguration.getAccessLogPath()),
                CustomRequestLog.EXTENDED_NCSA_FORMAT);

        server.setRequestLog(requestLog);
    }

    return server;
}
 
Example 7
Source File: JettyHttpTransport.java    From cougar with Apache License 2.0 4 votes vote down vote up
public void initialiseStaticJettyConfig() throws Exception {
        server.initialiseConnectors();

        ErrorHandler errorHandler = new CougarErrorHandler();
        wsdlStaticHandler = new StaticContentServiceHandler(
                wsdlContextPath,
                wsdlRegex,
                wsdlMediaType,
                uuidHeader,
                uuidParentsHeader,
                deserializer,
                geoIPLocator,
                requestLogger,
                true);
        wsdlStaticHandler.setUnknownCipherKeyLength(unknownCipherKeyLength);

        htmlStaticHandler = new StaticContentServiceHandler(
                htmlContextPath,
                htmlRegex,
                htmlMediaType,
                uuidHeader,
                uuidParentsHeader,
                deserializer,
                geoIPLocator,
                requestLogger,
                suppressCommasInAccessLogForStaticHtml);
        htmlStaticHandler.setUnknownCipherKeyLength(unknownCipherKeyLength);

        StatisticsHandler statisticsHandler = new StatisticsHandler();
        statisticsHandler.setServer(server.getJettyServer());

        handlerCollection.setServer(server.getJettyServer());

        JettyHandler defaultJettyServiceHandler = new AliasHandler(defaultCommandProcessor, suppressCommasInAccessLogForCalls, pathAliases);
        ContextHandler context = new ContextHandler();
        context.setContextPath("");
        context.setResourceBase(".");
        context.setHandler(defaultJettyServiceHandler);
        handlerCollection.addHandler(context);

        handlerCollection.addHandler(wsdlStaticHandler);
        handlerCollection.addHandler(htmlStaticHandler);
//        handlerCollection.addHandler(aliasHandler);
        statisticsHandler.setHandler(handlerCollection);

        // Register the errorhandler with the server itself
        server.addBean(errorHandler);
        server.setHandler(statisticsHandler);
    }