private Response runQuery(String endpoint, String query, String index, String type) throws IOException { StringBuilder sb = new StringBuilder() .append("/") .append(index); if (type != null && !type.equals("")) { sb.append("/") .append(type); } sb.append(String.format("/%s", endpoint)); HttpEntity queryEntity = new NStringEntity(query, ContentType.APPLICATION_JSON); return client.performRequest("POST", sb.toString(), Collections.emptyMap(), queryEntity); }
public static void errorInternalServerError(HttpResponse response, String message, Throwable exception) { int status = HttpStatus.SC_INTERNAL_SERVER_ERROR; response.setStatusCode(status); StringWriter sw = new StringWriter(); exception.printStackTrace(new PrintWriter(sw, true)); String logMessage = (message != null ? message + "\n" : "") + sw.toString(); logger.debug("Returning HTTP status " + status + ": " + logMessage); try { NStringEntity entity = new NStringEntity("<html><body><h1>Internal server error</h1><p>" + (message != null ? message : "") + "<pre>" + sw.toString() + "</pre></body></html>", "UTF-8"); entity.setContentType("text/html; charset=UTF-8"); response.setEntity(entity); } catch (UnsupportedEncodingException e) { } }
public static void errorInternalServerError(HttpResponse response, String message, Throwable exception) { int status = HttpStatus.SC_INTERNAL_SERVER_ERROR; response.setStatusCode(status); StringWriter sw = new StringWriter(); exception.printStackTrace(new PrintWriter(sw, true)); String logMessage = (message != null ? message + "\n" : "") + sw.toString(); logger.debug("Returning HTTP status " + status + ": " + logMessage); try { NStringEntity entity = new NStringEntity("<html><body><h1>Internal server error</h1><p>" + (message != null ? message : "") + "<pre>" + sw.toString() + "</pre></body></html>", "UTF-8"); entity.setContentType("text/html; charset=UTF-8"); response.setEntity(entity); } catch (UnsupportedEncodingException e) { } }
@Override public Status insert(final String table, final String key, final Map<String, ByteIterator> values) { try { final Map<String, String> data = StringByteIterator.getStringMap(values); data.put(KEY, key); final Response response = restClient.performRequest( "POST", "/" + indexKey + "/" + table + "/", Collections.<String, String>emptyMap(), new NStringEntity(new ObjectMapper().writeValueAsString(data), ContentType.APPLICATION_JSON)); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_CREATED) { return Status.ERROR; } if (!isRefreshNeeded) { synchronized (this) { isRefreshNeeded = true; } } return Status.OK; } catch (final Exception e) { e.printStackTrace(); return Status.ERROR; } }
public static void errorFileNotFound(HttpResponse response, String uri) { int status = HttpStatus.SC_NOT_FOUND; response.setStatusCode(status); String message = "File " + uri + " not found"; logger.debug("Returning HTTP status " + status + ": " + message); try { NStringEntity entity = new NStringEntity("<html><body><h1>File not found</h1><p>" + message + "</p></body></html>", "UTF-8"); entity.setContentType("text/html; charset=UTF-8"); response.setEntity(entity); } catch (UnsupportedEncodingException e) { } }
public static void errorFileNotFound(HttpResponse response, String uri) { int status = HttpStatus.SC_NOT_FOUND; response.setStatusCode(status); String message = "File " + uri + " not found"; logger.debug("Returning HTTP status " + status + ": " + message); try { NStringEntity entity = new NStringEntity("<html><body><h1>File not found</h1><p>" + message + "</p></body></html>", "UTF-8"); entity.setContentType("text/html; charset=UTF-8"); response.setEntity(entity); } catch (UnsupportedEncodingException e) { } }
public static void errorMissingQueryParameter(HttpResponse response, String param) { int status = HttpStatus.SC_BAD_REQUEST; response.setStatusCode(status); String message = "Request must contain the parameter " + param; logger.debug("Returning HTTP status " + status + ": " + message); try { NStringEntity entity = new NStringEntity("<html><body><h1>Bad request</h1><p>" + message + ".</h1></body></html>", "UTF-8"); entity.setContentType("text/html; charset=UTF-8"); response.setEntity(entity); } catch (UnsupportedEncodingException e) { } }
public static void errorMissingQueryParameter(HttpResponse response, String param) { int status = HttpStatus.SC_BAD_REQUEST; response.setStatusCode(status); String message = "Request must contain the parameter " + param; logger.debug("Returning HTTP status " + status + ": " + message); try { NStringEntity entity = new NStringEntity("<html><body><h1>Bad request</h1><p>" + message + ".</h1></body></html>", "UTF-8"); entity.setContentType("text/html; charset=UTF-8"); response.setEntity(entity); } catch (UnsupportedEncodingException e) { } }
public static void errorWrongQueryParameterValue(HttpResponse response, String paramName, String illegalValue, String explanation) { int status = HttpStatus.SC_BAD_REQUEST; response.setStatusCode(status); String message = "The value '" + illegalValue + "' of parameter '" + paramName + "' is not valid" + (explanation != null ? ": " + explanation : ""); logger.debug("Returning HTTP status " + status + ": " + message); try { NStringEntity entity = new NStringEntity("<html><body><h1>Bad request</h1><p>" + message + ".</h1></body></html>", "UTF-8"); entity.setContentType("text/html; charset=UTF-8"); response.setEntity(entity); } catch (UnsupportedEncodingException e) { } }
"/" + indexKey + "/" + table + "/" + hit.get("_id"), params, new NStringEntity(new ObjectMapper().writeValueAsString(source), ContentType.APPLICATION_JSON)); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { return Status.ERROR;
public static void errorWrongQueryParameterValue(HttpResponse response, String paramName, String illegalValue, String explanation) { int status = HttpStatus.SC_BAD_REQUEST; response.setStatusCode(status); String message = "The value '" + illegalValue + "' of parameter '" + paramName + "' is not valid" + (explanation != null ? ": " + explanation : ""); logger.debug("Returning HTTP status " + status + ": " + message); try { NStringEntity entity = new NStringEntity("<html><body><h1>Bad request</h1><p>" + message + ".</h1></body></html>", "UTF-8"); entity.setContentType("text/html; charset=UTF-8"); response.setEntity(entity); } catch (UnsupportedEncodingException e) { } }
@Override protected void handleClientRequest(String absPath, Map<String, String> queryItems, HttpResponse response, Address serverAddressAtClient) throws IOException { // Individual info request String infoResponse = handleInfoRequest(absPath, queryItems, response); if (infoResponse == null) { // error condition, handleInfoRequest has set an error message return; } response.setStatusCode(HttpStatus.SC_OK); try { NStringEntity entity = new NStringEntity(infoResponse, "UTF-8"); entity.setContentType("text/plain; charset=UTF-8"); response.setEntity(entity); } catch (UnsupportedEncodingException e) { } }
@Override protected void handleClientRequest(String absPath, Map<String, String> queryItems, HttpResponse response, Address serverAddressAtClient) throws IOException { // Individual info request String infoResponse = handleInfoRequest(absPath, queryItems, response); if (infoResponse == null) { // error condition, handleInfoRequest has set an error message return; } response.setStatusCode(HttpStatus.SC_OK); try { NStringEntity entity = new NStringEntity(infoResponse, "UTF-8"); entity.setContentType("text/plain; charset=UTF-8"); response.setEntity(entity); } catch (UnsupportedEncodingException e) { } }
HttpPost httpPost = new HttpPost(url); httpPost.setHeaders(getHeaders()); HttpEntity httpEntity = new NStringEntity(queryDslString, ContentType.APPLICATION_JSON); httpPost.setEntity(httpEntity);
HttpEntity httpEntity = new NStringEntity(queryDslString, ContentType.APPLICATION_JSON);
public void index(final String urlFormat, final String bulkString) throws IOException { String tempUrl = String.format(urlFormat, getNextBaseUrl()); HttpEntity entity = new NStringEntity(bulkString, ContentType.APPLICATION_JSON); int statusCode = 0;
private void deleteAllDocuments(String typeToEmpty) throws URISyntaxException, IOException { URIBuilder builder = new URIBuilder().setScheme("http") .setHost("127.0.0.1").setPort(9200) .setPath(typeToEmpty); HttpEntityEnclosingRequestBase delete = new HttpEntityEnclosingRequestBase() { @Override public String getMethod() { return "DELETE"; } }; delete.setURI(builder.build()); String deletePayload = "{\"query\":{\"match_all\":{}}}"; HttpEntity entity = new NStringEntity(deletePayload, ContentType.APPLICATION_JSON); delete.setEntity(entity); HttpClient client = HttpClientBuilder.create().build(); HttpResponse response = client.execute(delete); if(response.getStatusLine().getStatusCode() != 200) { System.out.println(String.format("Couldn't delete index [%s] after running tests.", typeToEmpty)); } else { System.out.println(String.format("Successfully deleted [%s] index after running tests.", typeToEmpty)); } }
@AfterClass public static void tearDownClass() throws Exception { URIBuilder builder = new URIBuilder().setScheme("http") .setHost("127.0.0.1").setPort(9200) .setPath("/events/graphite_event/_query"); HttpEntityEnclosingRequestBase delete = new HttpEntityEnclosingRequestBase() { @Override public String getMethod() { return "DELETE"; } }; delete.setURI(builder.build()); String deletePayload = "{\"query\":{\"match_all\":{}}}"; HttpEntity entity = new NStringEntity(deletePayload, ContentType.APPLICATION_JSON); delete.setEntity(entity); HttpClient client = HttpClientBuilder.create().build(); HttpResponse response = client.execute(delete); if(response.getStatusLine().getStatusCode() != 200) { System.out.println("Couldn't delete index after running tests."); } else { System.out.println("Successfully deleted index after running tests."); } } }
addTestFieldMappings(broTemplate, "bro_doc"); String broTemplateJson = JSONUtils.INSTANCE.toJSON(broTemplate, true); HttpEntity broEntity = new NStringEntity(broTemplateJson, ContentType.APPLICATION_JSON); Response response = lowLevelClient.performRequest("PUT", "/_template/bro_template", Collections.emptyMap(), broEntity); assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); addTestFieldMappings(snortTemplate, "snort_doc"); String snortTemplateJson = JSONUtils.INSTANCE.toJSON(snortTemplate, true); HttpEntity snortEntity = new NStringEntity(snortTemplateJson, ContentType.APPLICATION_JSON); response = lowLevelClient.performRequest("PUT", "/_template/snort_template", Collections.emptyMap(), snortEntity); assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
@Before public void setup() throws Exception { client = ElasticsearchClientFactory.create(globals()); retrieveDao = new ElasticsearchRetrieveLatestDao(client); writer = new ElasticsearchBulkDocumentWriter<>(client) .withRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL); // add bro template JSONObject broTemplate = JSONUtils.INSTANCE.load(new File(broTemplatePath), JSONObject.class); String broTemplateJson = JSONUtils.INSTANCE.toJSON(broTemplate, true); HttpEntity broEntity = new NStringEntity(broTemplateJson, ContentType.APPLICATION_JSON); Response response = client .getLowLevelClient() .performRequest("PUT", "/_template/bro_template", Collections.emptyMap(), broEntity); assertThat(response.getStatusLine().getStatusCode(), CoreMatchers.equalTo(200)); }