|
| 1 | +/* |
| 2 | + * Copyright 2018 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 | + * http://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.data.elasticsearch.client; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.List; |
| 22 | +import java.util.stream.Collectors; |
| 23 | + |
| 24 | +import org.apache.http.Header; |
| 25 | +import org.apache.http.HttpHost; |
| 26 | +import org.apache.http.message.BasicHeader; |
| 27 | +import org.elasticsearch.client.RestClient; |
| 28 | +import org.elasticsearch.client.RestClientBuilder; |
| 29 | +import org.elasticsearch.client.RestHighLevelClient; |
| 30 | +import org.springframework.data.elasticsearch.client.reactive.DefaultReactiveElasticsearchClient; |
| 31 | +import org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient; |
| 32 | +import org.springframework.http.HttpHeaders; |
| 33 | +import org.springframework.util.Assert; |
| 34 | + |
| 35 | +/** |
| 36 | + * Utility class for common access to Elasticsearch clients. {@link ElasticsearchClients} consolidates set up routines |
| 37 | + * for the various drivers into a single place. |
| 38 | + * |
| 39 | + * @author Christoph Strobl |
| 40 | + * @since 4.0 |
| 41 | + */ |
| 42 | +public final class ElasticsearchClients { |
| 43 | + |
| 44 | + private ElasticsearchClients() {} |
| 45 | + |
| 46 | + /** |
| 47 | + * Start here to create a new client tailored to your needs. |
| 48 | + * |
| 49 | + * @return new instance of {@link ClientBuilderWithRequiredHost}. |
| 50 | + */ |
| 51 | + public static ClientBuilderWithRequiredHost createClient() { |
| 52 | + return new ElasticsearchClientBuilderImpl(); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @author Christoph Strobl |
| 57 | + */ |
| 58 | + public interface ElasticsearchClientBuilder { |
| 59 | + |
| 60 | + /** |
| 61 | + * Apply the configuration to create a {@link ReactiveElasticsearchClient}. |
| 62 | + * |
| 63 | + * @return new instance of {@link ReactiveElasticsearchClient}. |
| 64 | + */ |
| 65 | + ReactiveElasticsearchClient reactive(); |
| 66 | + |
| 67 | + /** |
| 68 | + * Apply the configuration to create a {@link RestHighLevelClient}. |
| 69 | + * |
| 70 | + * @return new instance of {@link RestHighLevelClient}. |
| 71 | + */ |
| 72 | + RestHighLevelClient rest(); |
| 73 | + |
| 74 | + /** |
| 75 | + * Apply the configuration to create a {@link RestClient}. |
| 76 | + * |
| 77 | + * @return new instance of {@link RestClient}. |
| 78 | + */ |
| 79 | + default RestClient lowLevelRest() { |
| 80 | + return rest().getLowLevelClient(); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @author Christoph Strobl |
| 86 | + */ |
| 87 | + public interface ClientBuilderWithRequiredHost { |
| 88 | + |
| 89 | + /** |
| 90 | + * @param host the {@literal host} and {@literal port} formatted as String {@literal host:port}. You may leave out |
| 91 | + * {@literal http / https} and use {@link MaybeSecureClientBuilder#viaSsl() viaSsl}. |
| 92 | + * @return the {@link MaybeSecureClientBuilder}. |
| 93 | + */ |
| 94 | + default MaybeSecureClientBuilder connectedTo(String host) { |
| 95 | + return connectedTo(new String[] { host }); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @param hosts the list of {@literal host} and {@literal port} combinations formatted as String |
| 100 | + * {@literal host:port}. You may leave out {@literal http / https} and use |
| 101 | + * {@link MaybeSecureClientBuilder#viaSsl() viaSsl}. |
| 102 | + * @return the {@link MaybeSecureClientBuilder}. |
| 103 | + */ |
| 104 | + MaybeSecureClientBuilder connectedTo(String... hosts); |
| 105 | + |
| 106 | + /** |
| 107 | + * Obviously for testing. |
| 108 | + * |
| 109 | + * @return the {@link MaybeSecureClientBuilder}. |
| 110 | + */ |
| 111 | + default MaybeSecureClientBuilder connectedToLocalhost() { |
| 112 | + return connectedTo("localhost:9200"); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * @author Christoph Strobl |
| 118 | + */ |
| 119 | + public interface MaybeSecureClientBuilder extends ClientBuilderWithOptionalDefaultHeaders { |
| 120 | + |
| 121 | + /** |
| 122 | + * Connect via {@literal https} <br /> |
| 123 | + * <strong>NOTE</strong> You need to leave out the protocol in |
| 124 | + * {@link ClientBuilderWithRequiredHost#connectedTo(String)}. |
| 125 | + * |
| 126 | + * @return the {@link ClientBuilderWithOptionalDefaultHeaders}. |
| 127 | + */ |
| 128 | + ClientBuilderWithOptionalDefaultHeaders viaSsl(); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * @author Christoph Strobl |
| 133 | + */ |
| 134 | + public interface ClientBuilderWithOptionalDefaultHeaders extends ElasticsearchClientBuilder { |
| 135 | + |
| 136 | + /** |
| 137 | + * @param defaultHeaders |
| 138 | + * @return the {@link ElasticsearchClientBuilder} |
| 139 | + */ |
| 140 | + ElasticsearchClientBuilder withDefaultHeaders(HttpHeaders defaultHeaders); |
| 141 | + } |
| 142 | + |
| 143 | + private static class ElasticsearchClientBuilderImpl |
| 144 | + implements ElasticsearchClientBuilder, ClientBuilderWithRequiredHost, MaybeSecureClientBuilder { |
| 145 | + |
| 146 | + private List<String> hosts = new ArrayList<>(); |
| 147 | + private HttpHeaders headers = HttpHeaders.EMPTY; |
| 148 | + private String protocoll = "http"; |
| 149 | + |
| 150 | + @Override |
| 151 | + public ReactiveElasticsearchClient reactive() { |
| 152 | + return DefaultReactiveElasticsearchClient.create(headers, formattedHosts().toArray(new String[0])); |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public RestHighLevelClient rest() { |
| 157 | + |
| 158 | + HttpHost[] httpHosts = formattedHosts().stream().map(HttpHost::create).toArray(HttpHost[]::new); |
| 159 | + RestClientBuilder builder = RestClient.builder(httpHosts); |
| 160 | + |
| 161 | + if (!headers.isEmpty()) { |
| 162 | + |
| 163 | + Header[] httpHeaders = headers.toSingleValueMap().entrySet().stream() |
| 164 | + .map(it -> new BasicHeader(it.getKey(), it.getValue())).toArray(Header[]::new); |
| 165 | + builder = builder.setDefaultHeaders(httpHeaders); |
| 166 | + } |
| 167 | + |
| 168 | + return new RestHighLevelClient(builder); |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public MaybeSecureClientBuilder connectedTo(String... hosts) { |
| 173 | + |
| 174 | + Assert.notEmpty(hosts, "At least one host is required."); |
| 175 | + this.hosts.addAll(Arrays.asList(hosts)); |
| 176 | + return this; |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + public ClientBuilderWithOptionalDefaultHeaders withDefaultHeaders(HttpHeaders defaultHeaders) { |
| 181 | + |
| 182 | + Assert.notNull(defaultHeaders, "DefaultHeaders must not be null!"); |
| 183 | + this.headers = defaultHeaders; |
| 184 | + return this; |
| 185 | + } |
| 186 | + |
| 187 | + List<String> formattedHosts() { |
| 188 | + return hosts.stream().map(it -> it.startsWith("http") ? it : protocoll + "://" + it).collect(Collectors.toList()); |
| 189 | + } |
| 190 | + |
| 191 | + @Override |
| 192 | + public ClientBuilderWithOptionalDefaultHeaders viaSsl() { |
| 193 | + this.protocoll = "https"; |
| 194 | + return this; |
| 195 | + } |
| 196 | + } |
| 197 | +} |
0 commit comments