Search in sources :

Example 1 with UndertowXnioSsl

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

the class WebSocketClient13TestCase method testTextMessageWss.

@Test
public void testTextMessageWss() throws Exception {
    UndertowXnioSsl ssl = new UndertowXnioSsl(Xnio.getInstance(), OptionMap.EMPTY, DefaultServer.getClientSSLContext());
    final WebSocketClient.ConnectionBuilder connectionBuilder = WebSocketClient.connectionBuilder(worker, DefaultServer.getBufferPool(), new URI("wss://" + 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) Test(org.junit.Test)

Example 2 with UndertowXnioSsl

use of io.undertow.protocols.ssl.UndertowXnioSsl in project light-4j by networknt.

the class Http2ClientIT method testMultipleHttpGetSsl.

/*
    @Test
    public void testMultipleHttp2Post() throws Exception {
        //
        final Http2Client client = createClient();
        final String postMessage = "This is a post request";

        final List<String> responses = new CopyOnWriteArrayList<>();
        final CountDownLatch latch = new CountDownLatch(10);
        final ClientConnection connection = client.connect(ADDRESS, worker, pool, OptionMap.create(UndertowOptions.ENABLE_HTTP2, true)).get();
        try {
            connection.getIoThread().execute(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < 10; i++) {
                        final ClientRequest request = new ClientRequest().setMethod(Methods.POST).setPath(POST);
                        request.getRequestHeaders().put(Headers.HOST, "localhost");
                        request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
                        connection.sendRequest(request, new ClientCallback<ClientExchange>() {
                            @Override
                            public void completed(ClientExchange result) {
                                new StringWriteChannelListener(postMessage).setup(result.getRequestChannel());
                                result.setResponseListener(new ClientCallback<ClientExchange>() {
                                    @Override
                                    public void completed(ClientExchange result) {
                                        new StringReadChannelListener(pool) {

                                            @Override
                                            protected void stringDone(String string) {
                                                responses.add(string);
                                                latch.countDown();
                                            }

                                            @Override
                                            protected void error(IOException e) {
                                                e.printStackTrace();
                                                latch.countDown();
                                            }
                                        }.setup(result.getResponseChannel());
                                    }

                                    @Override
                                    public void failed(IOException e) {
                                        e.printStackTrace();
                                        latch.countDown();
                                    }
                                });
                            }

                            @Override
                            public void failed(IOException e) {
                                e.printStackTrace();
                                latch.countDown();
                            }
                        });
                    }
                }

            });

            latch.await(10, TimeUnit.SECONDS);

            Assert.assertEquals(10, responses.size());
            for (final String response : responses) {
                Assert.assertEquals(postMessage, response);
            }
        } finally {
            IoUtils.safeClose(connection);
        }
    }
    */
@Test
public void testMultipleHttpGetSsl() throws Exception {
    // 
    final Http2Client client = createClient();
    final List<AtomicReference<ClientResponse>> references = new CopyOnWriteArrayList<>();
    final CountDownLatch latch = new CountDownLatch(10);
    SSLContext context = client.createSSLContext();
    XnioSsl ssl = new UndertowXnioSsl(worker.getXnio(), OptionMap.EMPTY, Http2Client.SSL_BUFFER_POOL, context);
    final ClientConnection connection = client.connect(new URI("https://localhost:7778"), worker, ssl, Http2Client.POOL, OptionMap.EMPTY).get();
    try {
        connection.getIoThread().execute(new Runnable() {

            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    AtomicReference<ClientResponse> reference = new AtomicReference<>();
                    references.add(i, reference);
                    final ClientRequest request = new ClientRequest().setMethod(Methods.GET).setPath(MESSAGE);
                    request.getRequestHeaders().put(Headers.HOST, "localhost");
                    connection.sendRequest(request, client.createClientCallback(reference, latch));
                }
            }
        });
        latch.await(10, TimeUnit.SECONDS);
        Assert.assertEquals(10, references.size());
        for (final AtomicReference<ClientResponse> reference : references) {
            Assert.assertEquals(message, reference.get().getAttachment(Http2Client.RESPONSE_BODY));
            Assert.assertEquals("HTTP/1.1", reference.get().getProtocol().toString());
        }
    } finally {
        connection.getIoThread().execute(new Runnable() {

            @Override
            public void run() {
                IoUtils.safeClose(connection);
            }
        });
    }
}
Also used : XnioSsl(org.xnio.ssl.XnioSsl) UndertowXnioSsl(io.undertow.protocols.ssl.UndertowXnioSsl) AtomicReference(java.util.concurrent.atomic.AtomicReference) URI(java.net.URI) UndertowXnioSsl(io.undertow.protocols.ssl.UndertowXnioSsl) Test(org.junit.Test)

Example 3 with UndertowXnioSsl

use of io.undertow.protocols.ssl.UndertowXnioSsl in project light-4j by networknt.

the class Http2ClientTest method testSingleHttp2PostSsl.

