Search in sources :

Example 11 with ServerConnector

use of org.eclipse.jetty.server.ServerConnector in project hbase by apache.

the class HBaseRESTTestingUtility method startServletContainer.

public void startServletContainer(Configuration conf) throws Exception {
    if (server != null) {
        LOG.error("ServletContainer already running");
        return;
    }
    // Inject the conf for the test by being first to make singleton
    RESTServlet.getInstance(conf, UserProvider.instantiate(conf));
    // set up the Jersey servlet container for Jetty
    ResourceConfig app = new ResourceConfig().packages("org.apache.hadoop.hbase.rest").register(Jackson1Feature.class);
    ServletHolder sh = new ServletHolder(new ServletContainer(app));
    // set up Jetty and run the embedded server
    server = new Server(0);
    LOG.info("configured " + ServletContainer.class.getName());
    HttpConfiguration httpConfig = new HttpConfiguration();
    httpConfig.setSendDateHeader(false);
    httpConfig.setSendServerVersion(false);
    ServerConnector serverConnector = new ServerConnector(server, new HttpConnectionFactory(httpConfig));
    serverConnector.setPort(testServletPort);
    server.addConnector(serverConnector);
    // set up context
    ServletContextHandler ctxHandler = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
    ctxHandler.addServlet(sh, "/*");
    // Load filters specified from configuration.
    String[] filterClasses = conf.getStrings(Constants.FILTER_CLASSES, ArrayUtils.EMPTY_STRING_ARRAY);
    for (String filter : filterClasses) {
        filter = filter.trim();
        ctxHandler.addFilter(filter, "/*", EnumSet.of(DispatcherType.REQUEST));
    }
    LOG.info("Loaded filter classes :" + filterClasses);
    conf.set(RESTServer.REST_CSRF_BROWSER_USERAGENTS_REGEX_KEY, ".*");
    RESTServer.addCSRFFilter(ctxHandler, conf);
    HttpServerUtil.constrainHttpMethods(ctxHandler);
    // start the server
    server.start();
    // get the port
    testServletPort = ((ServerConnector) server.getConnectors()[0]).getLocalPort();
    LOG.info("started " + server.getClass().getName() + " on port " + testServletPort);
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ServletContainer(org.glassfish.jersey.servlet.ServletContainer) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 12 with ServerConnector

use of org.eclipse.jetty.server.ServerConnector in project hbase by apache.

the class TestHttpServer method checkBindAddress.

private HttpServer checkBindAddress(String host, int port, boolean findPort) throws Exception {
    HttpServer server = createServer(host, port);
    try {
        // not bound, ephemeral should return requested port (0 for ephemeral)
        List<?> listeners = (List<?>) Whitebox.getInternalState(server, "listeners");
        ServerConnector listener = (ServerConnector) Whitebox.getInternalState(listeners.get(0), "listener");
        assertEquals(port, listener.getPort());
        // verify hostname is what was given
        server.openListeners();
        assertEquals(host, server.getConnectorAddress(0).getHostName());
        int boundPort = server.getConnectorAddress(0).getPort();
        if (port == 0) {
            // ephemeral should now return bound port
            assertTrue(boundPort != 0);
        } else if (findPort) {
            assertTrue(boundPort > port);
            // allow a little wiggle room to prevent random test failures if
            // some consecutive ports are already in use
            assertTrue(boundPort - port < 8);
        }
    } catch (Exception e) {
        server.stop();
        throw e;
    }
    return server;
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) AccessControlList(org.apache.hadoop.security.authorize.AccessControlList) List(java.util.List) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 13 with ServerConnector

use of org.eclipse.jetty.server.ServerConnector in project zeppelin by apache.

the class ZeppelinServer method setupJettyServer.

private static Server setupJettyServer(ZeppelinConfiguration conf) {
    final Server server = new Server();
    ServerConnector connector;
    if (conf.useSsl()) {
        LOG.debug("Enabling SSL for Zeppelin Server on port " + conf.getServerSslPort());
        HttpConfiguration httpConfig = new HttpConfiguration();
        httpConfig.setSecureScheme("https");
        httpConfig.setSecurePort(conf.getServerSslPort());
        httpConfig.setOutputBufferSize(32768);
        httpConfig.setRequestHeaderSize(8192);
        httpConfig.setResponseHeaderSize(8192);
        httpConfig.setSendServerVersion(true);
        HttpConfiguration httpsConfig = new HttpConfiguration(httpConfig);
        SecureRequestCustomizer src = new SecureRequestCustomizer();
        // Only with Jetty 9.3.x
        // src.setStsMaxAge(2000);
        // src.setStsIncludeSubDomains(true);
        httpsConfig.addCustomizer(src);
        connector = new ServerConnector(server, new SslConnectionFactory(getSslContextFactory(conf), HttpVersion.HTTP_1_1.asString()), new HttpConnectionFactory(httpsConfig));
    } else {
        connector = new ServerConnector(server);
    }
    // Set some timeout options to make debugging easier.
    int timeout = 1000 * 30;
    connector.setIdleTimeout(timeout);
    connector.setSoLingerTime(-1);
    connector.setHost(conf.getServerAddress());
    if (conf.useSsl()) {
        connector.setPort(conf.getServerSslPort());
    } else {
        connector.setPort(conf.getServerPort());
    }
    server.addConnector(connector);
    return server;
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) SecureRequestCustomizer(org.eclipse.jetty.server.SecureRequestCustomizer) Server(org.eclipse.jetty.server.Server) NotebookServer(org.apache.zeppelin.socket.NotebookServer) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory)

