Provide a config prop for Elasticsearch client's path prefix

Closes gh-25010
This commit is contained in:
Andy Wilkinson
2021-09-16 17:51:53 +01:00
parent e2a355f003
commit 808aa77747
5 changed files with 40 additions and 0 deletions

View File

@@ -74,6 +74,7 @@ public class ReactiveElasticsearchRestClientAutoConfiguration {
.to((credentials) -> builder.withBasicAuth(credentials.getUsername(), credentials.getPassword()));
map.from(this.properties.getConnectionTimeout()).to(builder::withConnectTimeout);
map.from(this.properties.getSocketTimeout()).to(builder::withSocketTimeout);
map.from(this.properties.getPathPrefix()).to(builder::withPathPrefix);
configureExchangeStrategies(map, builder);
return builder.build();
}
@@ -164,6 +165,10 @@ public class ReactiveElasticsearchRestClientAutoConfiguration {
: this.restClientProperties.getMaxInMemorySize();
}
private String getPathPrefix() {
return this.deprecatedProperties.isCustomized() ? null : this.properties.getPathPrefix();
}
private static final class Credentials {
private final String username;

View File

@@ -57,6 +57,11 @@ public class ElasticsearchProperties {
*/
private Duration socketTimeout = Duration.ofSeconds(30);
/**
* Prefix added to the path of every request sent to Elasticsearch.
*/
private String pathPrefix;
public List<String> getUris() {
return this.uris;
}
@@ -97,4 +102,12 @@ public class ElasticsearchProperties {
this.socketTimeout = socketTimeout;
}
public String getPathPrefix() {
return this.pathPrefix;
}
public void setPathPrefix(String pathPrefix) {
this.pathPrefix = pathPrefix;
}
}

View File

@@ -80,6 +80,9 @@ class ElasticsearchRestClientConfigurations {
builderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(requestConfigBuilder));
return requestConfigBuilder;
});
if (this.properties.getPathPrefix() != null) {
builder.setPathPrefix(this.properties.properties.getPathPrefix());
}
builderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(builder));
return builder;
}
@@ -253,6 +256,10 @@ class ElasticsearchRestClientConfigurations {
: this.properties.getSocketTimeout();
}
private String getPathPrefix() {
return this.deprecatedProperties.isCustomized() ? null : this.properties.getPathPrefix();
}
}
}

View File

@@ -256,6 +256,13 @@ class ReactiveElasticsearchRestClientAutoConfigurationTests {
.isEqualTo(Duration.ofSeconds(2)));
}
@Test
void whenPathPrefixIsConfiguredThenClientConfigurationHasPathPrefix() {
this.contextRunner.withPropertyValues("spring.elasticsearch.path-prefix=/some/prefix")
.run((context) -> assertThat(context.getBean(ClientConfiguration.class).getPathPrefix())
.isEqualTo("/some/prefix"));
}
@ParameterizedPropertyPrefixTest
void whenCredentialsAreConfiguredThenClientConfigurationHasDefaultAuthorizationHeader(String prefix) {
this.contextRunner.withPropertyValues(prefix + "username=alice", prefix + "password=secret")

View File

@@ -201,6 +201,14 @@ class ElasticsearchRestClientAutoConfigurationTests {
});
}
@Test
void configureWithCustomPathPrefix() {
this.contextRunner.withPropertyValues("spring.elasticsearch.path-prefix=/some/prefix").run((context) -> {
RestClient client = context.getBean(RestHighLevelClient.class).getLowLevelClient();
assertThat(client).extracting("pathPrefix").isEqualTo("/some/prefix");
});
}
@Test
void configureWithoutSnifferLibraryShouldNotCreateSniffer() {
this.contextRunner.withClassLoader(new FilteredClassLoader("org.elasticsearch.client.sniff"))