Skip to content

Commit b8bf480

Browse files
authored
Clients: Switch to new performRequest (#30543)
Switch several calls in the client projects from the deprecated `performRequest` calls to the new version.
1 parent 1a71105 commit b8bf480

File tree

6 files changed

+38
-55
lines changed

6 files changed

+38
-55
lines changed

client/benchmark/src/main/java/org/elasticsearch/client/benchmark/rest/RestClientBenchmark.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,19 @@
1818
*/
1919
package org.elasticsearch.client.benchmark.rest;
2020

21-
import org.apache.http.HttpEntity;
2221
import org.apache.http.HttpHeaders;
2322
import org.apache.http.HttpHost;
2423
import org.apache.http.HttpStatus;
25-
import org.apache.http.client.config.RequestConfig;
26-
import org.apache.http.conn.ConnectionKeepAliveStrategy;
27-
import org.apache.http.entity.ContentType;
28-
import org.apache.http.entity.StringEntity;
29-
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
3024
import org.apache.http.message.BasicHeader;
31-
import org.apache.http.nio.entity.NStringEntity;
3225
import org.elasticsearch.ElasticsearchException;
26+
import org.elasticsearch.client.Request;
3327
import org.elasticsearch.client.Response;
3428
import org.elasticsearch.client.RestClient;
35-
import org.elasticsearch.client.RestClientBuilder;
3629
import org.elasticsearch.client.benchmark.AbstractBenchmark;
3730
import org.elasticsearch.client.benchmark.ops.bulk.BulkRequestExecutor;
3831
import org.elasticsearch.client.benchmark.ops.search.SearchRequestExecutor;
3932

4033
import java.io.IOException;
41-
import java.nio.charset.StandardCharsets;
4234
import java.util.Collections;
4335
import java.util.List;
4436
import java.util.Locale;
@@ -86,9 +78,10 @@ public boolean bulkIndex(List<String> bulkData) {
8678
bulkRequestBody.append(bulkItem);
8779
bulkRequestBody.append("\n");
8880
}
89-
HttpEntity entity = new NStringEntity(bulkRequestBody.toString(), ContentType.APPLICATION_JSON);
81+
Request request = new Request("POST", "/geonames/type/_noop_bulk");
82+
request.setJsonEntity(bulkRequestBody.toString());
9083
try {
91-
Response response = client.performRequest("POST", "/geonames/type/_noop_bulk", Collections.emptyMap(), entity);
84+
Response response = client.performRequest(request);
9285
return response.getStatusLine().getStatusCode() == HttpStatus.SC_OK;
9386
} catch (Exception e) {
9487
throw new ElasticsearchException(e);
@@ -107,9 +100,10 @@ private RestSearchRequestExecutor(RestClient client, String indexName) {
107100

108101
@Override
109102
public boolean search(String source) {
110-
HttpEntity searchBody = new NStringEntity(source, StandardCharsets.UTF_8);
103+
Request request = new Request("GET", endpoint);
104+
request.setJsonEntity(source);
111105
try {
112-
Response response = client.performRequest("GET", endpoint, Collections.emptyMap(), searchBody);
106+
Response response = client.performRequest(request);
113107
return response.getStatusLine().getStatusCode() == HttpStatus.SC_OK;
114108
} catch (IOException e) {
115109
throw new ElasticsearchException(e);

client/rest-high-level/src/test/java/org/elasticsearch/client/BulkProcessorIT.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,18 +194,16 @@ public void testBulkProcessorWaitOnClose() throws Exception {
194194
}
195195

196196
public void testBulkProcessorConcurrentRequestsReadOnlyIndex() throws Exception {
197-
198-
String createIndexBody = "{\n" +
197+
Request request = new Request("PUT", "/test-ro");
198+
request.setJsonEntity("{\n" +
199199
" \"settings\" : {\n" +
200200
" \"index\" : {\n" +
201201
" \"blocks.write\" : true\n" +
202202
" }\n" +
203203
" }\n" +
204204
" \n" +
205-
"}";
206-
207-
NStringEntity entity = new NStringEntity(createIndexBody, ContentType.APPLICATION_JSON);
208-
Response response = client().performRequest("PUT", "/test-ro", Collections.emptyMap(), entity);
205+
"}");
206+
Response response = client().performRequest(request);
209207
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
210208

211209
int bulkActions = randomIntBetween(10, 100);

client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919

2020
package org.elasticsearch.client;
2121

22-
import org.apache.http.client.methods.HttpPut;
23-
import org.apache.http.entity.ContentType;
24-
import org.apache.http.entity.StringEntity;
2522
import org.elasticsearch.ElasticsearchException;
2623
import org.elasticsearch.ElasticsearchStatusException;
2724
import org.elasticsearch.action.DocWriteRequest;
@@ -39,6 +36,7 @@
3936
import org.elasticsearch.action.get.MultiGetResponse;
4037
import org.elasticsearch.action.index.IndexRequest;
4138
import org.elasticsearch.action.index.IndexResponse;
39+
import org.elasticsearch.action.support.WriteRequest.RefreshPolicy;
4240
import org.elasticsearch.action.update.UpdateRequest;
4341
import org.elasticsearch.action.update.UpdateResponse;
4442
import org.elasticsearch.common.Strings;
@@ -147,11 +145,10 @@ public void testExists() throws IOException {
147145
GetRequest getRequest = new GetRequest("index", "type", "id");
148146
assertFalse(execute(getRequest, highLevelClient()::exists, highLevelClient()::existsAsync));
149147
}
150-
String document = "{\"field1\":\"value1\",\"field2\":\"value2\"}";
151-
StringEntity stringEntity = new StringEntity(document, ContentType.APPLICATION_JSON);
152-
Response response = client().performRequest(HttpPut.METHOD_NAME, "/index/type/id", Collections.singletonMap("refresh", "wait_for"),
153-
stringEntity);
154-
assertEquals(201, response.getStatusLine().getStatusCode());
148+
IndexRequest index = new IndexRequest("index", "type", "id");
149+
index.source("{\"field1\":\"value1\",\"field2\":\"value2\"}", XContentType.JSON);
150+
index.setRefreshPolicy(RefreshPolicy.IMMEDIATE);
151+
highLevelClient().index(index);
155152
{
156153
GetRequest getRequest = new GetRequest("index", "type", "id");
157154
assertTrue(execute(getRequest, highLevelClient()::exists, highLevelClient()::existsAsync));
@@ -175,12 +172,11 @@ public void testGet() throws IOException {
175172
assertEquals("Elasticsearch exception [type=index_not_found_exception, reason=no such index]", exception.getMessage());
176173
assertEquals("index", exception.getMetadata("es.index").get(0));
177174
}
178-
175+
IndexRequest index = new IndexRequest("index", "type", "id");
179176
String document = "{\"field1\":\"value1\",\"field2\":\"value2\"}";
180-
StringEntity stringEntity = new StringEntity(document, ContentType.APPLICATION_JSON);
181-
Response response = client().performRequest(HttpPut.METHOD_NAME, "/index/type/id", Collections.singletonMap("refresh", "wait_for"),
182-
stringEntity);
183-
assertEquals(201, response.getStatusLine().getStatusCode());
177+
index.source(document, XContentType.JSON);
178+
index.setRefreshPolicy(RefreshPolicy.IMMEDIATE);
179+
highLevelClient().index(index);
184180
{
185181
GetRequest getRequest = new GetRequest("index", "type", "id").version(2);
186182
ElasticsearchException exception = expectThrows(ElasticsearchException.class,
@@ -271,18 +267,15 @@ public void testMultiGet() throws IOException {
271267
assertEquals("Elasticsearch exception [type=index_not_found_exception, reason=no such index]",
272268
response.getResponses()[1].getFailure().getFailure().getMessage());
273269
}
274-
275-
String document = "{\"field\":\"value1\"}";
276-
StringEntity stringEntity = new StringEntity(document, ContentType.APPLICATION_JSON);
277-
Response r = client().performRequest(HttpPut.METHOD_NAME, "/index/type/id1", Collections.singletonMap("refresh", "true"),
278-
stringEntity);
279-
assertEquals(201, r.getStatusLine().getStatusCode());
280-
281-
document = "{\"field\":\"value2\"}";
282-
stringEntity = new StringEntity(document, ContentType.APPLICATION_JSON);
283-
r = client().performRequest(HttpPut.METHOD_NAME, "/index/type/id2", Collections.singletonMap("refresh", "true"), stringEntity);
284-
assertEquals(201, r.getStatusLine().getStatusCode());
285-
270+
BulkRequest bulk = new BulkRequest();
271+
bulk.setRefreshPolicy(RefreshPolicy.IMMEDIATE);
272+
IndexRequest index = new IndexRequest("index", "type", "id1");
273+
index.source("{\"field\":\"value1\"}", XContentType.JSON);
274+
bulk.add(index);
275+
index = new IndexRequest("index", "type", "id2");
276+
index.source("{\"field\":\"value2\"}", XContentType.JSON);
277+
bulk.add(index);
278+
highLevelClient().bulk(bulk);
286279
{
287280
MultiGetRequest multiGetRequest = new MultiGetRequest();
288281
multiGetRequest.add("index", "type", "id1");

client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/CRUDDocumentationIT.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
package org.elasticsearch.client.documentation;
2121

22-
import org.apache.http.HttpEntity;
23-
import org.apache.http.client.methods.HttpPost;
2422
import org.apache.http.entity.ContentType;
2523
import org.apache.http.nio.entity.NStringEntity;
2624
import org.elasticsearch.ElasticsearchException;
@@ -49,6 +47,7 @@
4947
import org.elasticsearch.action.update.UpdateRequest;
5048
import org.elasticsearch.action.update.UpdateResponse;
5149
import org.elasticsearch.client.ESRestHighLevelClientTestCase;
50+
import org.elasticsearch.client.Request;
5251
import org.elasticsearch.client.Response;
5352
import org.elasticsearch.client.RestHighLevelClient;
5453
import org.elasticsearch.common.Strings;
@@ -58,6 +57,7 @@
5857
import org.elasticsearch.common.xcontent.XContentBuilder;
5958
import org.elasticsearch.common.xcontent.XContentFactory;
6059
import org.elasticsearch.common.xcontent.XContentType;
60+
import org.elasticsearch.common.xcontent.json.JsonXContent;
6161
import org.elasticsearch.index.VersionType;
6262
import org.elasticsearch.index.get.GetResult;
6363
import org.elasticsearch.rest.RestStatus;
@@ -271,16 +271,15 @@ public void testUpdate() throws Exception {
271271
IndexResponse indexResponse = client.index(indexRequest);
272272
assertSame(indexResponse.status(), RestStatus.CREATED);
273273

274-
XContentType xContentType = XContentType.JSON;
275-
String script = Strings.toString(XContentBuilder.builder(xContentType.xContent())
274+
Request request = new Request("POST", "/_scripts/increment-field");
275+
request.setJsonEntity(Strings.toString(JsonXContent.contentBuilder()
276276
.startObject()
277277
.startObject("script")
278278
.field("lang", "painless")
279279
.field("code", "ctx._source.field += params.count")
280280
.endObject()
281-
.endObject());
282-
HttpEntity body = new NStringEntity(script, ContentType.create(xContentType.mediaType()));
283-
Response response = client().performRequest(HttpPost.METHOD_NAME, "/_scripts/increment-field", emptyMap(), body);
281+
.endObject()));
282+
Response response = client().performRequest(request);
284283
assertEquals(response.getStatusLine().getStatusCode(), RestStatus.OK.getStatus());
285284
}
286285
{

client/rest/src/test/java/org/elasticsearch/client/RestClientSingleHostIntegTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,12 @@ private Response bodyTest(final String method) throws IOException {
351351

352352
private Response bodyTest(final RestClient restClient, final String method) throws IOException {
353353
String requestBody = "{ \"field\": \"value\" }";
354-
StringEntity entity = new StringEntity(requestBody, ContentType.APPLICATION_JSON);
355354
int statusCode = randomStatusCode(getRandom());
355+
Request request = new Request(method, "/" + statusCode);
356+
request.setJsonEntity(requestBody);
356357
Response esResponse;
357358
try {
358-
esResponse = restClient.performRequest(method, "/" + statusCode, Collections.<String, String>emptyMap(), entity);
359+
esResponse = restClient.performRequest(request);
359360
} catch(ResponseException e) {
360361
esResponse = e.getResponse();
361362
}

client/rest/src/test/java/org/elasticsearch/client/RestClientSingleHostTests.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,9 @@
5858
import java.net.URI;
5959
import java.util.Arrays;
6060
import java.util.Collections;
61-
import java.util.HashMap;
6261
import java.util.HashSet;
6362
import java.util.Map;
6463
import java.util.Set;
65-
import java.util.TreeMap;
6664
import java.util.concurrent.ExecutorService;
6765
import java.util.concurrent.Executors;
6866
import java.util.concurrent.Future;

0 commit comments

Comments
 (0)