Search in sources :

Example 1 with LearningPushHandler

use of io.undertow.server.handlers.LearningPushHandler in project undertow by undertow-io.

the class Http2Server method main.

public static void main(final String[] args) throws Exception {
    String version = System.getProperty("java.version");
    System.out.println("Java version " + version);
    if (version.charAt(0) == '1' && Integer.parseInt(version.charAt(2) + "") < 8) {
        System.out.println("This example requires Java 1.8 or later");
        System.out.println("The HTTP2 spec requires certain cyphers that are not present in older JVM's");
        System.out.println("See section 9.2.2 of the HTTP2 specification for details");
        System.exit(1);
    }
    String bindAddress = System.getProperty("bind.address", "localhost");
    SSLContext sslContext = createSSLContext(loadKeyStore("server.keystore"), loadKeyStore("server.truststore"));
    Undertow server = Undertow.builder().setServerOption(UndertowOptions.ENABLE_HTTP2, true).addHttpListener(8080, bindAddress).addHttpsListener(8443, bindAddress, sslContext).setHandler(new SessionAttachmentHandler(new LearningPushHandler(100, -1, Handlers.header(predicate(secure(), resource(new PathResourceManager(Paths.get(System.getProperty("example.directory", System.getProperty("user.home"))), 100)).setDirectoryListingEnabled(true), new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseHeaders().add(Headers.LOCATION, "https://" + exchange.getHostName() + ":" + (exchange.getHostPort() + 363) + exchange.getRelativePath());
            exchange.setStatusCode(StatusCodes.TEMPORARY_REDIRECT);
        }
    }), "x-undertow-transport", ExchangeAttributes.transportProtocol())), new InMemorySessionManager("test"), new SessionCookieConfig())).build();
    server.start();
    SSLContext clientSslContext = createSSLContext(loadKeyStore("client.keystore"), loadKeyStore("client.truststore"));
    LoadBalancingProxyClient proxy = new LoadBalancingProxyClient().addHost(new URI("https://localhost:8443"), null, new UndertowXnioSsl(Xnio.getInstance(), OptionMap.EMPTY, clientSslContext), OptionMap.create(UndertowOptions.ENABLE_HTTP2, true)).setConnectionsPerThread(20);
    Undertow reverseProxy = Undertow.builder().setServerOption(UndertowOptions.ENABLE_HTTP2, true).addHttpListener(8081, bindAddress).addHttpsListener(8444, bindAddress, sslContext).setHandler(new ProxyHandler(proxy, 30000, ResponseCodeHandler.HANDLE_404)).build();
    reverseProxy.start();
}
Also used : HttpHandler(io.undertow.server.HttpHandler) ProxyHandler(io.undertow.server.handlers.proxy.ProxyHandler) SSLContext(javax.net.ssl.SSLContext) LearningPushHandler(io.undertow.server.handlers.LearningPushHandler) URI(java.net.URI) PathResourceManager(io.undertow.server.handlers.resource.PathResourceManager) LoadBalancingProxyClient(io.undertow.server.handlers.proxy.LoadBalancingProxyClient) HttpServerExchange(io.undertow.server.HttpServerExchange) SessionAttachmentHandler(io.undertow.server.session.SessionAttachmentHandler) SessionCookieConfig(io.undertow.server.session.SessionCookieConfig) UndertowXnioSsl(io.undertow.protocols.ssl.UndertowXnioSsl) Undertow(io.undertow.Undertow) InMemorySessionManager(io.undertow.server.session.InMemorySessionManager)

Aggregations

Undertow (io.undertow.Undertow)1 UndertowXnioSsl (io.undertow.protocols.ssl.UndertowXnioSsl)1 HttpHandler (io.undertow.server.HttpHandler)1 HttpServerExchange (io.undertow.server.HttpServerExchange)1 LearningPushHandler (io.undertow.server.handlers.LearningPushHandler)1 LoadBalancingProxyClient (io.undertow.server.handlers.proxy.LoadBalancingProxyClient)1 ProxyHandler (io.undertow.server.handlers.proxy.ProxyHandler)1 PathResourceManager (io.undertow.server.handlers.resource.PathResourceManager)1 InMemorySessionManager (io.undertow.server.session.InMemorySessionManager)1 SessionAttachmentHandler (io.undertow.server.session.SessionAttachmentHandler)1 SessionCookieConfig (io.undertow.server.session.SessionCookieConfig)1 URI (java.net.URI)1 SSLContext (javax.net.ssl.SSLContext)1