Search in sources :

Example 21 with ClientResponse

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

the class ConsulClientImpl method lookupHealthService.

@Override
public ConsulResponse<List<ConsulService>> lookupHealthService(String serviceName, String tag, long lastConsulIndex, String token) {
    ConsulResponse<List<ConsulService>> newResponse = null;
    String path = "/v1/health/service/" + serviceName + "?passing&wait=600s&index=" + lastConsulIndex;
    if (tag != null) {
        path = path + "&tag=" + tag;
    }
    if (logger.isDebugEnabled())
        logger.debug("path = " + path);
    ClientConnection connection = null;
    try {
        connection = client.connect(new URI(url), Http2Client.WORKER, Http2Client.SSL, Http2Client.POOL, OptionMap.EMPTY).get();
    } catch (Exception e) {
        logger.error("Exeption:", e);
    }
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<ClientResponse> reference = new AtomicReference<>();
    try {
        ClientRequest request = new ClientRequest().setMethod(Methods.GET).setPath(path);
        if (token != null)
            request.getRequestHeaders().put(Constants.CONSUL_TOKEN, token);
        request.getRequestHeaders().put(Headers.HOST, "localhost");
        connection.sendRequest(request, client.createClientCallback(reference, latch));
        latch.await();
        int statusCode = reference.get().getResponseCode();
        if (statusCode >= 300) {
            System.out.println("body = " + reference.get().getAttachment(Http2Client.RESPONSE_BODY));
            throw new Exception("Failed to unregister on Consul: " + statusCode);
        } else {
            String body = reference.get().getAttachment(Http2Client.RESPONSE_BODY);
            System.out.println("body = " + body);
            List<Map<String, Object>> services = Config.getInstance().getMapper().readValue(body, new TypeReference<List<Map<String, Object>>>() {
            });
            List<ConsulService> ConsulServcies = new ArrayList<>(services.size());
            for (Map<String, Object> service : services) {
                ConsulService newService = convertToConsulService((Map<String, Object>) service.get("Service"));
                ConsulServcies.add(newService);
            }
            if (!ConsulServcies.isEmpty()) {
                newResponse = new ConsulResponse<>();
                newResponse.setValue(ConsulServcies);
                newResponse.setConsulIndex(Long.parseLong(reference.get().getResponseHeaders().getFirst("X-Consul-Index")));
                newResponse.setConsulLastContact(Long.parseLong(reference.get().getResponseHeaders().getFirst("X-Consul-Lastcontact")));
                newResponse.setConsulKnownLeader(Boolean.parseBoolean(reference.get().getResponseHeaders().getFirst("X-Consul-Knownleader")));
            }
        }
    } catch (Exception e) {
        logger.error("Exception:", e);
    } finally {
        IoUtils.safeClose(connection);
    }
    return newResponse;
}
Also used : ClientResponse(io.undertow.client.ClientResponse) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) HttpString(io.undertow.util.HttpString) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ArrayList(java.util.ArrayList) List(java.util.List) ClientConnection(io.undertow.client.ClientConnection) ConsulService(com.networknt.consul.ConsulService) OptionMap(org.xnio.OptionMap) Map(java.util.Map) ClientRequest(io.undertow.client.ClientRequest)

Example 22 with ClientResponse

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

the class ConsulClientImpl method checkFail.

@Override
public void checkFail(String serviceId, String token) {
    if (logger.isDebugEnabled())
        logger.debug("checkFail serviceId = " + serviceId);
    String path = "/v1/agent/check/fail/" + "service:" + serviceId;
    ClientConnection connection = null;
    try {
        connection = client.connect(new URI(url), Http2Client.WORKER, Http2Client.SSL, Http2Client.POOL, OptionMap.EMPTY).get();
    } catch (Exception e) {
        logger.error("Exeption:", e);
    }
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<ClientResponse> reference = new AtomicReference<>();
    try {
        ClientRequest request = new ClientRequest().setMethod(Methods.PUT).setPath(path);
        request.getRequestHeaders().put(Headers.HOST, "localhost");
        if (token != null)
            request.getRequestHeaders().put(Constants.CONSUL_TOKEN, token);
        connection.sendRequest(request, client.createClientCallback(reference, latch));
        latch.await();
        int statusCode = reference.get().getResponseCode();
        if (statusCode >= 300) {
            logger.error("Failed to checkPass on Consul: " + statusCode + ":" + reference.get().getAttachment(Http2Client.RESPONSE_BODY));
            throw new Exception("Failed to checkPass on Consul: " + statusCode + ":" + reference.get().getAttachment(Http2Client.RESPONSE_BODY));
        }
    } catch (Exception e) {
        logger.error("CheckPass request exception", e);
    } finally {
        IoUtils.safeClose(connection);
    }
}
Also used : ClientResponse(io.undertow.client.ClientResponse) ClientConnection(io.undertow.client.ClientConnection) AtomicReference(java.util.concurrent.atomic.AtomicReference) HttpString(io.undertow.util.HttpString) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ClientRequest(io.undertow.client.ClientRequest)

Example 23 with ClientResponse

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

the class ExceptionHandlerTest method testUncaughtException.

@Test
public void testUncaughtException() throws Exception {
    final Http2Client client = Http2Client.getInstance();
    final CountDownLatch latch = new CountDownLatch(1);
    final ClientConnection connection;
    try {
        connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.SSL, Http2Client.POOL, OptionMap.EMPTY).get();
    } catch (Exception e) {
        throw new ClientException(e);
    }
    final AtomicReference<ClientResponse> reference = new AtomicReference<>();
    try {
        ClientRequest request = new ClientRequest().setPath("/uncaught").setMethod(Methods.GET);
        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);
    Assert.assertEquals(400, statusCode);
}
Also used : ClientResponse(io.undertow.client.ClientResponse) ClientConnection(io.undertow.client.ClientConnection) AtomicReference(java.util.concurrent.atomic.AtomicReference) Http2Client(com.networknt.client.Http2Client) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ClientRequest(io.undertow.client.ClientRequest) Test(org.junit.Test)

Example 24 with ClientResponse

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

the class ExceptionHandlerTest method testRuntimeException.

@Test
public void testRuntimeException() throws Exception {
    final Http2Client client = Http2Client.getInstance();
    final CountDownLatch latch = new CountDownLatch(1);
    final ClientConnection connection;
    try {
        connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.SSL, Http2Client.POOL, OptionMap.EMPTY).get();
    } catch (Exception e) {
        throw new ClientException(e);
    }
    final AtomicReference<ClientResponse> reference = new AtomicReference<>();
    try {
        ClientRequest request = new ClientRequest().setPath("/runtime").setMethod(Methods.GET);
        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);
    Assert.assertEquals(500, statusCode);
    if (statusCode == 500) {
        Status status = Config.getInstance().getMapper().readValue(body, Status.class);
        Assert.assertNotNull(status);
        Assert.assertEquals("ERR10010", status.getCode());
    }
}
Also used : ClientResponse(io.undertow.client.ClientResponse) Status(com.networknt.status.Status) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ClientConnection(io.undertow.client.ClientConnection) Http2Client(com.networknt.client.Http2Client) ClientRequest(io.undertow.client.ClientRequest) Test(org.junit.Test)

Example 25 with ClientResponse

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

the class CreateMenuTest method testCreateMenu.

@Test
public void testCreateMenu() 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<>();
    System.out.println("\n");
    System.out.println("json:" + s);
    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) SQLException(java.sql.SQLException) 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