Search in sources :

Example 1 with AsyncServerBootstrap

use of org.apache.hc.core5.http.impl.bootstrap.AsyncServerBootstrap in project commons-vfs by apache.

the class NHttpFileServer method start.

private NHttpFileServer start() throws KeyManagementException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException, InterruptedException, ExecutionException {
    final AsyncServerBootstrap bootstrap = AsyncServerBootstrap.bootstrap();
    SSLContext sslContext = null;
    if (port == 8443 || port == 443) {
        // Initialize SSL context
        final URL url = NHttpFileServer.class.getResource("/test.keystore");
        if (url == null) {
            println("Keystore not found");
            System.exit(1);
        }
        println("Loading keystore " + url);
        sslContext = SSLContexts.custom().loadKeyMaterial(url, "nopassword".toCharArray(), "nopassword".toCharArray()).build();
        bootstrap.setTlsStrategy(new BasicServerTlsStrategy(sslContext, new FixedPortStrategy(port)));
    }
    // @formatter:off
    final IOReactorConfig config = IOReactorConfig.custom().setSoTimeout(15, TimeUnit.SECONDS).setTcpNoDelay(true).build();
    // @formatter:on
    server = bootstrap.setIOReactorConfig(config).register("*", new HttpFileHandler(docRoot)).create();
    Runtime.getRuntime().addShutdownHook(new Thread(() -> close()));
    server.start();
    final Future<ListenerEndpoint> future = server.listen(new InetSocketAddress(port));
    listenerEndpoint = future.get();
    println("Serving " + docRoot + " on " + listenerEndpoint.getAddress() + (sslContext == null ? "" : " with " + sslContext.getProvider() + " " + sslContext.getProtocol()));
    return this;
}
Also used : IOReactorConfig(org.apache.hc.core5.reactor.IOReactorConfig) FixedPortStrategy(org.apache.hc.core5.http.nio.ssl.FixedPortStrategy) ListenerEndpoint(org.apache.hc.core5.reactor.ListenerEndpoint) InetSocketAddress(java.net.InetSocketAddress) BasicServerTlsStrategy(org.apache.hc.core5.http.nio.ssl.BasicServerTlsStrategy) SSLContext(javax.net.ssl.SSLContext) AsyncServerBootstrap(org.apache.hc.core5.http.impl.bootstrap.AsyncServerBootstrap) URL(java.net.URL)

Aggregations

InetSocketAddress (java.net.InetSocketAddress)1 URL (java.net.URL)1 SSLContext (javax.net.ssl.SSLContext)1 AsyncServerBootstrap (org.apache.hc.core5.http.impl.bootstrap.AsyncServerBootstrap)1 BasicServerTlsStrategy (org.apache.hc.core5.http.nio.ssl.BasicServerTlsStrategy)1 FixedPortStrategy (org.apache.hc.core5.http.nio.ssl.FixedPortStrategy)1 IOReactorConfig (org.apache.hc.core5.reactor.IOReactorConfig)1 ListenerEndpoint (org.apache.hc.core5.reactor.ListenerEndpoint)1