Search in sources :

Example 41 with ClientConnection

use of io.undertow.client.ClientConnection in project spring-framework by spring-projects.

the class UndertowXhrTransport method executeRequest.

protected ResponseEntity<String> executeRequest(URI url, HttpString method, HttpHeaders headers, @Nullable String body) {
    CountDownLatch latch = new CountDownLatch(1);
    List<ClientResponse> responses = new CopyOnWriteArrayList<>();
    try {
        ClientConnection connection = this.httpClient.connect(url, this.worker, this.bufferPool, this.optionMap).get();
        try {
            ClientRequest request = new ClientRequest().setMethod(method).setPath(url.getPath());
            request.getRequestHeaders().add(HttpString.tryFromString(HttpHeaders.HOST), url.getHost());
            if (StringUtils.hasLength(body)) {
                HttpString headerName = HttpString.tryFromString(HttpHeaders.CONTENT_LENGTH);
                request.getRequestHeaders().add(headerName, body.length());
            }
            addHttpHeaders(request, headers);
            connection.sendRequest(request, createRequestCallback(body, responses, latch));
            latch.await();
            ClientResponse response = responses.iterator().next();
            HttpStatus status = HttpStatus.valueOf(response.getResponseCode());
            HttpHeaders responseHeaders = toHttpHeaders(response.getResponseHeaders());
            String responseBody = response.getAttachment(RESPONSE_BODY);
            return (responseBody != null ? new ResponseEntity<>(responseBody, responseHeaders, status) : new ResponseEntity<>(responseHeaders, status));
        } finally {
            IoUtils.safeClose(connection);
        }
    } catch (IOException ex) {
        throw new SockJsTransportFailureException("Failed to execute request to " + url, ex);
    } catch (InterruptedException ex) {
        Thread.currentThread().interrupt();
        throw new SockJsTransportFailureException("Interrupted while processing request to " + url, ex);
    }
}
Also used : ClientResponse(io.undertow.client.ClientResponse) HttpHeaders(org.springframework.http.HttpHeaders) HttpStatus(org.springframework.http.HttpStatus) HttpString(io.undertow.util.HttpString) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) SockJsTransportFailureException(org.springframework.web.socket.sockjs.SockJsTransportFailureException) ResponseEntity(org.springframework.http.ResponseEntity) ClientConnection(io.undertow.client.ClientConnection) ClientRequest(io.undertow.client.ClientRequest) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) HttpString(io.undertow.util.HttpString)

Example 42 with ClientConnection

use of io.undertow.client.ClientConnection 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 43 with ClientConnection

use of io.undertow.client.ClientConnection 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 44 with ClientConnection

use of io.undertow.client.ClientConnection 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 45 with ClientConnection

use of io.undertow.client.ClientConnection 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)188 ClientResponse (io.undertow.client.ClientResponse)172 ClientRequest (io.undertow.client.ClientRequest)169 URI (java.net.URI)159 CountDownLatch (java.util.concurrent.CountDownLatch)152 AtomicReference (java.util.concurrent.atomic.AtomicReference)150 Http2Client (com.networknt.client.Http2Client)147 ClientException (com.networknt.exception.ClientException)146 Test (org.junit.Test)142 ApiException (com.networknt.exception.ApiException)48 HttpString (io.undertow.util.HttpString)39 IOException (java.io.IOException)38 Status (com.networknt.status.Status)30 URISyntaxException (java.net.URISyntaxException)25 UndertowClient (io.undertow.client.UndertowClient)18 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)15 SQLException (java.sql.SQLException)12 HashMap (java.util.HashMap)11 UndertowXnioSsl (io.undertow.protocols.ssl.UndertowXnioSsl)9 OptionMap (org.xnio.OptionMap)8