Search in sources :

Example 26 with HttpConnectionFactory

use of org.eclipse.jetty.server.HttpConnectionFactory in project killbill by killbill.

the class HttpServer method configureSslConnector.

private ServerConnector configureSslConnector(final HttpConfiguration httpConfiguration, final boolean isStatsOn, final int localSslPort, final String sslKeyStorePath, final String sslKeyStorePassword) {
    // SSL Context Factory for HTTPS
    final SslContextFactory sslContextFactory = new SslContextFactory();
    sslContextFactory.setKeyStorePath(sslKeyStorePath);
    sslContextFactory.setKeyStorePassword(sslKeyStorePassword);
    // HTTPS Configuration
    final HttpConfiguration httpsConfig = new HttpConfiguration(httpConfiguration);
    httpsConfig.addCustomizer(new SecureRequestCustomizer());
    // HTTPS connector
    final ServerConnector https = new ServerConnector(server, new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()), new HttpConnectionFactory(httpsConfig));
    https.setPort(localSslPort);
    if (isStatsOn) {
        final ConnectorStatistics stats = new ConnectorStatistics();
        https.addBean(stats);
    }
    return https;
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) SecureRequestCustomizer(org.eclipse.jetty.server.SecureRequestCustomizer) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) ConnectorStatistics(org.eclipse.jetty.server.ConnectorStatistics) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory)

Example 27 with HttpConnectionFactory

use of org.eclipse.jetty.server.HttpConnectionFactory in project ninja by ninjaframework.

the class NinjaJetty method doConfigure.

@Override
protected void doConfigure() throws Exception {
    // current value or system property or conf/application.conf or default value
    jettyConfiguration(overlayedNinjaProperties.get(KEY_NINJA_JETTY_CONFIGURATION, this.jettyConfiguration, DEFAULT_JETTY_CONFIGURATION));
    // build jetty server, context, and servlet
    if (this.jettyConfiguration != null) {
        String[] configs = this.jettyConfiguration.split(",");
        for (String config : configs) {
            jetty = buildServerOrApplyConfiguration(config, jetty);
        }
        // since we don't know host and port, try to get it from jetty
        tryToSetHostAndPortFromJetty();
    } else {
        // create very simple jetty configuration
        jetty = new Server();
        if (port > -1) {
            // build http cleartext connector
            ServerConnector http = new ServerConnector(jetty);
            http.setPort(port);
            http.setIdleTimeout(idleTimeout);
            if (host != null) {
                http.setHost(host);
            }
            jetty.addConnector(http);
        }
        if (sslPort > -1) {
            // build https secure connector
            // http://git.eclipse.org/c/jetty/org.eclipse.jetty.project.git/tree/examples/embedded/src/main/java/org/eclipse/jetty/embedded/ManyConnectors.java
            HttpConfiguration httpConfig = new HttpConfiguration();
            httpConfig.setSecureScheme("https");
            httpConfig.setSecurePort(sslPort);
            httpConfig.setOutputBufferSize(32768);
            HttpConfiguration httpsConfig = new HttpConfiguration(httpConfig);
            httpsConfig.addCustomizer(new SecureRequestCustomizer());
            // unfortunately jetty seems to only work when we pass a keystore
            // and truststore (as opposed to our own prepared SSLContext)
            // call createSSLContext() to simply verify configuration is correct
            this.createSSLContext();
            SslContextFactory sslContextFactory = new SslContextFactory();
            sslContextFactory.setKeyStore(StandaloneHelper.loadKeyStore(this.sslKeystoreUri, this.sslKeystorePassword.toCharArray()));
            sslContextFactory.setKeyManagerPassword(this.sslKeystorePassword);
            sslContextFactory.setTrustStore(StandaloneHelper.loadKeyStore(this.sslTruststoreUri, this.sslTruststorePassword.toCharArray()));
            ServerConnector https = new ServerConnector(jetty, new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()), new HttpConnectionFactory(httpsConfig));
            https.setPort(sslPort);
            https.setIdleTimeout(idleTimeout);
            jetty.addConnector(https);
        }
    }
    this.ninjaServletListener.setNinjaProperties(ninjaProperties);
    this.contextHandler = new ServletContextHandler(jetty, getContextPath());
    this.contextHandler.addEventListener(ninjaServletListener);
    this.contextHandler.addFilter(GuiceFilter.class, "/*", null);
    this.contextHandler.addServlet(DefaultServlet.class, "/");
    // disable directory browsing
    this.contextHandler.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false");
    // Add an error handler that does not print stack traces in case
    // something happens that is not under control of Ninja
    this.contextHandler.setErrorHandler(new SilentErrorHandler());
}
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) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 28 with HttpConnectionFactory

use of org.eclipse.jetty.server.HttpConnectionFactory in project camel by apache.

the class WebsocketComponent method createStaticResourcesServer.

protected Server createStaticResourcesServer(ServletContextHandler context, String host, int port, String home) throws Exception {
    Server server = new Server();
    HttpConfiguration httpConfig = new HttpConfiguration();
    ServerConnector connector = new ServerConnector(server, new HttpConnectionFactory(httpConfig));
    connector.setHost(host);
    connector.setPort(port);
    server.addConnector(connector);
    return createStaticResourcesServer(server, context, home);
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration)

