|
| 1 | +/* |
| 2 | + * Copyright 2012-2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.autoconfigure.elasticsearch; |
| 18 | + |
| 19 | +import java.net.URI; |
| 20 | +import java.net.URISyntaxException; |
| 21 | +import java.time.Duration; |
| 22 | + |
| 23 | +import org.apache.http.HttpHost; |
| 24 | +import org.apache.http.auth.AuthScope; |
| 25 | +import org.apache.http.auth.Credentials; |
| 26 | +import org.apache.http.auth.UsernamePasswordCredentials; |
| 27 | +import org.apache.http.client.config.RequestConfig; |
| 28 | +import org.apache.http.impl.client.BasicCredentialsProvider; |
| 29 | +import org.apache.http.impl.nio.client.HttpAsyncClientBuilder; |
| 30 | +import org.elasticsearch.client.RestClient; |
| 31 | +import org.elasticsearch.client.RestClientBuilder; |
| 32 | +import org.elasticsearch.client.RestHighLevelClient; |
| 33 | +import org.elasticsearch.client.sniff.Sniffer; |
| 34 | +import org.elasticsearch.client.sniff.SnifferBuilder; |
| 35 | + |
| 36 | +import org.springframework.beans.factory.ObjectProvider; |
| 37 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| 38 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 39 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate; |
| 40 | +import org.springframework.boot.context.properties.PropertyMapper; |
| 41 | +import org.springframework.context.annotation.Bean; |
| 42 | +import org.springframework.context.annotation.Configuration; |
| 43 | +import org.springframework.util.StringUtils; |
| 44 | + |
| 45 | +/** |
| 46 | + * Elasticsearch rest client configurations. |
| 47 | + * |
| 48 | + * @author Stephane Nicoll |
| 49 | + */ |
| 50 | +class ElasticsearchRestClientConfigurations { |
| 51 | + |
| 52 | + @Configuration(proxyBeanMethods = false) |
| 53 | + @ConditionalOnMissingBean(RestClientBuilder.class) |
| 54 | + static class RestClientBuilderConfiguration { |
| 55 | + |
| 56 | + @Bean |
| 57 | + RestClientBuilderCustomizer defaultRestClientBuilderCustomizer(ElasticsearchRestClientProperties properties) { |
| 58 | + return new DefaultRestClientBuilderCustomizer(properties); |
| 59 | + } |
| 60 | + |
| 61 | + @Bean |
| 62 | + RestClientBuilder elasticsearchRestClientBuilder(ElasticsearchRestClientProperties properties, |
| 63 | + ObjectProvider<RestClientBuilderCustomizer> builderCustomizers) { |
| 64 | + HttpHost[] hosts = properties.getUris().stream().map(this::createHttpHost).toArray(HttpHost[]::new); |
| 65 | + RestClientBuilder builder = RestClient.builder(hosts); |
| 66 | + builder.setHttpClientConfigCallback((httpClientBuilder) -> { |
| 67 | + builderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(httpClientBuilder)); |
| 68 | + return httpClientBuilder; |
| 69 | + }); |
| 70 | + builder.setRequestConfigCallback((requestConfigBuilder) -> { |
| 71 | + builderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(requestConfigBuilder)); |
| 72 | + return requestConfigBuilder; |
| 73 | + }); |
| 74 | + builderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(builder)); |
| 75 | + return builder; |
| 76 | + } |
| 77 | + |
| 78 | + private HttpHost createHttpHost(String uri) { |
| 79 | + try { |
| 80 | + return createHttpHost(URI.create(uri)); |
| 81 | + } |
| 82 | + catch (IllegalArgumentException ex) { |
| 83 | + return HttpHost.create(uri); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private HttpHost createHttpHost(URI uri) { |
| 88 | + if (!StringUtils.hasLength(uri.getUserInfo())) { |
| 89 | + return HttpHost.create(uri.toString()); |
| 90 | + } |
| 91 | + try { |
| 92 | + return HttpHost.create(new URI(uri.getScheme(), null, uri.getHost(), uri.getPort(), uri.getPath(), |
| 93 | + uri.getQuery(), uri.getFragment()).toString()); |
| 94 | + } |
| 95 | + catch (URISyntaxException ex) { |
| 96 | + throw new IllegalStateException(ex); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | + @Configuration(proxyBeanMethods = false) |
| 103 | + @ConditionalOnMissingBean(RestHighLevelClient.class) |
| 104 | + static class RestHighLevelClientConfiguration { |
| 105 | + |
| 106 | + @Bean |
| 107 | + RestHighLevelClient elasticsearchRestHighLevelClient(RestClientBuilder restClientBuilder) { |
| 108 | + return new RestHighLevelClient(restClientBuilder); |
| 109 | + } |
| 110 | + |
| 111 | + } |
| 112 | + |
| 113 | + @Configuration(proxyBeanMethods = false) |
| 114 | + @ConditionalOnClass(Sniffer.class) |
| 115 | + @ConditionalOnSingleCandidate(RestHighLevelClient.class) |
| 116 | + static class RestClientSnifferConfiguration { |
| 117 | + |
| 118 | + @Bean |
| 119 | + @ConditionalOnMissingBean |
| 120 | + Sniffer elasticsearchSniffer(RestHighLevelClient client, ElasticsearchRestClientProperties properties) { |
| 121 | + SnifferBuilder builder = Sniffer.builder(client.getLowLevelClient()); |
| 122 | + PropertyMapper get = PropertyMapper.get().alwaysApplyingWhenNonNull(); |
| 123 | + get.from(properties.getSniffer().getInterval()).asInt(Duration::toMillis) |
| 124 | + .to(builder::setSniffIntervalMillis); |
| 125 | + get.from(properties.getSniffer().getDelayAfterFailure()).asInt(Duration::toMillis) |
| 126 | + .to(builder::setSniffAfterFailureDelayMillis); |
| 127 | + return builder.build(); |
| 128 | + } |
| 129 | + |
| 130 | + } |
| 131 | + |
| 132 | + static class DefaultRestClientBuilderCustomizer implements RestClientBuilderCustomizer { |
| 133 | + |
| 134 | + private static final PropertyMapper map = PropertyMapper.get(); |
| 135 | + |
| 136 | + private final ElasticsearchRestClientProperties properties; |
| 137 | + |
| 138 | + DefaultRestClientBuilderCustomizer(ElasticsearchRestClientProperties properties) { |
| 139 | + this.properties = properties; |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public void customize(RestClientBuilder builder) { |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public void customize(HttpAsyncClientBuilder builder) { |
| 148 | + builder.setDefaultCredentialsProvider(new PropertiesCredentialsProvider(this.properties)); |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public void customize(RequestConfig.Builder builder) { |
| 153 | + map.from(this.properties::getConnectionTimeout).whenNonNull().asInt(Duration::toMillis) |
| 154 | + .to(builder::setConnectTimeout); |
| 155 | + map.from(this.properties::getReadTimeout).whenNonNull().asInt(Duration::toMillis) |
| 156 | + .to(builder::setSocketTimeout); |
| 157 | + } |
| 158 | + |
| 159 | + } |
| 160 | + |
| 161 | + private static class PropertiesCredentialsProvider extends BasicCredentialsProvider { |
| 162 | + |
| 163 | + PropertiesCredentialsProvider(ElasticsearchRestClientProperties properties) { |
| 164 | + if (StringUtils.hasText(properties.getUsername())) { |
| 165 | + Credentials credentials = new UsernamePasswordCredentials(properties.getUsername(), |
| 166 | + properties.getPassword()); |
| 167 | + setCredentials(AuthScope.ANY, credentials); |
| 168 | + } |
| 169 | + properties.getUris().stream().map(this::toUri).filter(this::hasUserInfo) |
| 170 | + .forEach(this::addUserInfoCredentials); |
| 171 | + } |
| 172 | + |
| 173 | + private URI toUri(String uri) { |
| 174 | + try { |
| 175 | + return URI.create(uri); |
| 176 | + } |
| 177 | + catch (IllegalArgumentException ex) { |
| 178 | + return null; |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + private boolean hasUserInfo(URI uri) { |
| 183 | + return uri != null && StringUtils.hasLength(uri.getUserInfo()); |
| 184 | + } |
| 185 | + |
| 186 | + private void addUserInfoCredentials(URI uri) { |
| 187 | + AuthScope authScope = new AuthScope(uri.getHost(), uri.getPort()); |
| 188 | + Credentials credentials = createUserInfoCredentials(uri.getUserInfo()); |
| 189 | + setCredentials(authScope, credentials); |
| 190 | + } |
| 191 | + |
| 192 | + private Credentials createUserInfoCredentials(String userInfo) { |
| 193 | + int delimiter = userInfo.indexOf(":"); |
| 194 | + if (delimiter == -1) { |
| 195 | + return new UsernamePasswordCredentials(userInfo, null); |
| 196 | + } |
| 197 | + String username = userInfo.substring(0, delimiter); |
| 198 | + String password = userInfo.substring(delimiter + 1); |
| 199 | + return new UsernamePasswordCredentials(username, password); |
| 200 | + } |
| 201 | + |
| 202 | + } |
| 203 | + |
| 204 | +} |
0 commit comments