Search in sources :

Example 71 with Connector

use of org.eclipse.jetty.server.Connector in project hive by apache.

the class HttpServer method initializeWebServer.

private void initializeWebServer(final Builder b, int queueSize) throws IOException {
    // Set handling for low resource conditions.
    final LowResourceMonitor low = new LowResourceMonitor(webServer);
    low.setLowResourcesIdleTimeout(10000);
    webServer.addBean(low);
    Connector connector = createChannelConnector(queueSize, b);
    webServer.addConnector(connector);
    RewriteHandler rwHandler = new RewriteHandler();
    rwHandler.setRewriteRequestURI(true);
    rwHandler.setRewritePathInfo(false);
    RewriteRegexRule rootRule = new RewriteRegexRule();
    rootRule.setRegex("^/$");
    rootRule.setReplacement(b.contextRootRewriteTarget);
    rootRule.setTerminating(true);
    rwHandler.addRule(rootRule);
    rwHandler.setHandler(webAppContext);
    // Configure web application contexts for the web server
    ContextHandlerCollection contexts = new ContextHandlerCollection();
    contexts.addHandler(rwHandler);
    webServer.setHandler(contexts);
    if (b.usePAM) {
        setupPam(b, contexts);
    }
    addServlet("jmx", "/jmx", JMXJsonServlet.class);
    addServlet("conf", "/conf", ConfServlet.class);
    addServlet("stacks", "/stacks", StackServlet.class);
    addServlet("conflog", "/conflog", Log4j2ConfiguratorServlet.class);
    for (Pair<String, Class<? extends HttpServlet>> p : b.servlets) {
        addServlet(p.getFirst(), "/" + p.getFirst(), p.getSecond());
    }
    ServletContextHandler staticCtx = new ServletContextHandler(contexts, "/static");
    staticCtx.setResourceBase(appDir + "/static");
    staticCtx.addServlet(DefaultServlet.class, "/*");
    staticCtx.setDisplayName("static");
    String logDir = getLogDir(b.conf);
    if (logDir != null) {
        ServletContextHandler logCtx = new ServletContextHandler(contexts, "/logs");
        setContextAttributes(logCtx.getServletContext(), b.contextAttrs);
        logCtx.addServlet(AdminAuthorizedServlet.class, "/*");
        logCtx.setResourceBase(logDir);
        logCtx.setDisplayName("logs");
    }
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) Connector(org.eclipse.jetty.server.Connector) RewriteRegexRule(org.eclipse.jetty.rewrite.handler.RewriteRegexRule) HttpServlet(javax.servlet.http.HttpServlet) LowResourceMonitor(org.eclipse.jetty.server.LowResourceMonitor) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) RewriteHandler(org.eclipse.jetty.rewrite.handler.RewriteHandler)

Example 72 with Connector

use of org.eclipse.jetty.server.Connector in project elastic-core-maven by OrdinaryDude.

the class Peers method shutdown.

public static void shutdown() {
    if (Init.peerServer != null) {
        try {
            Init.peerServer.stop();
            if (enablePeerUPnP) {
                Connector[] peerConnectors = Init.peerServer.getConnectors();
                for (Connector peerConnector : peerConnectors) {
                    if (peerConnector instanceof ServerConnector)
                        UPnP.deletePort(((ServerConnector) peerConnector).getPort());
                }
            }
        } catch (Exception e) {
            Logger.logShutdownMessage("Failed to stop peer server", e);
        }
    }
    ThreadPool.shutdownExecutor("sendingService", sendingService, 2);
    ThreadPool.shutdownExecutor("peersService", peersService, 5);
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) ServerConnector(org.eclipse.jetty.server.ServerConnector) Connector(org.eclipse.jetty.server.Connector) URISyntaxException(java.net.URISyntaxException) TimeoutException(java.util.concurrent.TimeoutException) SocketException(java.net.SocketException) UnknownHostException(java.net.UnknownHostException) ExecutionException(java.util.concurrent.ExecutionException)

Example 73 with Connector

use of org.eclipse.jetty.server.Connector in project cxf by apache.

the class JettyHTTPServerEngineFactoryTest method testMakeSureJetty9ConnectorConfigured.

