Search in sources :

Example 21 with GzipHandler

use of org.eclipse.jetty.server.handler.gzip.GzipHandler in project lucene-solr by apache.

the class JettySolrRunner method init.

private void init(int port) {
    QueuedThreadPool qtp = new QueuedThreadPool();
    qtp.setMaxThreads(THREAD_POOL_MAX_THREADS);
    qtp.setIdleTimeout(THREAD_POOL_MAX_IDLE_TIME_MS);
    qtp.setStopTimeout((int) TimeUnit.MINUTES.toMillis(1));
    server = new Server(qtp);
    server.manage(qtp);
    server.setStopAtShutdown(config.stopAtShutdown);
    if (System.getProperty("jetty.testMode") != null) {
        // if this property is true, then jetty will be configured to use SSL
        // leveraging the same system properties as java to specify
        // the keystore/truststore if they are set unless specific config
        // is passed via the constructor.
        //
        // This means we will use the same truststore, keystore (and keys) for
        // the server as well as any client actions taken by this JVM in
        // talking to that server, but for the purposes of testing that should 
        // be good enough
        final SslContextFactory sslcontext = SSLConfig.createContextFactory(config.sslConfig);
        ServerConnector connector;
        if (sslcontext != null) {
            HttpConfiguration configuration = new HttpConfiguration();
            configuration.setSecureScheme("https");
            configuration.addCustomizer(new SecureRequestCustomizer());
            connector = new ServerConnector(server, new SslConnectionFactory(sslcontext, "http/1.1"), new HttpConnectionFactory(configuration));
        } else {
            connector = new ServerConnector(server, new HttpConnectionFactory());
        }
        connector.setReuseAddress(true);
        connector.setSoLingerTime(-1);
        connector.setPort(port);
        connector.setHost("127.0.0.1");
        connector.setIdleTimeout(THREAD_POOL_MAX_IDLE_TIME_MS);
        server.setConnectors(new Connector[] { connector });
        server.setSessionIdManager(new HashSessionIdManager(new Random()));
    } else {
        ServerConnector connector = new ServerConnector(server, new HttpConnectionFactory());
        connector.setPort(port);
        connector.setSoLingerTime(-1);
        connector.setIdleTimeout(THREAD_POOL_MAX_IDLE_TIME_MS);
        server.setConnectors(new Connector[] { connector });
    }
    // Initialize the servlets
    final ServletContextHandler root = new ServletContextHandler(server, config.context, ServletContextHandler.SESSIONS);
    server.addLifeCycleListener(new LifeCycle.Listener() {

        @Override
        public void lifeCycleStopping(LifeCycle arg0) {
        }

        @Override
        public void lifeCycleStopped(LifeCycle arg0) {
        }

        @Override
        public void lifeCycleStarting(LifeCycle arg0) {
            synchronized (JettySolrRunner.this) {
                waitOnSolr = true;
                JettySolrRunner.this.notify();
            }
        }

        @Override
        public void lifeCycleStarted(LifeCycle arg0) {
            lastPort = getFirstConnectorPort();
            nodeProperties.setProperty("hostPort", Integer.toString(lastPort));
            nodeProperties.setProperty("hostContext", config.context);
            root.getServletContext().setAttribute(SolrDispatchFilter.PROPERTIES_ATTRIBUTE, nodeProperties);
            root.getServletContext().setAttribute(SolrDispatchFilter.SOLRHOME_ATTRIBUTE, solrHome);
            logger.info("Jetty properties: {}", nodeProperties);
            debugFilter = root.addFilter(DebugFilter.class, "*", EnumSet.of(DispatcherType.REQUEST));
            extraFilters = new LinkedList<>();
            for (Class<? extends Filter> filterClass : config.extraFilters.keySet()) {
                extraFilters.add(root.addFilter(filterClass, config.extraFilters.get(filterClass), EnumSet.of(DispatcherType.REQUEST)));
            }
            for (ServletHolder servletHolder : config.extraServlets.keySet()) {
                String pathSpec = config.extraServlets.get(servletHolder);
                root.addServlet(servletHolder, pathSpec);
            }
            dispatchFilter = root.getServletHandler().newFilterHolder(BaseHolder.Source.EMBEDDED);
            dispatchFilter.setHeldClass(SolrDispatchFilter.class);
            dispatchFilter.setInitParameter("excludePatterns", excludePatterns);
            root.addFilter(dispatchFilter, "*", EnumSet.of(DispatcherType.REQUEST));
        }

        @Override
        public void lifeCycleFailure(LifeCycle arg0, Throwable arg1) {
            System.clearProperty("hostPort");
        }
    });
    // for some reason, there must be a servlet for this to get applied
    root.addServlet(Servlet404.class, "/*");
    GzipHandler gzipHandler = new GzipHandler();
    gzipHandler.setHandler(root);
    gzipHandler.setMinGzipSize(0);
    gzipHandler.setCheckGzExists(false);
    gzipHandler.setCompressionLevel(-1);
    gzipHandler.setExcludedAgentPatterns(".*MSIE.6\\.0.*");
    gzipHandler.setIncludedMethods("GET");
    server.setHandler(gzipHandler);
}
Also used : LifeCycle(org.eclipse.jetty.util.component.LifeCycle) SolrDispatchFilter(org.apache.solr.servlet.SolrDispatchFilter) SecureRequestCustomizer(org.eclipse.jetty.server.SecureRequestCustomizer) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) HashSessionIdManager(org.eclipse.jetty.server.session.HashSessionIdManager) LinkedList(java.util.LinkedList) ServerConnector(org.eclipse.jetty.server.ServerConnector) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) Random(java.util.Random) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) SolrDispatchFilter(org.apache.solr.servlet.SolrDispatchFilter) Filter(javax.servlet.Filter) GzipHandler(org.eclipse.jetty.server.handler.gzip.GzipHandler) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 22 with GzipHandler

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

