Skip to content

Commit e974d26

Browse files
authored
Update http client package to resolve build failure (#168)
Signed-off-by: Heemin Kim <heemin@amazon.com>
1 parent 8269730 commit e974d26

File tree

8 files changed

+72
-56
lines changed

8 files changed

+72
-56
lines changed

src/test/java/org/opensearch/geospatial/GeospatialRestTestCase.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import java.util.Optional;
2525
import java.util.stream.IntStream;
2626

27-
import org.apache.http.util.EntityUtils;
27+
import org.apache.hc.core5.http.io.entity.EntityUtils;
2828
import org.hamcrest.MatcherAssert;
2929
import org.hamcrest.Matchers;
3030
import org.json.JSONArray;
@@ -142,7 +142,7 @@ protected Map<String, Object> buildGeoJSONFeatureProcessorConfig(Map<String, Str
142142
return featureProcessor;
143143
}
144144

145-
public Map<String, Object> getDocument(String docID, String indexName) throws IOException {
145+
public Map<String, Object> getDocument(String docID, String indexName) throws Exception {
146146
String path = String.join(URL_DELIMITER, indexName, DOC, docID);
147147
final Request request = new Request("GET", path);
148148
final Response response = client().performRequest(request);
@@ -187,7 +187,7 @@ protected void assertIndexNotExists(String indexName) throws IOException {
187187
/*
188188
Get index mapping as map
189189
*/
190-
protected Map<String, Object> getIndexMapping(String index) throws IOException {
190+
protected Map<String, Object> getIndexMapping(String index) throws Exception {
191191
String indexMappingURL = String.join(URL_DELIMITER, index, MAPPING);
192192
Request request = new Request("GET", indexMappingURL);
193193
Response response = client().performRequest(request);
@@ -201,13 +201,13 @@ protected Map<String, Object> getIndexMapping(String index) throws IOException {
201201
/*
202202
Get index mapping's properties as map
203203
*/
204-
protected Map<String, Object> getIndexProperties(String index) throws IOException {
204+
protected Map<String, Object> getIndexProperties(String index) throws Exception {
205205
final Map<String, Object> indexMapping = getIndexMapping(index);
206206
MatcherAssert.assertThat("No properties found for index: " + index, indexMapping, Matchers.hasKey(MAPPING_PROPERTIES_KEY));
207207
return (Map<String, Object>) indexMapping.get(MAPPING_PROPERTIES_KEY);
208208
}
209209

210-
protected int getIndexDocumentCount(String index) throws IOException {
210+
protected int getIndexDocumentCount(String index) throws Exception {
211211
String indexDocumentCountPath = String.join(URL_DELIMITER, index, COUNT);
212212
Request request = new Request("GET", indexDocumentCountPath);
213213
Response response = client().performRequest(request);
@@ -264,7 +264,7 @@ public String buildSearchBodyAsString(
264264
});
265265
}
266266

267-
public SearchResponse searchIndex(String indexName, String entity) throws IOException {
267+
public SearchResponse searchIndex(String indexName, String entity) throws Exception {
268268
String path = String.join(URL_DELIMITER, indexName, SEARCH);
269269
final Request request = new Request("GET", path);
270270
request.setJsonEntity(entity);
@@ -298,7 +298,7 @@ public String indexDocumentUsingGeoJSON(String indexName, String fieldName, Geom
298298
}
299299

300300
public SearchResponse searchUsingShapeRelation(String indexName, String fieldName, Geometry geometry, ShapeRelation shapeRelation)
301-
throws IOException {
301+
throws Exception {
302302
String searchEntity = buildSearchBodyAsString(builder -> {
303303
builder.field(DEFAULT_SHAPE_FIELD_NAME);
304304
GeoJson.toXContent(geometry, builder, EMPTY_PARAMS);
@@ -320,7 +320,7 @@ public SearchResponse searchUsingIndexedShapeIndex(
320320
String indexedShapePath,
321321
String docId,
322322
String fieldName
323-
) throws IOException {
323+
) throws Exception {
324324
String searchEntity = buildSearchBodyAsString(builder -> {
325325
builder.startObject(INDEXED_SHAPE_FIELD);
326326
builder.field(SHAPE_INDEX_FIELD, indexedShapeIndex);

src/test/java/org/opensearch/geospatial/OpenSearchSecureRestTestCase.java

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,29 @@
88

99
package org.opensearch.geospatial;
1010

11+
import static org.opensearch.client.RestClientBuilder.DEFAULT_MAX_CONN_PER_ROUTE;
12+
import static org.opensearch.client.RestClientBuilder.DEFAULT_MAX_CONN_TOTAL;
13+
1114
import java.io.IOException;
12-
import java.util.*;
15+
import java.util.Collections;
16+
import java.util.List;
17+
import java.util.Map;
18+
import java.util.Optional;
1319
import java.util.stream.Collectors;
1420

15-
import org.apache.http.Header;
16-
import org.apache.http.HttpHost;
17-
import org.apache.http.auth.AuthScope;
18-
import org.apache.http.auth.UsernamePasswordCredentials;
19-
import org.apache.http.client.CredentialsProvider;
20-
import org.apache.http.conn.ssl.NoopHostnameVerifier;
21-
import org.apache.http.impl.client.BasicCredentialsProvider;
22-
import org.apache.http.message.BasicHeader;
23-
import org.apache.http.ssl.SSLContextBuilder;
21+
import org.apache.hc.client5.http.auth.AuthScope;
22+
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
23+
import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
24+
import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager;
25+
import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder;
26+
import org.apache.hc.client5.http.ssl.ClientTlsStrategyBuilder;
27+
import org.apache.hc.client5.http.ssl.NoopHostnameVerifier;
28+
import org.apache.hc.core5.http.Header;
29+
import org.apache.hc.core5.http.HttpHost;
30+
import org.apache.hc.core5.http.message.BasicHeader;
31+
import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
32+
import org.apache.hc.core5.ssl.SSLContextBuilder;
33+
import org.apache.hc.core5.util.Timeout;
2434
import org.junit.After;
2535
import org.opensearch.client.Request;
2636
import org.opensearch.client.Response;
@@ -95,13 +105,20 @@ private void configureHttpsClient(RestClientBuilder builder, Settings settings)
95105
.orElseThrow(() -> new RuntimeException("user name is missing"));
96106
String password = Optional.ofNullable(System.getProperty(SYS_PROPERTY_KEY_PASSWORD))
97107
.orElseThrow(() -> new RuntimeException("password is missing"));
98-
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
99-
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));
108+
BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
109+
final AuthScope anyScope = new AuthScope(null, -1);
110+
credentialsProvider.setCredentials(anyScope, new UsernamePasswordCredentials(userName, password.toCharArray()));
100111
try {
101-
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)
102-
// disable the certificate since our testing cluster just uses the default security configuration
103-
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
104-
.setSSLContext(SSLContextBuilder.create().loadTrustMaterial(null, (chains, authType) -> true).build());
112+
final TlsStrategy tlsStrategy = ClientTlsStrategyBuilder.create()
113+
.setHostnameVerifier(NoopHostnameVerifier.INSTANCE)
114+
.setSslContext(SSLContextBuilder.create().loadTrustMaterial(null, (chains, authType) -> true).build())
115+
.build();
116+
final PoolingAsyncClientConnectionManager connectionManager = PoolingAsyncClientConnectionManagerBuilder.create()
117+
.setMaxConnPerRoute(DEFAULT_MAX_CONN_PER_ROUTE)
118+
.setMaxConnTotal(DEFAULT_MAX_CONN_TOTAL)
119+
.setTlsStrategy(tlsStrategy)
120+
.build();
121+
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider).setConnectionManager(connectionManager);
105122
} catch (Exception e) {
106123
throw new RuntimeException(e);
107124
}
@@ -112,7 +129,12 @@ private void configureHttpsClient(RestClientBuilder builder, Settings settings)
112129
socketTimeoutString == null ? DEFAULT_SOCKET_TIMEOUT : socketTimeoutString,
113130
CLIENT_SOCKET_TIMEOUT
114131
);
115-
builder.setRequestConfigCallback(conf -> conf.setSocketTimeout(Math.toIntExact(socketTimeout.getMillis())));
132+
builder.setRequestConfigCallback(conf -> {
133+
Timeout timeout = Timeout.ofMilliseconds(Math.toIntExact(socketTimeout.getMillis()));
134+
conf.setConnectTimeout(timeout);
135+
conf.setResponseTimeout(timeout);
136+
return conf;
137+
});
116138
if (settings.hasValue(CLIENT_PATH_PREFIX)) {
117139
builder.setPathPrefix(settings.get(CLIENT_PATH_PREFIX));
118140
}
@@ -129,7 +151,7 @@ protected boolean preserveIndicesUponCompletion() {
129151
@After
130152
public void deleteExternalIndices() throws IOException {
131153
Response response = client().performRequest(new Request("GET", "/_cat/indices?format=json&expand_wildcards=all"));
132-
XContentType xContentType = XContentType.fromMediaType(response.getEntity().getContentType().getValue());
154+
XContentType xContentType = XContentType.fromMediaType(response.getEntity().getContentType());
133155
try (
134156
XContentParser parser = xContentType.xContent()
135157
.createParser(

src/test/java/org/opensearch/geospatial/index/mapper/xypoint/XYPointFieldMapperIT.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
package org.opensearch.geospatial.index.mapper.xypoint;
77

8-
import java.io.IOException;
98
import java.util.List;
109
import java.util.Locale;
1110
import java.util.Map;
@@ -21,7 +20,7 @@ public class XYPointFieldMapperIT extends GeospatialRestTestCase {
2120
private static final String FIELD_X_KEY = "x";
2221
private static final String FIELD_Y_KEY = "y";
2322

24-
public void testMappingWithXYPointField() throws IOException {
23+
public void testMappingWithXYPointField() throws Exception {
2524
String indexName = GeospatialTestHelper.randomLowerCaseString();
2625
String fieldName = GeospatialTestHelper.randomLowerCaseString();
2726
createIndex(indexName, Settings.EMPTY, Map.of(fieldName, XYPointFieldMapper.CONTENT_TYPE));
@@ -32,7 +31,7 @@ public void testMappingWithXYPointField() throws IOException {
3231
deleteIndex(indexName);
3332
}
3433

35-
public void testIndexWithXYPointFieldAsWKTFormat() throws IOException {
34+
public void testIndexWithXYPointFieldAsWKTFormat() throws Exception {
3635
String indexName = GeospatialTestHelper.randomLowerCaseString();
3736
String fieldName = GeospatialTestHelper.randomLowerCaseString();
3837
createIndex(indexName, Settings.EMPTY, Map.of(fieldName, XYPointFieldMapper.CONTENT_TYPE));
@@ -45,7 +44,7 @@ public void testIndexWithXYPointFieldAsWKTFormat() throws IOException {
4544
deleteIndex(indexName);
4645
}
4746

48-
public void testIndexWithXYPointFieldAsArrayFormat() throws IOException {
47+
public void testIndexWithXYPointFieldAsArrayFormat() throws Exception {
4948
String indexName = GeospatialTestHelper.randomLowerCaseString();
5049
String fieldName = GeospatialTestHelper.randomLowerCaseString();
5150
createIndex(indexName, Settings.EMPTY, Map.of(fieldName, XYPointFieldMapper.CONTENT_TYPE));
@@ -58,7 +57,7 @@ public void testIndexWithXYPointFieldAsArrayFormat() throws IOException {
5857
deleteIndex(indexName);
5958
}
6059

61-
public void testIndexWithXYPointFieldAsStringFormat() throws IOException {
60+
public void testIndexWithXYPointFieldAsStringFormat() throws Exception {
6261
String indexName = GeospatialTestHelper.randomLowerCaseString();
6362
String fieldName = GeospatialTestHelper.randomLowerCaseString();
6463
createIndex(indexName, Settings.EMPTY, Map.of(fieldName, XYPointFieldMapper.CONTENT_TYPE));
@@ -72,7 +71,7 @@ public void testIndexWithXYPointFieldAsStringFormat() throws IOException {
7271
deleteIndex(indexName);
7372
}
7473

75-
public void testIndexWithXYPointFieldAsObjectFormat() throws IOException {
74+
public void testIndexWithXYPointFieldAsObjectFormat() throws Exception {
7675
String indexName = GeospatialTestHelper.randomLowerCaseString();
7776
String fieldName = GeospatialTestHelper.randomLowerCaseString();
7877
createIndex(indexName, Settings.EMPTY, Map.of(fieldName, XYPointFieldMapper.CONTENT_TYPE));
@@ -86,19 +85,19 @@ public void testIndexWithXYPointFieldAsObjectFormat() throws IOException {
8685
deleteIndex(indexName);
8786
}
8887

89-
private String getDocumentWithWKTValueForXYPoint(String fieldName, Geometry geometry) throws IOException {
88+
private String getDocumentWithWKTValueForXYPoint(String fieldName, Geometry geometry) throws Exception {
9089
return buildContentAsString(build -> build.field(fieldName, geometry.toString()));
9190
}
9291

93-
private String getDocumentWithArrayValueForXYPoint(String fieldName, Point point) throws IOException {
92+
private String getDocumentWithArrayValueForXYPoint(String fieldName, Point point) throws Exception {
9493
return buildContentAsString(build -> build.field(fieldName, new double[] { point.getY(), point.getX() }));
9594
}
9695

97-
private String getDocumentWithStringValueForXYPoint(String fieldName, String pointAsString) throws IOException {
96+
private String getDocumentWithStringValueForXYPoint(String fieldName, String pointAsString) throws Exception {
9897
return buildContentAsString(build -> build.field(fieldName, pointAsString));
9998
}
10099

101-
private String getDocumentWithObjectValueForXYPoint(String fieldName, Point point) throws IOException {
100+
private String getDocumentWithObjectValueForXYPoint(String fieldName, Point point) throws Exception {
102101
return buildContentAsString(build -> {
103102
build.startObject(fieldName);
104103
build.field(FIELD_X_KEY, point.getX());

src/test/java/org/opensearch/geospatial/index/mapper/xyshape/XYShapeFieldMapperIT.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ private String getDocumentWithWKTValueForXYShape(String fieldName, Geometry geom
3333
return buildContentAsString(build -> build.field(fieldName, geometry.toString()));
3434
}
3535

36-
public void testMappingWithXYShapeField() throws IOException {
36+
public void testMappingWithXYShapeField() throws Exception {
3737
String indexName = GeospatialTestHelper.randomLowerCaseString();
3838
String fieldName = GeospatialTestHelper.randomLowerCaseString();
3939
createIndex(indexName, Settings.EMPTY, Map.of(fieldName, XYShapeFieldMapper.CONTENT_TYPE));
@@ -44,7 +44,7 @@ public void testMappingWithXYShapeField() throws IOException {
4444
deleteIndex(indexName);
4545
}
4646

47-
public void testIndexWithXYShapeFieldAsWKTFormat() throws IOException {
47+
public void testIndexWithXYShapeFieldAsWKTFormat() throws Exception {
4848
String indexName = GeospatialTestHelper.randomLowerCaseString();
4949
String fieldName = GeospatialTestHelper.randomLowerCaseString();
5050
createIndex(indexName, Settings.EMPTY, Map.of(fieldName, XYShapeFieldMapper.CONTENT_TYPE));
@@ -57,7 +57,7 @@ public void testIndexWithXYShapeFieldAsWKTFormat() throws IOException {
5757
deleteIndex(indexName);
5858
}
5959

60-
public void testIndexWithXYShapeFieldAsGeoJSONFormat() throws IOException {
60+
public void testIndexWithXYShapeFieldAsGeoJSONFormat() throws Exception {
6161
String indexName = GeospatialTestHelper.randomLowerCaseString();
6262
String fieldName = GeospatialTestHelper.randomLowerCaseString();
6363
createIndex(indexName, Settings.EMPTY, Map.of(fieldName, XYShapeFieldMapper.CONTENT_TYPE));

src/test/java/org/opensearch/geospatial/plugin/GeospatialPluginIT.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55

66
package org.opensearch.geospatial.plugin;
77

8-
import java.io.IOException;
9-
10-
import org.apache.http.util.EntityUtils;
8+
import org.apache.hc.core5.http.io.entity.EntityUtils;
119
import org.opensearch.client.Request;
1210
import org.opensearch.client.Response;
1311
import org.opensearch.geospatial.GeospatialRestTestCase;
@@ -18,7 +16,7 @@ public class GeospatialPluginIT extends GeospatialRestTestCase {
1816
/**
1917
* Tests whether plugin is installed or not
2018
*/
21-
public void testPluginInstalled() throws IOException {
19+
public void testPluginInstalled() throws Exception {
2220
String restURI = String.join("/", "_cat", "plugins");
2321

2422
Request request = new Request("GET", restURI);

src/test/java/org/opensearch/geospatial/processor/FeatureProcessorIT.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@
1010
import static org.opensearch.geospatial.GeospatialObjectBuilder.randomGeoJSONFeature;
1111
import static org.opensearch.geospatial.GeospatialTestHelper.randomLowerCaseString;
1212

13-
import java.io.IOException;
1413
import java.util.ArrayList;
1514
import java.util.HashMap;
1615
import java.util.List;
1716
import java.util.Map;
1817
import java.util.Optional;
1918

20-
import org.apache.http.util.EntityUtils;
19+
import org.apache.hc.core5.http.io.entity.EntityUtils;
2120
import org.json.JSONObject;
2221
import org.opensearch.client.Request;
2322
import org.opensearch.client.Response;
@@ -27,7 +26,7 @@
2726

2827
public class FeatureProcessorIT extends GeospatialRestTestCase {
2928

30-
public void testProcessorAvailable() throws IOException {
29+
public void testProcessorAvailable() throws Exception {
3130
String nodeIngestURL = String.join("/", "_nodes", "ingest");
3231
String endpoint = nodeIngestURL + "?filter_path=nodes.*.ingest.processors&pretty";
3332
Request request = new Request("GET", endpoint);
@@ -39,7 +38,7 @@ public void testProcessorAvailable() throws IOException {
3938
assertTrue(responseBody.contains(FeatureProcessor.TYPE));
4039
}
4140

42-
public void testIndexGeoJSONSuccess() throws IOException {
41+
public void testIndexGeoJSONSuccess() throws Exception {
4342

4443
String indexName = randomLowerCaseString();
4544
String geoShapeField = randomLowerCaseString();

src/test/java/org/opensearch/geospatial/rest/action/upload/geojson/RestUploadGeoJSONActionIT.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class RestUploadGeoJSONActionIT extends GeospatialRestTestCase {
2929

3030
public static final int NUMBER_OF_FEATURES_TO_ADD = 3;
3131

32-
public void testGeoJSONUploadSuccessPostMethod() throws IOException {
32+
public void testGeoJSONUploadSuccessPostMethod() throws Exception {
3333

3434
final String index = randomLowerCaseStringWithSuffix(ACCEPTED_INDEX_SUFFIX_PATH);
3535
assertIndexNotExists(index);
@@ -55,7 +55,7 @@ public void testGeoJSONUploadFailIndexExists() throws IOException {
5555
assertTrue("Not an expected exception", responseException.getMessage().contains("resource_already_exists_exception"));
5656
}
5757

58-
public void testGeoJSONUploadSuccessPutMethod() throws IOException {
58+
public void testGeoJSONUploadSuccessPutMethod() throws Exception {
5959

6060
String index = randomLowerCaseStringWithSuffix(ACCEPTED_INDEX_SUFFIX_PATH);
6161
Response response = uploadGeoJSONFeaturesIntoExistingIndex(NUMBER_OF_FEATURES_TO_ADD, index, null);
@@ -64,7 +64,7 @@ public void testGeoJSONUploadSuccessPutMethod() throws IOException {
6464
assertEquals("failed to index documents", NUMBER_OF_FEATURES_TO_ADD, getIndexDocumentCount(index));
6565
}
6666

67-
public void testGeoJSONPutMethodUploadIndexExists() throws IOException {
67+
public void testGeoJSONPutMethodUploadIndexExists() throws Exception {
6868

6969
String index = randomLowerCaseStringWithSuffix(ACCEPTED_INDEX_SUFFIX_PATH);
7070
String geoFieldName = randomLowerCaseString();

src/test/java/org/opensearch/geospatial/stats/upload/RestUploadStatsActionIT.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
import static org.opensearch.geospatial.stats.upload.RestUploadStatsAction.ACTION_OBJECT;
1010
import static org.opensearch.geospatial.stats.upload.RestUploadStatsAction.ACTION_STATS;
1111

12-
import java.io.IOException;
13-
14-
import org.apache.http.util.EntityUtils;
12+
import org.apache.hc.core5.http.io.entity.EntityUtils;
1513
import org.opensearch.client.Request;
1614
import org.opensearch.client.Response;
1715
import org.opensearch.geospatial.GeospatialRestTestCase;
@@ -25,20 +23,20 @@ private String getUploadStatsPath() {
2523
return String.join(URL_DELIMITER, getPluginURLPrefix(), ACTION_OBJECT, ACTION_STATS);
2624
}
2725

28-
private String getStatsResponseAsString() throws IOException {
26+
private String getStatsResponseAsString() throws Exception {
2927
Request statsRequest = new Request("GET", getUploadStatsPath());
3028
Response statsResponse = client().performRequest(statsRequest);
3129
return EntityUtils.toString(statsResponse.getEntity());
3230
}
3331

34-
public void testStatsAPISuccess() throws IOException {
32+
public void testStatsAPISuccess() throws Exception {
3533

3634
Request request = new Request("GET", getUploadStatsPath());
3735
Response response = client().performRequest(request);
3836
assertEquals("Failed to retrieve stats", RestStatus.OK, RestStatus.fromCode(response.getStatusLine().getStatusCode()));
3937
}
4038

41-
public void testStatsAreUpdatedAfterUpload() throws IOException {
39+
public void testStatsAreUpdatedAfterUpload() throws Exception {
4240
// get current stats response
4341
final String currentUploadStats = getStatsResponseAsString();
4442
assertNotNull(currentUploadStats);

0 commit comments

Comments
 (0)