Skip to content

Commit

Permalink
Update http client package to resolve build failure (#168)
Browse files Browse the repository at this point in the history
Signed-off-by: Heemin Kim <heemin@amazon.com>
  • Loading branch information
heemin32 authored Oct 13, 2022
1 parent 8269730 commit e974d26
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import java.util.Optional;
import java.util.stream.IntStream;

import org.apache.http.util.EntityUtils;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.json.JSONArray;
Expand Down Expand Up @@ -142,7 +142,7 @@ protected Map<String, Object> buildGeoJSONFeatureProcessorConfig(Map<String, Str
return featureProcessor;
}

public Map<String, Object> getDocument(String docID, String indexName) throws IOException {
public Map<String, Object> getDocument(String docID, String indexName) throws Exception {
String path = String.join(URL_DELIMITER, indexName, DOC, docID);
final Request request = new Request("GET", path);
final Response response = client().performRequest(request);
Expand Down Expand Up @@ -187,7 +187,7 @@ protected void assertIndexNotExists(String indexName) throws IOException {
/*
Get index mapping as map
*/
protected Map<String, Object> getIndexMapping(String index) throws IOException {
protected Map<String, Object> getIndexMapping(String index) throws Exception {
String indexMappingURL = String.join(URL_DELIMITER, index, MAPPING);
Request request = new Request("GET", indexMappingURL);
Response response = client().performRequest(request);
Expand All @@ -201,13 +201,13 @@ protected Map<String, Object> getIndexMapping(String index) throws IOException {
/*
Get index mapping's properties as map
*/
protected Map<String, Object> getIndexProperties(String index) throws IOException {
protected Map<String, Object> getIndexProperties(String index) throws Exception {
final Map<String, Object> indexMapping = getIndexMapping(index);
MatcherAssert.assertThat("No properties found for index: " + index, indexMapping, Matchers.hasKey(MAPPING_PROPERTIES_KEY));
return (Map<String, Object>) indexMapping.get(MAPPING_PROPERTIES_KEY);
}

protected int getIndexDocumentCount(String index) throws IOException {
protected int getIndexDocumentCount(String index) throws Exception {
String indexDocumentCountPath = String.join(URL_DELIMITER, index, COUNT);
Request request = new Request("GET", indexDocumentCountPath);
Response response = client().performRequest(request);
Expand Down Expand Up @@ -264,7 +264,7 @@ public String buildSearchBodyAsString(
});
}

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

public SearchResponse searchUsingShapeRelation(String indexName, String fieldName, Geometry geometry, ShapeRelation shapeRelation)
throws IOException {
throws Exception {
String searchEntity = buildSearchBodyAsString(builder -> {
builder.field(DEFAULT_SHAPE_FIELD_NAME);
GeoJson.toXContent(geometry, builder, EMPTY_PARAMS);
Expand All @@ -320,7 +320,7 @@ public SearchResponse searchUsingIndexedShapeIndex(
String indexedShapePath,
String docId,
String fieldName
) throws IOException {
) throws Exception {
String searchEntity = buildSearchBodyAsString(builder -> {
builder.startObject(INDEXED_SHAPE_FIELD);
builder.field(SHAPE_INDEX_FIELD, indexedShapeIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,29 @@

package org.opensearch.geospatial;

import static org.opensearch.client.RestClientBuilder.DEFAULT_MAX_CONN_PER_ROUTE;
import static org.opensearch.client.RestClientBuilder.DEFAULT_MAX_CONN_TOTAL;

import java.io.IOException;
import java.util.*;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.message.BasicHeader;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.hc.client5.http.auth.AuthScope;
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager;
import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder;
import org.apache.hc.client5.http.ssl.ClientTlsStrategyBuilder;
import org.apache.hc.client5.http.ssl.NoopHostnameVerifier;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.message.BasicHeader;
import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
import org.apache.hc.core5.ssl.SSLContextBuilder;
import org.apache.hc.core5.util.Timeout;
import org.junit.After;
import org.opensearch.client.Request;
import org.opensearch.client.Response;
Expand Down Expand Up @@ -95,13 +105,20 @@ private void configureHttpsClient(RestClientBuilder builder, Settings settings)
.orElseThrow(() -> new RuntimeException("user name is missing"));
String password = Optional.ofNullable(System.getProperty(SYS_PROPERTY_KEY_PASSWORD))
.orElseThrow(() -> new RuntimeException("password is missing"));
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));
BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
final AuthScope anyScope = new AuthScope(null, -1);
credentialsProvider.setCredentials(anyScope, new UsernamePasswordCredentials(userName, password.toCharArray()));
try {
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)
// disable the certificate since our testing cluster just uses the default security configuration
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
.setSSLContext(SSLContextBuilder.create().loadTrustMaterial(null, (chains, authType) -> true).build());
final TlsStrategy tlsStrategy = ClientTlsStrategyBuilder.create()
.setHostnameVerifier(NoopHostnameVerifier.INSTANCE)
.setSslContext(SSLContextBuilder.create().loadTrustMaterial(null, (chains, authType) -> true).build())
.build();
final PoolingAsyncClientConnectionManager connectionManager = PoolingAsyncClientConnectionManagerBuilder.create()
.setMaxConnPerRoute(DEFAULT_MAX_CONN_PER_ROUTE)
.setMaxConnTotal(DEFAULT_MAX_CONN_TOTAL)
.setTlsStrategy(tlsStrategy)
.build();
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider).setConnectionManager(connectionManager);
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand All @@ -112,7 +129,12 @@ private void configureHttpsClient(RestClientBuilder builder, Settings settings)
socketTimeoutString == null ? DEFAULT_SOCKET_TIMEOUT : socketTimeoutString,
CLIENT_SOCKET_TIMEOUT
);
builder.setRequestConfigCallback(conf -> conf.setSocketTimeout(Math.toIntExact(socketTimeout.getMillis())));
builder.setRequestConfigCallback(conf -> {
Timeout timeout = Timeout.ofMilliseconds(Math.toIntExact(socketTimeout.getMillis()));
conf.setConnectTimeout(timeout);
conf.setResponseTimeout(timeout);
return conf;
});
if (settings.hasValue(CLIENT_PATH_PREFIX)) {
builder.setPathPrefix(settings.get(CLIENT_PATH_PREFIX));
}
Expand All @@ -129,7 +151,7 @@ protected boolean preserveIndicesUponCompletion() {
@After
public void deleteExternalIndices() throws IOException {
Response response = client().performRequest(new Request("GET", "/_cat/indices?format=json&expand_wildcards=all"));
XContentType xContentType = XContentType.fromMediaType(response.getEntity().getContentType().getValue());
XContentType xContentType = XContentType.fromMediaType(response.getEntity().getContentType());
try (
XContentParser parser = xContentType.xContent()
.createParser(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

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

import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand All @@ -21,7 +20,7 @@ public class XYPointFieldMapperIT extends GeospatialRestTestCase {
private static final String FIELD_X_KEY = "x";
private static final String FIELD_Y_KEY = "y";

public void testMappingWithXYPointField() throws IOException {
public void testMappingWithXYPointField() throws Exception {
String indexName = GeospatialTestHelper.randomLowerCaseString();
String fieldName = GeospatialTestHelper.randomLowerCaseString();
createIndex(indexName, Settings.EMPTY, Map.of(fieldName, XYPointFieldMapper.CONTENT_TYPE));
Expand All @@ -32,7 +31,7 @@ public void testMappingWithXYPointField() throws IOException {
deleteIndex(indexName);
}

public void testIndexWithXYPointFieldAsWKTFormat() throws IOException {
public void testIndexWithXYPointFieldAsWKTFormat() throws Exception {
String indexName = GeospatialTestHelper.randomLowerCaseString();
String fieldName = GeospatialTestHelper.randomLowerCaseString();
createIndex(indexName, Settings.EMPTY, Map.of(fieldName, XYPointFieldMapper.CONTENT_TYPE));
Expand All @@ -45,7 +44,7 @@ public void testIndexWithXYPointFieldAsWKTFormat() throws IOException {
deleteIndex(indexName);
}

public void testIndexWithXYPointFieldAsArrayFormat() throws IOException {
public void testIndexWithXYPointFieldAsArrayFormat() throws Exception {
String indexName = GeospatialTestHelper.randomLowerCaseString();
String fieldName = GeospatialTestHelper.randomLowerCaseString();
createIndex(indexName, Settings.EMPTY, Map.of(fieldName, XYPointFieldMapper.CONTENT_TYPE));
Expand All @@ -58,7 +57,7 @@ public void testIndexWithXYPointFieldAsArrayFormat() throws IOException {
deleteIndex(indexName);
}

public void testIndexWithXYPointFieldAsStringFormat() throws IOException {
public void testIndexWithXYPointFieldAsStringFormat() throws Exception {
String indexName = GeospatialTestHelper.randomLowerCaseString();
String fieldName = GeospatialTestHelper.randomLowerCaseString();
createIndex(indexName, Settings.EMPTY, Map.of(fieldName, XYPointFieldMapper.CONTENT_TYPE));
Expand All @@ -72,7 +71,7 @@ public void testIndexWithXYPointFieldAsStringFormat() throws IOException {
deleteIndex(indexName);
}

public void testIndexWithXYPointFieldAsObjectFormat() throws IOException {
public void testIndexWithXYPointFieldAsObjectFormat() throws Exception {
String indexName = GeospatialTestHelper.randomLowerCaseString();
String fieldName = GeospatialTestHelper.randomLowerCaseString();
createIndex(indexName, Settings.EMPTY, Map.of(fieldName, XYPointFieldMapper.CONTENT_TYPE));
Expand All @@ -86,19 +85,19 @@ public void testIndexWithXYPointFieldAsObjectFormat() throws IOException {
deleteIndex(indexName);
}

private String getDocumentWithWKTValueForXYPoint(String fieldName, Geometry geometry) throws IOException {
private String getDocumentWithWKTValueForXYPoint(String fieldName, Geometry geometry) throws Exception {
return buildContentAsString(build -> build.field(fieldName, geometry.toString()));
}

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

private String getDocumentWithStringValueForXYPoint(String fieldName, String pointAsString) throws IOException {
private String getDocumentWithStringValueForXYPoint(String fieldName, String pointAsString) throws Exception {
return buildContentAsString(build -> build.field(fieldName, pointAsString));
}

private String getDocumentWithObjectValueForXYPoint(String fieldName, Point point) throws IOException {
private String getDocumentWithObjectValueForXYPoint(String fieldName, Point point) throws Exception {
return buildContentAsString(build -> {
build.startObject(fieldName);
build.field(FIELD_X_KEY, point.getX());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private String getDocumentWithWKTValueForXYShape(String fieldName, Geometry geom
return buildContentAsString(build -> build.field(fieldName, geometry.toString()));
}

public void testMappingWithXYShapeField() throws IOException {
public void testMappingWithXYShapeField() throws Exception {
String indexName = GeospatialTestHelper.randomLowerCaseString();
String fieldName = GeospatialTestHelper.randomLowerCaseString();
createIndex(indexName, Settings.EMPTY, Map.of(fieldName, XYShapeFieldMapper.CONTENT_TYPE));
Expand All @@ -44,7 +44,7 @@ public void testMappingWithXYShapeField() throws IOException {
deleteIndex(indexName);
}

public void testIndexWithXYShapeFieldAsWKTFormat() throws IOException {
public void testIndexWithXYShapeFieldAsWKTFormat() throws Exception {
String indexName = GeospatialTestHelper.randomLowerCaseString();
String fieldName = GeospatialTestHelper.randomLowerCaseString();
createIndex(indexName, Settings.EMPTY, Map.of(fieldName, XYShapeFieldMapper.CONTENT_TYPE));
Expand All @@ -57,7 +57,7 @@ public void testIndexWithXYShapeFieldAsWKTFormat() throws IOException {
deleteIndex(indexName);
}

public void testIndexWithXYShapeFieldAsGeoJSONFormat() throws IOException {
public void testIndexWithXYShapeFieldAsGeoJSONFormat() throws Exception {
String indexName = GeospatialTestHelper.randomLowerCaseString();
String fieldName = GeospatialTestHelper.randomLowerCaseString();
createIndex(indexName, Settings.EMPTY, Map.of(fieldName, XYShapeFieldMapper.CONTENT_TYPE));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

package org.opensearch.geospatial.plugin;

import java.io.IOException;

import org.apache.http.util.EntityUtils;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.opensearch.client.Request;
import org.opensearch.client.Response;
import org.opensearch.geospatial.GeospatialRestTestCase;
Expand All @@ -18,7 +16,7 @@ public class GeospatialPluginIT extends GeospatialRestTestCase {
/**
* Tests whether plugin is installed or not
*/
public void testPluginInstalled() throws IOException {
public void testPluginInstalled() throws Exception {
String restURI = String.join("/", "_cat", "plugins");

Request request = new Request("GET", restURI);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@
import static org.opensearch.geospatial.GeospatialObjectBuilder.randomGeoJSONFeature;
import static org.opensearch.geospatial.GeospatialTestHelper.randomLowerCaseString;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.apache.http.util.EntityUtils;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.json.JSONObject;
import org.opensearch.client.Request;
import org.opensearch.client.Response;
Expand All @@ -27,7 +26,7 @@

public class FeatureProcessorIT extends GeospatialRestTestCase {

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

public void testIndexGeoJSONSuccess() throws IOException {
public void testIndexGeoJSONSuccess() throws Exception {

String indexName = randomLowerCaseString();
String geoShapeField = randomLowerCaseString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class RestUploadGeoJSONActionIT extends GeospatialRestTestCase {

public static final int NUMBER_OF_FEATURES_TO_ADD = 3;

public void testGeoJSONUploadSuccessPostMethod() throws IOException {
public void testGeoJSONUploadSuccessPostMethod() throws Exception {

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

public void testGeoJSONUploadSuccessPutMethod() throws IOException {
public void testGeoJSONUploadSuccessPutMethod() throws Exception {

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

public void testGeoJSONPutMethodUploadIndexExists() throws IOException {
public void testGeoJSONPutMethodUploadIndexExists() throws Exception {

String index = randomLowerCaseStringWithSuffix(ACCEPTED_INDEX_SUFFIX_PATH);
String geoFieldName = randomLowerCaseString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
import static org.opensearch.geospatial.stats.upload.RestUploadStatsAction.ACTION_OBJECT;
import static org.opensearch.geospatial.stats.upload.RestUploadStatsAction.ACTION_STATS;

import java.io.IOException;

import org.apache.http.util.EntityUtils;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.opensearch.client.Request;
import org.opensearch.client.Response;
import org.opensearch.geospatial.GeospatialRestTestCase;
Expand All @@ -25,20 +23,20 @@ private String getUploadStatsPath() {
return String.join(URL_DELIMITER, getPluginURLPrefix(), ACTION_OBJECT, ACTION_STATS);
}

private String getStatsResponseAsString() throws IOException {
private String getStatsResponseAsString() throws Exception {
Request statsRequest = new Request("GET", getUploadStatsPath());
Response statsResponse = client().performRequest(statsRequest);
return EntityUtils.toString(statsResponse.getEntity());
}

public void testStatsAPISuccess() throws IOException {
public void testStatsAPISuccess() throws Exception {

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

public void testStatsAreUpdatedAfterUpload() throws IOException {
public void testStatsAreUpdatedAfterUpload() throws Exception {
// get current stats response
final String currentUploadStats = getStatsResponseAsString();
assertNotNull(currentUploadStats);
Expand Down

0 comments on commit e974d26

Please sign in to comment.