Search in sources :

Example 26 with ClientResponse

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

the class GetMenuByHostTest method testGetMenuByHost.

// As we are using live database which is shared by multiple users, need to find a way to create test
// menu item and test menu before running this test and clean up later on. The created data need to have
// UUID as key so that multiple users can build and test at the same time. For now, just disable it.
// @Test
public void testGetMenuByHost() 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<>();
    final String requestBody = "{\"host\":\"lightapi.net\",\"service\":\"menu\",\"action\":\"getMenuByHost\",\"version\":\"0.1.0\",\"data\":{\"host\":\"example.com\"}}";
    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)

Example 27 with ClientResponse

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

the class GetMenuTest method testGetMenu.

@Test
public void testGetMenu() 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<>();
    final String requestBody = "{\"host\":\"lightapi.net\",\"service\":\"menu\",\"action\":\"getMenu\",\"version\":\"0.1.0\"}";
    System.out.println("json:" + requestBody);
    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 28 with ClientResponse

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

the class RegistryTest method testRegistry.

@Test
public void testRegistry() throws ClientException, ApiException {
    final String s = "{\"host\":\"lightapi.net\",\"service\":\"certification\",\"action\":\"certifyRegistry\",\"version\":\"0.1.0\",\"data\":{\"registryUrl\":\"https://consul:8500\",\"environment\":\"prod\"}}";
    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 29 with ClientResponse

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

the class ServerInfoRetriever method retrieve.

/**
 * @param host the protocol, host and port
 * @param path the path of the server info endpoint
 * @param token the JWT token to access the server info endpoint
 * @return a map that represent the server info or error status
 * @throws ClientException client exception
 */
public static Map<String, Object> retrieve(String host, String path, String token) throws ClientException {
    Map<String, Object> map;
    final Http2Client client = Http2Client.getInstance();
    final CountDownLatch latch = new CountDownLatch(1);
    final ClientConnection connection;
    try {
        connection = client.connect(new URI(host), Http2Client.WORKER, Http2Client.SSL, Http2Client.POOL, OptionMap.create(UndertowOptions.ENABLE_HTTP2, true)).get();
    } catch (Exception e) {
        throw new ClientException(e);
    }
    final AtomicReference<ClientResponse> reference = new AtomicReference<>();
    try {
        ClientRequest request = new ClientRequest().setMethod(Methods.GET).setPath(path);
        request.getRequestHeaders().put(Headers.AUTHORIZATION, "Bearer " + token);
        connection.sendRequest(request, client.createClientCallback(reference, latch));
        latch.await();
        // int statusCode = reference.get().getResponseCode();
        String body = reference.get().getAttachment(Http2Client.RESPONSE_BODY);
        // regardless there is an error, convert the body to map anyway and the caller will process the
        // result accordingly.
        map = Config.getInstance().getMapper().readValue(body, new TypeReference<HashMap<String, Object>>() {
        });
    } catch (Exception e) {
        logger.error("Exception: ", e);
        throw new ClientException(e);
    } finally {
        IoUtils.safeClose(connection);
    }
    return map;
}
Also used : ClientResponse(io.undertow.client.ClientResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ClientException(com.networknt.exception.ClientException) ClientConnection(io.undertow.client.ClientConnection) Http2Client(com.networknt.client.Http2Client) ClientException(com.networknt.exception.ClientException) TypeReference(com.fasterxml.jackson.core.type.TypeReference) ClientRequest(io.undertow.client.ClientRequest)

Example 30 with ClientResponse

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

the class JwtTokenTest method testJwtToken.

@Test
public void testJwtToken() throws ClientException, ApiException {
    final String requestBody = "{\"host\":\"lightapi.net\",\"service\":\"playground\",\"action\":\"generateJwt\",\"version\":\"0.1.0\",\"data\":{\"expiredInMinutes\":5256000,\"scope\":[\"jwt.r\",\"server.info.r\"]}}";
    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, 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);
    logger.debug("body = " + 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)

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