/** * Retrieves all services registered with the Agent. * <p> * GET /v1/agent/services * * @return Map of Service ID to Services. */ public Map<String, Service> getServices() { final URI uri = createURI("/services"); final HTTP.Response response = HTTP.getResponse(uri.toString()); final JsonParserAndMapper jsonParserAndMapper = new JsonParserFactory().create(); if (response.status() == 200) { final Map<String, Object> map = jsonParserAndMapper.parseMap(response.payloadAsString()); final Map<String, Service> returnMap = new HashMap<>(map.size()); map.entrySet().forEach(entry -> { @SuppressWarnings("unchecked") Service service = fromMap((Map<String, Object>) entry.getValue(), Service.class); returnMap.put(entry.getKey(), service); }); return returnMap; } die("Unable to get list of services", uri, response.status(), response.payloadAsString()); return null; }
/** * Retrieves all checks registered with the Agent. * <p> * GET /v1/agent/checks * * @return Map of Check ID to Checks. */ public Map<String, HealthCheck> getChecks() { final URI uri = createURI("/checks"); final HTTP.Response response = HTTP.getResponse(uri.toString()); final JsonParserAndMapper jsonParserAndMapper = new JsonParserFactory().create(); if (response.status() == 200) { final Map<String, Object> map = jsonParserAndMapper.parseMap(response.payloadAsString()); final Map<String, HealthCheck> returnMap = new HashMap<>(map.size()); map.entrySet().forEach(entry -> { @SuppressWarnings("unchecked") HealthCheck healthCheck = fromMap((Map<String, Object>) entry.getValue(), HealthCheck.class); returnMap.put(entry.getKey(), healthCheck); }); return returnMap; } die("Unable to get health checks", uri, response.status(), response.statusMessageAsString(), response.payloadAsString()); return null; }
@Test public void testPing() { HTTP.Response response = HTTP.getResponse(buildURL("ping")); assertEquals(200, response.status()); final List<String> controls = response.headers().get(HttpHeaders.CACHE_CONTROL); Assert.assertEquals("no-cache, no-store", controls.get(0)); Assert.assertEquals("max-age=0", controls.get(1)); assertEquals("\"love rocket\"", response.body()); }
/** * De-registers a Health Check with the Agent * * @param checkId the id of the Check to deregister */ public void deregisterCheck(String checkId) { final URI uri = createURI("/check/deregister/" + checkId); HTTP.Response response = HTTP.getResponse(uri.toString()); if (response.status() != 200) { die("Error removing registration of service with Consul", uri, checkId, response.status(), response.statusMessageAsString(), response.payloadAsString()); } }
public int code() { return status(); }
/** * Register a service with Consul. * * @param registration The registration payload. */ public void register(final Registration registration) { final URI uri = createURI("/service/register"); HTTP.Response response = HTTP.jsonRestCallViaPUT(uri.toString(), toJson(registration)); if (response.status() != 200) { die("Error registering service with Consul", uri, registration, response.payloadAsString()); } }
/** * Pings the Consul Agent. */ public void pingAgent() { HTTP.Response response = HTTP.getResponse(createURI("/self").toString()); if (response.status() != 200) { die("Error pinging Consul", response.payloadAsString()); } }
@Test public void testBadJSONWithReturn() { serviceEndpointServer.stop(); openPort = PortUtils.findOpenPort(); serviceEndpointServer = EndpointServerBuilder.endpointServerBuilder() .setPort(openPort) .setErrorHandler(new Consumer<Throwable>() { @Override public void accept(Throwable throwable) { final Optional<HttpRequest> httpRequest = new HttpContext().getHttpRequest(); if (httpRequest.isPresent()) { httpRequest.get().getReceiver().respondOK("\"Bad JSON" + throwable.getMessage() + "\""); httpRequest.get().handled(); } } }) .build(); serviceEndpointServer.initServices(new TestService()); serviceEndpointServer.startServerAndWait(); HTTP.Response response = HTTP.jsonRestCallViaPOST(buildURL("addall2"), "\"i\": 1, \"s\": \"string\"}, " + "{\"i\": 2, \"s\": \"string2\"}]"); puts(response); assertEquals(200, response.status()); }
@Test public void testBadJSONCustomHandler() { serviceEndpointServer.stop(); openPort = PortUtils.findOpenPort(); serviceEndpointServer = EndpointServerBuilder.endpointServerBuilder() .setPort(openPort) .setErrorHandler(new Consumer<Throwable>() { @Override public void accept(Throwable throwable) { final Optional<HttpRequest> httpRequest = new HttpContext().getHttpRequest(); if (httpRequest.isPresent()) { httpRequest.get().getReceiver().respondOK("\"Bad JSON" + throwable.getMessage() + "\""); httpRequest.get().handled(); } } }) .build(); serviceEndpointServer.initServices(new TestService()); serviceEndpointServer.startServerAndWait(); HTTP.Response response = HTTP.jsonRestCallViaPOST(buildURL("addall"), "\"i\": 1, \"s\": \"string\"}, " + "{\"i\": 2, \"s\": \"string2\"}]"); assertEquals(200, response.status()); }
/** * <p> * Retrieves the Agent's configuration and member information. * </p> * GET /v1/agent/self * * @return The Agent information. */ public AgentInfo getAgentInfo() { final URI uri = createURI("/self"); HTTP.Response response = HTTP.getResponse(uri.toString()); if (response.status() != 200) { die("Error getting info about this agent", uri, response.status(), response.statusMessageAsString(), response.payloadAsString()); } return fromJson(response.payloadAsString(), AgentInfo.class); }
/** * De-registers a Health Check with the Agent * * @param checkId the id of the Check to deregister */ public void deregisterCheck(String checkId) { final URI uri = createURI("/check/deregister/" + checkId); HTTP.Response response = HTTP.getResponse(uri.toString()); if (response.status() != 200) { die("Error removing registration of service with Consul", uri, checkId, response.status(), response.statusMessageAsString(), response.payloadAsString()); } }
/** * Registers a Health Check with the Agent. * * @param check The Check to register. */ public void registerCheck(Check check) { final URI uri = createURI("/check/register"); HTTP.Response response = HTTP.jsonRestCallViaPUT(uri.toString(), toJson(check)); if (response.status() != 200) { die("Error removing registration of service with Consul", uri, check, response.status(), response.statusMessageAsString(), response.payloadAsString()); } }
/** * Remove registration of a particular service. * * @param serviceId the service id that you want to remove. */ public void deregister(final String serviceId) { final URI uri = createURI("/service/deregister/" + serviceId); HTTP.Response response = HTTP.getResponse(uri.toString()); if (response.status() != 200) { die("Error removing registration of service with Consul", uri, serviceId, response.status(), response.payloadAsString()); } }
public int code() { return status(); }
@Test public void test() { HTTP.Response response = HTTP.jsonRestCallViaPOST(buildURL("addall"), "[{\"i\": 1, \"s\": \"string\"}, " + "{\"i\": 2, \"s\": \"string2\"}]"); assertEquals(202, response.status()); while (ref.get() == null) { Sys.sleep(10); } assertNotNull(ref.get()); }
@Test public void testPing3() { HTTP.Response response = HTTP.jsonRestCallViaPUT(buildURL("ping3"), "\"foo\""); assertEquals(777, response.status()); assertEquals("hello mom foo", response.body()); }
/** * Pings the Consul Agent. */ public void pingAgent() { HTTP.Response response = HTTP.getResponse(createURI("/self").toString()); if (response.status() != 200) { die("Error pinging Consul", response.payloadAsString()); } }
/** * Register a service with Consul. * * @param registration The registration payload. */ public void register(final Registration registration) { final URI uri = createURI("/service/register"); HTTP.Response response = HTTP.jsonRestCallViaPUT(uri.toString(), toJson(registration)); if (response.status() != 200) { die("Error registering service with Consul", uri, registration, response.payloadAsString()); } }
@Test public void testBadJSON() { HTTP.Response response = HTTP.jsonRestCallViaPOST(buildURL("addall"), "\"i\": 1, \"s\": \"string\"}, " + "{\"i\": 2, \"s\": \"string2\"}]"); assertEquals(400, response.status()); }
@Test public void testPing2() { HTTP.Response response = HTTP.getResponse(buildURL("ping2")); assertEquals(777, response.status()); assertEquals("hello mom", response.body()); }