Example 14 with ServerConnector

use of org.eclipse.jetty.server.ServerConnector in project dropwizard by dropwizard.

the class HttpConnectorFactory method buildConnector.

protected ServerConnector buildConnector(Server server, Scheduler scheduler, ByteBufferPool bufferPool, String name, ThreadPool threadPool, ConnectionFactory... factories) {
    final ServerConnector connector = new ServerConnector(server, threadPool, scheduler, bufferPool, acceptorThreads.orElse(-1), selectorThreads.orElse(-1), factories);
    connector.setPort(port);
    connector.setHost(bindHost);
    connector.setInheritChannel(inheritChannel);
    if (acceptQueueSize != null) {
        connector.setAcceptQueueSize(acceptQueueSize);
    } else {
        // if we do not set the acceptQueueSize, when jetty
        // creates the ServerSocket, it uses the default backlog of 50, and
        // not the value from the OS.  Therefore we set to the value
        // obtained from NetUtil, which will attempt to read the value from the OS.
        // somaxconn setting
        connector.setAcceptQueueSize(NetUtil.getTcpBacklog());
    }
    connector.setReuseAddress(reuseAddress);
    if (soLingerTime != null) {
        connector.setSoLingerTime((int) soLingerTime.toSeconds());
    }
    connector.setIdleTimeout(idleTimeout.toMilliseconds());
    connector.setName(name);
    return connector;
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector)

Example 15 with ServerConnector

use of org.eclipse.jetty.server.ServerConnector in project dropwizard by dropwizard.

the class HttpsConnectorFactoryTest method testBuild.

