This is an automated email from the ASF dual-hosted git repository. nfilotto pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push: new aacf048e47e CAMEL-17197: camel-elasticsearch - Avoid using deprecated APIs (#8381) aacf048e47e is described below commit aacf048e47e7ba67d25865e88e6327c616bacd6a Author: Nicolas Filotto <essob...@users.noreply.github.com> AuthorDate: Thu Sep 15 13:52:50 2022 +0200 CAMEL-17197: camel-elasticsearch - Avoid using deprecated APIs (#8381) ## Motivation When running test IT tests some warnings appear in the console, we would like to get rid of them. ## Modifications * No more set the mapping type in the requests since it is [deprecated](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/removal-of-types.html) * Re-add the test `ElasticsearchSizeLimitIT` as it passes (not related to the initial issue) * Clean up the code by removing existing warnings raised by IntelliJ. (not related to the initial issue) --- .../camel/component/elasticsearch/elasticsearch-rest.json | 2 +- .../component/elasticsearch/ElasticsearchComponent.java | 2 +- .../camel/component/elasticsearch/ElasticsearchEndpoint.java | 6 +++--- .../component/elasticsearch/ElasticsearchOperation.java | 2 +- .../camel/component/elasticsearch/ElasticsearchProducer.java | 12 ++---------- .../elasticsearch/ElasticsearchScrollRequestIterator.java | 4 ++-- .../aggregation/BulkRequestAggregationStrategy.java | 2 +- .../converter/ElasticsearchActionRequestConverter.java | 12 ++++++------ .../elasticsearch/integration/ElasticsearchBulkIT.java | 5 ++--- .../ElasticsearchGetSearchDeleteExistsUpdateIT.java | 10 ++++------ .../elasticsearch/integration/ElasticsearchSizeLimitIT.java | 2 -- 11 files changed, 23 insertions(+), 36 deletions(-) diff --git a/components/camel-elasticsearch-rest/src/generated/resources/org/apache/camel/component/elasticsearch/elasticsearch-rest.json b/components/camel-elasticsearch-rest/src/generated/resources/org/apache/camel/component/elasticsearch/elasticsearch-rest.json index 73bb688b0b6..7757fa5e19e 100644 --- a/components/camel-elasticsearch-rest/src/generated/resources/org/apache/camel/component/elasticsearch/elasticsearch-rest.json +++ b/components/camel-elasticsearch-rest/src/generated/resources/org/apache/camel/component/elasticsearch/elasticsearch-rest.json @@ -28,7 +28,7 @@ "maxRetryTimeout": { "kind": "property", "displayName": "Max Retry Timeout", "group": "producer", "label": "", "required": false, "type": "integer", "javaType": "int", "deprecated": false, "autowired": false, "secret": false, "defaultValue": 30000, "description": "The time in ms before retry" }, "socketTimeout": { "kind": "property", "displayName": "Socket Timeout", "group": "producer", "label": "", "required": false, "type": "integer", "javaType": "int", "deprecated": false, "autowired": false, "secret": false, "defaultValue": 30000, "description": "The timeout in ms to wait before the socket will timeout." }, "autowiredEnabled": { "kind": "property", "displayName": "Autowired Enabled", "group": "advanced", "label": "advanced", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "description": "Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which t [...] - "client": { "kind": "property", "displayName": "Client", "group": "advanced", "label": "advanced", "required": false, "type": "object", "javaType": "org.elasticsearch.client.RestClient", "deprecated": false, "autowired": true, "secret": false, "description": "To use an existing configured Elasticsearch client, instead of creating a client per endpoint. This allow to customize the client with specific settings." }, + "client": { "kind": "property", "displayName": "Client", "group": "advanced", "label": "advanced", "required": false, "type": "object", "javaType": "org.elasticsearch.client.RestClient", "deprecated": false, "autowired": true, "secret": false, "description": "To use an existing configured Elasticsearch client, instead of creating a client per endpoint. This allows to customize the client with specific settings." }, "enableSniffer": { "kind": "property", "displayName": "Enable Sniffer", "group": "advanced", "label": "advanced", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "Enable automatically discover nodes from a running Elasticsearch cluster. If this option is used in conjunction with Spring Boot then it's managed by the Spring Boot configuration (see: Disable Sniffer in Spring Boot)." }, "sniffAfterFailureDelay": { "kind": "property", "displayName": "Sniff After Failure Delay", "group": "advanced", "label": "advanced", "required": false, "type": "integer", "javaType": "int", "deprecated": false, "autowired": false, "secret": false, "defaultValue": 60000, "description": "The delay of a sniff execution scheduled after a failure (in milliseconds)" }, "snifferInterval": { "kind": "property", "displayName": "Sniffer Interval", "group": "advanced", "label": "advanced", "required": false, "type": "integer", "javaType": "int", "deprecated": false, "autowired": false, "secret": false, "defaultValue": 300000, "description": "The interval between consecutive ordinary sniff executions in milliseconds. Will be honoured when sniffOnFailure is disabled or when there are no failures between consecutive sniff executions" }, diff --git a/components/camel-elasticsearch-rest/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchComponent.java b/components/camel-elasticsearch-rest/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchComponent.java index 19d33859e4b..454b4fc41f2 100644 --- a/components/camel-elasticsearch-rest/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchComponent.java +++ b/components/camel-elasticsearch-rest/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchComponent.java @@ -114,7 +114,7 @@ public class ElasticsearchComponent extends DefaultComponent { } /** - * To use an existing configured Elasticsearch client, instead of creating a client per endpoint. This allow to + * To use an existing configured Elasticsearch client, instead of creating a client per endpoint. This allows to * customize the client with specific settings. */ public void setClient(RestClient client) { diff --git a/components/camel-elasticsearch-rest/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchEndpoint.java b/components/camel-elasticsearch-rest/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchEndpoint.java index a6615ee27d8..184fb64059a 100644 --- a/components/camel-elasticsearch-rest/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchEndpoint.java +++ b/components/camel-elasticsearch-rest/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchEndpoint.java @@ -36,7 +36,7 @@ public class ElasticsearchEndpoint extends DefaultEndpoint { @UriParam private final ElasticsearchConfiguration configuration; - private RestClient client; + private final RestClient client; public ElasticsearchEndpoint(String uri, ElasticsearchComponent component, ElasticsearchConfiguration config, RestClient client) { @@ -50,12 +50,12 @@ public class ElasticsearchEndpoint extends DefaultEndpoint { } @Override - public Producer createProducer() throws Exception { + public Producer createProducer() { return new ElasticsearchProducer(this, configuration); } @Override - public Consumer createConsumer(Processor processor) throws Exception { + public Consumer createConsumer(Processor processor) { throw new UnsupportedOperationException("Cannot consume from an ElasticsearchEndpoint: " + getEndpointUri()); } diff --git a/components/camel-elasticsearch-rest/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchOperation.java b/components/camel-elasticsearch-rest/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchOperation.java index 326f74d672a..84c26a59141 100644 --- a/components/camel-elasticsearch-rest/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchOperation.java +++ b/components/camel-elasticsearch-rest/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchOperation.java @@ -18,7 +18,7 @@ package org.apache.camel.component.elasticsearch; /** * The ElasticSearch server operations list which are implemented - * + * <p> * Index - Index a document associated with a given index and type Update - Updates a document based on a script Bulk - * Executes a bulk of index / delete operations BulkIndex - Executes a bulk of index / delete operations GetById - Gets * the document that was indexed from an index with a type and id MultiGet - Multiple get documents Delete - Deletes a diff --git a/components/camel-elasticsearch-rest/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchProducer.java b/components/camel-elasticsearch-rest/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchProducer.java index dd504c934c2..8b82f97835f 100644 --- a/components/camel-elasticsearch-rest/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchProducer.java +++ b/components/camel-elasticsearch-rest/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchProducer.java @@ -181,14 +181,7 @@ public class ElasticsearchProducer extends DefaultProducer { throw new IllegalArgumentException("Wrong body type. Only String or GetRequest is allowed as a type"); } message.setBody(restHighLevelClient.get(getRequest, RequestOptions.DEFAULT)); - } else if (operation == ElasticsearchOperation.Bulk) { - BulkRequest bulkRequest = message.getBody(BulkRequest.class); - if (bulkRequest == null) { - throw new IllegalArgumentException( - "Wrong body type. Only List, Collection or BulkRequest is allowed as a type"); - } - message.setBody(restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT).getItems()); - } else if (operation == ElasticsearchOperation.BulkIndex) { + } else if (operation == ElasticsearchOperation.Bulk || operation == ElasticsearchOperation.BulkIndex) { BulkRequest bulkRequest = message.getBody(BulkRequest.class); if (bulkRequest == null) { throw new IllegalArgumentException( @@ -282,7 +275,6 @@ public class ElasticsearchProducer extends DefaultProducer { } @Override - @SuppressWarnings("unchecked") protected void doStart() throws Exception { super.doStart(); if (!configuration.isDisconnect()) { @@ -344,7 +336,7 @@ public class ElasticsearchProducer extends DefaultProducer { private static final class HighLevelClient extends RestHighLevelClient { private HighLevelClient(RestClient restClient) { - super(restClient, client -> { + super(restClient, c -> { }, Collections.emptyList()); } } diff --git a/components/camel-elasticsearch-rest/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchScrollRequestIterator.java b/components/camel-elasticsearch-rest/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchScrollRequestIterator.java index e9db44483e6..2f2cebe111a 100644 --- a/components/camel-elasticsearch-rest/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchScrollRequestIterator.java +++ b/components/camel-elasticsearch-rest/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchScrollRequestIterator.java @@ -38,7 +38,7 @@ public class ElasticsearchScrollRequestIterator implements Iterator<SearchHit>, private final RestHighLevelClient restHighLevelClient; private Iterator<SearchHit> currentSearchHits; private final int scrollKeepAliveMs; - private Exchange exchange; + private final Exchange exchange; private String scrollId; private boolean closed; private int requestCount; @@ -52,7 +52,7 @@ public class ElasticsearchScrollRequestIterator implements Iterator<SearchHit>, this.closed = false; this.requestCount = 0; - // add scroll option on the the first query + // add scroll option on the first query searchRequest.scroll(TimeValue.timeValueMillis(scrollKeepAliveMs)); setFirstCurrentSearchHits(); diff --git a/components/camel-elasticsearch-rest/src/main/java/org/apache/camel/component/elasticsearch/aggregation/BulkRequestAggregationStrategy.java b/components/camel-elasticsearch-rest/src/main/java/org/apache/camel/component/elasticsearch/aggregation/BulkRequestAggregationStrategy.java index 9a24f24036e..7158d98fd53 100644 --- a/components/camel-elasticsearch-rest/src/main/java/org/apache/camel/component/elasticsearch/aggregation/BulkRequestAggregationStrategy.java +++ b/components/camel-elasticsearch-rest/src/main/java/org/apache/camel/component/elasticsearch/aggregation/BulkRequestAggregationStrategy.java @@ -36,7 +36,7 @@ public class BulkRequestAggregationStrategy implements AggregationStrategy { throw new InvalidPayloadRuntimeException(newExchange, DocWriteRequest[].class); } - DocWriteRequest[] newBody = (DocWriteRequest[]) objBody; + DocWriteRequest<?>[] newBody = (DocWriteRequest<?>[]) objBody; BulkRequest request; if (oldExchange == null) { request = new BulkRequest(); diff --git a/components/camel-elasticsearch-rest/src/main/java/org/apache/camel/component/elasticsearch/converter/ElasticsearchActionRequestConverter.java b/components/camel-elasticsearch-rest/src/main/java/org/apache/camel/component/elasticsearch/converter/ElasticsearchActionRequestConverter.java index fccbda48f34..7e7e0d72725 100644 --- a/components/camel-elasticsearch-rest/src/main/java/org/apache/camel/component/elasticsearch/converter/ElasticsearchActionRequestConverter.java +++ b/components/camel-elasticsearch-rest/src/main/java/org/apache/camel/component/elasticsearch/converter/ElasticsearchActionRequestConverter.java @@ -85,7 +85,7 @@ public final class ElasticsearchActionRequestConverter { if (document instanceof byte[]) { indexRequest.source((byte[]) document, XContentFactory.xContentType((byte[]) document)); } else if (document instanceof Map) { - indexRequest.source((Map<String, Object>) document); + indexRequest.source((Map<String, ?>) document); } else if (document instanceof String) { indexRequest.source((String) document, XContentFactory.xContentType((String) document)); } else if (document instanceof XContentBuilder) { @@ -187,17 +187,17 @@ public final class ElasticsearchActionRequestConverter { String queryText = null; if (queryObject instanceof Map<?, ?>) { - Map<String, Object> mapQuery = (Map<String, Object>) queryObject; + Map<String, ?> mapQuery = (Map<String, ?>) queryObject; // Remove 'query' prefix from the query object for backward // compatibility if (mapQuery.containsKey(ES_QUERY_DSL_PREFIX)) { - mapQuery = (Map<String, Object>) mapQuery.get(ES_QUERY_DSL_PREFIX); + mapQuery = (Map<String, ?>) mapQuery.get(ES_QUERY_DSL_PREFIX); } - try { - XContentBuilder contentBuilder = XContentFactory.contentBuilder(XContentType.JSON); + try (XContentBuilder contentBuilder = XContentFactory.contentBuilder(XContentType.JSON)) { queryText = Strings.toString(contentBuilder.map(mapQuery)); } catch (IOException e) { LOG.error("Cannot build the QueryText from the map.", e); + return null; } } else if (queryObject instanceof String) { queryText = (String) queryObject; @@ -231,7 +231,7 @@ public final class ElasticsearchActionRequestConverter { } if (documents instanceof List) { BulkRequest request = new BulkRequest(); - for (Object document : (List<Object>) documents) { + for (Object document : (List<?>) documents) { request.add(createIndexRequest(document, exchange)); } return request; diff --git a/components/camel-elasticsearch-rest/src/test/java/org/apache/camel/component/elasticsearch/integration/ElasticsearchBulkIT.java b/components/camel-elasticsearch-rest/src/test/java/org/apache/camel/component/elasticsearch/integration/ElasticsearchBulkIT.java index df5e3617db4..291d7b7cc0b 100644 --- a/components/camel-elasticsearch-rest/src/test/java/org/apache/camel/component/elasticsearch/integration/ElasticsearchBulkIT.java +++ b/components/camel-elasticsearch-rest/src/test/java/org/apache/camel/component/elasticsearch/integration/ElasticsearchBulkIT.java @@ -76,10 +76,9 @@ public class ElasticsearchBulkIT extends ElasticsearchTestSupport { // given BulkRequest request = new BulkRequest(); request.add( - new IndexRequest(prefix + "foo", prefix + "bar", prefix + "baz").source(prefix + "content", prefix + "hello")); + new IndexRequest(prefix + "foo").id(prefix + "baz").source(prefix + "content", prefix + "hello")); // when - @SuppressWarnings("unchecked") BulkItemResponse[] response = template.requestBody("direct:bulk_index", request, BulkItemResponse[].class); // then @@ -96,7 +95,7 @@ public class ElasticsearchBulkIT extends ElasticsearchTestSupport { // given BulkRequest request = new BulkRequest(); request.add( - new IndexRequest(prefix + "foo", prefix + "bar", prefix + "baz").source(prefix + "content", prefix + "hello")); + new IndexRequest(prefix + "foo").id(prefix + "baz").source(prefix + "content", prefix + "hello")); // when BulkItemResponse[] response = (BulkItemResponse[]) template.requestBody("direct:bulk", request); diff --git a/components/camel-elasticsearch-rest/src/test/java/org/apache/camel/component/elasticsearch/integration/ElasticsearchGetSearchDeleteExistsUpdateIT.java b/components/camel-elasticsearch-rest/src/test/java/org/apache/camel/component/elasticsearch/integration/ElasticsearchGetSearchDeleteExistsUpdateIT.java index 4c6e17ba79c..b8e1615726a 100644 --- a/components/camel-elasticsearch-rest/src/test/java/org/apache/camel/component/elasticsearch/integration/ElasticsearchGetSearchDeleteExistsUpdateIT.java +++ b/components/camel-elasticsearch-rest/src/test/java/org/apache/camel/component/elasticsearch/integration/ElasticsearchGetSearchDeleteExistsUpdateIT.java @@ -145,10 +145,8 @@ public class ElasticsearchGetSearchDeleteExistsUpdateIT extends ElasticsearchTes //now, verify GET succeeded SearchRequest req = new SearchRequest(); req.indices("twitter"); - req.types("tweet"); SearchRequest req1 = new SearchRequest(); req.indices("twitter"); - req.types("tweets"); MultiSearchRequest request = new MultiSearchRequest().add(req1).add(req); Item[] response = template.requestBody("direct:search", request, Item[].class); assertNotNull(response, "response should not be null"); @@ -275,11 +273,11 @@ public class ElasticsearchGetSearchDeleteExistsUpdateIT extends ElasticsearchTes String prefix = createPrefix(); // given - GetRequest request = new GetRequest(prefix + "foo").type(prefix + "bar"); + GetRequest request = new GetRequest(prefix + "foo"); // when String documentId = template.requestBody("direct:index", - new IndexRequest(prefix + "foo", prefix + "bar", prefix + "testId") + new IndexRequest(prefix + "foo").id(prefix + "testId") .source(prefix + "content", prefix + "hello"), String.class); GetResponse response = template.requestBody("direct:get", @@ -296,11 +294,11 @@ public class ElasticsearchGetSearchDeleteExistsUpdateIT extends ElasticsearchTes String prefix = createPrefix(); // given - DeleteRequest request = new DeleteRequest(prefix + "foo").type(prefix + "bar"); + DeleteRequest request = new DeleteRequest(prefix + "foo"); // when String documentId = template.requestBody("direct:index", - new IndexRequest("" + prefix + "foo", "" + prefix + "bar", "" + prefix + "testId") + new IndexRequest("" + prefix + "foo").id("" + prefix + "testId") .source(prefix + "content", prefix + "hello"), String.class); DeleteResponse.Result response diff --git a/components/camel-elasticsearch-rest/src/test/java/org/apache/camel/component/elasticsearch/integration/ElasticsearchSizeLimitIT.java b/components/camel-elasticsearch-rest/src/test/java/org/apache/camel/component/elasticsearch/integration/ElasticsearchSizeLimitIT.java index cc2d045223e..840397a17b1 100644 --- a/components/camel-elasticsearch-rest/src/test/java/org/apache/camel/component/elasticsearch/integration/ElasticsearchSizeLimitIT.java +++ b/components/camel-elasticsearch-rest/src/test/java/org/apache/camel/component/elasticsearch/integration/ElasticsearchSizeLimitIT.java @@ -23,12 +23,10 @@ import java.util.concurrent.TimeUnit; import org.apache.camel.builder.RouteBuilder; import org.awaitility.Awaitility; import org.elasticsearch.search.SearchHits; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; public class ElasticsearchSizeLimitIT extends ElasticsearchTestSupport { - @Disabled("Looks like there were some assumption related to test executed before") @Test public void testSize() { //put 4