Search in sources :

Example 1 with UndertowClient

use of io.undertow.client.UndertowClient in project camel by apache.

the class UndertowProducer method process.

@Override
public boolean process(Exchange exchange, AsyncCallback callback) {
    ClientConnection connection = null;
    try {
        final UndertowClient client = UndertowClient.getInstance();
        IoFuture<ClientConnection> connect = client.connect(endpoint.getHttpURI(), worker, pool, options);
        // creating the url to use takes 2-steps
        final String exchangeUri = UndertowHelper.createURL(exchange, getEndpoint());
        final URI uri = UndertowHelper.createURI(exchange, exchangeUri, getEndpoint());
        final String pathAndQuery = URISupport.pathAndQueryOf(uri);
        // what http method to use
        HttpString method = UndertowHelper.createMethod(exchange, endpoint, exchange.getIn().getBody() != null);
        ClientRequest request = new ClientRequest();
        request.setProtocol(Protocols.HTTP_1_1);
        request.setPath(pathAndQuery);
        request.setMethod(method);
        final HeaderMap requestHeaders = request.getRequestHeaders();
        // Set the Host header
        Message message = exchange.getIn();
        final String host = message.getHeader("Host", String.class);
        requestHeaders.put(Headers.HOST, Optional.ofNullable(host).orElseGet(() -> uri.getAuthority()));
        Object body = getRequestBody(request, exchange);
        TypeConverter tc = endpoint.getCamelContext().getTypeConverter();
        ByteBuffer bodyAsByte = tc.tryConvertTo(ByteBuffer.class, body);
        if (body != null) {
            requestHeaders.put(Headers.CONTENT_LENGTH, bodyAsByte.array().length);
        }
        if (getEndpoint().getCookieHandler() != null) {
            Map<String, List<String>> cookieHeaders = getEndpoint().getCookieHandler().loadCookies(exchange, uri);
            for (Map.Entry<String, List<String>> entry : cookieHeaders.entrySet()) {
                requestHeaders.putAll(new HttpString(entry.getKey()), entry.getValue());
            }
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Executing http {} method: {}", method, pathAndQuery);
        }
        connection = connect.get();
        connection.sendRequest(request, new UndertowProducerCallback(connection, bodyAsByte, exchange, callback));
    } catch (Exception e) {
        IOHelper.close(connection);
        exchange.setException(e);
        callback.done(true);
        return true;
    }
    // use async routing engine
    return false;
}
Also used : Message(org.apache.camel.Message) UndertowClient(io.undertow.client.UndertowClient) HttpString(io.undertow.util.HttpString) URI(java.net.URI) ByteBuffer(java.nio.ByteBuffer) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) TypeConverter(org.apache.camel.TypeConverter) HeaderMap(io.undertow.util.HeaderMap) ClientConnection(io.undertow.client.ClientConnection) LinkedList(java.util.LinkedList) List(java.util.List) HashMap(java.util.HashMap) OptionMap(org.xnio.OptionMap) HeaderMap(io.undertow.util.HeaderMap) Map(java.util.Map) ClientRequest(io.undertow.client.ClientRequest) HttpString(io.undertow.util.HttpString)

Example 2 with UndertowClient

use of io.undertow.client.UndertowClient 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 3 with UndertowClient

use of io.undertow.client.UndertowClient 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)

Example 4 with UndertowClient

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

the class AjpClientTestCase method testSendPing.

