|
16 | 16 |
|
17 | 17 | package org.springframework.boot.autoconfigure.elasticsearch.rest;
|
18 | 18 |
|
19 |
| -import org.apache.http.HttpHost; |
20 |
| -import org.apache.http.auth.AuthScope; |
21 |
| -import org.apache.http.auth.Credentials; |
22 |
| -import org.apache.http.auth.UsernamePasswordCredentials; |
23 |
| -import org.apache.http.client.CredentialsProvider; |
24 |
| -import org.apache.http.impl.client.BasicCredentialsProvider; |
25 | 19 | import org.elasticsearch.client.RestClient;
|
26 |
| -import org.elasticsearch.client.RestClientBuilder; |
27 |
| -import org.elasticsearch.client.RestHighLevelClient; |
28 | 20 |
|
29 |
| -import org.springframework.beans.factory.ObjectProvider; |
30 | 21 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
31 |
| -import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; |
32 | 22 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
33 |
| -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
34 | 23 | import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
35 |
| -import org.springframework.boot.context.properties.PropertyMapper; |
36 |
| -import org.springframework.context.annotation.Bean; |
37 | 24 | import org.springframework.context.annotation.Configuration;
|
| 25 | +import org.springframework.context.annotation.Import; |
38 | 26 |
|
39 | 27 | /**
|
40 | 28 | * {@link EnableAutoConfiguration Auto-configuration} for Elasticsearch REST clients.
|
41 | 29 | *
|
42 | 30 | * @author Brian Clozel
|
| 31 | + * @author Stephane Nicoll |
43 | 32 | * @since 2.1.0
|
44 | 33 | */
|
45 | 34 | @Configuration
|
46 | 35 | @ConditionalOnClass(RestClient.class)
|
47 | 36 | @EnableConfigurationProperties(RestClientProperties.class)
|
| 37 | +@Import({ RestClientConfigurations.RestClientBuilderConfiguration.class, |
| 38 | + RestClientConfigurations.RestHighLevelClientConfiguration.class, |
| 39 | + RestClientConfigurations.RestClientFallbackConfiguration.class }) |
48 | 40 | public class RestClientAutoConfiguration {
|
49 | 41 |
|
50 |
| - private final RestClientProperties properties; |
51 |
| - |
52 |
| - private final ObjectProvider<RestClientBuilderCustomizer> builderCustomizers; |
53 |
| - |
54 |
| - public RestClientAutoConfiguration(RestClientProperties properties, |
55 |
| - ObjectProvider<RestClientBuilderCustomizer> builderCustomizers) { |
56 |
| - this.properties = properties; |
57 |
| - this.builderCustomizers = builderCustomizers; |
58 |
| - } |
59 |
| - |
60 |
| - @Bean |
61 |
| - @ConditionalOnMissingBean |
62 |
| - public RestClient restClient(RestClientBuilder builder) { |
63 |
| - return builder.build(); |
64 |
| - } |
65 |
| - |
66 |
| - @Bean |
67 |
| - @ConditionalOnMissingBean |
68 |
| - public RestClientBuilder restClientBuilder() { |
69 |
| - HttpHost[] hosts = this.properties.getUris().stream().map(HttpHost::create).toArray(HttpHost[]::new); |
70 |
| - RestClientBuilder builder = RestClient.builder(hosts); |
71 |
| - PropertyMapper map = PropertyMapper.get(); |
72 |
| - map.from(this.properties::getUsername).whenHasText().to((username) -> { |
73 |
| - CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); |
74 |
| - Credentials credentials = new UsernamePasswordCredentials(this.properties.getUsername(), |
75 |
| - this.properties.getPassword()); |
76 |
| - credentialsProvider.setCredentials(AuthScope.ANY, credentials); |
77 |
| - builder.setHttpClientConfigCallback( |
78 |
| - (httpClientBuilder) -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)); |
79 |
| - }); |
80 |
| - this.builderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(builder)); |
81 |
| - return builder; |
82 |
| - } |
83 |
| - |
84 |
| - @Configuration |
85 |
| - @ConditionalOnClass(RestHighLevelClient.class) |
86 |
| - public static class RestHighLevelClientConfiguration { |
87 |
| - |
88 |
| - @Bean |
89 |
| - @ConditionalOnMissingBean |
90 |
| - public RestHighLevelClient restHighLevelClient(RestClientBuilder restClientBuilder) { |
91 |
| - return new RestHighLevelClient(restClientBuilder); |
92 |
| - } |
93 |
| - |
94 |
| - } |
95 |
| - |
96 |
| - /** |
97 |
| - * Configuration to configure a {@link RestClient} bean from a |
98 |
| - * {@link RestHighLevelClient} if such a bean has been registered by the application. |
99 |
| - * If {@link RestHighLevelClient} is not unique or does not exist then |
100 |
| - * {@link RestClientBuilder#build()} will be used. |
101 |
| - */ |
102 |
| - @Configuration(proxyBeanMethods = false) |
103 |
| - @ConditionalOnClass(RestHighLevelClient.class) |
104 |
| - @ConditionalOnBean(RestHighLevelClient.class) |
105 |
| - public static class RestClientConfiguration { |
106 |
| - |
107 |
| - @Bean |
108 |
| - @ConditionalOnMissingBean |
109 |
| - public RestClient restClient(ObjectProvider<RestHighLevelClient> restHighLevelClient, |
110 |
| - RestClientBuilder builder) { |
111 |
| - RestHighLevelClient client = restHighLevelClient.getIfUnique(); |
112 |
| - if (client != null) { |
113 |
| - return client.getLowLevelClient(); |
114 |
| - } |
115 |
| - return builder.build(); |
116 |
| - } |
117 |
| - |
118 |
| - } |
119 |
| - |
120 | 42 | }
|
0 commit comments