|
23 | 23 | import org.apache.http.auth.Credentials;
|
24 | 24 | import org.apache.http.auth.UsernamePasswordCredentials;
|
25 | 25 | import org.apache.http.client.CredentialsProvider;
|
| 26 | +import org.apache.http.client.config.RequestConfig; |
26 | 27 | import org.apache.http.impl.client.BasicCredentialsProvider;
|
| 28 | +import org.apache.http.impl.nio.client.HttpAsyncClientBuilder; |
27 | 29 | import org.elasticsearch.client.RestClient;
|
28 | 30 | import org.elasticsearch.client.RestClientBuilder;
|
29 | 31 | import org.elasticsearch.client.RestHighLevelClient;
|
|
40 | 42 | *
|
41 | 43 | * @author Brian Clozel
|
42 | 44 | * @author Stephane Nicoll
|
| 45 | + * @author Vedran Pavic |
43 | 46 | */
|
44 | 47 | class ElasticsearchRestClientConfigurations {
|
45 | 48 |
|
46 | 49 | @Configuration(proxyBeanMethods = false)
|
| 50 | + @ConditionalOnMissingBean(RestClientBuilder.class) |
47 | 51 | static class RestClientBuilderConfiguration {
|
48 | 52 |
|
49 | 53 | @Bean
|
50 |
| - @ConditionalOnMissingBean |
| 54 | + RestClientBuilderCustomizer defaultRestClientBuilderCustomizer(ElasticsearchRestClientProperties properties) { |
| 55 | + return new DefaultRestClientBuilderCustomizer(properties); |
| 56 | + } |
| 57 | + |
| 58 | + @Bean |
51 | 59 | RestClientBuilder elasticsearchRestClientBuilder(ElasticsearchRestClientProperties properties,
|
52 | 60 | ObjectProvider<RestClientBuilderCustomizer> builderCustomizers) {
|
53 | 61 | HttpHost[] hosts = properties.getUris().stream().map(HttpHost::create).toArray(HttpHost[]::new);
|
54 | 62 | RestClientBuilder builder = RestClient.builder(hosts);
|
55 |
| - PropertyMapper map = PropertyMapper.get(); |
56 |
| - map.from(properties::getUsername).whenHasText().to((username) -> { |
57 |
| - CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); |
58 |
| - Credentials credentials = new UsernamePasswordCredentials(properties.getUsername(), |
59 |
| - properties.getPassword()); |
60 |
| - credentialsProvider.setCredentials(AuthScope.ANY, credentials); |
61 |
| - builder.setHttpClientConfigCallback( |
62 |
| - (httpClientBuilder) -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)); |
| 63 | + builder.setHttpClientConfigCallback((httpClientBuilder) -> { |
| 64 | + builderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(httpClientBuilder)); |
| 65 | + return httpClientBuilder; |
63 | 66 | });
|
64 | 67 | builder.setRequestConfigCallback((requestConfigBuilder) -> {
|
65 |
| - map.from(properties::getConnectionTimeout).whenNonNull().asInt(Duration::toMillis) |
66 |
| - .to(requestConfigBuilder::setConnectTimeout); |
67 |
| - map.from(properties::getReadTimeout).whenNonNull().asInt(Duration::toMillis) |
68 |
| - .to(requestConfigBuilder::setSocketTimeout); |
| 68 | + builderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(requestConfigBuilder)); |
69 | 69 | return requestConfigBuilder;
|
70 | 70 | });
|
71 | 71 | builderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(builder));
|
@@ -108,4 +108,39 @@ RestClient elasticsearchRestClient(RestClientBuilder builder) {
|
108 | 108 |
|
109 | 109 | }
|
110 | 110 |
|
| 111 | + static class DefaultRestClientBuilderCustomizer implements RestClientBuilderCustomizer { |
| 112 | + |
| 113 | + private static final PropertyMapper map = PropertyMapper.get(); |
| 114 | + |
| 115 | + private final ElasticsearchRestClientProperties properties; |
| 116 | + |
| 117 | + DefaultRestClientBuilderCustomizer(ElasticsearchRestClientProperties properties) { |
| 118 | + this.properties = properties; |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public void customize(RestClientBuilder builder) { |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public void customize(HttpAsyncClientBuilder builder) { |
| 127 | + map.from(this.properties::getUsername).whenHasText().to((username) -> { |
| 128 | + CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); |
| 129 | + Credentials credentials = new UsernamePasswordCredentials(this.properties.getUsername(), |
| 130 | + this.properties.getPassword()); |
| 131 | + credentialsProvider.setCredentials(AuthScope.ANY, credentials); |
| 132 | + builder.setDefaultCredentialsProvider(credentialsProvider); |
| 133 | + }); |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public void customize(RequestConfig.Builder builder) { |
| 138 | + map.from(this.properties::getConnectionTimeout).whenNonNull().asInt(Duration::toMillis) |
| 139 | + .to(builder::setConnectTimeout); |
| 140 | + map.from(this.properties::getReadTimeout).whenNonNull().asInt(Duration::toMillis) |
| 141 | + .to(builder::setSocketTimeout); |
| 142 | + } |
| 143 | + |
| 144 | + } |
| 145 | + |
111 | 146 | }
|
0 commit comments