Search in sources :

Example 6 with ClientExchange

use of io.undertow.client.ClientExchange in project undertow by undertow-io.

the class Http2ClientTestCase method testPostRequest.

@Test
public void testPostRequest() throws Exception {
    // 
    final UndertowClient 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, 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.POST).setPath(POST);
                    request.getRequestHeaders().put(Headers.HOST, DefaultServer.getHostAddress());
                    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(DefaultServer.getBufferPool()) {

                                        @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);
    }
}
Also used : ClientExchange(io.undertow.client.ClientExchange) ClientCallback(io.undertow.client.ClientCallback) StringReadChannelListener(io.undertow.util.StringReadChannelListener) UndertowClient(io.undertow.client.UndertowClient) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ClientConnection(io.undertow.client.ClientConnection) StringWriteChannelListener(io.undertow.util.StringWriteChannelListener) UndertowXnioSsl(io.undertow.protocols.ssl.UndertowXnioSsl) ClientRequest(io.undertow.client.ClientRequest) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.Test)

Example 7 with ClientExchange

use of io.undertow.client.ClientExchange in project undertow by undertow-io.

the class AjpClientTestCase method testPostRequest.

@Test
public void testPostRequest() throws Exception {
    // 
    final UndertowClient 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, DefaultServer.getBufferPool(), OptionMap.EMPTY).get();
    try {
        connection.getIoThread().execute(() -> {
            for (int i = 0; i < 10; i++) {
                final ClientRequest request = new ClientRequest().setMethod(Methods.POST).setPath(POST);
                request.getRequestHeaders().put(Headers.HOST, DefaultServer.getHostAddress());
                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(DefaultServer.getBufferPool()) {

                                    @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();
                    }
                });
            }
        });
        assertTrue(latch.await(10, TimeUnit.SECONDS));
        Assert.assertEquals(10, responses.size());
        for (final String response : responses) {
            Assert.assertEquals(postMessage, response);
        }
    } finally {
        IoUtils.safeClose(connection);
    }
}
Also used : ClientExchange(io.undertow.client.ClientExchange) ClientCallback(io.undertow.client.ClientCallback) StringReadChannelListener(io.undertow.util.StringReadChannelListener) UndertowClient(io.undertow.client.UndertowClient) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ClientConnection(io.undertow.client.ClientConnection) StringWriteChannelListener(io.undertow.util.StringWriteChannelListener) ClientRequest(io.undertow.client.ClientRequest) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.Test)

Example 8 with ClientExchange

use of io.undertow.client.ClientExchange in project undertow by undertow-io.

the class H2CUpgradeResetTestCase method createClientCallback.

/**
 * Create the callback to receive the response and assign it to the list.
 * @param responses The list where the response will be added
 * @param latch The latch to count down when the response is received
 * @param message The message to send if it's a POST message (if null nothing is send)
 * @param boolean reset if true a RST is sent for the received the frame ID after completed
 * @return The created callback
 */
private static ClientCallback<ClientExchange> createClientCallback(final List<ClientResponse> responses, final CountDownLatch latch, String message, boolean reset) {
    return new ClientCallback<ClientExchange>() {

        @Override
        public void completed(ClientExchange result) {
            if (message != null) {
                new StringWriteChannelListener(message).setup(result.getRequestChannel());
            }
            result.setResponseListener(new ClientCallback<ClientExchange>() {

                @Override
                public void completed(final ClientExchange result) {
                    responses.add(result.getResponse());
                    new StringReadChannelListener(result.getConnection().getBufferPool()) {

                        @Override
                        protected void stringDone(String string) {
                            result.getResponse().putAttachment(RESPONSE_BODY, string);
                            if (reset) {
                                Http2StreamSourceChannel res = (Http2StreamSourceChannel) result.getResponseChannel();
                                res.getHttp2Channel().sendRstStream(res.getStreamId(), Http2Channel.ERROR_STREAM_CLOSED);
                            }
                            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();
        }
    };
}
Also used : ClientExchange(io.undertow.client.ClientExchange) ClientCallback(io.undertow.client.ClientCallback) StringReadChannelListener(io.undertow.util.StringReadChannelListener) StringWriteChannelListener(io.undertow.util.StringWriteChannelListener) IOException(java.io.IOException) Http2StreamSourceChannel(io.undertow.protocols.http2.Http2StreamSourceChannel)

Aggregations

ClientCallback (io.undertow.client.ClientCallback)8 ClientExchange (io.undertow.client.ClientExchange)8 IOException (java.io.IOException)8 StringReadChannelListener (io.undertow.util.StringReadChannelListener)6 ClientConnection (io.undertow.client.ClientConnection)5 ClientRequest (io.undertow.client.ClientRequest)5 UndertowClient (io.undertow.client.UndertowClient)5 StringWriteChannelListener (io.undertow.util.StringWriteChannelListener)5 Test (org.junit.Test)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 UndertowXnioSsl (io.undertow.protocols.ssl.UndertowXnioSsl)3 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)3 HttpString (io.undertow.util.HttpString)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 Undertow (io.undertow.Undertow)1 ClientResponse (io.undertow.client.ClientResponse)1 Http2StreamSourceChannel (io.undertow.protocols.http2.Http2StreamSourceChannel)1 HttpHandler (io.undertow.server.HttpHandler)1 HttpServerExchange (io.undertow.server.HttpServerExchange)1