Search in sources :

Example 51 with ClientConnection

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

the class UpdateUserByIdTest method testUpdateUserById.

@Test
public void testUpdateUserById() throws ClientException, ApiException {
    System.out.println("s:" + s);
    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("/api/json").setMethod(Methods.POST);
        request.getRequestHeaders().put(Headers.CONTENT_TYPE, "application/json");
        request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
        connection.sendRequest(request, client.createClientCallback(reference, latch, s));
        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);
    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) ApiException(com.networknt.exception.ApiException) ClientRequest(io.undertow.client.ClientRequest) Test(org.junit.Test)

Example 52 with ClientConnection

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

the class loginUserTest method testloginUser.

@Test
public void testloginUser() 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 {
        System.out.println("json:" + s);
        ClientRequest request = new ClientRequest().setPath("/api/json").setMethod(Methods.POST);
        request.getRequestHeaders().put(Headers.CONTENT_TYPE, "application/json");
        request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
        connection.sendRequest(request, client.createClientCallback(reference, latch, s));
        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);
    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) ApiException(com.networknt.exception.ApiException) ClientRequest(io.undertow.client.ClientRequest) Test(org.junit.Test)

Example 53 with ClientConnection

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

the class GetFormTest method testGetForm.

@Test
public void testGetForm() 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 String requestBody = "{\"host\":\"lightapi.net\",\"service\":\"form\",\"action\":\"getForm\",\"version\":\"0.1.0\"}";
    System.out.println("json:" + requestBody);
    final AtomicReference<ClientResponse> reference = new AtomicReference<>();
    try {
        ClientRequest request = new ClientRequest().setPath("/api/json").setMethod(Methods.POST);
        request.getRequestHeaders().put(Headers.CONTENT_TYPE, "application/json");
        request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
        connection.sendRequest(request, client.createClientCallback(reference, latch, requestBody));
        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);
    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) ApiException(com.networknt.exception.ApiException) ClientRequest(io.undertow.client.ClientRequest) Test(org.junit.Test)

Example 54 with ClientConnection

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

Aggregations

ClientConnection (io.undertow.client.ClientConnection)120 ClientRequest (io.undertow.client.ClientRequest)113 CountDownLatch (java.util.concurrent.CountDownLatch)107 ClientResponse (io.undertow.client.ClientResponse)104 URI (java.net.URI)101 Test (org.junit.Test)99 AtomicReference (java.util.concurrent.atomic.AtomicReference)94 Http2Client (com.networknt.client.Http2Client)87 ClientException (com.networknt.exception.ClientException)82 ApiException (com.networknt.exception.ApiException)44 IOException (java.io.IOException)41 HttpString (io.undertow.util.HttpString)25 UndertowClient (io.undertow.client.UndertowClient)18 Status (com.networknt.status.Status)17 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)15 SQLException (java.sql.SQLException)12 UndertowXnioSsl (io.undertow.protocols.ssl.UndertowXnioSsl)9 ClientCallback (io.undertow.client.ClientCallback)6 ClientExchange (io.undertow.client.ClientExchange)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5