Example 29 with HttpConnectionFactory

use of org.eclipse.jetty.server.HttpConnectionFactory in project rest.li by linkedin.

the class HttpServerBuilder method build.

public Server build() {
    Server server = new Server();
    // HTTP Configuration
    HttpConfiguration configuration = new HttpConfiguration();
    configuration.setSendXPoweredBy(true);
    configuration.setSendServerVersion(true);
    configuration.setSendXPoweredBy(false);
    configuration.setSendServerVersion(false);
    configuration.setSendDateHeader(false);
    // HTTP Connector
    ServerConnector http = new ServerConnector(server, new HttpConnectionFactory(configuration), new HTTP2CServerConnectionFactory(configuration));
    http.setIdleTimeout(_idleTimeout);
    http.setPort(HTTP_PORT);
    server.addConnector(http);
    ServletContextHandler handler = new ServletContextHandler(server, "");
    handler.addServlet(new ServletHolder(new HttpServlet() {

        private static final long serialVersionUID = 0;

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
            awaitLatch();
            readEntity(req.getReader());
            addStatus(resp);
            addHeader(resp);
            addContent(resp);
        }

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            awaitLatch();
            readEntity(req.getReader());
            addStatus(resp);
            addHeader(resp);
            addContent(resp);
        }

        @Override
        protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            awaitLatch();
            readEntity(req.getReader());
            addStatus(resp);
            addHeader(resp);
            addContent(resp);
        }

        private void addStatus(HttpServletResponse resp) throws IOException {
            resp.setStatus(_status);
        }

        private void addHeader(HttpServletResponse resp) throws IOException {
            if (_headerSize <= 0) {
                return;
            }
            int valueSize = _headerSize - HEADER_NAME.length();
            char[] headerValue = new char[valueSize];
            Arrays.fill(headerValue, 'a');
            resp.addHeader(HEADER_NAME, new String(headerValue));
        }

        private void addContent(HttpServletResponse resp) throws IOException {
            if (_responseSize <= 0) {
                return;
            }
            char[] content = new char[_responseSize];
            Arrays.fill(content, 'a');
            resp.getWriter().write(content);
        }

        private void awaitLatch() {
            if (_responseLatch != null) {
                try {
                    _responseLatch.await(RESPONSE_LATCH_TIMEOUT, RESPONSE_LATCH_TIMEUNIT);
                } catch (InterruptedException e) {
                }
            }
        }

        private void readEntity(BufferedReader reader) throws IOException {
            while (true) {
                char[] bytes = new char[8192];
                int read = reader.read(bytes);
                if (read < 0) {
                    break;
                }
            }
        }
    }), "/*");
    return server;
}
Also used : Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) ServerConnector(org.eclipse.jetty.server.ServerConnector) HttpServletRequest(javax.servlet.http.HttpServletRequest) HTTP2CServerConnectionFactory(org.eclipse.jetty.http2.server.HTTP2CServerConnectionFactory) BufferedReader(java.io.BufferedReader) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 30 with HttpConnectionFactory

use of org.eclipse.jetty.server.HttpConnectionFactory in project rest.li by linkedin.

the class HttpsJettyServer method getConnectors.

@Override
protected Connector[] getConnectors(Server server) {
    SslContextFactory sslContextFactory = new SslContextFactory();
    sslContextFactory.setKeyStorePath(_keyStore);
    sslContextFactory.setKeyStorePassword(_keyStorePassword);
    sslContextFactory.setTrustStorePath(_keyStore);
    sslContextFactory.setTrustStorePassword(_keyStorePassword);
    HttpConfiguration configuration = new HttpConfiguration();
    configuration.addCustomizer(new SecureRequestCustomizer());
    ServerConnector sslConnector = new ServerConnector(server, sslContextFactory, new HttpConnectionFactory(configuration, HttpCompliance.RFC2616));
    sslConnector.setPort(_sslPort);
    Connector[] httpConnectors = super.getConnectors(server);
    Connector[] connectors = new Connector[httpConnectors.length + 1];
    int i = 0;
    for (Connector c : httpConnectors) {
        connectors[i++] = c;
    }
    connectors[i++] = sslConnector;
    return connectors;
}
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) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration)

Aggregations

HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)90 ServerConnector (org.eclipse.jetty.server.ServerConnector)79 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)69 Server (org.eclipse.jetty.server.Server)56 SslConnectionFactory (org.eclipse.jetty.server.SslConnectionFactory)44 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)43 SecureRequestCustomizer (org.eclipse.jetty.server.SecureRequestCustomizer)39 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)18 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)16 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)15 File (java.io.File)12 Connector (org.eclipse.jetty.server.Connector)12 IOException (java.io.IOException)11 ServletException (javax.servlet.ServletException)10 HttpServletRequest (javax.servlet.http.HttpServletRequest)9 HTTP2ServerConnectionFactory (org.eclipse.jetty.http2.server.HTTP2ServerConnectionFactory)9 Before (org.junit.Before)9 HttpServletResponse (javax.servlet.http.HttpServletResponse)8 ALPNServerConnectionFactory (org.eclipse.jetty.alpn.server.ALPNServerConnectionFactory)8 Request (org.eclipse.jetty.server.Request)8