@Test
public void testSingleHttp2PostSsl() throws Exception {
    // 
    final Http2Client client = createClient();
    final String postMessage = "This is a post request";
    final List<String> responses = new CopyOnWriteArrayList<>();
    final CountDownLatch latch = new CountDownLatch(1);
    SSLContext context = client.createSSLContext();
    XnioSsl ssl = new UndertowXnioSsl(worker.getXnio(), OptionMap.EMPTY, Http2Client.SSL_BUFFER_POOL, context);
    final ClientConnection connection = client.connect(new URI("https://localhost:7778"), worker, ssl, Http2Client.POOL, OptionMap.create(UndertowOptions.ENABLE_HTTP2, true)).get();
    try {
        connection.getIoThread().execute(new Runnable() {

            @Override
            public void run() {
                final ClientRequest request = new ClientRequest().setMethod(Methods.POST).setPath(POST);
                request.getRequestHeaders().put(Headers.HOST, "localhost");
                request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
                connection.sendRequest(request, new ClientCallback<ClientExchange>() {

                    @Override
                    public void completed(ClientExchange result) {
                        new StringWriteChannelListener(postMessage).setup(result.getRequestChannel());
                        result.setResponseListener(new ClientCallback<ClientExchange>() {

                            @Override
                            public void completed(ClientExchange result) {
                                new StringReadChannelListener(Http2Client.POOL) {

                                    @Override
                                    protected void stringDone(String string) {
                                        responses.add(string);
                                        latch.countDown();
                                    }

                                    @Override
                                    protected void error(IOException e) {
                                        e.printStackTrace();
                                        latch.countDown();
                                    }
                                }.setup(result.getResponseChannel());
                            }

                            @Override
                            public void failed(IOException e) {
                                e.printStackTrace();
                                latch.countDown();
                            }
                        });
                    }

                    @Override
                    public void failed(IOException e) {
                        e.printStackTrace();
                        latch.countDown();
                    }
                });
            }
        });
        latch.await(10, TimeUnit.SECONDS);
        Assert.assertEquals(1, responses.size());
        for (final String response : responses) {
            Assert.assertEquals(postMessage, response);
        }
    } finally {
        IoUtils.safeClose(connection);
    }
}
Also used : XnioSsl(org.xnio.ssl.XnioSsl) UndertowXnioSsl(io.undertow.protocols.ssl.UndertowXnioSsl) IOException(java.io.IOException) URI(java.net.URI) UndertowXnioSsl(io.undertow.protocols.ssl.UndertowXnioSsl) Test(org.junit.Test)

Example 4 with UndertowXnioSsl

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

the class Http2ClientTestCase method testHeadRequest.

@Test
public void testHeadRequest() throws Exception {
    // 
    final UndertowClient client = createClient();
    final List<ClientResponse> responses = new CopyOnWriteArrayList<>();
    final CountDownLatch latch = new CountDownLatch(10);
    final ClientConnection connection = client.connect(ADDRESS, worker, new UndertowXnioSsl(worker.getXnio(), OptionMap.EMPTY, DefaultServer.getClientSSLContext()), DefaultServer.getBufferPool(), OptionMap.create(UndertowOptions.ENABLE_HTTP2, true)).get();
    try {
        connection.getIoThread().execute(new Runnable() {

            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    final ClientRequest request = new ClientRequest().setMethod(Methods.HEAD).setPath(MESSAGE);
                    request.getRequestHeaders().put(Headers.HOST, DefaultServer.getHostAddress());
                    connection.sendRequest(request, createClientCallback(responses, latch));
                }
            }
        });
        latch.await(10, TimeUnit.SECONDS);
        Assert.assertEquals(10, responses.size());
        for (final ClientResponse response : responses) {
            Assert.assertEquals("", response.getAttachment(RESPONSE_BODY));
        }
    } finally {
        IoUtils.safeClose(connection);
    }
}
Also used : ClientResponse(io.undertow.client.ClientResponse) UndertowClient(io.undertow.client.UndertowClient) ClientConnection(io.undertow.client.ClientConnection) UndertowXnioSsl(io.undertow.protocols.ssl.UndertowXnioSsl) CountDownLatch(java.util.concurrent.CountDownLatch) ClientRequest(io.undertow.client.ClientRequest) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.Test)

Example 5 with UndertowXnioSsl

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

the class Http2ClientTestCase method testSimpleBasic.

@Test
public void testSimpleBasic() throws Exception {
    // 
    final UndertowClient client = createClient();
    final List<ClientResponse> responses = new CopyOnWriteArrayList<>();
    final CountDownLatch latch = new CountDownLatch(10);
    final ClientConnection connection = client.connect(ADDRESS, worker, new UndertowXnioSsl(worker.getXnio(), OptionMap.EMPTY, DefaultServer.getClientSSLContext()), DefaultServer.getBufferPool(), OptionMap.create(UndertowOptions.ENABLE_HTTP2, true)).get();
    try {
        connection.getIoThread().execute(new Runnable() {

            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    final ClientRequest request = new ClientRequest().setMethod(Methods.GET).setPath(MESSAGE);
                    request.getRequestHeaders().put(Headers.HOST, DefaultServer.getHostAddress());
                    connection.sendRequest(request, createClientCallback(responses, latch));
                }
            }
        });
        latch.await(10, TimeUnit.SECONDS);
        Assert.assertEquals(10, responses.size());
        for (final ClientResponse response : responses) {
            Assert.assertEquals(message, response.getAttachment(RESPONSE_BODY));
        }
    } finally {
        IoUtils.safeClose(connection);
    }
}
Also used : ClientResponse(io.undertow.client.ClientResponse) UndertowClient(io.undertow.client.UndertowClient) ClientConnection(io.undertow.client.ClientConnection) UndertowXnioSsl(io.undertow.protocols.ssl.UndertowXnioSsl) CountDownLatch(java.util.concurrent.CountDownLatch) ClientRequest(io.undertow.client.ClientRequest) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.Test)

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