@Test
public void testMakeSureJetty9ConnectorConfigured() throws Exception {
    URL config = getClass().getResource("server-engine-factory-jetty9-connector.xml");
    bus = new SpringBusFactory().createBus(config, true);
    JettyHTTPServerEngineFactory factory = bus.getExtension(JettyHTTPServerEngineFactory.class);
    assertNotNull("EngineFactory is not configured.", factory);
    JettyHTTPServerEngine engine = null;
    engine = factory.createJettyHTTPServerEngine(1234, "http");
    assertNotNull("Engine is not available.", engine);
    assertEquals(1234, engine.getPort());
    assertEquals("Not http", "http", engine.getProtocol());
    Connector connector = engine.getConnector();
    Collection<ConnectionFactory> connectionFactories = connector.getConnectionFactories();
    assertEquals("Has one HttpConnectionFactory", 1, connectionFactories.size());
    ConnectionFactory connectionFactory = connectionFactories.iterator().next();
    assertTrue(connectionFactory instanceof HttpConnectionFactory);
    HttpConfiguration httpConfiguration = ((HttpConnectionFactory) connectionFactory).getHttpConfiguration();
    assertEquals("Has one ForwardedRequestCustomizer", 1, httpConfiguration.getCustomizers().size());
    assertTrue(httpConfiguration.getCustomizers().iterator().next() instanceof org.eclipse.jetty.server.ForwardedRequestCustomizer);
}
Also used : Connector(org.eclipse.jetty.server.Connector) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) ConnectionFactory(org.eclipse.jetty.server.ConnectionFactory) SpringBusFactory(org.apache.cxf.bus.spring.SpringBusFactory) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) URL(java.net.URL) Test(org.junit.Test)

Example 74 with Connector

use of org.eclipse.jetty.server.Connector in project scheduling by ow2-proactive.

the class JettyStarter method createHttpServer.

public Server createHttpServer(int httpPort, int httpsPort, boolean httpsEnabled, boolean redirectHttpToHttps) {
    int maxThreads = 100;
    if (WebProperties.WEB_MAX_THREADS.isSet()) {
        maxThreads = WebProperties.WEB_MAX_THREADS.getValueAsInt();
    }
    QueuedThreadPool threadPool = new QueuedThreadPool(maxThreads);
    Server server = new Server(threadPool);
    HttpConfiguration httpConfiguration = new HttpConfiguration();
    httpConfiguration.setSendDateHeader(false);
    httpConfiguration.setSendServerVersion(false);
    Connector[] connectors;
    if (httpsEnabled) {
        SslContextFactory sslContextFactory = new SslContextFactory();
        String httpsKeystore = WebProperties.WEB_HTTPS_KEYSTORE.getValueAsStringOrNull();
        String httpsKeystorePassword = WebProperties.WEB_HTTPS_KEYSTORE_PASSWORD.getValueAsStringOrNull();
        checkPropertyNotNull(WebProperties.WEB_HTTPS_KEYSTORE.getKey(), httpsKeystore);
        checkPropertyNotNull(WebProperties.WEB_HTTPS_KEYSTORE_PASSWORD.getKey(), httpsKeystorePassword);
        sslContextFactory.setKeyStorePath(absolutePathOrRelativeToSchedulerHome(httpsKeystore));
        sslContextFactory.setKeyStorePassword(httpsKeystorePassword);
        if (WebProperties.WEB_HTTPS_TRUSTSTORE.isSet() && WebProperties.WEB_HTTPS_TRUSTSTORE_PASSWORD.isSet()) {
            String httpsTrustStore = WebProperties.WEB_HTTPS_TRUSTSTORE.getValueAsString();
            String httpsTrustStorePassword = WebProperties.WEB_HTTPS_TRUSTSTORE_PASSWORD.getValueAsString();
            sslContextFactory.setTrustStorePath(httpsTrustStore);
            sslContextFactory.setTrustStorePassword(httpsTrustStorePassword);
        }
        HttpConfiguration secureHttpConfiguration = new HttpConfiguration(httpConfiguration);
        secureHttpConfiguration.addCustomizer(new SecureRequestCustomizer());
        secureHttpConfiguration.setSecurePort(httpsPort);
        secureHttpConfiguration.setSecureScheme("https");
        secureHttpConfiguration.setSendDateHeader(false);
        secureHttpConfiguration.setSendServerVersion(false);
        // Connector to listen for HTTPS requests
        ServerConnector httpsConnector = new ServerConnector(server, new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.toString()), new HttpConnectionFactory(secureHttpConfiguration));
        httpsConnector.setName(HTTPS_CONNECTOR_NAME);
        httpsConnector.setPort(httpsPort);
        if (redirectHttpToHttps) {
            // The next two settings allow !403 errors to be redirected to HTTPS
            httpConfiguration.setSecureScheme("https");
            httpConfiguration.setSecurePort(httpsPort);
            // Connector to listen for HTTP requests that are redirected to HTTPS
            ServerConnector httpConnector = createHttpConnector(server, httpConfiguration, httpPort);
            connectors = new Connector[] { httpConnector, httpsConnector };
        } else {
            connectors = new Connector[] { httpsConnector };
        }
    } else {
        ServerConnector httpConnector = createHttpConnector(server, httpConfiguration, httpPort);
        connectors = new Connector[] { httpConnector };
    }
    server.setConnectors(connectors);
    return server;
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) ServerConnector(org.eclipse.jetty.server.ServerConnector) Connector(org.eclipse.jetty.server.Connector) 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) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory)

