Search in sources :

Example 51 with HandlerCollection

use of org.eclipse.jetty.server.handler.HandlerCollection in project sonarqube by SonarSource.

the class SSLTest method startSSLTransparentReverseProxy.

public static void startSSLTransparentReverseProxy(boolean requireClientAuth) throws Exception {
    int httpPort = NetworkUtils.getNextAvailablePort();
    httpsPort = NetworkUtils.getNextAvailablePort();
    // Setup Threadpool
    QueuedThreadPool threadPool = new QueuedThreadPool();
    threadPool.setMaxThreads(500);
    server = new Server(threadPool);
    // HTTP Configuration
    HttpConfiguration httpConfig = new HttpConfiguration();
    httpConfig.setSecureScheme("https");
    httpConfig.setSecurePort(httpsPort);
    httpConfig.setSendServerVersion(true);
    httpConfig.setSendDateHeader(false);
    // Handler Structure
    HandlerCollection handlers = new HandlerCollection();
    handlers.setHandlers(new Handler[] { proxyHandler(), new DefaultHandler() });
    server.setHandler(handlers);
    ServerConnector http = new ServerConnector(server, new HttpConnectionFactory(httpConfig));
    http.setPort(httpPort);
    server.addConnector(http);
    Path serverKeyStore = Paths.get(SSLTest.class.getResource("/analysis/SSLTest/serverkeystore.jks").toURI()).toAbsolutePath();
    String keyStorePassword = "serverkeystorepwd";
    String serverKeyPassword = "serverp12pwd";
    Path serverTrustStore = Paths.get(SSLTest.class.getResource("/analysis/SSLTest/servertruststore.jks").toURI()).toAbsolutePath();
    String trustStorePassword = "servertruststorepwd";
    // SSL Context Factory
    SslContextFactory sslContextFactory = new SslContextFactory();
    sslContextFactory.setKeyStorePath(serverKeyStore.toString());
    sslContextFactory.setKeyStorePassword(keyStorePassword);
    sslContextFactory.setKeyManagerPassword(serverKeyPassword);
    sslContextFactory.setTrustStorePath(serverTrustStore.toString());
    sslContextFactory.setTrustStorePassword(trustStorePassword);
    sslContextFactory.setNeedClientAuth(requireClientAuth);
    sslContextFactory.setExcludeCipherSuites("SSL_RSA_WITH_DES_CBC_SHA", "SSL_DHE_RSA_WITH_DES_CBC_SHA", "SSL_DHE_DSS_WITH_DES_CBC_SHA", "SSL_RSA_EXPORT_WITH_RC4_40_MD5", "SSL_RSA_EXPORT_WITH_DES40_CBC_SHA", "SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA", "SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA");
    // SSL HTTP Configuration
    HttpConfiguration httpsConfig = new HttpConfiguration(httpConfig);
    // SSL Connector
    ServerConnector sslConnector = new ServerConnector(server, new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()), new HttpConnectionFactory(httpsConfig));
    sslConnector.setPort(httpsPort);
    server.addConnector(sslConnector);
    server.start();
}
Also used : Path(java.nio.file.Path) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) ServerConnector(org.eclipse.jetty.server.ServerConnector) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection)

Example 52 with HandlerCollection

use of org.eclipse.jetty.server.handler.HandlerCollection in project pulsar by yahoo.

the class WebService method start.

public void start() throws PulsarServerException {
    try {
        RequestLogHandler requestLogHandler = new RequestLogHandler();
        Slf4jRequestLog requestLog = new Slf4jRequestLog();
        requestLog.setExtended(true);
        requestLog.setLogTimeZone(WebService.HANDLER_REQUEST_LOG_TZ);
        requestLog.setLogLatency(true);
        requestLogHandler.setRequestLog(requestLog);
        handlers.add(0, new ContextHandlerCollection());
        handlers.add(requestLogHandler);
        ContextHandlerCollection contexts = new ContextHandlerCollection();
        contexts.setHandlers(handlers.toArray(new Handler[handlers.size()]));
        HandlerCollection handlerCollection = new HandlerCollection();
        handlerCollection.setHandlers(new Handler[] { contexts, new DefaultHandler(), requestLogHandler });
        server.setHandler(handlerCollection);
        server.start();
        log.info("Web Service started at {}", pulsar.getWebServiceAddress());
    } catch (Exception e) {
        throw new PulsarServerException(e);
    }
}
Also used : Slf4jRequestLog(org.eclipse.jetty.server.Slf4jRequestLog) PulsarServerException(com.yahoo.pulsar.broker.PulsarServerException) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Handler(org.eclipse.jetty.server.Handler) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) GeneralSecurityException(java.security.GeneralSecurityException) PulsarServerException(com.yahoo.pulsar.broker.PulsarServerException) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler)

Example 53 with HandlerCollection

use of org.eclipse.jetty.server.handler.HandlerCollection in project geode by apache.

the class JettyHelper method initJetty.

