Search in sources :

Example 96 with ServerConnector

use of org.eclipse.jetty.server.ServerConnector in project spark by perwendel.

the class EmbeddedJettyServer method ignite.

/**
     * {@inheritDoc}
     */
@Override
public int ignite(String host, int port, SslStores sslStores, int maxThreads, int minThreads, int threadIdleTimeoutMillis) {
    if (port == 0) {
        try (ServerSocket s = new ServerSocket(0)) {
            port = s.getLocalPort();
        } catch (IOException e) {
            logger.error("Could not get first available port (port set to 0), using default: {}", SPARK_DEFAULT_PORT);
            port = SPARK_DEFAULT_PORT;
        }
    }
    server = JettyServer.create(maxThreads, minThreads, threadIdleTimeoutMillis);
    ServerConnector connector;
    if (sslStores == null) {
        connector = SocketConnectorFactory.createSocketConnector(server, host, port);
    } else {
        connector = SocketConnectorFactory.createSecureSocketConnector(server, host, port, sslStores);
    }
    server = connector.getServer();
    server.setConnectors(new Connector[] { connector });
    ServletContextHandler webSocketServletContextHandler = WebSocketServletContextHandlerFactory.create(webSocketHandlers, webSocketIdleTimeoutMillis);
    // Handle web socket routes
    if (webSocketServletContextHandler == null) {
        server.setHandler(handler);
    } else {
        List<Handler> handlersInList = new ArrayList<>();
        handlersInList.add(handler);
        // WebSocket handler must be the last one
        if (webSocketServletContextHandler != null) {
            handlersInList.add(webSocketServletContextHandler);
        }
        HandlerList handlers = new HandlerList();
        handlers.setHandlers(handlersInList.toArray(new Handler[handlersInList.size()]));
        server.setHandler(handlers);
    }
    try {
        logger.info("== {} has ignited ...", NAME);
        logger.info(">> Listening on {}:{}", host, port);
        server.start();
    } catch (Exception e) {
        logger.error("ignite failed", e);
        System.exit(100);
    }
    return port;
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) HandlerList(org.eclipse.jetty.server.handler.HandlerList) ArrayList(java.util.ArrayList) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Handler(org.eclipse.jetty.server.Handler) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) IOException(java.io.IOException)

Example 97 with ServerConnector

use of org.eclipse.jetty.server.ServerConnector in project spark by perwendel.

the class SocketConnectorFactoryTest method testCreateSocketConnector.

@Test
public void testCreateSocketConnector() {
    final String host = "localhost";
    final int port = 8888;
    Server server = new Server();
    ServerConnector serverConnector = SocketConnectorFactory.createSocketConnector(server, "localhost", 8888);
    String internalHost = Whitebox.getInternalState(serverConnector, "_host");
    int internalPort = Whitebox.getInternalState(serverConnector, "_port");
    Server internalServerConnector = Whitebox.getInternalState(serverConnector, "_server");
    assertEquals("Server Connector Host should be set to the specified server", host, internalHost);
    assertEquals("Server Connector Port should be set to the specified port", port, internalPort);
    assertEquals("Server Connector Server should be set to the specified server", internalServerConnector, server);
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) Server(org.eclipse.jetty.server.Server) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 98 with ServerConnector

use of org.eclipse.jetty.server.ServerConnector in project spark by perwendel.

the class ServletTest method setup.

@BeforeClass
public static void setup() throws InterruptedException {
    testUtil = new SparkTestUtil(PORT);
    final Server server = new Server();
    ServerConnector connector = new ServerConnector(server);
    // Set some timeout options to make debugging easier.
    connector.setIdleTimeout(1000 * 60 * 60);
    connector.setSoLingerTime(-1);
    connector.setPort(PORT);
    server.setConnectors(new Connector[] { connector });
    WebAppContext bb = new WebAppContext();
    bb.setServer(server);
    bb.setContextPath(SOMEPATH);
    bb.setWar("src/test/webapp");
    server.setHandler(bb);
    CountDownLatch latch = new CountDownLatch(1);
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                LOGGER.info(">>> STARTING EMBEDDED JETTY SERVER for jUnit testing of SparkFilter");
                server.start();
                latch.countDown();
                System.in.read();
                LOGGER.info(">>> STOPPING EMBEDDED JETTY SERVER");
                server.stop();
                server.join();
            } catch (Exception e) {
                e.printStackTrace();
                System.exit(100);
            }
        }
    }).start();
    latch.await();
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) Server(org.eclipse.jetty.server.Server) SparkTestUtil(spark.util.SparkTestUtil) CountDownLatch(java.util.concurrent.CountDownLatch) BeforeClass(org.junit.BeforeClass)

Example 99 with ServerConnector

use of org.eclipse.jetty.server.ServerConnector in project spring-boot by spring-projects.

the class JettyServletWebServerFactoryTests method sslEnabledProtocolsConfiguration.

@Test
public void sslEnabledProtocolsConfiguration() throws Exception {
    Ssl ssl = new Ssl();
    ssl.setKeyStore("src/test/resources/test.jks");
    ssl.setKeyStorePassword("secret");
    ssl.setKeyPassword("password");
    ssl.setCiphers(new String[] { "ALPHA", "BRAVO", "CHARLIE" });
    ssl.setEnabledProtocols(new String[] { "TLSv1.1" });
    JettyServletWebServerFactory factory = getFactory();
    factory.setSsl(ssl);
    this.webServer = factory.getWebServer();
    this.webServer.start();
    JettyWebServer jettyWebServer = (JettyWebServer) this.webServer;
    ServerConnector connector = (ServerConnector) jettyWebServer.getServer().getConnectors()[0];
    SslConnectionFactory connectionFactory = connector.getConnectionFactory(SslConnectionFactory.class);
    assertThat(connectionFactory.getSslContextFactory().getIncludeProtocols()).isEqualTo(new String[] { "TLSv1.1" });
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) Ssl(org.springframework.boot.web.server.Ssl) Test(org.junit.Test)

Example 100 with ServerConnector

use of org.eclipse.jetty.server.ServerConnector in project spring-framework by spring-projects.

the class JettyHttpServer method initServer.

@Override
protected void initServer() throws Exception {
    this.jettyServer = new Server();
    ServletHttpHandlerAdapter servlet = createServletAdapter();
    ServletHolder servletHolder = new ServletHolder(servlet);
    this.contextHandler = new ServletContextHandler(this.jettyServer, "", false, false);
    this.contextHandler.addServlet(servletHolder, "/");
    this.contextHandler.start();
    ServerConnector connector = new ServerConnector(this.jettyServer);
    connector.setHost(getHost());
    connector.setPort(getPort());
    this.jettyServer.addConnector(connector);
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) Server(org.eclipse.jetty.server.Server) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ServletHttpHandlerAdapter(org.springframework.http.server.reactive.ServletHttpHandlerAdapter) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

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