Example 75 with Connector

use of org.eclipse.jetty.server.Connector in project jetty-bootstrap by teknux-org.

the class JettyBootstrap method createConnectors.

/**
 * Creates and returns the necessary {@link ServerConnector} based on the given {@link IJettyConfiguration}.
 *
 * @param iJettyConfiguration
 *            Jetty Configuration
 * @param server
 *            the server to
 * @return Connector[]
 * @throws JettyBootstrapException
 */
protected Connector[] createConnectors(IJettyConfiguration iJettyConfiguration, Server server) throws JettyBootstrapException {
    LOG.trace("Creating Jetty Connectors...");
    List<Connector> connectors = new ArrayList<>();
    if (iJettyConfiguration.hasJettyConnector(JettyConnector.HTTP)) {
        LOG.trace("Adding HTTP Connector...");
        ServerConnector serverConnector;
        if (iJettyConfiguration.hasJettyConnector(JettyConnector.HTTPS)) {
            HttpConfiguration httpConfiguration = new HttpConfiguration();
            httpConfiguration.setSecurePort(iJettyConfiguration.getSslPort());
            httpConfiguration.setSecureScheme(HttpScheme.HTTPS.asString());
            httpConfiguration.setIdleTimeout(iJettyConfiguration.getIdleTimeout());
            httpConfiguration.setBlockingTimeout(iJettyConfiguration.getBlockingTimeout());
            HttpConnectionFactory httpConnectionFactory = new HttpConnectionFactory(httpConfiguration);
            serverConnector = new ServerConnector(server, httpConnectionFactory);
        } else {
            serverConnector = new ServerConnector(server);
            serverConnector.getConnectionFactories().stream().filter(HttpConnectionFactory.class::isInstance).map(HttpConnectionFactory.class::cast).forEach(httpConnectionFactory -> httpConnectionFactory.getHttpConfiguration().setBlockingTimeout(iJettyConfiguration.getBlockingTimeout()));
        }
        serverConnector.setIdleTimeout(iJettyConfiguration.getIdleTimeout());
        serverConnector.setHost(iJettyConfiguration.getHost());
        serverConnector.setPort(iJettyConfiguration.getPort());
        connectors.add(serverConnector);
    }
    if (iJettyConfiguration.hasJettyConnector(JettyConnector.HTTPS)) {
        LOG.trace("Adding HTTPS Connector...");
        SslContextFactory sslContextFactory = new SslContextFactory();
        if (iJettyConfiguration.getSslKeyStore() != null) {
            // Use keyStore object if available
            sslContextFactory.setKeyStore(iJettyConfiguration.getSslKeyStore());
        } else if (iJettyConfiguration.getSslPrivateKeyPath() != null && !iJettyConfiguration.getSslPrivateKeyPath().isEmpty() && iJettyConfiguration.getSslCertificatePath() != null && !iJettyConfiguration.getSslCertificatePath().isEmpty()) {
            // Use private key and certificate if available
            JettyKeystoreConvertorBuilder jettyKeystoreConvertorBuilder = new JettyKeystoreConvertorBuilder();
            try {
                File sslPrivateKeyFile = new File(iJettyConfiguration.getSslPrivateKeyPath());
                if (!sslPrivateKeyFile.exists() || !sslPrivateKeyFile.canRead()) {
                    throw new JettyBootstrapException("Private key not exists or unreadable");
                }
                File sslCertificateFile = new File(iJettyConfiguration.getSslCertificatePath());
                if (!sslCertificateFile.exists() || !sslCertificateFile.canRead()) {
                    throw new JettyBootstrapException("Certificate not exists or unreadable");
                }
                try (InputStream sslPrivateKeyInputStream = new FileInputStream(sslPrivateKeyFile);
                    InputStream sslCertificateInputStream = new FileInputStream(sslCertificateFile)) {
                    switch(iJettyConfiguration.getSslCertificateFormat()) {
                        case PKCS8:
                            jettyKeystoreConvertorBuilder.setCertificateFromPKCS8(sslCertificateInputStream);
                            break;
                        case PKCS12:
                            jettyKeystoreConvertorBuilder.setCertificateFromPKCS12(sslCertificateInputStream, iJettyConfiguration.getSslCertificatePassword());
                            break;
                        case UNKNOWN:
                            throw new JettyBootstrapException("Unknown Certificate Format");
                        default:
                            throw new JettyBootstrapException("Certificate Format not setted");
                    }
                    switch(iJettyConfiguration.getSslPrivateKeyFormat()) {
                        case PKCS8:
                            jettyKeystoreConvertorBuilder.setPrivateKeyFromPKCS8(sslPrivateKeyInputStream);
                            break;
                        case PKCS12:
                            jettyKeystoreConvertorBuilder.setPrivateKeyFromPKCS12(sslPrivateKeyInputStream, iJettyConfiguration.getSslPrivateKeyPassword());
                            break;
                        case UNKNOWN:
                            throw new JettyBootstrapException("Unknown Private key Format");
                        default:
                            throw new JettyBootstrapException("Private key Format not setted");
                    }
                }
                KeyStore keyStore = jettyKeystoreConvertorBuilder.build(iJettyConfiguration.getSslKeyStoreAlias(), iJettyConfiguration.getSslKeyStorePassword());
                sslContextFactory.setKeyStore(keyStore);
            } catch (JettyKeystoreException | IOException e) {
                throw new JettyBootstrapException("Can not load SSL private key or SSL certificate", e);
            }
        } else {
            // Use keystore path
            sslContextFactory.setKeyStorePath(iJettyConfiguration.getSslKeyStorePath());
        }
        sslContextFactory.setKeyStorePassword(iJettyConfiguration.getSslKeyStorePassword());
        ServerConnector serverConnector = new ServerConnector(server, sslContextFactory);
        serverConnector.setIdleTimeout(iJettyConfiguration.getIdleTimeout());
        serverConnector.setHost(iJettyConfiguration.getHost());
        serverConnector.setPort(iJettyConfiguration.getSslPort());
        connectors.add(serverConnector);
    }
    return connectors.toArray(new Connector[connectors.size()]);
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) Connector(org.eclipse.jetty.server.Connector) JettyConnector(org.teknux.jettybootstrap.configuration.JettyConnector) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) JettyKeystoreConvertorBuilder(org.teknux.jettybootstrap.keystore.JettyKeystoreConvertorBuilder) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) ServerConnector(org.eclipse.jetty.server.ServerConnector) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) File(java.io.File)

Aggregations

Connector (org.eclipse.jetty.server.Connector)79 Server (org.eclipse.jetty.server.Server)41 ServerConnector (org.eclipse.jetty.server.ServerConnector)25 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)22 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)17 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)15 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)13 IOException (java.io.IOException)12 NetworkConnector (org.eclipse.jetty.server.NetworkConnector)12 Test (org.junit.Test)11 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)10 SelectChannelConnector (org.eclipse.jetty.server.nio.SelectChannelConnector)8 Test (org.testng.annotations.Test)8 Handler (org.eclipse.jetty.server.Handler)7 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)7 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 SecureRequestCustomizer (org.eclipse.jetty.server.SecureRequestCustomizer)6 SslConnectionFactory (org.eclipse.jetty.server.SslConnectionFactory)6 ServletException (javax.servlet.ServletException)5