public static Server initJetty(final String bindAddress, final int port, SSLConfig sslConfig) {
    final Server jettyServer = new Server();
    // Add a handler collection here, so that each new context adds itself
    // to this collection.
    jettyServer.setHandler(new HandlerCollection());
    ServerConnector connector = null;
    HttpConfiguration httpConfig = new HttpConfiguration();
    httpConfig.setSecureScheme(HTTPS);
    httpConfig.setSecurePort(port);
    if (sslConfig.isEnabled()) {
        SslContextFactory sslContextFactory = new SslContextFactory();
        if (StringUtils.isNotBlank(sslConfig.getAlias())) {
            sslContextFactory.setCertAlias(sslConfig.getAlias());
        }
        sslContextFactory.setNeedClientAuth(sslConfig.isRequireAuth());
        if (StringUtils.isNotBlank(sslConfig.getCiphers()) && !"any".equalsIgnoreCase(sslConfig.getCiphers())) {
            // If use has mentioned "any" let the SSL layer decide on the ciphers
            sslContextFactory.setIncludeCipherSuites(SSLUtil.readArray(sslConfig.getCiphers()));
        }
        String protocol = SSLUtil.getSSLAlgo(SSLUtil.readArray(sslConfig.getProtocols()));
        if (protocol != null) {
            sslContextFactory.setProtocol(protocol);
        } else {
            logger.warn(ManagementStrings.SSL_PROTOCOAL_COULD_NOT_BE_DETERMINED);
        }
        if (StringUtils.isBlank(sslConfig.getKeystore())) {
            throw new GemFireConfigException("Key store can't be empty if SSL is enabled for HttpService");
        }
        sslContextFactory.setKeyStorePath(sslConfig.getKeystore());
        if (StringUtils.isNotBlank(sslConfig.getKeystoreType())) {
            sslContextFactory.setKeyStoreType(sslConfig.getKeystoreType());
        }
        if (StringUtils.isNotBlank(sslConfig.getKeystorePassword())) {
            sslContextFactory.setKeyStorePassword(sslConfig.getKeystorePassword());
        }
        if (StringUtils.isNotBlank(sslConfig.getTruststore())) {
            sslContextFactory.setTrustStorePath(sslConfig.getTruststore());
        }
        if (StringUtils.isNotBlank(sslConfig.getTruststorePassword())) {
            sslContextFactory.setTrustStorePassword(sslConfig.getTruststorePassword());
        }
        httpConfig.addCustomizer(new SecureRequestCustomizer());
        // Somehow With HTTP_2.0 Jetty throwing NPE. Need to investigate further whether all GemFire
        // web application(Pulse, REST) can do with HTTP_1.1
        connector = new ServerConnector(jettyServer, new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()), new HttpConnectionFactory(httpConfig));
        connector.setPort(port);
    } else {
        connector = new ServerConnector(jettyServer, new HttpConnectionFactory(httpConfig));
        connector.setPort(port);
    }
    jettyServer.setConnectors(new Connector[] { connector });
    if (StringUtils.isNotBlank(bindAddress)) {
        connector.setHost(bindAddress);
    }
    if (bindAddress != null && !bindAddress.isEmpty()) {
        JettyHelper.bindAddress = bindAddress;
    }
    JettyHelper.port = port;
    return jettyServer;
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) SecureRequestCustomizer(org.eclipse.jetty.server.SecureRequestCustomizer) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) GemFireConfigException(org.apache.geode.GemFireConfigException) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory)

Example 54 with HandlerCollection

use of org.eclipse.jetty.server.handler.HandlerCollection in project geode by apache.

the class JettyHelper method addWebApplication.

public static Server addWebApplication(final Server jetty, final String webAppContext, final String warFilePath) {
    WebAppContext webapp = new WebAppContext();
    webapp.setContextPath(webAppContext);
    webapp.setWar(warFilePath);
    webapp.setParentLoaderPriority(false);
    webapp.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false");
    File tmpPath = new File(getWebAppBaseDirectory(webAppContext));
    tmpPath.mkdirs();
    webapp.setTempDirectory(tmpPath);
    ((HandlerCollection) jetty.getHandler()).addHandler(webapp);
    return jetty;
}
Also used : WebAppContext(org.eclipse.jetty.webapp.WebAppContext) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) File(java.io.File)

Example 55 with HandlerCollection

use of org.eclipse.jetty.server.handler.HandlerCollection in project gocd by gocd.

the class Jetty9ServerTest method shouldSkipDefaultHeadersIfContextPathIsGoRootPath.

@Test
public void shouldSkipDefaultHeadersIfContextPathIsGoRootPath() throws Exception {
    ArgumentCaptor<HandlerCollection> captor = ArgumentCaptor.forClass(HandlerCollection.class);
    jetty9Server.configure();
    verify(server, times(1)).setHandler(captor.capture());
    HandlerCollection handlerCollection = captor.getValue();
    Jetty9Server.GoServerWelcomeFileHandler handler = (Jetty9Server.GoServerWelcomeFileHandler) handlerCollection.getHandlers()[0];
    Handler rootPathHandler = handler.getHandler();
    HttpServletResponse response = mock(HttpServletResponse.class);
    when(response.getWriter()).thenReturn(mock(PrintWriter.class));
    HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getPathInfo()).thenReturn("/go");
    rootPathHandler.handle("/go", mock(Request.class), request, response);
    verify(response, never()).setHeader("X-XSS-Protection", "1; mode=block");
    verify(response, never()).setHeader("X-Content-Type-Options", "nosniff");
    verify(response, never()).setHeader("X-Frame-Options", "SAMEORIGIN");
    verify(response, never()).setHeader("X-UA-Compatible", "chrome=1");
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) GzipHandler(org.eclipse.jetty.servlets.gzip.GzipHandler) HttpServletResponse(javax.servlet.http.HttpServletResponse) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Aggregations

HandlerCollection (org.eclipse.jetty.server.handler.HandlerCollection)76 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)44 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)30 ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)27 Server (org.eclipse.jetty.server.Server)26 URISyntaxException (java.net.URISyntaxException)18 ServerConnector (org.eclipse.jetty.server.ServerConnector)18 RequestLogHandler (org.eclipse.jetty.server.handler.RequestLogHandler)18 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)16 Handler (org.eclipse.jetty.server.Handler)15 Test (org.junit.Test)13 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)9 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)9 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)8 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)7 GzipHandler (org.eclipse.jetty.servlets.gzip.GzipHandler)6 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)6 File (java.io.File)5 URI (java.net.URI)5 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)5