Search in sources :

Example 16 with GzipHandler

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

the class AthenzJettyContainer method addServletHandlers.

public void addServletHandlers(String serverHostName) {
    // Handler Structure
    RewriteHandler rewriteHandler = new RewriteHandler();
    // Check whether or not to disable Keep-Alive support in Jetty.
    // This will be the first handler in our array so we always set
    // the appropriate header in response. However, since we're now
    // behind ATS, we want to keep the connections alive so ATS
    // can re-use them as necessary
    boolean keepAlive = Boolean.parseBoolean(System.getProperty(AthenzConsts.ATHENZ_PROP_KEEP_ALIVE, "true"));
    if (!keepAlive) {
        HeaderPatternRule disableKeepAliveRule = new HeaderPatternRule();
        disableKeepAliveRule.setPattern("/*");
        disableKeepAliveRule.setName(HttpHeader.CONNECTION.asString());
        disableKeepAliveRule.setValue(HttpHeaderValue.CLOSE.asString());
        rewriteHandler.addRule(disableKeepAliveRule);
    }
    // Add response-headers, according to configuration
    final String responseHeadersJson = System.getProperty(AthenzConsts.ATHENZ_PROP_RESPONSE_HEADERS_JSON);
    if (!StringUtil.isEmpty(responseHeadersJson)) {
        HashMap<String, String> responseHeaders;
        try {
            responseHeaders = new ObjectMapper().readValue(responseHeadersJson, new TypeReference<HashMap<String, String>>() {
            });
        } catch (Exception exception) {
            throw new RuntimeException("System-property \"" + AthenzConsts.ATHENZ_PROP_RESPONSE_HEADERS_JSON + "\" must be a JSON object with string values. System property's value: " + responseHeadersJson);
        }
        for (Map.Entry<String, String> responseHeader : responseHeaders.entrySet()) {
            HeaderPatternRule rule = new HeaderPatternRule();
            rule.setPattern("/*");
            rule.setName(responseHeader.getKey());
            rule.setValue(responseHeader.getValue());
            rewriteHandler.addRule(rule);
        }
    }
    // Return a Host field in the response so during debugging
    // we know what server was handling request
    HeaderPatternRule hostNameRule = new HeaderPatternRule();
    hostNameRule.setPattern("/*");
    hostNameRule.setName(HttpHeader.HOST.asString());
    hostNameRule.setValue(serverHostName);
    rewriteHandler.addRule(hostNameRule);
    handlers.addHandler(rewriteHandler);
    ContextHandlerCollection contexts = new ContextHandlerCollection();
    // check to see if gzip support is enabled
    boolean gzipSupport = Boolean.parseBoolean(System.getProperty(AthenzConsts.ATHENZ_PROP_GZIP_SUPPORT, "false"));
    if (gzipSupport) {
        int gzipMinSize = Integer.parseInt(System.getProperty(AthenzConsts.ATHENZ_PROP_GZIP_MIN_SIZE, "1024"));
        GzipHandler gzipHandler = new GzipHandler();
        gzipHandler.setMinGzipSize(gzipMinSize);
        gzipHandler.setIncludedMimeTypes("application/json");
        gzipHandler.setHandler(contexts);
        handlers.addHandler(gzipHandler);
    }
    // check to see if graceful shutdown support is enabled
    boolean gracefulShutdown = Boolean.parseBoolean(System.getProperty(AthenzConsts.ATHENZ_PROP_GRACEFUL_SHUTDOWN, "false"));
    if (gracefulShutdown) {
        server.setStopAtShutdown(true);
        long stopTimeout = Long.parseLong(System.getProperty(AthenzConsts.ATHENZ_PROP_GRACEFUL_SHUTDOWN_TIMEOUT, "30000"));
        server.setStopTimeout(stopTimeout);
        StatisticsHandler statisticsHandler = new StatisticsHandler();
        statisticsHandler.setHandler(contexts);
        handlers.addHandler(statisticsHandler);
    }
    handlers.addHandler(contexts);
    // now setup our default servlet handler for filters
    ServletContextHandler servletCtxHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
    servletCtxHandler.setContextPath("/");
    FilterHolder filterHolder = new FilterHolder(HealthCheckFilter.class);
    final String healthCheckPath = System.getProperty(AthenzConsts.ATHENZ_PROP_HEALTH_CHECK_PATH, getRootDir());
    filterHolder.setInitParameter(AthenzConsts.ATHENZ_PROP_HEALTH_CHECK_PATH, healthCheckPath);
    final String checkList = System.getProperty(AthenzConsts.ATHENZ_PROP_HEALTH_CHECK_URI_LIST);
    if (!StringUtil.isEmpty(checkList)) {
        String[] checkUriArray = checkList.split(",");
        for (String checkUri : checkUriArray) {
            servletCtxHandler.addFilter(filterHolder, checkUri.trim(), EnumSet.of(DispatcherType.REQUEST));
        }
    }
    contexts.addHandler(servletCtxHandler);
    DeploymentManager deployer = new DeploymentManager();
    boolean debug = Boolean.parseBoolean(System.getProperty(AthenzConsts.ATHENZ_PROP_DEBUG, "false"));
    if (debug) {
        DebugListener debugListener = new DebugListener(System.err, true, true, true);
        server.addBean(debugListener);
        deployer.addLifeCycleBinding(new DebugListenerBinding(debugListener));
    }
    deployer.setContexts(contexts);
    deployer.setContextAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", ".*/servlet-api-[^/]*\\.jar$");
    final String jettyHome = System.getProperty(AthenzConsts.ATHENZ_PROP_JETTY_HOME, getRootDir());
    WebAppProvider webappProvider = new WebAppProvider();
    webappProvider.setMonitoredDirName(jettyHome + "/webapps");
    webappProvider.setScanInterval(60);
    webappProvider.setExtractWars(true);
    webappProvider.setConfigurationManager(new PropertiesConfigurationManager());
    webappProvider.setParentLoaderPriority(true);
    // set up a Default web.xml file.  file is applied to a Web application before it's own WEB_INF/web.xml
    setDefaultsDescriptor(webappProvider, jettyHome);
    final String jettyTemp = System.getProperty(AthenzConsts.ATHENZ_PROP_JETTY_TEMP, jettyHome + "/temp");
    webappProvider.setTempDir(new File(jettyTemp));
    deployer.addAppProvider(webappProvider);
    server.addBean(deployer);
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) DeploymentManager(org.eclipse.jetty.deploy.DeploymentManager) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) DebugListenerBinding(org.eclipse.jetty.deploy.bindings.DebugListenerBinding) PropertiesConfigurationManager(org.eclipse.jetty.deploy.PropertiesConfigurationManager) TypeReference(com.fasterxml.jackson.core.type.TypeReference) RewriteHandler(org.eclipse.jetty.rewrite.handler.RewriteHandler) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DebugListener(org.eclipse.jetty.server.DebugListener) WebAppProvider(org.eclipse.jetty.deploy.providers.WebAppProvider) HeaderPatternRule(org.eclipse.jetty.rewrite.handler.HeaderPatternRule) GzipHandler(org.eclipse.jetty.server.handler.gzip.GzipHandler) StatisticsHandler(org.eclipse.jetty.server.handler.StatisticsHandler) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Map(java.util.Map) HashMap(java.util.HashMap) ConfigProviderFile(com.yahoo.athenz.common.server.util.config.providers.ConfigProviderFile) File(java.io.File)

