Search in sources :

Example 26 with UndertowXnioSsl

use of io.undertow.protocols.ssl.UndertowXnioSsl in project undertow by undertow-io.

the class WebSocketClient13TestCase method testTextMessageSecure.

public void testTextMessageSecure(final String urlProtocol) throws Exception {
    UndertowXnioSsl ssl = new UndertowXnioSsl(Xnio.getInstance(), OptionMap.EMPTY, DefaultServer.getClientSSLContext());
    final WebSocketClient.ConnectionBuilder connectionBuilder = WebSocketClient.connectionBuilder(worker, DefaultServer.getBufferPool(), new URI(urlProtocol + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostSSLPort("default"))).setSsl(ssl);
    IoFuture<WebSocketChannel> future = connectionBuilder.connect();
    future.await(4, TimeUnit.SECONDS);
    final WebSocketChannel webSocketChannel = future.get();
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<String> result = new AtomicReference<>();
    webSocketChannel.getReceiveSetter().set(new AbstractReceiveListener() {

        @Override
        protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) throws IOException {
            String data = message.getData();
            result.set(data);
            latch.countDown();
        }

        @Override
        protected void onError(WebSocketChannel channel, Throwable error) {
            super.onError(channel, error);
            error.printStackTrace();
            latch.countDown();
        }
    });
    webSocketChannel.resumeReceives();
    StreamSinkFrameChannel sendChannel = webSocketChannel.send(WebSocketFrameType.TEXT);
    new StringWriteChannelListener("Hello World").setup(sendChannel);
    latch.await(10, TimeUnit.SECONDS);
    Assert.assertEquals("Hello World", result.get());
    webSocketChannel.sendClose();
}
Also used : WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) WebSocketClient(io.undertow.websockets.client.WebSocketClient) CountDownLatch(java.util.concurrent.CountDownLatch) StreamSinkFrameChannel(io.undertow.websockets.core.StreamSinkFrameChannel) URI(java.net.URI) BufferedTextMessage(io.undertow.websockets.core.BufferedTextMessage) AbstractReceiveListener(io.undertow.websockets.core.AbstractReceiveListener) StringWriteChannelListener(io.undertow.util.StringWriteChannelListener) UndertowXnioSsl(io.undertow.protocols.ssl.UndertowXnioSsl)

Example 27 with UndertowXnioSsl

use of io.undertow.protocols.ssl.UndertowXnioSsl in project undertow by undertow-io.

the class LoadBalancingProxyHTTP2TestCase method setup.

@BeforeClass
public static void setup() throws URISyntaxException {
    int port = DefaultServer.getHostPort("default");
    final HttpHandler handler1 = getRootHandler("s1", "server1");
    server1 = Undertow.builder().addHttpsListener(port + 1, DefaultServer.getHostAddress("default"), DefaultServer.getServerSslContext()).setServerOption(UndertowOptions.ENABLE_HTTP2, true).setServerOption(UndertowOptions.NO_REQUEST_TIMEOUT, IDLE_TIMEOUT).setSocketOption(Options.REUSE_ADDRESSES, true).setHandler(new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            if (!(exchange.getConnection() instanceof Http2ServerConnection)) {
                throw new RuntimeException("Not HTTP2");
            }
            exchange.getResponseHeaders().add(new HttpString("X-Custom-Header"), "foo");
            handler1.handleRequest(exchange);
        }
    }).build();
    final HttpHandler handler2 = getRootHandler("s2", "server2");
    server2 = Undertow.builder().addHttpsListener(port + 2, DefaultServer.getHostAddress("default"), DefaultServer.getServerSslContext()).setServerOption(UndertowOptions.ENABLE_HTTP2, true).setSocketOption(Options.REUSE_ADDRESSES, true).setServerOption(UndertowOptions.NO_REQUEST_TIMEOUT, IDLE_TIMEOUT).setHandler(new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            if (!(exchange.getConnection() instanceof Http2ServerConnection)) {
                throw new RuntimeException("Not HTTP2");
            }
            exchange.getResponseHeaders().add(new HttpString("X-Custom-Header"), "foo");
            handler2.handleRequest(exchange);
        }
    }).build();
    server1.start();
    server2.start();
    UndertowXnioSsl ssl = new UndertowXnioSsl(DefaultServer.getWorker().getXnio(), OptionMap.EMPTY, DefaultServer.SSL_BUFFER_POOL, DefaultServer.createClientSslContext());
    DefaultServer.setRootHandler(ProxyHandler.builder().setProxyClient(new LoadBalancingProxyClient().setConnectionsPerThread(4).addHost(new URI("https", null, DefaultServer.getHostAddress("default"), port + 1, null, null, null), "s1", ssl, OptionMap.create(UndertowOptions.ENABLE_HTTP2, true)).addHost(new URI("https", null, DefaultServer.getHostAddress("default"), port + 2, null, null, null), "s2", ssl, OptionMap.create(UndertowOptions.ENABLE_HTTP2, true))).setMaxRequestTime(10000).setMaxConnectionRetries(2).build());
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) UndertowXnioSsl(io.undertow.protocols.ssl.UndertowXnioSsl) Http2ServerConnection(io.undertow.server.protocol.http2.Http2ServerConnection) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) HttpString(io.undertow.util.HttpString) BeforeClass(org.junit.BeforeClass)

