Search in sources :

Example 26 with GzipHandler

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

the class ThriftHttpCLIService method initServer.

/**
 * Configure Jetty to serve http requests. Example of a client connection URL:
 * http://localhost:10000/servlets/thrifths2/ A gateway may cause actual target
 * URL to differ, e.g. http://gateway:port/hive2/servlets/thrifths2/
 */
@Override
protected void initServer() {
    try {
        // Server thread pool
        // Start with minWorkerThreads, expand till maxWorkerThreads and reject
        // subsequent requests
        String threadPoolName = "HiveServer2-HttpHandler-Pool";
        ThreadPoolExecutor executorService = new ThreadPoolExecutor(minWorkerThreads, maxWorkerThreads, workerKeepAliveTime, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), new ThreadFactoryWithGarbageCleanup(threadPoolName));
        ExecutorThreadPool threadPool = new ExecutorThreadPool(executorService);
        // HTTP Server
        server = new Server(threadPool);
        ServerConnector connector;
        final HttpConfiguration conf = new HttpConfiguration();
        // Configure header size
        int requestHeaderSize = hiveConf.getIntVar(ConfVars.HIVE_SERVER2_THRIFT_HTTP_REQUEST_HEADER_SIZE);
        int responseHeaderSize = hiveConf.getIntVar(ConfVars.HIVE_SERVER2_THRIFT_HTTP_RESPONSE_HEADER_SIZE);
        conf.setRequestHeaderSize(requestHeaderSize);
        conf.setResponseHeaderSize(responseHeaderSize);
        final HttpConnectionFactory http = new HttpConnectionFactory(conf) {

            public Connection newConnection(Connector connector, EndPoint endPoint) {
                Connection connection = super.newConnection(connector, endPoint);
                connection.addListener(new Connection.Listener() {

                    public void onOpened(Connection connection) {
                        openConnection();
                    }

                    public void onClosed(Connection connection) {
                        closeConnection();
                    }
                });
                return connection;
            }
        };
        boolean useSsl = hiveConf.getBoolVar(ConfVars.HIVE_SERVER2_USE_SSL);
        String schemeName = useSsl ? "https" : "http";
        // Change connector if SSL is used
        if (useSsl) {
            String keyStorePath = hiveConf.getVar(ConfVars.HIVE_SERVER2_SSL_KEYSTORE_PATH).trim();
            String keyStorePassword = ShimLoader.getHadoopShims().getPassword(hiveConf, HiveConf.ConfVars.HIVE_SERVER2_SSL_KEYSTORE_PASSWORD.varname);
            if (keyStorePath.isEmpty()) {
                throw new IllegalArgumentException(ConfVars.HIVE_SERVER2_SSL_KEYSTORE_PATH.varname + " Not configured for SSL connection");
            }
            String keyStoreType = hiveConf.getVar(ConfVars.HIVE_SERVER2_SSL_KEYSTORE_TYPE).trim();
            if (keyStoreType.isEmpty()) {
                keyStoreType = KeyStore.getDefaultType();
            }
            String keyStoreAlgorithm = hiveConf.getVar(ConfVars.HIVE_SERVER2_SSL_KEYMANAGERFACTORY_ALGORITHM).trim();
            if (keyStoreAlgorithm.isEmpty()) {
                keyStoreAlgorithm = KeyManagerFactory.getDefaultAlgorithm();
            }
            SslContextFactory sslContextFactory = new SslContextFactory();
            String[] excludedProtocols = hiveConf.getVar(ConfVars.HIVE_SSL_PROTOCOL_BLACKLIST).split(",");
            LOG.info("HTTP Server SSL: adding excluded protocols: " + Arrays.toString(excludedProtocols));
            sslContextFactory.addExcludeProtocols(excludedProtocols);
            LOG.info("HTTP Server SSL: SslContextFactory.getExcludeProtocols = " + Arrays.toString(sslContextFactory.getExcludeProtocols()));
            sslContextFactory.setKeyStorePath(keyStorePath);
            sslContextFactory.setKeyStorePassword(keyStorePassword);
            sslContextFactory.setKeyStoreType(keyStoreType);
            sslContextFactory.setKeyManagerFactoryAlgorithm(keyStoreAlgorithm);
            String excludeCiphersuites = hiveConf.getVar(ConfVars.HIVE_SERVER2_SSL_HTTP_EXCLUDE_CIPHERSUITES).trim();
            if (!excludeCiphersuites.trim().isEmpty()) {
                Set<String> excludeCS = Sets.newHashSet(Splitter.on(",").trimResults().omitEmptyStrings().split(excludeCiphersuites.trim()));
                int eSize = excludeCS.size();
                if (eSize > 0) {
                    sslContextFactory.setExcludeCipherSuites(excludeCS.toArray(new String[eSize]));
                }
            }
            connector = new ServerConnector(server, sslContextFactory, http);
        } else {
            connector = new ServerConnector(server, http);
        }
        connector.setPort(portNum);
        // Linux:yes, Windows:no
        connector.setReuseAddress(true);
        int maxIdleTime = (int) hiveConf.getTimeVar(ConfVars.HIVE_SERVER2_THRIFT_HTTP_MAX_IDLE_TIME, TimeUnit.MILLISECONDS);
        connector.setIdleTimeout(maxIdleTime);
        connector.setAcceptQueueSize(maxWorkerThreads);
        server.addConnector(connector);
        // Thrift configs
        hiveAuthFactory = new HiveAuthFactory(hiveConf);
        TProcessor processor = new TCLIService.Processor<Iface>(this);
        TProtocolFactory protocolFactory = new TBinaryProtocol.Factory();
        // Set during the init phase of HiveServer2 if auth mode is kerberos
        // UGI for the hive/_HOST (kerberos) principal
        UserGroupInformation serviceUGI = cliService.getServiceUGI();
        // UGI for the http/_HOST (SPNego) principal
        UserGroupInformation httpUGI = cliService.getHttpUGI();
        String authType = hiveConf.getVar(ConfVars.HIVE_SERVER2_AUTHENTICATION);
        TServlet thriftHttpServlet = new ThriftHttpServlet(processor, protocolFactory, authType, serviceUGI, httpUGI, hiveAuthFactory, hiveConf);
        // Context handler
        final ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath("/");
        if (hiveConf.getBoolean(ConfVars.HIVE_SERVER2_XSRF_FILTER_ENABLED.varname, false)) {
            // context.addFilter(Utils.getXSRFFilterHolder(null, null), "/" ,
            // FilterMapping.REQUEST);
            // Filtering does not work here currently, doing filter in ThriftHttpServlet
            LOG.debug("XSRF filter enabled");
        } else {
            LOG.warn("XSRF filter disabled");
        }
        final String httpPath = getHttpPath(hiveConf.getVar(HiveConf.ConfVars.HIVE_SERVER2_THRIFT_HTTP_PATH));
        if (HiveConf.getBoolVar(hiveConf, ConfVars.HIVE_SERVER2_THRIFT_HTTP_COMPRESSION_ENABLED)) {
            final GzipHandler gzipHandler = new GzipHandler();
            gzipHandler.setHandler(context);
            gzipHandler.addIncludedMethods(HttpMethod.POST);
            gzipHandler.addIncludedMimeTypes(APPLICATION_THRIFT);
            server.setHandler(gzipHandler);
        } else {
            server.setHandler(context);
        }
        context.addServlet(new ServletHolder(thriftHttpServlet), httpPath);
        if (HiveSamlUtils.isSamlAuthMode(authType)) {
            String ssoPath = HiveSamlUtils.getCallBackPath(hiveConf);
            context.addServlet(new ServletHolder(new HiveSamlHttpServlet(hiveConf)), ssoPath);
        }
        constrainHttpMethods(context, false);
        // TODO: check defaults: maxTimeout, keepalive, maxBodySize,
        // bodyRecieveDuration, etc.
        // Finally, start the server
        server.start();
        String msg = "Started " + ThriftHttpCLIService.class.getSimpleName() + " in " + schemeName + " mode on port " + portNum + " path=" + httpPath + " with " + minWorkerThreads + "..." + maxWorkerThreads + " worker threads";
        LOG.info(msg);
    } catch (Exception e) {
        throw new RuntimeException("Failed to init HttpServer", e);
    }
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) Connector(org.eclipse.jetty.server.Connector) TProtocolFactory(org.apache.thrift.protocol.TProtocolFactory) ThreadFactoryWithGarbageCleanup(org.apache.hive.service.server.ThreadFactoryWithGarbageCleanup) TProcessor(org.apache.thrift.TProcessor) Server(org.eclipse.jetty.server.Server) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) HiveAuthFactory(org.apache.hive.service.auth.HiveAuthFactory) TProtocolFactory(org.apache.thrift.protocol.TProtocolFactory) MetricsFactory(org.apache.hadoop.hive.common.metrics.common.MetricsFactory) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) EndPoint(org.eclipse.jetty.io.EndPoint) TServlet(org.apache.thrift.server.TServlet) ServerConnector(org.eclipse.jetty.server.ServerConnector) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) TProcessor(org.apache.thrift.TProcessor) HiveSamlHttpServlet(org.apache.hive.service.auth.saml.HiveSamlHttpServlet) UserGroupInformation(org.apache.hadoop.security.UserGroupInformation) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) Connection(org.eclipse.jetty.io.Connection) EndPoint(org.eclipse.jetty.io.EndPoint) Constraint(org.eclipse.jetty.util.security.Constraint) GzipHandler(org.eclipse.jetty.server.handler.gzip.GzipHandler) ExecutorThreadPool(org.eclipse.jetty.util.thread.ExecutorThreadPool) HiveAuthFactory(org.apache.hive.service.auth.HiveAuthFactory) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 27 with GzipHandler

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