the class HttpServer method createServletContext.

private static ServletContextHandler createServletContext(Servlet theServlet, Map<String, String> parameters, Set<Filter> filters, TraceTokenManager tokenManager, LoginService loginService, String... connectorNames) {
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
    context.addFilter(new FilterHolder(new TimingFilter()), "/*", null);
    if (tokenManager != null) {
        context.addFilter(new FilterHolder(new TraceTokenFilter(tokenManager)), "/*", null);
    }
    // -- security handler
    if (loginService != null) {
        SecurityHandler securityHandler = createSecurityHandler(loginService);
        context.setSecurityHandler(securityHandler);
    }
    // -- user provided filters
    for (Filter filter : filters) {
        context.addFilter(new FilterHolder(filter), "/*", null);
    }
    // -- gzip handler
    context.setGzipHandler(new GzipHandler());
    // -- the servlet
    ServletHolder servletHolder = new ServletHolder(theServlet);
    servletHolder.setInitParameters(ImmutableMap.copyOf(parameters));
    context.addServlet(servletHolder, "/*");
    // Starting with Jetty 9 there is no way to specify connectors directly, but
    // there is this wonky @ConnectorName virtual hosts automatically added
    String[] virtualHosts = new String[connectorNames.length];
    for (int i = 0; i < connectorNames.length; i++) {
        virtualHosts[i] = "@" + connectorNames[i];
    }
    context.setVirtualHosts(virtualHosts);
    return context;
}
Also used : ConstraintSecurityHandler(org.eclipse.jetty.security.ConstraintSecurityHandler) SecurityHandler(org.eclipse.jetty.security.SecurityHandler) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) Filter(javax.servlet.Filter) GzipHandler(org.eclipse.jetty.server.handler.gzip.GzipHandler) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Constraint(org.eclipse.jetty.util.security.Constraint)

Example 23 with GzipHandler

use of org.eclipse.jetty.server.handler.gzip.GzipHandler in project nakadi by zalando.

the class JettyConfig method jettyEmbeddedServletContainerFactory.

