public <T> T get(String path, Class<T> clazz) throws ElasticsearchClientException { String uri = String.format("http://%s:%d%s", hostname, port, path); log.debug(String.format("Sending GET request to %s", uri)); HttpGet request = new HttpGet(uri); request.setHeader(HttpHeaders.ACCEPT, ContentType.APPLICATION_JSON.getMimeType()); String content = executeRequest(request); T result = deserialize(content, clazz); return result; }
/** * Check whether the cluster with the given name exists in the ES referenced by the client. * @param clusterName the ES cluster name * @param client the ES client to use to connect to ES * @return true if the instance is running, false otherwise */ public static boolean isInstanceRunning(String clusterName, ElasticsearchClient client) { boolean result; try { @SuppressWarnings("unchecked") Map<String, Object> response = client.get("/", Map.class); result = clusterName.equals(response.get("cluster_name")); } catch (ElasticsearchClientException e) { // failure is allowed result = false; } return result; }
@Test(expected = ElasticsearchSetupException.class) public void testExecuteInitCommandWithException() throws ElasticsearchClientException { ElasticsearchCommand command = mock(ElasticsearchCommand.class); when(command.getRequestMethod()).thenReturn(RequestMethod.DELETE); when(command.getRelativeUrl()).thenReturn("index/type/id"); doThrow(ElasticsearchClientException.class).when(client).delete("/index/type/id"); step.executeInitCommand(client, log, command); }
@Test public void testExecuteInitCommandPost() throws ElasticsearchClientException { ElasticsearchCommand command = mock(ElasticsearchCommand.class); when(command.getRequestMethod()).thenReturn(RequestMethod.POST); when(command.getJson()).thenReturn("json"); when(command.getRelativeUrl()).thenReturn("index/type/id"); step.executeInitCommand(client, log, command); verify(client).post("/index/type/id", "json", String.class); }
@Test public void testExecuteInitCommandPut() throws ElasticsearchClientException { ElasticsearchCommand command = mock(ElasticsearchCommand.class); when(command.getRequestMethod()).thenReturn(RequestMethod.PUT); when(command.getJson()).thenReturn("json"); when(command.getRelativeUrl()).thenReturn("index/type/id"); step.executeInitCommand(client, log, command); verify(client).put("/index/type/id", "json"); }
public void delete(String path) throws ElasticsearchClientException { String uri = String.format("http://%s:%d%s", hostname, port, path); log.debug(String.format("Sending DELETE request to %s", uri)); HttpDelete request = new HttpDelete(uri); executeRequest(request); }
public ElasticsearchClient build() { return new ElasticsearchClient( log, buildObjectMapper(), buildHttpClient(socketTimeout), hostname, port); }
protected String executeRequest(HttpRequestBase request) throws ElasticsearchClientException { Validate.notNull(httpClient, "Has the ElasticsearchClient been initialized?"); try { HttpResponse response = httpClient.execute(request); int statusCode = response.getStatusLine().getStatusCode(); String content = readContent(response.getEntity()); log.debug(String.format( "Response with status code %d and content: %s", statusCode, content)); // some PUT requests return 200, some 201 :-O if (statusCode != 200 && statusCode != 201) { throw new ElasticsearchClientException(request.getMethod(), statusCode, content); } return content; } catch (IOException e) { throw new ElasticsearchClientException(e); } finally { request.releaseConnection(); } }
@Test public void testExecuteInitCommandDelete() throws ElasticsearchClientException { ElasticsearchCommand command = mock(ElasticsearchCommand.class); when(command.getRequestMethod()).thenReturn(RequestMethod.DELETE); when(command.getRelativeUrl()).thenReturn("index/type/id"); step.executeInitCommand(client, log, command); verify(client).delete("/index/type/id"); }
public void put(String path, String entity) throws ElasticsearchClientException { String uri = String.format("http://%s:%d%s", hostname, port, path); log.info(String.format("Sending PUT request to %s with entity '%s'", uri, entity)); HttpPut request = new HttpPut(uri); request.setEntity(new StringEntity(entity, ContentType.APPLICATION_JSON)); executeRequest(request); }
public <T> T post(String path, String entity, Class<T> clazz) throws ElasticsearchClientException { String uri = String.format("http://%s:%d%s", hostname, port, path); log.debug(String.format("Sending POST request to %s with entity '%s'", uri, entity)); HttpPost request = new HttpPost(uri); request.setHeader(HttpHeaders.ACCEPT, ContentType.APPLICATION_JSON.getMimeType()); request.setEntity(new StringEntity(entity, ContentType.APPLICATION_JSON)); String content = executeRequest(request); T result = deserialize(content, clazz); return result; }
Map<String, Object> response = client.get("/_nodes", Map.class); result = clusterName.equals(response.get("cluster_name"));
public <T> T get(String path, String entity, Class<T> clazz) throws ElasticsearchClientException { String uri = String.format("http://%s:%d%s", hostname, port, path); log.debug(String.format("Sending GET request to %s with entity '%s'", uri, entity)); HttpGetWithEntity request = new HttpGetWithEntity(uri); request.setHeader(HttpHeaders.ACCEPT, ContentType.APPLICATION_JSON.getMimeType()); request.setEntity(new StringEntity(entity, ContentType.APPLICATION_JSON)); String content = executeRequest(request); T result = deserialize(content, clazz); return result; }