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;
}
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);
}
}
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);
}
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());
}
}
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);
}
Aggregations