Search in sources :

Example 51 with ClientResponse

use of io.undertow.client.ClientResponse in project light-portal by networknt.

the class UserIdDeleteHandlerTest method testUserIdDeleteHandlerTest.

@Test
public void testUserIdDeleteHandlerTest() throws ClientException, ApiException {
    final Http2Client client = Http2Client.getInstance();
    final CountDownLatch latch = new CountDownLatch(1);
    final ClientConnection connection;
    try {
        connection = client.connect(new URI(url), Http2Client.WORKER, Http2Client.SSL, Http2Client.POOL, enableHttp2 ? OptionMap.create(UndertowOptions.ENABLE_HTTP2, true) : OptionMap.EMPTY).get();
    } catch (Exception e) {
        throw new ClientException(e);
    }
    final AtomicReference<ClientResponse> reference = new AtomicReference<>();
    try {
        ClientRequest request = new ClientRequest().setPath("/v1/user/122222").setMethod(Methods.DELETE);
        connection.sendRequest(request, client.createClientCallback(reference, latch));
        latch.await();
    } catch (Exception e) {
        logger.error("Exception: ", e);
        throw new ClientException(e);
    } finally {
        IoUtils.safeClose(connection);
    }
    int statusCode = reference.get().getResponseCode();
    String body = reference.get().getAttachment(Http2Client.RESPONSE_BODY);
    System.out.println("response:" + body);
    Assert.assertEquals(200, statusCode);
    Assert.assertNotNull(body);
}
Also used : ClientResponse(io.undertow.client.ClientResponse) ClientConnection(io.undertow.client.ClientConnection) AtomicReference(java.util.concurrent.atomic.AtomicReference) Http2Client(com.networknt.client.Http2Client) ClientException(com.networknt.exception.ClientException) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ClientException(com.networknt.exception.ClientException) SQLException(java.sql.SQLException) IOException(java.io.IOException) ApiException(com.networknt.exception.ApiException) ClientRequest(io.undertow.client.ClientRequest) Test(org.junit.Test)

Example 52 with ClientResponse

use of io.undertow.client.ClientResponse 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 53 with ClientResponse

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

the class H2CUpgradeContinuationTestCase method sendRequest.

/**
 * Method that sends a GET or POST request adding count number of custom
 * headers and sending contentLength data. GET is used if no content length
 * is passed, POST if contentLength is greater than 0.
 * @param connection The connection to use
 * @param requestCount The number of requests to send
 * @param headersCount The number of custom headers to send
 * @param contentLength The content length to send (POST method used if >0)
 * @throws Exception Some error
 */
private void sendRequest(ClientConnection connection, int requestCount, int headersCount, int contentLength) throws Exception {
    final CountDownLatch latch = new CountDownLatch(requestCount);
    final List<ClientResponse> responses = new CopyOnWriteArrayList<>();
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < contentLength; i++) {
        sb.append(i % 10);
    }
    final String content = sb.length() > 0 ? sb.toString() : null;
    connection.getIoThread().execute(new Runnable() {

        @Override
        public void run() {
            for (int i = 0; i < requestCount; i++) {
                final ClientRequest request = new ClientRequest().setMethod(contentLength > 0 ? Methods.POST : Methods.GET).setPath(ECHO_PATH);
                request.getRequestHeaders().put(Headers.HOST, DefaultServer.getHostAddress());
                if (contentLength > 0) {
                    request.getRequestHeaders().put(Headers.CONTENT_LENGTH, contentLength);
                }
                for (int j = 0; j < headersCount; j++) {
                    request.getRequestHeaders().put(new HttpString(HEADER_PREFFIX + j), HEADER_PREFFIX + j);
                }
                connection.sendRequest(request, createClientCallback(responses, latch, content));
            }
        }
    });
    latch.await(10, TimeUnit.SECONDS);
    Assert.assertEquals("No responses received from server in 10s", requestCount, responses.size());
    for (int i = 0; i < requestCount; i++) {
        Assert.assertEquals("Response " + i + " code was not OK", StatusCodes.OK, responses.get(i).getResponseCode());
        Assert.assertEquals("Incorrect data received for response " + i, contentLength > 0 ? content : "", responses.get(i).getAttachment(RESPONSE_BODY));
        int headersReturned = 0;
        for (HeaderValues header : responses.get(i).getResponseHeaders()) {
            if (header.getFirst().startsWith(HEADER_PREFFIX)) {
                headersReturned += header.size();
            }
        }
        Assert.assertEquals("Incorrect number of headers returned for response " + i, headersCount, headersReturned);
    }
}
Also used : ClientResponse(io.undertow.client.ClientResponse) HeaderValues(io.undertow.util.HeaderValues) HttpString(io.undertow.util.HttpString) CountDownLatch(java.util.concurrent.CountDownLatch) ClientRequest(io.undertow.client.ClientRequest) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) HttpString(io.undertow.util.HttpString)

Example 54 with ClientResponse

use of io.undertow.client.ClientResponse 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 55 with ClientResponse

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

ClientResponse (io.undertow.client.ClientResponse)108 ClientRequest (io.undertow.client.ClientRequest)105 ClientConnection (io.undertow.client.ClientConnection)103 CountDownLatch (java.util.concurrent.CountDownLatch)103 URI (java.net.URI)94 AtomicReference (java.util.concurrent.atomic.AtomicReference)92 Test (org.junit.Test)92 Http2Client (com.networknt.client.Http2Client)87 ClientException (com.networknt.exception.ClientException)82 ApiException (com.networknt.exception.ApiException)44 IOException (java.io.IOException)34 HttpString (io.undertow.util.HttpString)23 Status (com.networknt.status.Status)17 SQLException (java.sql.SQLException)12 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)12 UndertowClient (io.undertow.client.UndertowClient)10 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 UserDto (com.networknt.portal.usermanagement.model.common.domain.UserDto)4 UndertowXnioSsl (io.undertow.protocols.ssl.UndertowXnioSsl)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3