From 3f065c784076034913b988d3a1146e11ee27d113 Mon Sep 17 00:00:00 2001 From: Luca Cavanna Date: Thu, 25 Jan 2018 14:34:56 +0100 Subject: [PATCH] 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 --- .../org/elasticsearch/client/Request.java | 6 ++--- .../elasticsearch/client/RequestTests.java | 26 ++++++++++++------- .../client/RestHighLevelClientTests.java | 2 +- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/Request.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/Request.java index 7a95553c3c003..0a035882ad8b4 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/Request.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/Request.java @@ -356,17 +356,17 @@ static Request search(SearchRequest searchRequest) throws IOException { 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); } private static HttpEntity createEntity(ToXContent toXContent, XContentType xContentType) throws IOException { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestTests.java index b602b33a2617d..4c457fb97480c 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestTests.java @@ -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; @@ -116,7 +121,7 @@ public void testPing() { assertEquals("/", request.getEndpoint()); assertEquals(0, request.getParameters().size()); assertNull(request.getEntity()); - assertEquals("HEAD", request.getMethod()); + assertEquals(HttpHead.METHOD_NAME, request.getMethod()); } public void testInfo() { @@ -124,11 +129,11 @@ public void testInfo() { 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 testDelete() throws IOException { @@ -165,7 +170,7 @@ public void testDelete() throws IOException { } public void testExists() { - getAndExistsTest(Request::exists, "HEAD"); + getAndExistsTest(Request::exists, HttpHead.METHOD_NAME); } private static void getAndExistsTest(Function requestConverter, String method) { @@ -250,9 +255,9 @@ public void testIndex() throws IOException { Map 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); } @@ -403,7 +408,7 @@ public void testUpdate() throws IOException { 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); @@ -521,7 +526,7 @@ public void testBulk() throws IOException { 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()) { @@ -749,6 +754,7 @@ public void testSearch() throws Exception { 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()); @@ -761,7 +767,7 @@ public void testSearchScroll() throws IOException { 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()); @@ -775,7 +781,7 @@ public void testClearScroll() throws IOException { 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()); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientTests.java index a6d015afca713..a2e47d2fe9f41 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientTests.java @@ -164,7 +164,7 @@ public void testSearchScroll() throws IOException { 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))); }