Search in sources :

Example 11 with HandlerCollection

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

the class JettyHttpServer method findContextHandlerCollection.

private ContextHandlerCollection findContextHandlerCollection(Handler[] handlers) {
    if (handlers == null)
        return null;
    for (Handler handler : handlers) {
        if (handler instanceof ContextHandlerCollection) {
            return (ContextHandlerCollection) handler;
        }
        if (handler instanceof HandlerCollection) {
            HandlerCollection hc = (HandlerCollection) handler;
            ContextHandlerCollection chc = findContextHandlerCollection(hc.getHandlers());
            if (chc != null)
                return chc;
        }
    }
    return null;
}
Also used : Handler(org.eclipse.jetty.server.Handler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) HttpHandler(com.sun.net.httpserver.HttpHandler) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection)

Example 12 with HandlerCollection

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

the class JettyHttpServerProvider method createHttpServer.

@Override
public HttpServer createHttpServer(InetSocketAddress addr, int backlog) throws IOException {
    Server server = _server;
    boolean shared = true;
    if (server == null) {
        ThreadPool threadPool = new DelegatingThreadPool(new QueuedThreadPool());
        server = new Server(threadPool);
        HandlerCollection handlerCollection = new HandlerCollection();
        handlerCollection.setHandlers(new Handler[] { new ContextHandlerCollection(), new DefaultHandler() });
        server.setHandler(handlerCollection);
        shared = false;
    }
    JettyHttpServer jettyHttpServer = new JettyHttpServer(server, shared);
    jettyHttpServer.bind(addr, backlog);
    return jettyHttpServer;
}
Also used : HttpServer(com.sun.net.httpserver.HttpServer) HttpsServer(com.sun.net.httpserver.HttpsServer) Server(org.eclipse.jetty.server.Server) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ThreadPool(org.eclipse.jetty.util.thread.ThreadPool) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler)

Example 13 with HandlerCollection

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

the class AdminConsolePlugin method startup.

/**
     * Starts the Jetty instance.
     */
public void startup() {
    restartNeeded = false;
    // Add listener for certificate events
    certificateListener = new CertificateListener();
    CertificateManager.addListener(certificateListener);
    // the number of threads allocated to each connector/port
    int serverThreads = JiveGlobals.getXMLProperty("adminConsole.serverThreads", 2);
    adminPort = JiveGlobals.getXMLProperty("adminConsole.port", 9090);
    adminSecurePort = JiveGlobals.getXMLProperty("adminConsole.securePort", 9091);
    final QueuedThreadPool tp = new QueuedThreadPool();
    tp.setName("Jetty-QTP-AdminConsole");
    adminServer = new Server(tp);
    if (JMXManager.isEnabled()) {
        JMXManager jmx = JMXManager.getInstance();
        adminServer.addBean(jmx.getContainer());
    }
    // Create connector for http traffic if it's enabled.
    if (adminPort > 0) {
        final HttpConfiguration httpConfig = new HttpConfiguration();
        // Do not send Jetty info in HTTP headers
        httpConfig.setSendServerVersion(false);
        final ServerConnector httpConnector = new ServerConnector(adminServer, null, null, null, -1, serverThreads, new HttpConnectionFactory(httpConfig));
        // Listen on a specific network interface if it has been set.
        String bindInterface = getBindInterface();
        httpConnector.setHost(bindInterface);
        httpConnector.setPort(adminPort);
        adminServer.addConnector(httpConnector);
    }
    // Create a connector for https traffic if it's enabled.
    sslEnabled = false;
    try {
        IdentityStore identityStore = null;
        if (XMPPServer.getInstance().getCertificateStoreManager() == null) {
            Log.warn("Admin console: CertifcateStoreManager has not been initialized yet. HTTPS will be unavailable.");
        } else {
            identityStore = XMPPServer.getInstance().getCertificateStoreManager().getIdentityStore(ConnectionType.WEBADMIN);
        }
        if (identityStore != null && adminSecurePort > 0) {
            if (identityStore.getAllCertificates().isEmpty()) {
                Log.warn("Admin console: Identity store does not have any certificates. HTTPS will be unavailable.");
            } else {
                if (!identityStore.containsDomainCertificate("RSA")) {
                    Log.warn("Admin console: Using RSA certificates but they are not valid for the hosted domain");
                }
                final ConnectionManagerImpl connectionManager = ((ConnectionManagerImpl) XMPPServer.getInstance().getConnectionManager());
                final ConnectionConfiguration configuration = connectionManager.getListener(ConnectionType.WEBADMIN, true).generateConnectionConfiguration();
                final SslContextFactory sslContextFactory = new EncryptionArtifactFactory(configuration).getSslContextFactory();
                final ServerConnector httpsConnector;
                if ("npn".equals(JiveGlobals.getXMLProperty("spdy.protocol", ""))) {
                    httpsConnector = new HTTPSPDYServerConnector(adminServer, sslContextFactory);
                } else {
                    final HttpConfiguration httpsConfig = new HttpConfiguration();
                    httpsConfig.setSendServerVersion(false);
                    httpsConfig.setSecureScheme("https");
                    httpsConfig.setSecurePort(adminSecurePort);
                    httpsConfig.addCustomizer(new SecureRequestCustomizer());
                    final HttpConnectionFactory httpConnectionFactory = new HttpConnectionFactory(httpsConfig);
                    final SslConnectionFactory sslConnectionFactory = new SslConnectionFactory(sslContextFactory, org.eclipse.jetty.http.HttpVersion.HTTP_1_1.toString());
                    httpsConnector = new ServerConnector(adminServer, null, null, null, -1, serverThreads, sslConnectionFactory, httpConnectionFactory);
                }
                final String bindInterface = getBindInterface();
                httpsConnector.setHost(bindInterface);
                httpsConnector.setPort(adminSecurePort);
                adminServer.addConnector(httpsConnector);
                sslEnabled = true;
            }
        }
    } catch (Exception e) {
        Log.error("An exception occurred while trying to make available the admin console via HTTPS.", e);
    }
    // Make sure that at least one connector was registered.
    if (adminServer.getConnectors() == null || adminServer.getConnectors().length == 0) {
        adminServer = null;
        // Log warning.
        log(LocaleUtils.getLocalizedString("admin.console.warning"));
        return;
    }
    HandlerCollection collection = new HandlerCollection();
    adminServer.setHandler(collection);
    collection.setHandlers(new Handler[] { contexts, new DefaultHandler() });
    try {
        adminServer.start();
        // Log the ports that the admin server is listening on.
        logAdminConsolePorts();
    } catch (Exception e) {
        Log.error("Could not start admin console server", e);
    }
}
Also used : JMXManager(org.jivesoftware.openfire.JMXManager) SecureRequestCustomizer(org.eclipse.jetty.server.SecureRequestCustomizer) XMPPServer(org.jivesoftware.openfire.XMPPServer) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) ConnectionManagerImpl(org.jivesoftware.openfire.spi.ConnectionManagerImpl) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) HTTPSPDYServerConnector(org.eclipse.jetty.spdy.server.http.HTTPSPDYServerConnector) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) HTTPSPDYServerConnector(org.eclipse.jetty.spdy.server.http.HTTPSPDYServerConnector) ServerConnector(org.eclipse.jetty.server.ServerConnector) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) ConnectionConfiguration(org.jivesoftware.openfire.spi.ConnectionConfiguration) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) EncryptionArtifactFactory(org.jivesoftware.openfire.spi.EncryptionArtifactFactory) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) IdentityStore(org.jivesoftware.openfire.keystore.IdentityStore)

