public JestResult execute(Action clientRequest) throws IOException { String elasticSearchRestUrl = getRequestURL(getElasticSearchServer(), clientRequest.getURI()); HttpUriRequest request = constructHttpMethod( clientRequest.getRestMethodName(), elasticSearchRestUrl, clientRequest.getData()); // add headers added to action if (!clientRequest.getHeaders().isEmpty()) { for (Entry<String, Object> header : clientRequest.getHeaders().entrySet()) { request.addHeader(header.getKey(), header.getValue() + ""); } } HttpResponse response = httpClient.execute(request); // If head method returns no content, it is added according to response code thanks to // https://github.com/hlassiege if (request.getMethod().equalsIgnoreCase("HEAD")) { if (response.getEntity() == null) { if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { response.setEntity(new StringEntity("{\"ok\" : true, \"found\" : true}")); } else if (response.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND) { response.setEntity(new StringEntity("{\"ok\" : false, \"found\" : false}")); } } } return deserializeResponse(response, clientRequest.getName(), clientRequest.getPathToResult()); }
private void attachSignature( final HttpUriRequest request, final List<NameValuePair> params, final byte[] content) { final RequestDigestBuffer digest = RequestDigestBuffer.newBuilder(config) .withMethod(request.getMethod()) .withPath(request.getURI().getPath()) .withQueryParams(params) .withTimestamp(Instant.now(Clock.systemUTC()).toEpochMilli()) .build(); final byte[] signature = digest.doFinal(content); for (final Header h : digest.requestHeaders(signature)) { request.addHeader(h); } }
private <T> T executeRequest(final HttpUriRequest request, final ResponseHandler<T> handler) throws IOException { final CloseableHttpClient client = createClientInstance(); try { final CloseableHttpResponse response = client.execute(request); // Wrap the response in a buffer to facilitate error handlers re-playing the content if the // response // size is smaller than the max allowable buffer if (response.getEntity().getContentLength() >= 0 && response.getEntity().getContentLength() < config.getMaxBufferSize()) { EntityUtils.updateEntity(response, new BufferedHttpEntity(response.getEntity())); } // Explicit check for the authorization status of the API key if (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) { throw new HyppoAuthException(config); } try { log.debug( "{} - {} : {}", request.getMethod(), request.getURI().getPath(), response.getStatusLine().getStatusCode()); return handler.handleResponse(response); } finally { IOUtils.closeQuietly(response); } } catch (Exception e) { log.error( "{} - {} : FAILED - {}", request.getMethod(), request.getURI().getPath(), e.toString()); throw e; } finally { IOUtils.closeQuietly(client); } }