@Test
public void testBuild() throws Exception {
    final HttpsConnectorFactory https = new HttpsConnectorFactory();
    https.setBindHost("127.0.0.1");
    https.setPort(8443);
    https.setKeyStorePath("/etc/app/server.ks");
    https.setKeyStoreType("JKS");
    https.setKeyStorePassword("correct_horse");
    https.setKeyStoreProvider("BC");
    https.setTrustStorePath("/etc/app/server.ts");
    https.setTrustStoreType("JKS");
    https.setTrustStorePassword("battery_staple");
    https.setTrustStoreProvider("BC");
    https.setKeyManagerPassword("new_overlords");
    https.setNeedClientAuth(true);
    https.setWantClientAuth(true);
    https.setCertAlias("alt_server");
    https.setCrlPath(new File("/etc/ctr_list.txt"));
    https.setEnableCRLDP(true);
    https.setEnableOCSP(true);
    https.setMaxCertPathLength(4);
    https.setOcspResponderUrl(new URI("http://windc1/ocsp"));
    https.setJceProvider("BC");
    https.setAllowRenegotiation(false);
    https.setEndpointIdentificationAlgorithm("HTTPS");
    https.setValidateCerts(true);
    https.setValidatePeers(true);
    https.setSupportedProtocols(ImmutableList.of("TLSv1.1", "TLSv1.2"));
    https.setSupportedCipherSuites(ImmutableList.of("TLS_DHE_RSA.*", "TLS_ECDHE.*"));
    final Server server = new Server();
    final MetricRegistry metrics = new MetricRegistry();
    final ThreadPool threadPool = new QueuedThreadPool();
    final Connector connector = https.build(server, metrics, "test-https-connector", threadPool);
    assertThat(connector).isInstanceOf(ServerConnector.class);
    final ServerConnector serverConnector = (ServerConnector) connector;
    assertThat(serverConnector.getPort()).isEqualTo(8443);
    assertThat(serverConnector.getHost()).isEqualTo("127.0.0.1");
    assertThat(serverConnector.getName()).isEqualTo("test-https-connector");
    assertThat(serverConnector.getServer()).isSameAs(server);
    assertThat(serverConnector.getScheduler()).isInstanceOf(ScheduledExecutorScheduler.class);
    assertThat(serverConnector.getExecutor()).isSameAs(threadPool);
    final Jetty93InstrumentedConnectionFactory jetty93SslConnectionFacttory = (Jetty93InstrumentedConnectionFactory) serverConnector.getConnectionFactory("ssl");
    assertThat(jetty93SslConnectionFacttory).isInstanceOf(Jetty93InstrumentedConnectionFactory.class);
    assertThat(jetty93SslConnectionFacttory.getTimer()).isSameAs(metrics.timer("org.eclipse.jetty.server.HttpConnectionFactory.127.0.0.1.8443.connections"));
    final SslContextFactory sslContextFactory = ((SslConnectionFactory) jetty93SslConnectionFacttory.getConnectionFactory()).getSslContextFactory();
    assertThat(getField(SslContextFactory.class, "_keyStoreResource", true).get(sslContextFactory)).isEqualTo(Resource.newResource("/etc/app/server.ks"));
    assertThat(sslContextFactory.getKeyStoreType()).isEqualTo("JKS");
    assertThat(getField(SslContextFactory.class, "_keyStorePassword", true).get(sslContextFactory).toString()).isEqualTo("correct_horse");
    assertThat(sslContextFactory.getKeyStoreProvider()).isEqualTo("BC");
    assertThat(getField(SslContextFactory.class, "_trustStoreResource", true).get(sslContextFactory)).isEqualTo(Resource.newResource("/etc/app/server.ts"));
    assertThat(sslContextFactory.getKeyStoreType()).isEqualTo("JKS");
    assertThat(getField(SslContextFactory.class, "_trustStorePassword", true).get(sslContextFactory).toString()).isEqualTo("battery_staple");
    assertThat(sslContextFactory.getKeyStoreProvider()).isEqualTo("BC");
    assertThat(getField(SslContextFactory.class, "_keyManagerPassword", true).get(sslContextFactory).toString()).isEqualTo("new_overlords");
    assertThat(sslContextFactory.getNeedClientAuth()).isTrue();
    assertThat(sslContextFactory.getWantClientAuth()).isTrue();
    assertThat(sslContextFactory.getCertAlias()).isEqualTo("alt_server");
    assertThat(sslContextFactory.getCrlPath()).isEqualTo(new File("/etc/ctr_list.txt").getAbsolutePath());
    assertThat(sslContextFactory.isEnableCRLDP()).isTrue();
    assertThat(sslContextFactory.isEnableOCSP()).isTrue();
    assertThat(sslContextFactory.getMaxCertPathLength()).isEqualTo(4);
    assertThat(sslContextFactory.getOcspResponderURL()).isEqualTo("http://windc1/ocsp");
    assertThat(sslContextFactory.getProvider()).isEqualTo("BC");
    assertThat(sslContextFactory.isRenegotiationAllowed()).isFalse();
    assertThat(getField(SslContextFactory.class, "_endpointIdentificationAlgorithm", true).get(sslContextFactory)).isEqualTo("HTTPS");
    assertThat(sslContextFactory.isValidateCerts()).isTrue();
    assertThat(sslContextFactory.isValidatePeerCerts()).isTrue();
    assertThat(sslContextFactory.getIncludeProtocols()).containsOnly("TLSv1.1", "TLSv1.2");
    assertThat(sslContextFactory.getIncludeCipherSuites()).containsOnly("TLS_DHE_RSA.*", "TLS_ECDHE.*");
    final ConnectionFactory httpConnectionFactory = serverConnector.getConnectionFactory("http/1.1");
    assertThat(httpConnectionFactory).isInstanceOf(HttpConnectionFactory.class);
    final HttpConfiguration httpConfiguration = ((HttpConnectionFactory) httpConnectionFactory).getHttpConfiguration();
    assertThat(httpConfiguration.getSecureScheme()).isEqualTo("https");
    assertThat(httpConfiguration.getSecurePort()).isEqualTo(8443);
    assertThat(httpConfiguration.getCustomizers()).hasAtLeastOneElementOfType(SecureRequestCustomizer.class);
    connector.stop();
    server.stop();
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) Connector(org.eclipse.jetty.server.Connector) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) MetricRegistry(com.codahale.metrics.MetricRegistry) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ThreadPool(org.eclipse.jetty.util.thread.ThreadPool) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) URI(java.net.URI) ServerConnector(org.eclipse.jetty.server.ServerConnector) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) ConnectionFactory(org.eclipse.jetty.server.ConnectionFactory) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) File(java.io.File) Test(org.junit.Test)

Aggregations

ServerConnector (org.eclipse.jetty.server.ServerConnector)272 Server (org.eclipse.jetty.server.Server)205 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)80 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)73 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)63 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)53 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)51 SslConnectionFactory (org.eclipse.jetty.server.SslConnectionFactory)49 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)42 SecureRequestCustomizer (org.eclipse.jetty.server.SecureRequestCustomizer)40 URI (java.net.URI)37 Test (org.junit.Test)31 File (java.io.File)28 BeforeClass (org.junit.BeforeClass)28 IOException (java.io.IOException)26 Before (org.junit.Before)25 BeforeClass (org.testng.annotations.BeforeClass)22 ServletException (javax.servlet.ServletException)21 HttpServletRequest (javax.servlet.http.HttpServletRequest)19 HttpServletResponse (javax.servlet.http.HttpServletResponse)18