Example 14 with HandlerCollection

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

the class JettyServletWebServerFactoryTests method wrappedHandlers.

@Test
public void wrappedHandlers() throws Exception {
    JettyServletWebServerFactory factory = getFactory();
    factory.setServerCustomizers(Arrays.asList(new JettyServerCustomizer() {

        @Override
        public void customize(Server server) {
            Handler handler = server.getHandler();
            HandlerWrapper wrapper = new HandlerWrapper();
            wrapper.setHandler(handler);
            HandlerCollection collection = new HandlerCollection();
            collection.addHandler(wrapper);
            server.setHandler(collection);
        }
    }));
    this.webServer = factory.getWebServer(exampleServletRegistration());
    this.webServer.start();
    assertThat(getResponse(getLocalUrl("/hello"))).isEqualTo("Hello World");
}
Also used : Server(org.eclipse.jetty.server.Server) Handler(org.eclipse.jetty.server.Handler) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) HandlerWrapper(org.eclipse.jetty.server.handler.HandlerWrapper) Test(org.junit.Test)

Example 15 with HandlerCollection

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

the class Jetty9ServerTest method shouldSkipDefaultHeadersIfContextPathIsAnyOtherUrlWithinGo.

@Test
public void shouldSkipDefaultHeadersIfContextPathIsAnyOtherUrlWithinGo() 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/pipelines");
    rootPathHandler.handle("/go/pipelines", 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) 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)50 ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)25 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)22 Server (org.eclipse.jetty.server.Server)21 RequestLogHandler (org.eclipse.jetty.server.handler.RequestLogHandler)18 ServerConnector (org.eclipse.jetty.server.ServerConnector)17 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)14 Handler (org.eclipse.jetty.server.Handler)13 Test (org.junit.Test)12 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)8 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)8 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)7 URI (java.net.URI)6 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)6 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)6 File (java.io.File)5 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)5 HttpURLConnection (java.net.HttpURLConnection)4 NCSARequestLog (org.eclipse.jetty.server.NCSARequestLog)4 SecureRequestCustomizer (org.eclipse.jetty.server.SecureRequestCustomizer)4