@Test
public void testSendPing() throws Exception {
    // 
    final UndertowClient client = createClient();
    final List<ClientResponse> responses = new CopyOnWriteArrayList<>();
    final FutureResult<Boolean> result = new FutureResult<>();
    final CountDownLatch latch = new CountDownLatch(3);
    final ClientConnection connection = client.connect(ADDRESS, worker, DefaultServer.getBufferPool(), OptionMap.EMPTY).get();
    assertTrue(connection.isPingSupported());
    try {
        connection.getIoThread().execute(() -> {
            final ClientRequest request = new ClientRequest().setMethod(Methods.GET).setPath(MESSAGE);
            request.getRequestHeaders().put(Headers.HOST, DefaultServer.getHostAddress());
            connection.sendRequest(request, createClientCallback(responses, latch));
            connection.sendPing(new ClientConnection.PingListener() {

                @Override
                public void acknowledged() {
                    result.setResult(true);
                    latch.countDown();
                }

                @Override
                public void failed(IOException e) {
                    result.setException(e);
                    latch.countDown();
                }
            }, 5, TimeUnit.SECONDS);
            connection.sendRequest(request, createClientCallback(responses, latch));
        });
        assertTrue(latch.await(10, TimeUnit.SECONDS));
        Assert.assertEquals(2, responses.size());
        assertTrue(result.getIoFuture().get());
        for (final ClientResponse response : responses) {
            Assert.assertEquals(message, response.getAttachment(RESPONSE_BODY));
        }
        // now try a failed ping
        try {
            undertow.stop();
            final FutureResult<Boolean> failResult = new FutureResult<>();
            connection.getIoThread().execute(() -> connection.sendPing(new ClientConnection.PingListener() {

                @Override
                public void acknowledged() {
                    failResult.setResult(true);
                }

                @Override
                public void failed(IOException e) {
                    failResult.setException(e);
                }
            }, 4, TimeUnit.SECONDS));
            try {
                failResult.getIoFuture().get();
                Assert.fail("ping should have failed");
            } catch (IOException e) {
            // ignored
            }
        } finally {
            // add an extra sleep time to make sure we are not getting a BindException
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            // ignore
            }
            undertow.start();
        }
    } finally {
        IoUtils.safeClose(connection);
    }
}
Also used : ClientResponse(io.undertow.client.ClientResponse) UndertowClient(io.undertow.client.UndertowClient) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) FutureResult(org.xnio.FutureResult) ClientConnection(io.undertow.client.ClientConnection) ClientRequest(io.undertow.client.ClientRequest) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.Test)

Example 5 with UndertowClient

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

the class AjpClientTestCase method testConnectionClose.

@Test
public void testConnectionClose() throws Exception {
    // 
    final UndertowClient client = createClient();
    final CountDownLatch latch = new CountDownLatch(1);
    final ClientConnection connection = client.connect(ADDRESS, worker, DefaultServer.getBufferPool(), OptionMap.EMPTY).get();
    try {
        ClientRequest request = new ClientRequest().setPath(MESSAGE).setMethod(Methods.GET);
        request.getRequestHeaders().put(Headers.HOST, DefaultServer.getHostAddress());
        final List<ClientResponse> responses = new CopyOnWriteArrayList<>();
        request.getRequestHeaders().add(Headers.CONNECTION, Headers.CLOSE.toString());
        connection.sendRequest(request, createClientCallback(responses, latch));
        latch.await();
        final ClientResponse response = responses.iterator().next();
        Assert.assertEquals(message, response.getAttachment(RESPONSE_BODY));
        Assert.assertFalse(connection.isOpen());
    } finally {
        IoUtils.safeClose(connection);
    }
}
Also used : ClientResponse(io.undertow.client.ClientResponse) UndertowClient(io.undertow.client.UndertowClient) ClientConnection(io.undertow.client.ClientConnection) CountDownLatch(java.util.concurrent.CountDownLatch) ClientRequest(io.undertow.client.ClientRequest) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.Test)

Aggregations

ClientConnection (io.undertow.client.ClientConnection)18 UndertowClient (io.undertow.client.UndertowClient)18 Test (org.junit.Test)17 ClientRequest (io.undertow.client.ClientRequest)16 CountDownLatch (java.util.concurrent.CountDownLatch)14 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)12 ClientResponse (io.undertow.client.ClientResponse)10 UndertowXnioSsl (io.undertow.protocols.ssl.UndertowXnioSsl)9 IOException (java.io.IOException)7 URI (java.net.URI)7 ClientCallback (io.undertow.client.ClientCallback)5 ClientExchange (io.undertow.client.ClientExchange)5 StringReadChannelListener (io.undertow.util.StringReadChannelListener)4 StringWriteChannelListener (io.undertow.util.StringWriteChannelListener)3 URISyntaxException (java.net.URISyntaxException)3 HttpString (io.undertow.util.HttpString)2 FutureResult (org.xnio.FutureResult)2 OptionMap (org.xnio.OptionMap)2 Undertow (io.undertow.Undertow)1 HttpHandler (io.undertow.server.HttpHandler)1