Example 17 with GzipHandler

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

the class ServletContextHandlerTest method testGzipHandlerSet.

@Test
public void testGzipHandlerSet() throws Exception {
    ServletContextHandler context = new ServletContextHandler();
    context.setSessionHandler(new SessionHandler());
    context.setGzipHandler(new GzipHandler());
    GzipHandler gzip = context.getGzipHandler();
    _server.start();
    assertEquals(context.getSessionHandler(), context.getHandler());
    assertEquals(gzip, context.getSessionHandler().getHandler());
    assertEquals(context.getServletHandler(), gzip.getHandler());
}
Also used : SessionHandler(org.eclipse.jetty.server.session.SessionHandler) GzipHandler(org.eclipse.jetty.server.handler.gzip.GzipHandler) Test(org.junit.Test)

Example 18 with GzipHandler

use of org.eclipse.jetty.server.handler.gzip.GzipHandler in project druid by druid-io.

the class JettyServerInitUtils method wrapWithDefaultGzipHandler.

public static GzipHandler wrapWithDefaultGzipHandler(final Handler handler) {
    GzipHandler gzipHandler = new GzipHandler();
    gzipHandler.setMinGzipSize(0);
    gzipHandler.setIncludedMethods(GZIP_METHODS);
    // We don't actually have any precomputed .gz resources, and checking for them inside jars is expensive.
    gzipHandler.setCheckGzExists(false);
    gzipHandler.setHandler(handler);
    return gzipHandler;
}
Also used : GzipHandler(org.eclipse.jetty.server.handler.gzip.GzipHandler)

Example 19 with GzipHandler

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

the class GzipHandlerTest method testAddGetPaths.

@Test
public void testAddGetPaths() {
    GzipHandler gzip = new GzipHandler();
    gzip.addIncludedPaths("/foo");
    gzip.addIncludedPaths("^/bar.*$");
    String[] includedPaths = gzip.getIncludedPaths();
    assertThat("Included Paths.size", includedPaths.length, is(2));
    assertThat("Included Paths", Arrays.asList(includedPaths), contains("/foo", "^/bar.*$"));
}
Also used : GzipHandler(org.eclipse.jetty.server.handler.gzip.GzipHandler) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 20 with GzipHandler

use of org.eclipse.jetty.server.handler.gzip.GzipHandler in project spring-boot by spring-projects.

the class JettyServletWebServerFactory method createGzipHandler.

private HandlerWrapper createGzipHandler() {
    GzipHandler handler = new GzipHandler();
    Compression compression = getCompression();
    handler.setMinGzipSize(compression.getMinResponseSize());
    handler.setIncludedMimeTypes(compression.getMimeTypes());
    if (compression.getExcludedUserAgents() != null) {
        handler.setExcludedAgentPatterns(compression.getExcludedUserAgents());
    }
    return handler;
}
Also used : Compression(org.springframework.boot.web.server.Compression) GzipHandler(org.eclipse.jetty.server.handler.gzip.GzipHandler)

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