rename ElasticsearchResponse to Response and ElasticsearchResponseException to ResponseException

This commit is contained in:
javanna 2016-06-09 14:38:32 +02:00 committed by Luca Cavanna
parent be5e2e145b
commit 437c4f210b
21 changed files with 124 additions and 124 deletions

View File

@ -26,7 +26,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.elasticsearch.client.ElasticsearchResponse; import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClient;
import java.io.IOException; import java.io.IOException;
@ -62,7 +62,7 @@ public class HostsSniffer {
* Calls the elasticsearch nodes info api, parses the response and returns all the found http hosts * Calls the elasticsearch nodes info api, parses the response and returns all the found http hosts
*/ */
public List<HttpHost> sniffHosts() throws IOException { public List<HttpHost> sniffHosts() throws IOException {
try (ElasticsearchResponse response = restClient.performRequest("get", "/_nodes/http", sniffRequestParams, null)) { try (Response response = restClient.performRequest("get", "/_nodes/http", sniffRequestParams, null)) {
return readHosts(response.getEntity()); return readHosts(response.getEntity());
} }
} }

View File

@ -31,8 +31,8 @@ import org.apache.http.Consts;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.LuceneTestCase;
import org.elasticsearch.client.ElasticsearchResponse; import org.elasticsearch.client.Response;
import org.elasticsearch.client.ElasticsearchResponseException; import org.elasticsearch.client.ResponseException;
import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClient;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
@ -93,8 +93,8 @@ public class HostsSnifferTests extends LuceneTestCase {
for (HttpHost sniffedHost : sniffedHosts) { for (HttpHost sniffedHost : sniffedHosts) {
assertEquals(sniffedHost, responseHostsIterator.next()); assertEquals(sniffedHost, responseHostsIterator.next());
} }
} catch(ElasticsearchResponseException e) { } catch(ResponseException e) {
ElasticsearchResponse response = e.getElasticsearchResponse(); Response response = e.getResponse();
if (sniffResponse.isFailure) { if (sniffResponse.isFailure) {
assertThat(e.getMessage(), containsString("GET " + httpHost + "/_nodes/http?timeout=" + sniffRequestTimeout + "ms")); assertThat(e.getMessage(), containsString("GET " + httpHost + "/_nodes/http?timeout=" + sniffRequestTimeout + "ms"));
assertThat(e.getMessage(), containsString(Integer.toString(sniffResponse.nodesInfoResponseCode))); assertThat(e.getMessage(), containsString(Integer.toString(sniffResponse.nodesInfoResponseCode)));

View File

@ -35,13 +35,13 @@ import java.util.Objects;
* its corresponding {@link RequestLine} and {@link HttpHost}. * its corresponding {@link RequestLine} and {@link HttpHost}.
* It must be closed to free any resource held by it, as well as the corresponding connection in the connection pool. * It must be closed to free any resource held by it, as well as the corresponding connection in the connection pool.
*/ */
public class ElasticsearchResponse implements Closeable { public class Response implements Closeable {
private final RequestLine requestLine; private final RequestLine requestLine;
private final HttpHost host; private final HttpHost host;
private final CloseableHttpResponse response; private final CloseableHttpResponse response;
ElasticsearchResponse(RequestLine requestLine, HttpHost host, CloseableHttpResponse response) { Response(RequestLine requestLine, HttpHost host, CloseableHttpResponse response) {
Objects.requireNonNull(requestLine, "requestLine cannot be null"); Objects.requireNonNull(requestLine, "requestLine cannot be null");
Objects.requireNonNull(host, "node cannot be null"); Objects.requireNonNull(host, "node cannot be null");
Objects.requireNonNull(response, "response cannot be null"); Objects.requireNonNull(response, "response cannot be null");
@ -101,7 +101,7 @@ public class ElasticsearchResponse implements Closeable {
@Override @Override
public String toString() { public String toString() {
return "ElasticsearchResponse{" + return "Response{" +
"requestLine=" + requestLine + "requestLine=" + requestLine +
", host=" + host + ", host=" + host +
", response=" + response.getStatusLine() + ", response=" + response.getStatusLine() +

View File

@ -23,21 +23,21 @@ import java.io.IOException;
/** /**
* Exception thrown when an elasticsearch node responds to a request with a status code that indicates an error. * Exception thrown when an elasticsearch node responds to a request with a status code that indicates an error.
* Note that the response body gets passed in as a string and read eagerly, which means that the ElasticsearchResponse object * Note that the response body gets passed in as a string and read eagerly, which means that the Response object
* is expected to be closed and available only to read metadata like status line, request line, response headers. * is expected to be closed and available only to read metadata like status line, request line, response headers.
*/ */
public class ElasticsearchResponseException extends IOException { public class ResponseException extends IOException {
private ElasticsearchResponse elasticsearchResponse; private Response response;
private final String responseBody; private final String responseBody;
ElasticsearchResponseException(ElasticsearchResponse elasticsearchResponse, String responseBody) throws IOException { ResponseException(Response response, String responseBody) throws IOException {
super(buildMessage(elasticsearchResponse,responseBody)); super(buildMessage(response,responseBody));
this.elasticsearchResponse = elasticsearchResponse; this.response = response;
this.responseBody = responseBody; this.responseBody = responseBody;
} }
private static String buildMessage(ElasticsearchResponse response, String responseBody) { private static String buildMessage(Response response, String responseBody) {
String message = response.getRequestLine().getMethod() + " " + response.getHost() + response.getRequestLine().getUri() String message = response.getRequestLine().getMethod() + " " + response.getHost() + response.getRequestLine().getUri()
+ ": " + response.getStatusLine().toString(); + ": " + response.getStatusLine().toString();
if (responseBody != null) { if (responseBody != null) {
@ -47,17 +47,17 @@ public class ElasticsearchResponseException extends IOException {
} }
/** /**
* Returns the {@link ElasticsearchResponse} that caused this exception to be thrown. * Returns the {@link Response} that caused this exception to be thrown.
* Expected to be used only to read metadata like status line, request line, response headers. The response body should * Expected to be used only to read metadata like status line, request line, response headers. The response body should
* be retrieved using {@link #getResponseBody()} * be retrieved using {@link #getResponseBody()}
*/ */
public ElasticsearchResponse getElasticsearchResponse() { public Response getResponse() {
return elasticsearchResponse; return response;
} }
/** /**
* Returns the response body as a string or null if there wasn't any. * Returns the response body as a string or null if there wasn't any.
* The body is eagerly consumed when an ElasticsearchResponseException gets created, and its corresponding ElasticsearchResponse * The body is eagerly consumed when an ResponseException gets created, and its corresponding Response
* gets closed straightaway so this method is the only way to get back the response body that was returned. * gets closed straightaway so this method is the only way to get back the response body that was returned.
*/ */
public String getResponseBody() { public String getResponseBody() {

View File

@ -131,10 +131,10 @@ public final class RestClient implements Closeable {
* @return the response returned by elasticsearch * @return the response returned by elasticsearch
* @throws IOException in case of a problem or the connection was aborted * @throws IOException in case of a problem or the connection was aborted
* @throws ClientProtocolException in case of an http protocol error * @throws ClientProtocolException in case of an http protocol error
* @throws ElasticsearchResponseException in case elasticsearch responded with a status code that indicated an error * @throws ResponseException in case elasticsearch responded with a status code that indicated an error
*/ */
public ElasticsearchResponse performRequest(String method, String endpoint, Map<String, String> params, public Response performRequest(String method, String endpoint, Map<String, String> params,
HttpEntity entity, Header... headers) throws IOException { HttpEntity entity, Header... headers) throws IOException {
URI uri = buildUri(endpoint, params); URI uri = buildUri(endpoint, params);
HttpRequestBase request = createHttpRequest(method, uri, entity); HttpRequestBase request = createHttpRequest(method, uri, entity);
setHeaders(request, headers); setHeaders(request, headers);
@ -167,7 +167,7 @@ public final class RestClient implements Closeable {
lastSeenException = addSuppressedException(lastSeenException, e); lastSeenException = addSuppressedException(lastSeenException, e);
continue; continue;
} }
ElasticsearchResponse elasticsearchResponse = new ElasticsearchResponse(request.getRequestLine(), host, response); Response elasticsearchResponse = new Response(request.getRequestLine(), host, response);
int statusCode = response.getStatusLine().getStatusCode(); int statusCode = response.getStatusLine().getStatusCode();
if (statusCode < 300 || (request.getMethod().equals(HttpHead.METHOD_NAME) && statusCode == 404) ) { if (statusCode < 300 || (request.getMethod().equals(HttpHead.METHOD_NAME) && statusCode == 404) ) {
RequestLogger.log(logger, "request succeeded", request, host, response); RequestLogger.log(logger, "request succeeded", request, host, response);
@ -185,9 +185,9 @@ public final class RestClient implements Closeable {
} finally { } finally {
elasticsearchResponse.close(); elasticsearchResponse.close();
} }
ElasticsearchResponseException elasticsearchResponseException = new ElasticsearchResponseException( ResponseException responseException = new ResponseException(
elasticsearchResponse, responseBody); elasticsearchResponse, responseBody);
lastSeenException = addSuppressedException(lastSeenException, elasticsearchResponseException); lastSeenException = addSuppressedException(lastSeenException, responseException);
//clients don't retry on 500 because elasticsearch still misuses it instead of 400 in some places //clients don't retry on 500 because elasticsearch still misuses it instead of 400 in some places
if (statusCode == 502 || statusCode == 503 || statusCode == 504) { if (statusCode == 502 || statusCode == 503 || statusCode == 504) {
onFailure(host); onFailure(host);

View File

@ -144,12 +144,12 @@ public class RestClientIntegTests extends LuceneTestCase {
} }
int statusCode = randomStatusCode(random()); int statusCode = randomStatusCode(random());
ElasticsearchResponse esResponse; Response esResponse;
try (ElasticsearchResponse response = restClient.performRequest(method, "/" + statusCode, try (Response response = restClient.performRequest(method, "/" + statusCode,
Collections.<String, String>emptyMap(), null, headers)) { Collections.<String, String>emptyMap(), null, headers)) {
esResponse = response; esResponse = response;
} catch(ElasticsearchResponseException e) { } catch(ResponseException e) {
esResponse = e.getElasticsearchResponse(); esResponse = e.getResponse();
} }
assertThat(esResponse.getStatusLine().getStatusCode(), equalTo(statusCode)); assertThat(esResponse.getStatusLine().getStatusCode(), equalTo(statusCode));
for (Header responseHeader : esResponse.getHeaders()) { for (Header responseHeader : esResponse.getHeaders()) {
@ -187,16 +187,16 @@ public class RestClientIntegTests extends LuceneTestCase {
private void testBody(String method) throws Exception { private void testBody(String method) throws Exception {
String requestBody = "{ \"field\": \"value\" }"; String requestBody = "{ \"field\": \"value\" }";
StringEntity entity = new StringEntity(requestBody); StringEntity entity = new StringEntity(requestBody);
ElasticsearchResponse esResponse; Response esResponse;
String responseBody; String responseBody;
int statusCode = randomStatusCode(random()); int statusCode = randomStatusCode(random());
try (ElasticsearchResponse response = restClient.performRequest(method, "/" + statusCode, try (Response response = restClient.performRequest(method, "/" + statusCode,
Collections.<String, String>emptyMap(), entity)) { Collections.<String, String>emptyMap(), entity)) {
responseBody = EntityUtils.toString(response.getEntity()); responseBody = EntityUtils.toString(response.getEntity());
esResponse = response; esResponse = response;
} catch(ElasticsearchResponseException e) { } catch(ResponseException e) {
responseBody = e.getResponseBody(); responseBody = e.getResponseBody();
esResponse = e.getElasticsearchResponse(); esResponse = e.getResponse();
} }
assertEquals(statusCode, esResponse.getStatusLine().getStatusCode()); assertEquals(statusCode, esResponse.getStatusLine().getStatusCode());
assertEquals(requestBody, responseBody); assertEquals(requestBody, responseBody);

View File

@ -102,7 +102,7 @@ public class RestClientMultipleHostsTests extends LuceneTestCase {
Collections.addAll(hostsSet, httpHosts); Collections.addAll(hostsSet, httpHosts);
for (int j = 0; j < httpHosts.length; j++) { for (int j = 0; j < httpHosts.length; j++) {
int statusCode = randomOkStatusCode(random()); int statusCode = randomOkStatusCode(random());
try (ElasticsearchResponse response = restClient.performRequest(randomHttpMethod(random()), "/" + statusCode, try (Response response = restClient.performRequest(randomHttpMethod(random()), "/" + statusCode,
Collections.<String, String>emptyMap(), null)) { Collections.<String, String>emptyMap(), null)) {
assertThat(response.getStatusLine().getStatusCode(), equalTo(statusCode)); assertThat(response.getStatusLine().getStatusCode(), equalTo(statusCode));
assertTrue("host not found: " + response.getHost(), hostsSet.remove(response.getHost())); assertTrue("host not found: " + response.getHost(), hostsSet.remove(response.getHost()));
@ -121,7 +121,7 @@ public class RestClientMultipleHostsTests extends LuceneTestCase {
for (int j = 0; j < httpHosts.length; j++) { for (int j = 0; j < httpHosts.length; j++) {
String method = randomHttpMethod(random()); String method = randomHttpMethod(random());
int statusCode = randomErrorNoRetryStatusCode(random()); int statusCode = randomErrorNoRetryStatusCode(random());
try (ElasticsearchResponse response = restClient.performRequest(method, "/" + statusCode, try (Response response = restClient.performRequest(method, "/" + statusCode,
Collections.<String, String>emptyMap(), null)) { Collections.<String, String>emptyMap(), null)) {
if (method.equals("HEAD") && statusCode == 404) { if (method.equals("HEAD") && statusCode == 404) {
//no exception gets thrown although we got a 404 //no exception gets thrown although we got a 404
@ -131,11 +131,11 @@ public class RestClientMultipleHostsTests extends LuceneTestCase {
} else { } else {
fail("request should have failed"); fail("request should have failed");
} }
} catch(ElasticsearchResponseException e) { } catch(ResponseException e) {
if (method.equals("HEAD") && statusCode == 404) { if (method.equals("HEAD") && statusCode == 404) {
throw e; throw e;
} }
ElasticsearchResponse response = e.getElasticsearchResponse(); Response response = e.getResponse();
assertThat(response.getStatusLine().getStatusCode(), equalTo(statusCode)); assertThat(response.getStatusLine().getStatusCode(), equalTo(statusCode));
assertTrue("host not found: " + response.getHost(), hostsSet.remove(response.getHost())); assertTrue("host not found: " + response.getHost(), hostsSet.remove(response.getHost()));
assertEquals(0, e.getSuppressed().length); assertEquals(0, e.getSuppressed().length);
@ -151,21 +151,21 @@ public class RestClientMultipleHostsTests extends LuceneTestCase {
try { try {
restClient.performRequest(randomHttpMethod(random()), retryEndpoint, Collections.<String, String>emptyMap(), null); restClient.performRequest(randomHttpMethod(random()), retryEndpoint, Collections.<String, String>emptyMap(), null);
fail("request should have failed"); fail("request should have failed");
} catch(ElasticsearchResponseException e) { } catch(ResponseException e) {
Set<HttpHost> hostsSet = new HashSet<>(); Set<HttpHost> hostsSet = new HashSet<>();
Collections.addAll(hostsSet, httpHosts); Collections.addAll(hostsSet, httpHosts);
//first request causes all the hosts to be blacklisted, the returned exception holds one suppressed exception each //first request causes all the hosts to be blacklisted, the returned exception holds one suppressed exception each
failureListener.assertCalled(httpHosts); failureListener.assertCalled(httpHosts);
do { do {
ElasticsearchResponse response = e.getElasticsearchResponse(); Response response = e.getResponse();
assertThat(response.getStatusLine().getStatusCode(), equalTo(Integer.parseInt(retryEndpoint.substring(1)))); assertThat(response.getStatusLine().getStatusCode(), equalTo(Integer.parseInt(retryEndpoint.substring(1))));
assertTrue("host [" + response.getHost() + "] not found, most likely used multiple times", assertTrue("host [" + response.getHost() + "] not found, most likely used multiple times",
hostsSet.remove(response.getHost())); hostsSet.remove(response.getHost()));
if (e.getSuppressed().length > 0) { if (e.getSuppressed().length > 0) {
assertEquals(1, e.getSuppressed().length); assertEquals(1, e.getSuppressed().length);
Throwable suppressed = e.getSuppressed()[0]; Throwable suppressed = e.getSuppressed()[0];
assertThat(suppressed, instanceOf(ElasticsearchResponseException.class)); assertThat(suppressed, instanceOf(ResponseException.class));
e = (ElasticsearchResponseException)suppressed; e = (ResponseException)suppressed;
} else { } else {
e = null; e = null;
} }
@ -201,8 +201,8 @@ public class RestClientMultipleHostsTests extends LuceneTestCase {
try { try {
restClient.performRequest(randomHttpMethod(random()), retryEndpoint, Collections.<String, String>emptyMap(), null); restClient.performRequest(randomHttpMethod(random()), retryEndpoint, Collections.<String, String>emptyMap(), null);
fail("request should have failed"); fail("request should have failed");
} catch(ElasticsearchResponseException e) { } catch(ResponseException e) {
ElasticsearchResponse response = e.getElasticsearchResponse(); Response response = e.getResponse();
assertThat(response.getStatusLine().getStatusCode(), equalTo(Integer.parseInt(retryEndpoint.substring(1)))); assertThat(response.getStatusLine().getStatusCode(), equalTo(Integer.parseInt(retryEndpoint.substring(1))));
assertTrue("host [" + response.getHost() + "] not found, most likely used multiple times", assertTrue("host [" + response.getHost() + "] not found, most likely used multiple times",
hostsSet.remove(response.getHost())); hostsSet.remove(response.getHost()));
@ -224,13 +224,13 @@ public class RestClientMultipleHostsTests extends LuceneTestCase {
int iters = RandomInts.randomIntBetween(random(), 2, 10); int iters = RandomInts.randomIntBetween(random(), 2, 10);
for (int y = 0; y < iters; y++) { for (int y = 0; y < iters; y++) {
int statusCode = randomErrorNoRetryStatusCode(random()); int statusCode = randomErrorNoRetryStatusCode(random());
ElasticsearchResponse response; Response response;
try (ElasticsearchResponse esResponse = restClient.performRequest(randomHttpMethod(random()), "/" + statusCode, try (Response esResponse = restClient.performRequest(randomHttpMethod(random()), "/" + statusCode,
Collections.<String, String>emptyMap(), null)) { Collections.<String, String>emptyMap(), null)) {
response = esResponse; response = esResponse;
} }
catch(ElasticsearchResponseException e) { catch(ResponseException e) {
response = e.getElasticsearchResponse(); response = e.getResponse();
} }
assertThat(response.getStatusLine().getStatusCode(), equalTo(statusCode)); assertThat(response.getStatusLine().getStatusCode(), equalTo(statusCode));
if (selectedHost == null) { if (selectedHost == null) {
@ -248,8 +248,8 @@ public class RestClientMultipleHostsTests extends LuceneTestCase {
restClient.performRequest(randomHttpMethod(random()), retryEndpoint, restClient.performRequest(randomHttpMethod(random()), retryEndpoint,
Collections.<String, String>emptyMap(), null); Collections.<String, String>emptyMap(), null);
fail("request should have failed"); fail("request should have failed");
} catch(ElasticsearchResponseException e) { } catch(ResponseException e) {
ElasticsearchResponse response = e.getElasticsearchResponse(); Response response = e.getResponse();
assertThat(response.getStatusLine().getStatusCode(), equalTo(Integer.parseInt(retryEndpoint.substring(1)))); assertThat(response.getStatusLine().getStatusCode(), equalTo(Integer.parseInt(retryEndpoint.substring(1))));
assertThat(response.getHost(), equalTo(selectedHost)); assertThat(response.getHost(), equalTo(selectedHost));
failureListener.assertCalled(selectedHost); failureListener.assertCalled(selectedHost);

View File

@ -183,7 +183,7 @@ public class RestClientSingleHostTests extends LuceneTestCase {
public void testOkStatusCodes() throws Exception { public void testOkStatusCodes() throws Exception {
for (String method : getHttpMethods()) { for (String method : getHttpMethods()) {
for (int okStatusCode : getOkStatusCodes()) { for (int okStatusCode : getOkStatusCodes()) {
ElasticsearchResponse response = restClient.performRequest(method, "/" + okStatusCode, Response response = restClient.performRequest(method, "/" + okStatusCode,
Collections.<String, String>emptyMap(), null); Collections.<String, String>emptyMap(), null);
assertThat(response.getStatusLine().getStatusCode(), equalTo(okStatusCode)); assertThat(response.getStatusLine().getStatusCode(), equalTo(okStatusCode));
} }
@ -198,7 +198,7 @@ public class RestClientSingleHostTests extends LuceneTestCase {
for (String method : getHttpMethods()) { for (String method : getHttpMethods()) {
//error status codes should cause an exception to be thrown //error status codes should cause an exception to be thrown
for (int errorStatusCode : getAllErrorStatusCodes()) { for (int errorStatusCode : getAllErrorStatusCodes()) {
try (ElasticsearchResponse response = restClient.performRequest(method, "/" + errorStatusCode, try (Response response = restClient.performRequest(method, "/" + errorStatusCode,
Collections.<String, String>emptyMap(), null)) { Collections.<String, String>emptyMap(), null)) {
if (method.equals("HEAD") && errorStatusCode == 404) { if (method.equals("HEAD") && errorStatusCode == 404) {
//no exception gets thrown although we got a 404 //no exception gets thrown although we got a 404
@ -206,11 +206,11 @@ public class RestClientSingleHostTests extends LuceneTestCase {
} else { } else {
fail("request should have failed"); fail("request should have failed");
} }
} catch(ElasticsearchResponseException e) { } catch(ResponseException e) {
if (method.equals("HEAD") && errorStatusCode == 404) { if (method.equals("HEAD") && errorStatusCode == 404) {
throw e; throw e;
} }
assertThat(e.getElasticsearchResponse().getStatusLine().getStatusCode(), equalTo(errorStatusCode)); assertThat(e.getResponse().getStatusLine().getStatusCode(), equalTo(errorStatusCode));
} }
if (errorStatusCode <= 500) { if (errorStatusCode <= 500) {
failureListener.assertNotCalled(); failureListener.assertNotCalled();
@ -250,7 +250,7 @@ public class RestClientSingleHostTests extends LuceneTestCase {
StringEntity entity = new StringEntity(body); StringEntity entity = new StringEntity(body);
for (String method : Arrays.asList("DELETE", "GET", "PATCH", "POST", "PUT")) { for (String method : Arrays.asList("DELETE", "GET", "PATCH", "POST", "PUT")) {
for (int okStatusCode : getOkStatusCodes()) { for (int okStatusCode : getOkStatusCodes()) {
try (ElasticsearchResponse response = restClient.performRequest(method, "/" + okStatusCode, try (Response response = restClient.performRequest(method, "/" + okStatusCode,
Collections.<String, String>emptyMap(), entity)) { Collections.<String, String>emptyMap(), entity)) {
assertThat(response.getStatusLine().getStatusCode(), equalTo(okStatusCode)); assertThat(response.getStatusLine().getStatusCode(), equalTo(okStatusCode));
assertThat(EntityUtils.toString(response.getEntity()), equalTo(body)); assertThat(EntityUtils.toString(response.getEntity()), equalTo(body));
@ -260,8 +260,8 @@ public class RestClientSingleHostTests extends LuceneTestCase {
try { try {
restClient.performRequest(method, "/" + errorStatusCode, Collections.<String, String>emptyMap(), entity); restClient.performRequest(method, "/" + errorStatusCode, Collections.<String, String>emptyMap(), entity);
fail("request should have failed"); fail("request should have failed");
} catch(ElasticsearchResponseException e) { } catch(ResponseException e) {
ElasticsearchResponse response = e.getElasticsearchResponse(); Response response = e.getResponse();
assertThat(response.getStatusLine().getStatusCode(), equalTo(errorStatusCode)); assertThat(response.getStatusLine().getStatusCode(), equalTo(errorStatusCode));
assertThat(EntityUtils.toString(response.getEntity()), equalTo(body)); assertThat(EntityUtils.toString(response.getEntity()), equalTo(body));
} }
@ -326,12 +326,12 @@ public class RestClientSingleHostTests extends LuceneTestCase {
} }
int statusCode = randomStatusCode(random()); int statusCode = randomStatusCode(random());
ElasticsearchResponse esResponse; Response esResponse;
try (ElasticsearchResponse response = restClient.performRequest(method, "/" + statusCode, try (Response response = restClient.performRequest(method, "/" + statusCode,
Collections.<String, String>emptyMap(), null, headers)) { Collections.<String, String>emptyMap(), null, headers)) {
esResponse = response; esResponse = response;
} catch(ElasticsearchResponseException e) { } catch(ResponseException e) {
esResponse = e.getElasticsearchResponse(); esResponse = e.getResponse();
} }
assertThat(esResponse.getStatusLine().getStatusCode(), equalTo(statusCode)); assertThat(esResponse.getStatusLine().getStatusCode(), equalTo(statusCode));
for (Header responseHeader : esResponse.getHeaders()) { for (Header responseHeader : esResponse.getHeaders()) {
@ -413,7 +413,7 @@ public class RestClientSingleHostTests extends LuceneTestCase {
try { try {
restClient.performRequest(method, uriAsString, params, entity, headers); restClient.performRequest(method, uriAsString, params, entity, headers);
} catch(ElasticsearchResponseException e) { } catch(ResponseException e) {
//all good //all good
} }
return request; return request;

View File

@ -27,7 +27,7 @@ import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader; import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpContext;
import org.elasticsearch.client.ElasticsearchResponse; import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClient;
import org.elasticsearch.common.network.NetworkModule; import org.elasticsearch.common.network.NetworkModule;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
@ -62,7 +62,7 @@ public class NettyHttpCompressionIT extends ESIntegTestCase {
// we need to intercept early, otherwise internal logic in HttpClient will just remove the header and we cannot verify it // we need to intercept early, otherwise internal logic in HttpClient will just remove the header and we cannot verify it
ContentEncodingHeaderExtractor headerExtractor = new ContentEncodingHeaderExtractor(); ContentEncodingHeaderExtractor headerExtractor = new ContentEncodingHeaderExtractor();
try (RestClient client = createRestClient(HttpClients.custom().addInterceptorFirst(headerExtractor).build())) { try (RestClient client = createRestClient(HttpClients.custom().addInterceptorFirst(headerExtractor).build())) {
try (ElasticsearchResponse response = client.performRequest("GET", "/", Collections.emptyMap(), null, try (Response response = client.performRequest("GET", "/", Collections.emptyMap(), null,
new BasicHeader(HttpHeaders.ACCEPT_ENCODING, GZIP_ENCODING))) { new BasicHeader(HttpHeaders.ACCEPT_ENCODING, GZIP_ENCODING))) {
assertEquals(200, response.getStatusLine().getStatusCode()); assertEquals(200, response.getStatusLine().getStatusCode());
assertTrue(headerExtractor.hasContentEncodingHeader()); assertTrue(headerExtractor.hasContentEncodingHeader());
@ -76,7 +76,7 @@ public class NettyHttpCompressionIT extends ESIntegTestCase {
ContentEncodingHeaderExtractor headerExtractor = new ContentEncodingHeaderExtractor(); ContentEncodingHeaderExtractor headerExtractor = new ContentEncodingHeaderExtractor();
CloseableHttpClient httpClient = HttpClients.custom().disableContentCompression().addInterceptorFirst(headerExtractor).build(); CloseableHttpClient httpClient = HttpClients.custom().disableContentCompression().addInterceptorFirst(headerExtractor).build();
try (RestClient client = createRestClient(httpClient)) { try (RestClient client = createRestClient(httpClient)) {
try (ElasticsearchResponse response = client.performRequest("GET", "/", Collections.emptyMap(), null)) { try (Response response = client.performRequest("GET", "/", Collections.emptyMap(), null)) {
assertEquals(200, response.getStatusLine().getStatusCode()); assertEquals(200, response.getStatusLine().getStatusCode());
assertFalse(headerExtractor.hasContentEncodingHeader()); assertFalse(headerExtractor.hasContentEncodingHeader());
} }
@ -89,7 +89,7 @@ public class NettyHttpCompressionIT extends ESIntegTestCase {
// this disable content compression in both directions (request and response) // this disable content compression in both directions (request and response)
CloseableHttpClient httpClient = HttpClients.custom().disableContentCompression().addInterceptorFirst(headerExtractor).build(); CloseableHttpClient httpClient = HttpClients.custom().disableContentCompression().addInterceptorFirst(headerExtractor).build();
try (RestClient client = createRestClient(httpClient)) { try (RestClient client = createRestClient(httpClient)) {
try (ElasticsearchResponse response = client.performRequest("POST", "/company/employees/1", try (Response response = client.performRequest("POST", "/company/employees/1",
Collections.emptyMap(), SAMPLE_DOCUMENT)) { Collections.emptyMap(), SAMPLE_DOCUMENT)) {
assertEquals(201, response.getStatusLine().getStatusCode()); assertEquals(201, response.getStatusLine().getStatusCode());
assertFalse(headerExtractor.hasContentEncodingHeader()); assertFalse(headerExtractor.hasContentEncodingHeader());
@ -102,7 +102,7 @@ public class NettyHttpCompressionIT extends ESIntegTestCase {
ContentEncodingHeaderExtractor headerExtractor = new ContentEncodingHeaderExtractor(); ContentEncodingHeaderExtractor headerExtractor = new ContentEncodingHeaderExtractor();
// we don't call #disableContentCompression() hence the client will send the content compressed // we don't call #disableContentCompression() hence the client will send the content compressed
try (RestClient client = createRestClient(HttpClients.custom().addInterceptorFirst(headerExtractor).build())) { try (RestClient client = createRestClient(HttpClients.custom().addInterceptorFirst(headerExtractor).build())) {
try (ElasticsearchResponse response = client.performRequest("POST", "/company/employees/2", try (Response response = client.performRequest("POST", "/company/employees/2",
Collections.emptyMap(), SAMPLE_DOCUMENT)) { Collections.emptyMap(), SAMPLE_DOCUMENT)) {
assertEquals(201, response.getStatusLine().getStatusCode()); assertEquals(201, response.getStatusLine().getStatusCode());
assertEquals(GZIP_ENCODING, headerExtractor.getContentEncodingHeader().getValue()); assertEquals(GZIP_ENCODING, headerExtractor.getContentEncodingHeader().getValue());

View File

@ -19,8 +19,8 @@
package org.elasticsearch.options.detailederrors; package org.elasticsearch.options.detailederrors;
import org.elasticsearch.client.ElasticsearchResponse; import org.elasticsearch.client.Response;
import org.elasticsearch.client.ElasticsearchResponseException; import org.elasticsearch.client.ResponseException;
import org.elasticsearch.common.network.NetworkModule; import org.elasticsearch.common.network.NetworkModule;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.http.HttpTransportSettings; import org.elasticsearch.http.HttpTransportSettings;
@ -51,8 +51,8 @@ public class DetailedErrorsDisabledIT extends ESIntegTestCase {
try { try {
getRestClient().performRequest("DELETE", "/", Collections.singletonMap("error_trace", "true"), null); getRestClient().performRequest("DELETE", "/", Collections.singletonMap("error_trace", "true"), null);
fail("request should have failed"); fail("request should have failed");
} catch(ElasticsearchResponseException e) { } catch(ResponseException e) {
ElasticsearchResponse response = e.getElasticsearchResponse(); Response response = e.getResponse();
assertThat(response.getHeader("Content-Type"), is("application/json")); assertThat(response.getHeader("Content-Type"), is("application/json"));
assertThat(e.getResponseBody(), is("{\"error\":\"error traces in responses are disabled.\"}")); assertThat(e.getResponseBody(), is("{\"error\":\"error traces in responses are disabled.\"}"));
assertThat(response.getStatusLine().getStatusCode(), is(400)); assertThat(response.getStatusLine().getStatusCode(), is(400));

View File

@ -19,8 +19,8 @@
package org.elasticsearch.options.detailederrors; package org.elasticsearch.options.detailederrors;
import org.elasticsearch.client.ElasticsearchResponse; import org.elasticsearch.client.Response;
import org.elasticsearch.client.ElasticsearchResponseException; import org.elasticsearch.client.ResponseException;
import org.elasticsearch.common.network.NetworkModule; import org.elasticsearch.common.network.NetworkModule;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.test.ESIntegTestCase; import org.elasticsearch.test.ESIntegTestCase;
@ -49,8 +49,8 @@ public class DetailedErrorsEnabledIT extends ESIntegTestCase {
try { try {
getRestClient().performRequest("DELETE", "/", Collections.singletonMap("error_trace", "true"), null); getRestClient().performRequest("DELETE", "/", Collections.singletonMap("error_trace", "true"), null);
fail("request should have failed"); fail("request should have failed");
} catch(ElasticsearchResponseException e) { } catch(ResponseException e) {
ElasticsearchResponse response = e.getElasticsearchResponse(); Response response = e.getResponse();
assertThat(response.getHeader("Content-Type"), containsString("application/json")); assertThat(response.getHeader("Content-Type"), containsString("application/json"));
assertThat(e.getResponseBody(), containsString("\"stack_trace\":\"[Validation Failed: 1: index / indices is missing;]; " + assertThat(e.getResponseBody(), containsString("\"stack_trace\":\"[Validation Failed: 1: index / indices is missing;]; " +
"nested: ActionRequestValidationException[Validation Failed: 1:")); "nested: ActionRequestValidationException[Validation Failed: 1:"));
@ -59,8 +59,8 @@ public class DetailedErrorsEnabledIT extends ESIntegTestCase {
try { try {
getRestClient().performRequest("DELETE", "/", Collections.emptyMap(), null); getRestClient().performRequest("DELETE", "/", Collections.emptyMap(), null);
fail("request should have failed"); fail("request should have failed");
} catch(ElasticsearchResponseException e) { } catch(ResponseException e) {
ElasticsearchResponse response = e.getElasticsearchResponse(); Response response = e.getResponse();
assertThat(response.getHeader("Content-Type"), containsString("application/json")); assertThat(response.getHeader("Content-Type"), containsString("application/json"));
assertThat(e.getResponseBody(), not(containsString("\"stack_trace\":\"[Validation Failed: 1: index / indices is missing;]; " assertThat(e.getResponseBody(), not(containsString("\"stack_trace\":\"[Validation Failed: 1: index / indices is missing;]; "
+ "nested: ActionRequestValidationException[Validation Failed: 1:"))); + "nested: ActionRequestValidationException[Validation Failed: 1:")));

View File

@ -19,8 +19,8 @@
package org.elasticsearch.plugins; package org.elasticsearch.plugins;
import org.apache.http.message.BasicHeader; import org.apache.http.message.BasicHeader;
import org.elasticsearch.client.ElasticsearchResponse; import org.elasticsearch.client.Response;
import org.elasticsearch.client.ElasticsearchResponseException; import org.elasticsearch.client.ResponseException;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.plugins.responseheader.TestResponseHeaderPlugin; import org.elasticsearch.plugins.responseheader.TestResponseHeaderPlugin;
import org.elasticsearch.test.ESIntegTestCase; import org.elasticsearch.test.ESIntegTestCase;
@ -55,13 +55,13 @@ public class ResponseHeaderPluginIT extends ESIntegTestCase {
try { try {
getRestClient().performRequest("GET", "/_protected", Collections.emptyMap(), null); getRestClient().performRequest("GET", "/_protected", Collections.emptyMap(), null);
fail("request should have failed"); fail("request should have failed");
} catch(ElasticsearchResponseException e) { } catch(ResponseException e) {
ElasticsearchResponse response = e.getElasticsearchResponse(); Response response = e.getResponse();
assertThat(response.getStatusLine().getStatusCode(), equalTo(401)); assertThat(response.getStatusLine().getStatusCode(), equalTo(401));
assertThat(response.getHeader("Secret"), equalTo("required")); assertThat(response.getHeader("Secret"), equalTo("required"));
} }
try (ElasticsearchResponse authResponse = getRestClient().performRequest("GET", "/_protected", Collections.emptyMap(), null, try (Response authResponse = getRestClient().performRequest("GET", "/_protected", Collections.emptyMap(), null,
new BasicHeader("Secret", "password"))) { new BasicHeader("Secret", "password"))) {
assertThat(authResponse.getStatusLine().getStatusCode(), equalTo(200)); assertThat(authResponse.getStatusLine().getStatusCode(), equalTo(200));
assertThat(authResponse.getHeader("Secret"), equalTo("granted")); assertThat(authResponse.getHeader("Secret"), equalTo("granted"));

View File

@ -20,7 +20,7 @@
package org.elasticsearch.rest; package org.elasticsearch.rest;
import org.apache.http.message.BasicHeader; import org.apache.http.message.BasicHeader;
import org.elasticsearch.client.ElasticsearchResponse; import org.elasticsearch.client.Response;
import org.elasticsearch.common.network.NetworkModule; import org.elasticsearch.common.network.NetworkModule;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.test.ESIntegTestCase; import org.elasticsearch.test.ESIntegTestCase;
@ -46,7 +46,7 @@ public class CorsNotSetIT extends ESIntegTestCase {
public void testCorsSettingDefaultBehaviourDoesNotReturnAnything() throws Exception { public void testCorsSettingDefaultBehaviourDoesNotReturnAnything() throws Exception {
String corsValue = "http://localhost:9200"; String corsValue = "http://localhost:9200";
try (ElasticsearchResponse response = getRestClient().performRequest("GET", "/", Collections.emptyMap(), null, try (Response response = getRestClient().performRequest("GET", "/", Collections.emptyMap(), null,
new BasicHeader("User-Agent", "Mozilla Bar"), new BasicHeader("Origin", corsValue))) { new BasicHeader("User-Agent", "Mozilla Bar"), new BasicHeader("Origin", corsValue))) {
assertThat(response.getStatusLine().getStatusCode(), is(200)); assertThat(response.getStatusLine().getStatusCode(), is(200));
assertThat(response.getHeader("Access-Control-Allow-Origin"), nullValue()); assertThat(response.getHeader("Access-Control-Allow-Origin"), nullValue());
@ -55,7 +55,7 @@ public class CorsNotSetIT extends ESIntegTestCase {
} }
public void testThatOmittingCorsHeaderDoesNotReturnAnything() throws Exception { public void testThatOmittingCorsHeaderDoesNotReturnAnything() throws Exception {
try (ElasticsearchResponse response = getRestClient().performRequest("GET", "/", Collections.emptyMap(), null)) { try (Response response = getRestClient().performRequest("GET", "/", Collections.emptyMap(), null)) {
assertThat(response.getStatusLine().getStatusCode(), is(200)); assertThat(response.getStatusLine().getStatusCode(), is(200));
assertThat(response.getHeader("Access-Control-Allow-Origin"), nullValue()); assertThat(response.getHeader("Access-Control-Allow-Origin"), nullValue());
assertThat(response.getHeader("Access-Control-Allow-Credentials"), nullValue()); assertThat(response.getHeader("Access-Control-Allow-Credentials"), nullValue());

View File

@ -19,8 +19,8 @@
package org.elasticsearch.rest; package org.elasticsearch.rest;
import org.apache.http.message.BasicHeader; import org.apache.http.message.BasicHeader;
import org.elasticsearch.client.ElasticsearchResponse; import org.elasticsearch.client.Response;
import org.elasticsearch.client.ElasticsearchResponseException; import org.elasticsearch.client.ResponseException;
import org.elasticsearch.common.logging.ESLogger; import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.network.NetworkModule; import org.elasticsearch.common.network.NetworkModule;
@ -61,12 +61,12 @@ public class CorsRegexIT extends ESIntegTestCase {
public void testThatRegularExpressionWorksOnMatch() throws Exception { public void testThatRegularExpressionWorksOnMatch() throws Exception {
String corsValue = "http://localhost:9200"; String corsValue = "http://localhost:9200";
try (ElasticsearchResponse response = getRestClient().performRequest("GET", "/", Collections.emptyMap(), null, try (Response response = getRestClient().performRequest("GET", "/", Collections.emptyMap(), null,
new BasicHeader("User-Agent", "Mozilla Bar"), new BasicHeader("Origin", corsValue))) { new BasicHeader("User-Agent", "Mozilla Bar"), new BasicHeader("Origin", corsValue))) {
assertResponseWithOriginheader(response, corsValue); assertResponseWithOriginheader(response, corsValue);
} }
corsValue = "https://localhost:9200"; corsValue = "https://localhost:9200";
try (ElasticsearchResponse response = getRestClient().performRequest("GET", "/", Collections.emptyMap(), null, try (Response response = getRestClient().performRequest("GET", "/", Collections.emptyMap(), null,
new BasicHeader("User-Agent", "Mozilla Bar"), new BasicHeader("Origin", corsValue));) { new BasicHeader("User-Agent", "Mozilla Bar"), new BasicHeader("Origin", corsValue));) {
assertResponseWithOriginheader(response, corsValue); assertResponseWithOriginheader(response, corsValue);
assertThat(response.getHeader("Access-Control-Allow-Credentials"), is("true")); assertThat(response.getHeader("Access-Control-Allow-Credentials"), is("true"));
@ -78,8 +78,8 @@ public class CorsRegexIT extends ESIntegTestCase {
getRestClient().performRequest("GET", "/", Collections.emptyMap(), null, new BasicHeader("User-Agent", "Mozilla Bar"), getRestClient().performRequest("GET", "/", Collections.emptyMap(), null, new BasicHeader("User-Agent", "Mozilla Bar"),
new BasicHeader("Origin", "http://evil-host:9200")); new BasicHeader("Origin", "http://evil-host:9200"));
fail("request should have failed"); fail("request should have failed");
} catch(ElasticsearchResponseException e) { } catch(ResponseException e) {
ElasticsearchResponse response = e.getElasticsearchResponse(); Response response = e.getResponse();
// a rejected origin gets a FORBIDDEN - 403 // a rejected origin gets a FORBIDDEN - 403
assertThat(response.getStatusLine().getStatusCode(), is(403)); assertThat(response.getStatusLine().getStatusCode(), is(403));
assertThat(response.getHeader("Access-Control-Allow-Origin"), nullValue()); assertThat(response.getHeader("Access-Control-Allow-Origin"), nullValue());
@ -87,7 +87,7 @@ public class CorsRegexIT extends ESIntegTestCase {
} }
public void testThatSendingNoOriginHeaderReturnsNoAccessControlHeader() throws Exception { public void testThatSendingNoOriginHeaderReturnsNoAccessControlHeader() throws Exception {
try (ElasticsearchResponse response = getRestClient().performRequest("GET", "/", Collections.emptyMap(), null, try (Response response = getRestClient().performRequest("GET", "/", Collections.emptyMap(), null,
new BasicHeader("User-Agent", "Mozilla Bar"))) { new BasicHeader("User-Agent", "Mozilla Bar"))) {
assertThat(response.getStatusLine().getStatusCode(), is(200)); assertThat(response.getStatusLine().getStatusCode(), is(200));
assertThat(response.getHeader("Access-Control-Allow-Origin"), nullValue()); assertThat(response.getHeader("Access-Control-Allow-Origin"), nullValue());
@ -95,7 +95,7 @@ public class CorsRegexIT extends ESIntegTestCase {
} }
public void testThatRegularExpressionIsNotAppliedWithoutCorrectBrowserOnMatch() throws Exception { public void testThatRegularExpressionIsNotAppliedWithoutCorrectBrowserOnMatch() throws Exception {
try (ElasticsearchResponse response = getRestClient().performRequest("GET", "/", Collections.emptyMap(), null)) { try (Response response = getRestClient().performRequest("GET", "/", Collections.emptyMap(), null)) {
assertThat(response.getStatusLine().getStatusCode(), is(200)); assertThat(response.getStatusLine().getStatusCode(), is(200));
assertThat(response.getHeader("Access-Control-Allow-Origin"), nullValue()); assertThat(response.getHeader("Access-Control-Allow-Origin"), nullValue());
} }
@ -103,7 +103,7 @@ public class CorsRegexIT extends ESIntegTestCase {
public void testThatPreFlightRequestWorksOnMatch() throws Exception { public void testThatPreFlightRequestWorksOnMatch() throws Exception {
String corsValue = "http://localhost:9200"; String corsValue = "http://localhost:9200";
try (ElasticsearchResponse response = getRestClient().performRequest("OPTIONS", "/", Collections.emptyMap(), null, try (Response response = getRestClient().performRequest("OPTIONS", "/", Collections.emptyMap(), null,
new BasicHeader("User-Agent", "Mozilla Bar"), new BasicHeader("Origin", corsValue), new BasicHeader("User-Agent", "Mozilla Bar"), new BasicHeader("Origin", corsValue),
new BasicHeader(HttpHeaders.Names.ACCESS_CONTROL_REQUEST_METHOD, "GET"));) { new BasicHeader(HttpHeaders.Names.ACCESS_CONTROL_REQUEST_METHOD, "GET"));) {
assertResponseWithOriginheader(response, corsValue); assertResponseWithOriginheader(response, corsValue);
@ -117,8 +117,8 @@ public class CorsRegexIT extends ESIntegTestCase {
new BasicHeader("Origin", "http://evil-host:9200"), new BasicHeader("Origin", "http://evil-host:9200"),
new BasicHeader(HttpHeaders.Names.ACCESS_CONTROL_REQUEST_METHOD, "GET")); new BasicHeader(HttpHeaders.Names.ACCESS_CONTROL_REQUEST_METHOD, "GET"));
fail("request should have failed"); fail("request should have failed");
} catch(ElasticsearchResponseException e) { } catch(ResponseException e) {
ElasticsearchResponse response = e.getElasticsearchResponse(); Response response = e.getResponse();
// a rejected origin gets a FORBIDDEN - 403 // a rejected origin gets a FORBIDDEN - 403
assertThat(response.getStatusLine().getStatusCode(), is(403)); assertThat(response.getStatusLine().getStatusCode(), is(403));
assertThat(response.getHeader("Access-Control-Allow-Origin"), nullValue()); assertThat(response.getHeader("Access-Control-Allow-Origin"), nullValue());
@ -126,7 +126,7 @@ public class CorsRegexIT extends ESIntegTestCase {
} }
} }
protected static void assertResponseWithOriginheader(ElasticsearchResponse response, String expectedCorsHeader) { protected static void assertResponseWithOriginheader(Response response, String expectedCorsHeader) {
assertThat(response.getStatusLine().getStatusCode(), is(200)); assertThat(response.getStatusLine().getStatusCode(), is(200));
assertThat(response.getHeader("Access-Control-Allow-Origin"), is(expectedCorsHeader)); assertThat(response.getHeader("Access-Control-Allow-Origin"), is(expectedCorsHeader));
} }

View File

@ -19,7 +19,7 @@
package org.elasticsearch.rest.action.main; package org.elasticsearch.rest.action.main;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.ElasticsearchResponse; import org.elasticsearch.client.Response;
import org.elasticsearch.common.network.NetworkModule; import org.elasticsearch.common.network.NetworkModule;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.test.ESIntegTestCase; import org.elasticsearch.test.ESIntegTestCase;
@ -40,14 +40,14 @@ public class RestMainActionIT extends ESIntegTestCase {
} }
public void testHeadRequest() throws IOException { public void testHeadRequest() throws IOException {
try (ElasticsearchResponse response = getRestClient().performRequest("HEAD", "/", Collections.emptyMap(), null)) { try (Response response = getRestClient().performRequest("HEAD", "/", Collections.emptyMap(), null)) {
assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
assertNull(response.getEntity()); assertNull(response.getEntity());
} }
} }
public void testGetRequest() throws IOException { public void testGetRequest() throws IOException {
try (ElasticsearchResponse response = getRestClient().performRequest("GET", "/", Collections.emptyMap(), null)) { try (Response response = getRestClient().performRequest("GET", "/", Collections.emptyMap(), null)) {
assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
assertNotNull(response.getEntity()); assertNotNull(response.getEntity());
assertThat(EntityUtils.toString(response.getEntity()), containsString("cluster_name")); assertThat(EntityUtils.toString(response.getEntity()), containsString("cluster_name"));

View File

@ -32,7 +32,7 @@ import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.ActionFilter; import org.elasticsearch.action.support.ActionFilter;
import org.elasticsearch.action.termvectors.MultiTermVectorsRequest; import org.elasticsearch.action.termvectors.MultiTermVectorsRequest;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
import org.elasticsearch.client.ElasticsearchResponse; import org.elasticsearch.client.Response;
import org.elasticsearch.common.inject.AbstractModule; import org.elasticsearch.common.inject.AbstractModule;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.inject.Module; import org.elasticsearch.common.inject.Module;
@ -218,7 +218,7 @@ public class ContextAndHeaderTransportIT extends ESIntegTestCase {
restController.registerRelevantHeaders(relevantHeaderName); restController.registerRelevantHeaders(relevantHeaderName);
} }
try (ElasticsearchResponse response = getRestClient().performRequest( try (Response response = getRestClient().performRequest(
"GET", "/" + queryIndex + "/_search", Collections.emptyMap(), null, "GET", "/" + queryIndex + "/_search", Collections.emptyMap(), null,
new BasicHeader(randomHeaderKey, randomHeaderValue), new BasicHeader(relevantHeaderName, randomHeaderValue))) { new BasicHeader(randomHeaderKey, randomHeaderValue), new BasicHeader(relevantHeaderName, randomHeaderValue))) {
assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); assertThat(response.getStatusLine().getStatusCode(), equalTo(200));

View File

@ -22,7 +22,7 @@ package org.elasticsearch.test.rest;
import com.carrotsearch.randomizedtesting.RandomizedTest; import com.carrotsearch.randomizedtesting.RandomizedTest;
import org.apache.lucene.util.IOUtils; import org.apache.lucene.util.IOUtils;
import org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksAction; import org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksAction;
import org.elasticsearch.client.ElasticsearchResponseException; import org.elasticsearch.client.ResponseException;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.SuppressForbidden; import org.elasticsearch.common.SuppressForbidden;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
@ -276,9 +276,9 @@ public abstract class ESRestTestCase extends ESTestCase {
deleteIndicesArgs.put("index", "*"); deleteIndicesArgs.put("index", "*");
try { try {
adminExecutionContext.callApi("indices.delete", deleteIndicesArgs, Collections.emptyList(), Collections.emptyMap()); adminExecutionContext.callApi("indices.delete", deleteIndicesArgs, Collections.emptyList(), Collections.emptyMap());
} catch (ElasticsearchResponseException e) { } catch (ResponseException e) {
// 404 here just means we had no indexes // 404 here just means we had no indexes
if (e.getElasticsearchResponse().getStatusLine().getStatusCode() != 404) { if (e.getResponse().getStatusLine().getStatusCode() != 404) {
throw e; throw e;
} }
} }

View File

@ -19,7 +19,7 @@
package org.elasticsearch.test.rest; package org.elasticsearch.test.rest;
import org.elasticsearch.Version; import org.elasticsearch.Version;
import org.elasticsearch.client.ElasticsearchResponseException; import org.elasticsearch.client.ResponseException;
import org.elasticsearch.common.logging.ESLogger; import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
@ -78,7 +78,7 @@ public class RestTestExecutionContext implements Closeable {
//we always stash the last response body //we always stash the last response body
stash.stashResponse(response); stash.stashResponse(response);
return response; return response;
} catch(ElasticsearchResponseException e) { } catch(ResponseException e) {
response = new RestTestResponse(e); response = new RestTestResponse(e);
throw e; throw e;
} }

View File

@ -33,8 +33,8 @@ import org.apache.http.message.BasicHeader;
import org.apache.http.ssl.SSLContexts; import org.apache.http.ssl.SSLContexts;
import org.apache.lucene.util.IOUtils; import org.apache.lucene.util.IOUtils;
import org.elasticsearch.Version; import org.elasticsearch.Version;
import org.elasticsearch.client.ElasticsearchResponse; import org.elasticsearch.client.Response;
import org.elasticsearch.client.ElasticsearchResponseException; import org.elasticsearch.client.ResponseException;
import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClient;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.PathUtils; import org.elasticsearch.common.io.PathUtils;
@ -106,8 +106,8 @@ public class RestTestClient implements Closeable {
//we don't really use the urls here, we rely on the client doing round-robin to touch all the nodes in the cluster //we don't really use the urls here, we rely on the client doing round-robin to touch all the nodes in the cluster
String method = restApi.getMethods().get(0); String method = restApi.getMethods().get(0);
String endpoint = restApi.getPaths().get(0); String endpoint = restApi.getPaths().get(0);
ElasticsearchResponse elasticsearchResponse = restClient.performRequest(method, endpoint, Collections.emptyMap(), null); Response response = restClient.performRequest(method, endpoint, Collections.emptyMap(), null);
RestTestResponse restTestResponse = new RestTestResponse(elasticsearchResponse); RestTestResponse restTestResponse = new RestTestResponse(response);
Object latestVersion = restTestResponse.evaluate("version.number"); Object latestVersion = restTestResponse.evaluate("version.number");
if (latestVersion == null) { if (latestVersion == null) {
throw new RuntimeException("elasticsearch version not found in the response"); throw new RuntimeException("elasticsearch version not found in the response");
@ -143,7 +143,7 @@ public class RestTestClient implements Closeable {
entity = new StringEntity(body, RestClient.JSON_CONTENT_TYPE); entity = new StringEntity(body, RestClient.JSON_CONTENT_TYPE);
} }
// And everything else is a url parameter! // And everything else is a url parameter!
ElasticsearchResponse response = restClient.performRequest(method, path, queryStringParams, entity); Response response = restClient.performRequest(method, path, queryStringParams, entity);
return new RestTestResponse(response); return new RestTestResponse(response);
} }
@ -247,11 +247,11 @@ public class RestTestClient implements Closeable {
logger.debug("calling api [{}]", apiName); logger.debug("calling api [{}]", apiName);
try { try {
ElasticsearchResponse response = restClient.performRequest(requestMethod, requestPath, Response response = restClient.performRequest(requestMethod, requestPath,
queryStringParams, requestBody, requestHeaders); queryStringParams, requestBody, requestHeaders);
return new RestTestResponse(response); return new RestTestResponse(response);
} catch(ElasticsearchResponseException e) { } catch(ResponseException e) {
if (ignores.contains(e.getElasticsearchResponse().getStatusLine().getStatusCode())) { if (ignores.contains(e.getResponse().getStatusLine().getStatusCode())) {
return new RestTestResponse(e); return new RestTestResponse(e);
} }
throw e; throw e;

View File

@ -21,8 +21,8 @@ package org.elasticsearch.test.rest.client;
import org.apache.http.client.methods.HttpHead; import org.apache.http.client.methods.HttpHead;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.apache.lucene.util.IOUtils; import org.apache.lucene.util.IOUtils;
import org.elasticsearch.client.ElasticsearchResponse; import org.elasticsearch.client.Response;
import org.elasticsearch.client.ElasticsearchResponseException; import org.elasticsearch.client.ResponseException;
import org.elasticsearch.test.rest.Stash; import org.elasticsearch.test.rest.Stash;
import org.elasticsearch.test.rest.json.JsonPath; import org.elasticsearch.test.rest.json.JsonPath;
@ -35,11 +35,11 @@ import java.nio.charset.StandardCharsets;
*/ */
public class RestTestResponse { public class RestTestResponse {
private final ElasticsearchResponse response; private final Response response;
private final String body; private final String body;
private JsonPath parsedResponse; private JsonPath parsedResponse;
public RestTestResponse(ElasticsearchResponse response) { public RestTestResponse(Response response) {
this.response = response; this.response = response;
if (response.getEntity() != null) { if (response.getEntity() != null) {
try { try {
@ -55,8 +55,8 @@ public class RestTestResponse {
} }
} }
public RestTestResponse(ElasticsearchResponseException responseException) { public RestTestResponse(ResponseException responseException) {
this.response = responseException.getElasticsearchResponse(); this.response = responseException.getResponse();
this.body = responseException.getResponseBody(); this.body = responseException.getResponseBody();
} }

View File

@ -18,7 +18,7 @@
*/ */
package org.elasticsearch.test.rest.section; package org.elasticsearch.test.rest.section;
import org.elasticsearch.client.ElasticsearchResponseException; import org.elasticsearch.client.ResponseException;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.logging.ESLogger; import org.elasticsearch.common.logging.ESLogger;
@ -102,7 +102,7 @@ public class DoSection implements ExecutableSection {
} }
fail(formatStatusCodeMessage(restTestResponse, catchStatusCode)); fail(formatStatusCodeMessage(restTestResponse, catchStatusCode));
} }
} catch(ElasticsearchResponseException e) { } catch(ResponseException e) {
RestTestResponse restTestResponse = new RestTestResponse(e); RestTestResponse restTestResponse = new RestTestResponse(e);
if (!Strings.hasLength(catchParam)) { if (!Strings.hasLength(catchParam)) {
fail(formatStatusCodeMessage(restTestResponse, "2xx")); fail(formatStatusCodeMessage(restTestResponse, "2xx"));
@ -111,7 +111,7 @@ public class DoSection implements ExecutableSection {
} else if (catchParam.length() > 2 && catchParam.startsWith("/") && catchParam.endsWith("/")) { } else if (catchParam.length() > 2 && catchParam.startsWith("/") && catchParam.endsWith("/")) {
//the text of the error message matches regular expression //the text of the error message matches regular expression
assertThat(formatStatusCodeMessage(restTestResponse, "4xx|5xx"), assertThat(formatStatusCodeMessage(restTestResponse, "4xx|5xx"),
e.getElasticsearchResponse().getStatusLine().getStatusCode(), greaterThanOrEqualTo(400)); e.getResponse().getStatusLine().getStatusCode(), greaterThanOrEqualTo(400));
Object error = executionContext.response("error"); Object error = executionContext.response("error");
assertThat("error was expected in the response", error, notNullValue()); assertThat("error was expected in the response", error, notNullValue());
//remove delimiters from regex //remove delimiters from regex