Example 28 with UndertowXnioSsl

use of io.undertow.protocols.ssl.UndertowXnioSsl in project undertow by undertow-io.

the class LoadBalancingProxyHTTP2TestCase method testHttp2ClientMultipleStreamsThreadSafety.

@Test
public void testHttp2ClientMultipleStreamsThreadSafety() throws IOException, URISyntaxException, ExecutionException, InterruptedException, TimeoutException {
    // not actually a proxy test
    // but convent to put it here
    UndertowXnioSsl ssl = new UndertowXnioSsl(DefaultServer.getWorker().getXnio(), OptionMap.EMPTY, DefaultServer.SSL_BUFFER_POOL, DefaultServer.createClientSslContext());
    final UndertowClient client = UndertowClient.getInstance();
    final ClientConnection connection = client.connect(new URI("https", null, DefaultServer.getHostAddress(), DefaultServer.getHostPort() + 1, "/", null, null), DefaultServer.getWorker(), ssl, DefaultServer.getBufferPool(), OptionMap.create(UndertowOptions.ENABLE_HTTP2, true)).get();
    final ExecutorService service = Executors.newFixedThreadPool(10);
    try {
        Deque<FutureResult<String>> futures = new ArrayDeque<>();
        for (int i = 0; i < 100; ++i) {
            final FutureResult<String> future = new FutureResult<>();
            futures.add(future);
            service.submit(new Callable<String>() {

                @Override
                public String call() throws Exception {
                    ClientRequest cr = new ClientRequest().setMethod(Methods.GET).setPath("/path").setProtocol(Protocols.HTTP_1_1);
                    connection.sendRequest(cr, new ClientCallback<ClientExchange>() {

                        @Override
                        public void completed(ClientExchange result) {
                            result.setResponseListener(new ClientCallback<ClientExchange>() {

                                @Override
                                public void completed(ClientExchange result) {
                                    new StringReadChannelListener(DefaultServer.getBufferPool()) {

                                        @Override
                                        protected void stringDone(String string) {
                                            future.setResult(string);
                                        }

                                        @Override
                                        protected void error(IOException e) {
                                            future.setException(e);
                                        }
                                    }.setup(result.getResponseChannel());
                                }

                                @Override
                                public void failed(IOException e) {
                                    future.setException(e);
                                }
                            });
                        }

                        @Override
                        public void failed(IOException e) {
                            future.setException(e);
                        }
                    });
                    return null;
                }
            });
        }
        while (!futures.isEmpty()) {
            FutureResult<String> future = futures.poll();
            Assert.assertNotEquals(IoFuture.Status.WAITING, future.getIoFuture().awaitInterruptibly(10, TimeUnit.SECONDS));
            Assert.assertEquals("/path", future.getIoFuture().get());
        }
    } finally {
        service.shutdownNow();
    }
}
Also used : ClientExchange(io.undertow.client.ClientExchange) ClientCallback(io.undertow.client.ClientCallback) StringReadChannelListener(io.undertow.util.StringReadChannelListener) UndertowClient(io.undertow.client.UndertowClient) HttpString(io.undertow.util.HttpString) IOException(java.io.IOException) URI(java.net.URI) ArrayDeque(java.util.ArrayDeque) URISyntaxException(java.net.URISyntaxException) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) FutureResult(org.xnio.FutureResult) ExecutorService(java.util.concurrent.ExecutorService) ClientConnection(io.undertow.client.ClientConnection) UndertowXnioSsl(io.undertow.protocols.ssl.UndertowXnioSsl) ClientRequest(io.undertow.client.ClientRequest) Test(org.junit.Test)

