This is an automated email from the ASF dual-hosted git repository. rouazana pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 9a542534394478d43558d5206aa27d43592cdafd Author: Raphael Ouazana <raphael.ouaz...@linagora.com> AuthorDate: Thu Nov 21 18:01:20 2019 +0100 JAMES-2995 Fix ElasticSearch mapping request when behind a proxy --- .../james/backends/es/NodeMappingFactory.java | 2 +- .../backends/es/NodeMappingFactoryAuthTest.java | 115 +++++++++++++++++++++ 2 files changed, 116 insertions(+), 1 deletion(-) diff --git a/backends-common/elasticsearch/src/main/java/org/apache/james/backends/es/NodeMappingFactory.java b/backends-common/elasticsearch/src/main/java/org/apache/james/backends/es/NodeMappingFactory.java index 05e15d4..f4359f4 100644 --- a/backends-common/elasticsearch/src/main/java/org/apache/james/backends/es/NodeMappingFactory.java +++ b/backends-common/elasticsearch/src/main/java/org/apache/james/backends/es/NodeMappingFactory.java @@ -66,7 +66,7 @@ public class NodeMappingFactory { @SuppressWarnings("deprecation") public static boolean mappingAlreadyExist(RestHighLevelClient client, IndexName indexName) throws IOException { try { - client.getLowLevelClient().performRequest("GET", indexName.getValue() + "/_mapping/" + NodeMappingFactory.DEFAULT_MAPPING_NAME); + client.getLowLevelClient().performRequest("GET", "/" + indexName.getValue() + "/_mapping/" + NodeMappingFactory.DEFAULT_MAPPING_NAME); return true; } catch (ResponseException e) { if (e.getResponse().getStatusLine().getStatusCode() != HttpStatus.SC_NOT_FOUND) { diff --git a/backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/NodeMappingFactoryAuthTest.java b/backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/NodeMappingFactoryAuthTest.java new file mode 100644 index 0000000..dddd34a --- /dev/null +++ b/backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/NodeMappingFactoryAuthTest.java @@ -0,0 +1,115 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.backends.es; + +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; + +import java.io.IOException; +import java.util.Optional; + +import org.elasticsearch.client.RestHighLevelClient; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +class NodeMappingFactoryAuthTest { + private static final String MESSAGE = "message"; + private static final IndexName INDEX_NAME = new IndexName("index"); + private static final ReadAliasName ALIAS_NAME = new ReadAliasName("alias"); + + @RegisterExtension + static ElasticSearchClusterExtension extension = new ElasticSearchClusterExtension(new ElasticSearchClusterExtension.ElasticSearchCluster( + DockerAuthElasticSearchSingleton.INSTANCE, + new DockerElasticSearch.WithAuth())); + + private RestHighLevelClient client; + + @BeforeEach + void setUp(ElasticSearchClusterExtension.ElasticSearchCluster esCluster) throws Exception { + client = new ClientProvider(ElasticSearchConfiguration.builder() + .credential(Optional.of(DockerElasticSearch.WithAuth.DEFAULT_CREDENTIAL)) + .hostScheme(Optional.of(ElasticSearchConfiguration.HostScheme.HTTPS)) + .sslTrustConfiguration(ElasticSearchConfiguration.SSLConfiguration.builder() + .strategyIgnore() + .acceptAnyHostNameVerifier() + .build()) + .addHost(esCluster.es1.getHttpHost()) + .build()).get(); + new IndexCreationFactory(ElasticSearchConfiguration.DEFAULT_CONFIGURATION) + .useIndex(INDEX_NAME) + .addAlias(ALIAS_NAME) + .createIndexAndAliases(client); + NodeMappingFactory.applyMapping(client, + INDEX_NAME, + getMappingsSources()); + } + + @AfterEach + void tearDown() throws IOException { + client.close(); + } + + @Test + void applyMappingShouldNotThrowWhenCalledSeveralTime() throws Exception { + NodeMappingFactory.applyMapping(client, + INDEX_NAME, + getMappingsSources()); + } + + @Test + void applyMappingShouldNotThrowWhenIncrementalChanges(ElasticSearchClusterExtension.ElasticSearchCluster esCluster) throws Exception { + NodeMappingFactory.applyMapping(client, + INDEX_NAME, + getMappingsSources()); + + esCluster.es1.flushIndices(); + + assertThatCode(() -> NodeMappingFactory.applyMapping(client, + INDEX_NAME, + getOtherMappingsSources())) + .doesNotThrowAnyException(); + } + + private XContentBuilder getMappingsSources() throws Exception { + return jsonBuilder() + .startObject() + .startObject(NodeMappingFactory.PROPERTIES) + .startObject(MESSAGE) + .field(NodeMappingFactory.TYPE, NodeMappingFactory.TEXT) + .endObject() + .endObject() + .endObject(); + } + + private XContentBuilder getOtherMappingsSources() throws Exception { + return jsonBuilder() + .startObject() + .startObject(NodeMappingFactory.PROPERTIES) + .startObject(MESSAGE) + .field(NodeMappingFactory.TYPE, NodeMappingFactory.TEXT) + .field(NodeMappingFactory.INDEX, false) + .endObject() + .endObject() + .endObject(); + } +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org