@Bean
public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory(@Value("${server.port:8080}") final String port, @Value("${jetty.threadPool.maxThreads:200}") final String maxThreads, @Value("${jetty.threadPool.minThreads:8}") final String minThreads, @Value("${jetty.threadPool.idleTimeout:60000}") final String idleTimeout) {
    final JettyEmbeddedServletContainerFactory factory = new JettyEmbeddedServletContainerFactory(Integer.valueOf(port));
    factory.addServerCustomizers((JettyServerCustomizer) server -> {
        final QueuedThreadPool threadPool = server.getBean(QueuedThreadPool.class);
        threadPool.setMaxThreads(Integer.valueOf(maxThreads));
        threadPool.setMinThreads(Integer.valueOf(minThreads));
        threadPool.setIdleTimeout(Integer.valueOf(idleTimeout));
        final GzipHandler gzipHandler = new GzipHandler();
        gzipHandler.setHandler(server.getHandler());
        gzipHandler.setSyncFlush(true);
        server.setHandler(gzipHandler);
    });
    return factory;
}
Also used : Value(org.springframework.beans.factory.annotation.Value) Configuration(org.springframework.context.annotation.Configuration) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) JettyEmbeddedServletContainerFactory(org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory) JettyServerCustomizer(org.springframework.boot.context.embedded.jetty.JettyServerCustomizer) Bean(org.springframework.context.annotation.Bean) GzipHandler(org.eclipse.jetty.server.handler.gzip.GzipHandler) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) GzipHandler(org.eclipse.jetty.server.handler.gzip.GzipHandler) JettyEmbeddedServletContainerFactory(org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory) Bean(org.springframework.context.annotation.Bean)

Example 24 with GzipHandler

use of org.eclipse.jetty.server.handler.gzip.GzipHandler in project vespa by vespa-engine.

the class JettyHttpServer method newGzipHandler.

private GzipHandler newGzipHandler(ServerConfig serverConfig) {
    GzipHandler gzipHandler = new GzipHandlerWithVaryHeaderFixed();
    gzipHandler.setCompressionLevel(serverConfig.responseCompressionLevel());
    gzipHandler.setCheckGzExists(false);
    gzipHandler.setIncludedMethods("GET", "POST");
    return gzipHandler;
}
Also used : GzipHandler(org.eclipse.jetty.server.handler.gzip.GzipHandler)

Example 25 with GzipHandler

use of org.eclipse.jetty.server.handler.gzip.GzipHandler in project vespa by vespa-engine.

the class JettyHttpServer method getHandlerCollection.

private HandlerCollection getHandlerCollection(ServerConfig serverConfig, ServletPathsConfig servletPathsConfig, ServletHolder jdiscServlet, ComponentRegistry<ServletHolder> servletHolders, FilterHolder jDiscFilterInvokerFilter, RequestLog requestLog) {
    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, "/*");
    GzipHandler gzipHandler = newGzipHandler(serverConfig);
    gzipHandler.setHandler(servletContextHandler);
    StatisticsHandler statisticsHandler = newStatisticsHandler();
    statisticsHandler.setHandler(gzipHandler);
    RequestLogHandler requestLogHandler = new RequestLogHandler();
    requestLogHandler.setRequestLog(requestLog);
    HandlerCollection handlerCollection = new HandlerCollection();
    handlerCollection.setHandlers(new Handler[] { statisticsHandler, requestLogHandler });
    return handlerCollection;
}
Also used : GzipHandler(org.eclipse.jetty.server.handler.gzip.GzipHandler) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) DispatcherType(javax.servlet.DispatcherType) StatisticsHandler(org.eclipse.jetty.server.handler.StatisticsHandler)

Aggregations

GzipHandler (org.eclipse.jetty.server.handler.gzip.GzipHandler)32 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)11 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)9 Server (org.eclipse.jetty.server.Server)8 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)6 ServerConnector (org.eclipse.jetty.server.ServerConnector)6 FilterHolder (org.eclipse.jetty.servlet.FilterHolder)6 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)5 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)5 File (java.io.File)4 Filter (javax.servlet.Filter)3 Handler (org.eclipse.jetty.server.Handler)3 HandlerCollection (org.eclipse.jetty.server.handler.HandlerCollection)3 RequestLogHandler (org.eclipse.jetty.server.handler.RequestLogHandler)3 Constraint (org.eclipse.jetty.util.security.Constraint)3 Test (org.junit.jupiter.api.Test)3 IOException (java.io.IOException)2 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 DispatcherType (javax.servlet.DispatcherType)2