Example 29 with UndertowXnioSsl

use of io.undertow.protocols.ssl.UndertowXnioSsl in project undertow by undertow-io.

the class LoadBalancingProxyHttpsTestCase method setup.

@BeforeClass
public static void setup() throws URISyntaxException {
    final SessionCookieConfig sessionConfig = new SessionCookieConfig();
    int port = DefaultServer.getHostPort("default");
    server1 = Undertow.builder().addHttpsListener(port + 1, DefaultServer.getHostAddress("default"), DefaultServer.getServerSslContext()).setSocketOption(Options.REUSE_ADDRESSES, true).setServerOption(UndertowOptions.NO_REQUEST_TIMEOUT, IDLE_TIMEOUT).setHandler(getRootHandler("s1", "server1")).build();
    server2 = Undertow.builder().addHttpsListener(port + 2, DefaultServer.getHostAddress("default"), DefaultServer.getServerSslContext()).setServerOption(UndertowOptions.ENABLE_SPDY, false).setServerOption(UndertowOptions.NO_REQUEST_TIMEOUT, IDLE_TIMEOUT).setSocketOption(Options.REUSE_ADDRESSES, true).setHandler(getRootHandler("s2", "server2")).build();
    server1.start();
    server2.start();
    UndertowXnioSsl ssl = new UndertowXnioSsl(DefaultServer.getWorker().getXnio(), OptionMap.EMPTY, DefaultServer.SSL_BUFFER_POOL, DefaultServer.createClientSslContext());
    DefaultServer.setRootHandler(ProxyHandler.builder().setProxyClient(new LoadBalancingProxyClient().setConnectionsPerThread(4).addHost(new URI("https", null, DefaultServer.getHostAddress("default"), port + 1, null, null, null), "s1", ssl).addHost(new URI("https", null, DefaultServer.getHostAddress("default"), port + 2, null, null, null), "s2", ssl)).setMaxRequestTime(10000).setMaxConnectionRetries(2).build());
}
Also used : SessionCookieConfig(io.undertow.server.session.SessionCookieConfig) UndertowXnioSsl(io.undertow.protocols.ssl.UndertowXnioSsl) URI(java.net.URI) BeforeClass(org.junit.BeforeClass)

Example 30 with UndertowXnioSsl

use of io.undertow.protocols.ssl.UndertowXnioSsl in project undertow by undertow-io.

the class AbstractModClusterTestBase method beforeClass.

@BeforeClass
public static final void beforeClass() {
    port = getHostPort("default");
    hostName = getHostAddress("default");
    xnioSsl = new UndertowXnioSsl(DefaultServer.getWorker().getXnio(), OptionMap.EMPTY, DefaultServer.SSL_BUFFER_POOL, getClientSSLContext());
}
Also used : UndertowXnioSsl(io.undertow.protocols.ssl.UndertowXnioSsl) BeforeClass(org.junit.BeforeClass)

Aggregations

UndertowXnioSsl (io.undertow.protocols.ssl.UndertowXnioSsl)36 URI (java.net.URI)21 IOException (java.io.IOException)18 Test (org.junit.Test)17 XnioSsl (org.xnio.ssl.XnioSsl)15 ClientConnection (io.undertow.client.ClientConnection)9 UndertowClient (io.undertow.client.UndertowClient)9 CountDownLatch (java.util.concurrent.CountDownLatch)9 ClientRequest (io.undertow.client.ClientRequest)7 HttpHandler (io.undertow.server.HttpHandler)7 SSLContext (javax.net.ssl.SSLContext)7 InetSocketAddress (java.net.InetSocketAddress)6 HttpServerExchange (io.undertow.server.HttpServerExchange)5 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)5 OptionMap (org.xnio.OptionMap)5 LoadBalancingProxyClient (io.undertow.server.handlers.proxy.LoadBalancingProxyClient)4 HttpOpenListener (io.undertow.server.protocol.http.HttpOpenListener)4 Http2UpgradeHandler (io.undertow.server.protocol.http2.Http2UpgradeHandler)4 StringWriteChannelListener (io.undertow.util.StringWriteChannelListener)4 Undertow (io.undertow.Undertow)3