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 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) { } }
/** * Creates a NStringEntity with the specified content and content type. * * @param s content to be used. Not {@code null}. * @param contentType content type to be used. May be {@code null}, in which case * {@link ContentType#TEXT_PLAIN} is assumed. * * @throws IllegalArgumentException if the string parameter is null * * @since 4.2 */ public NStringEntity(final String s, final ContentType contentType) { Args.notNull(s, "Source string"); Charset charset = contentType != null ? contentType.getCharset() : null; if (charset == null) { charset = HTTP.DEF_CONTENT_CHARSET; } this.b = s.getBytes(charset); this.buf = ByteBuffer.wrap(this.b); this.content = b; this.buffer = this.buf; if (contentType != null) { setContentType(contentType.toString()); } }
/** * {@inheritDoc} * * @deprecated (4.2) use {@link #close()} */ @Deprecated public void finish() { close(); }
/** * Creates a NStringEntity with the specified content and content type. * * @param s content to be used. Not {@code null}. * @param contentType content type to be used. May be {@code null}, in which case * {@link ContentType#TEXT_PLAIN} is assumed. * * @throws IllegalArgumentException if the string parameter is null * * @since 4.2 */ public NStringEntity(final String s, final ContentType contentType) { Args.notNull(s, "Source string"); Charset charset = contentType != null ? contentType.getCharset() : null; if (charset == null) { charset = HTTP.DEF_CONTENT_CHARSET; } this.b = s.getBytes(charset); this.buf = ByteBuffer.wrap(this.b); this.content = b; this.buffer = this.buf; if (contentType != null) { setContentType(contentType.toString()); } }
/** * {@inheritDoc} * * @deprecated (4.2) use {@link #close()} */ @Deprecated public void finish() { close(); }
@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) { } }
"/" + 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 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) { } }
HttpPost httpPost = new HttpPost(url); httpPost.setHeaders(getHeaders()); HttpEntity httpEntity = new NStringEntity(queryDslString, ContentType.APPLICATION_JSON); httpPost.setEntity(httpEntity);
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 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;
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) { } }
HttpEntity httpEntity = new NStringEntity(queryDslString, ContentType.APPLICATION_JSON);
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) { } }
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)); } }
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) { } }
@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."); } } }
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) { } }