the class JettyHandlerWrappers method createGzipHandlerWrapper.

static HandlerWrapper createGzipHandlerWrapper(Compression compression) {
    GzipHandler handler = new GzipHandler();
    handler.setMinGzipSize((int) compression.getMinResponseSize().toBytes());
    handler.setIncludedMimeTypes(compression.getMimeTypes());
    for (HttpMethod httpMethod : HttpMethod.values()) {
        handler.addIncludedMethods(httpMethod.name());
    }
    return handler;
}
Also used : GzipHandler(org.eclipse.jetty.server.handler.gzip.GzipHandler) HttpMethod(org.eclipse.jetty.http.HttpMethod)

Example 28 with GzipHandler

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

the class WebConfiguration method customize.

@Override
public void customize(Server server) {
    customizeSsl(server);
    GzipHandler gzipHandler = new GzipHandler();
    gzipHandler.setIncludedMethods("PUT", "POST", "GET");
    gzipHandler.setInflateBufferSize(2048);
    gzipHandler.setHandler(server.getHandler());
    server.setHandler(gzipHandler);
}
Also used : GzipHandler(org.eclipse.jetty.server.handler.gzip.GzipHandler)

Example 29 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, Set<HttpResourceBinding> resources, 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);
    }
    // -- static resources
    for (HttpResourceBinding resource : resources) {
        ClassPathResourceFilter servlet = new ClassPathResourceFilter(resource.getBaseUri(), resource.getClassPathResourceBase(), resource.getWelcomeFiles());
        context.addFilter(new FilterHolder(servlet), servlet.getBaseUri() + "/*", 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) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HttpResourceBinding(io.airlift.http.server.HttpServerBinder.HttpResourceBinding) Constraint(org.eclipse.jetty.util.security.Constraint) Filter(javax.servlet.Filter) GzipHandler(org.eclipse.jetty.server.handler.gzip.GzipHandler) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 30 with GzipHandler

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

the class ServerDaemon method createHandlers.

private Pair<SessionHandler, HandlerCollection> createHandlers() {
    final WebAppContext webApp = new WebAppContext();
    webApp.setContextPath(contextPath);
    webApp.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false");
    // GZIP handler
    final GzipHandler gzipHandler = new GzipHandler();
    gzipHandler.addIncludedMimeTypes("text/html", "text/xml", "text/css", "text/plain", "text/javascript", "application/javascript", "application/json", "application/xml");
    gzipHandler.setIncludedMethods("GET", "POST");
    gzipHandler.setCompressionLevel(9);
    gzipHandler.setHandler(webApp);
    if (StringUtils.isEmpty(webAppLocation)) {
        webApp.setWar(getShadedWarUrl());
    } else {
        webApp.setWar(webAppLocation);
    }
    // Request log handler
    final RequestLogHandler log = new RequestLogHandler();
    log.setRequestLog(createRequestLog());
    // Redirect root context handler_war
    MovedContextHandler rootRedirect = new MovedContextHandler();
    rootRedirect.setContextPath("/");
    rootRedirect.setNewContextURL(contextPath);
    rootRedirect.setPermanent(true);
    // Put rootRedirect at the end!
    return new Pair<>(webApp.getSessionHandler(), new HandlerCollection(log, gzipHandler, rootRedirect));
}
Also used : WebAppContext(org.eclipse.jetty.webapp.WebAppContext) MovedContextHandler(org.eclipse.jetty.server.handler.MovedContextHandler) GzipHandler(org.eclipse.jetty.server.handler.gzip.GzipHandler) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) Pair(com.cloud.utils.Pair)

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