REST high-level client: move to POST when calling API to retrieve which support request body (#28342)

It has been pointed out that GET with body may cause problems to some proxies. We are then switching to POST the API that retrieve info and support a request body.

Closes #28326
This commit is contained in:
Luca Cavanna 2018-01-25 14:34:56 +01:00 committed by GitHub
parent 9db23e48cd
commit 45e1fe8bfc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 22 deletions

View File

@ -358,7 +358,7 @@ public final class Request {
parameters.withRealtime(multiGetRequest.realtime());
parameters.withRefresh(multiGetRequest.refresh());
HttpEntity entity = createEntity(multiGetRequest, REQUEST_BODY_CONTENT_TYPE);
return new Request(HttpGet.METHOD_NAME, "/_mget", parameters.getParams(), entity);
return new Request(HttpPost.METHOD_NAME, "/_mget", parameters.getParams(), entity);
}
static Request index(IndexRequest indexRequest) {
@ -447,17 +447,17 @@ public final class Request {
if (searchRequest.source() != null) {
entity = createEntity(searchRequest.source(), REQUEST_BODY_CONTENT_TYPE);
}
return new Request(HttpGet.METHOD_NAME, endpoint, params.getParams(), entity);
return new Request(HttpPost.METHOD_NAME, endpoint, params.getParams(), entity);
}
static Request searchScroll(SearchScrollRequest searchScrollRequest) throws IOException {
HttpEntity entity = createEntity(searchScrollRequest, REQUEST_BODY_CONTENT_TYPE);
return new Request("GET", "/_search/scroll", Collections.emptyMap(), entity);
return new Request(HttpPost.METHOD_NAME, "/_search/scroll", Collections.emptyMap(), entity);
}
static Request clearScroll(ClearScrollRequest clearScrollRequest) throws IOException {
HttpEntity entity = createEntity(clearScrollRequest, REQUEST_BODY_CONTENT_TYPE);
return new Request("DELETE", "/_search/scroll", Collections.emptyMap(), entity);
return new Request(HttpDelete.METHOD_NAME, "/_search/scroll", Collections.emptyMap(), entity);
}
static Request multiSearch(MultiSearchRequest multiSearchRequest) throws IOException {
@ -469,7 +469,7 @@ public final class Request {
XContent xContent = REQUEST_BODY_CONTENT_TYPE.xContent();
byte[] source = MultiSearchRequest.writeMultiLineFormat(multiSearchRequest, xContent);
HttpEntity entity = new ByteArrayEntity(source, createContentType(xContent.type()));
return new Request("GET", "/_msearch", params.getParams(), entity);
return new Request(HttpPost.METHOD_NAME, "/_msearch", params.getParams(), entity);
}
private static HttpEntity createEntity(ToXContent toXContent, XContentType xContentType) throws IOException {

View File

@ -20,6 +20,11 @@
package org.elasticsearch.client;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
@ -138,7 +143,7 @@ public class RequestTests extends ESTestCase {
assertEquals("/", request.getEndpoint());
assertEquals(0, request.getParameters().size());
assertNull(request.getEntity());
assertEquals("HEAD", request.getMethod());
assertEquals(HttpHead.METHOD_NAME, request.getMethod());
}
public void testInfo() {
@ -146,11 +151,11 @@ public class RequestTests extends ESTestCase {
assertEquals("/", request.getEndpoint());
assertEquals(0, request.getParameters().size());
assertNull(request.getEntity());
assertEquals("GET", request.getMethod());
assertEquals(HttpGet.METHOD_NAME, request.getMethod());
}
public void testGet() {
getAndExistsTest(Request::get, "GET");
getAndExistsTest(Request::get, HttpGet.METHOD_NAME);
}
public void testMultiGet() throws IOException {
@ -200,7 +205,7 @@ public class RequestTests extends ESTestCase {
}
Request request = Request.multiGet(multiGetRequest);
assertEquals("GET", request.getMethod());
assertEquals(HttpPost.METHOD_NAME, request.getMethod());
assertEquals("/_mget", request.getEndpoint());
assertEquals(expectedParams, request.getParameters());
assertToXContentBody(multiGetRequest, request.getEntity());
@ -240,7 +245,7 @@ public class RequestTests extends ESTestCase {
}
public void testExists() {
getAndExistsTest(Request::exists, "HEAD");
getAndExistsTest(Request::exists, HttpHead.METHOD_NAME);
}
private static void getAndExistsTest(Function<GetRequest, Request> requestConverter, String method) {
@ -317,7 +322,7 @@ public class RequestTests extends ESTestCase {
Request request = Request.createIndex(createIndexRequest);
assertEquals("/" + indexName, request.getEndpoint());
assertEquals(expectedParams, request.getParameters());
assertEquals("PUT", request.getMethod());
assertEquals(HttpPut.METHOD_NAME, request.getMethod());
assertToXContentBody(createIndexRequest, request.getEntity());
}
@ -365,7 +370,7 @@ public class RequestTests extends ESTestCase {
assertEquals(endpoint.toString(), request.getEndpoint());
assertEquals(expectedParams, request.getParameters());
assertEquals("PUT", request.getMethod());
assertEquals(HttpPut.METHOD_NAME, request.getMethod());
assertToXContentBody(putMappingRequest, request.getEntity());
}
@ -382,7 +387,7 @@ public class RequestTests extends ESTestCase {
Request request = Request.deleteIndex(deleteIndexRequest);
assertEquals("/" + String.join(",", indices), request.getEndpoint());
assertEquals(expectedParams, request.getParameters());
assertEquals("DELETE", request.getMethod());
assertEquals(HttpDelete.METHOD_NAME, request.getMethod());
assertNull(request.getEntity());
}
@ -401,7 +406,7 @@ public class RequestTests extends ESTestCase {
StringJoiner endpoint = new StringJoiner("/", "/", "").add(String.join(",", indices)).add("_open");
assertThat(endpoint.toString(), equalTo(request.getEndpoint()));
assertThat(expectedParams, equalTo(request.getParameters()));
assertThat(request.getMethod(), equalTo("POST"));
assertThat(request.getMethod(), equalTo(HttpPost.METHOD_NAME));
assertThat(request.getEntity(), nullValue());
}
@ -418,7 +423,7 @@ public class RequestTests extends ESTestCase {
StringJoiner endpoint = new StringJoiner("/", "/", "").add(String.join(",", indices)).add("_close");
assertThat(endpoint.toString(), equalTo(request.getEndpoint()));
assertThat(expectedParams, equalTo(request.getParameters()));
assertThat(request.getMethod(), equalTo("POST"));
assertThat(request.getMethod(), equalTo(HttpPost.METHOD_NAME));
assertThat(request.getEntity(), nullValue());
}
@ -432,9 +437,9 @@ public class RequestTests extends ESTestCase {
Map<String, String> expectedParams = new HashMap<>();
String method = "POST";
String method = HttpPost.METHOD_NAME;
if (id != null) {
method = "PUT";
method = HttpPut.METHOD_NAME;
if (randomBoolean()) {
indexRequest.opType(DocWriteRequest.OpType.CREATE);
}
@ -569,7 +574,7 @@ public class RequestTests extends ESTestCase {
Request request = Request.update(updateRequest);
assertEquals("/" + index + "/" + type + "/" + id + "/_update", request.getEndpoint());
assertEquals(expectedParams, request.getParameters());
assertEquals("POST", request.getMethod());
assertEquals(HttpPost.METHOD_NAME, request.getMethod());
HttpEntity entity = request.getEntity();
assertTrue(entity instanceof ByteArrayEntity);
@ -683,7 +688,7 @@ public class RequestTests extends ESTestCase {
Request request = Request.bulk(bulkRequest);
assertEquals("/_bulk", request.getEndpoint());
assertEquals(expectedParams, request.getParameters());
assertEquals("POST", request.getMethod());
assertEquals(HttpPost.METHOD_NAME, request.getMethod());
assertEquals(xContentType.mediaTypeWithoutParameters(), request.getEntity().getContentType().getValue());
byte[] content = new byte[(int) request.getEntity().getContentLength()];
try (InputStream inputStream = request.getEntity().getContent()) {
@ -894,6 +899,7 @@ public class RequestTests extends ESTestCase {
endpoint.add(type);
}
endpoint.add("_search");
assertEquals(HttpPost.METHOD_NAME, request.getMethod());
assertEquals(endpoint.toString(), request.getEndpoint());
assertEquals(expectedParams, request.getParameters());
assertToXContentBody(searchSourceBuilder, request.getEntity());
@ -932,6 +938,7 @@ public class RequestTests extends ESTestCase {
Request request = Request.multiSearch(multiSearchRequest);
assertEquals("/_msearch", request.getEndpoint());
assertEquals(HttpPost.METHOD_NAME, request.getMethod());
assertEquals(expectedParams, request.getParameters());
List<SearchRequest> requests = new ArrayList<>();
@ -955,7 +962,7 @@ public class RequestTests extends ESTestCase {
searchScrollRequest.scroll(randomPositiveTimeValue());
}
Request request = Request.searchScroll(searchScrollRequest);
assertEquals("GET", request.getMethod());
assertEquals(HttpPost.METHOD_NAME, request.getMethod());
assertEquals("/_search/scroll", request.getEndpoint());
assertEquals(0, request.getParameters().size());
assertToXContentBody(searchScrollRequest, request.getEntity());
@ -969,7 +976,7 @@ public class RequestTests extends ESTestCase {
clearScrollRequest.addScrollId(randomAlphaOfLengthBetween(5, 10));
}
Request request = Request.clearScroll(clearScrollRequest);
assertEquals("DELETE", request.getMethod());
assertEquals(HttpDelete.METHOD_NAME, request.getMethod());
assertEquals("/_search/scroll", request.getEndpoint());
assertEquals(0, request.getParameters().size());
assertToXContentBody(clearScrollRequest, request.getEntity());

View File

@ -179,7 +179,7 @@ public class RestHighLevelClientTests extends ESTestCase {
assertEquals(5, searchResponse.getTotalShards());
assertEquals(5, searchResponse.getSuccessfulShards());
assertEquals(100, searchResponse.getTook().getMillis());
verify(restClient).performRequest(eq("GET"), eq("/_search/scroll"), eq(Collections.emptyMap()),
verify(restClient).performRequest(eq("POST"), eq("/_search/scroll"), eq(Collections.emptyMap()),
isNotNull(HttpEntity.class), argThat(new HeadersVarargMatcher(headers)));
}