|
| 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 | +package org.springframework.data.elasticsearch.client; |
| 17 | + |
| 18 | +import java.net.InetSocketAddress; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Optional; |
| 21 | + |
| 22 | +import javax.net.ssl.SSLContext; |
| 23 | + |
| 24 | +import org.springframework.http.HttpHeaders; |
| 25 | + |
| 26 | +/** |
| 27 | + * Configuration interface exposing common client configuration properties for Elasticsearch clients. |
| 28 | + * |
| 29 | + * @author Mark Paluch |
| 30 | + * @since 4.0 |
| 31 | + */ |
| 32 | +public interface ClientConfiguration { |
| 33 | + |
| 34 | + /** |
| 35 | + * Creates a new {@link ClientConfigurationBuilder} instance. |
| 36 | + * |
| 37 | + * @return a new {@link ClientConfigurationBuilder} instance. |
| 38 | + */ |
| 39 | + static ClientConfigurationBuilderWithRequiredEndpoint builder() { |
| 40 | + return new ClientConfigurationBuilder(); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Creates a new {@link ClientConfiguration} instance configured to a single host given {@code hostAndPort}. |
| 45 | + * <p/> |
| 46 | + * For example given the endpoint http://localhost:9200 |
| 47 | + * |
| 48 | + * <pre class="code"> |
| 49 | + * ClientConfiguration configuration = ClientConfiguration.create("localhost:9200"); |
| 50 | + * </pre> |
| 51 | + * |
| 52 | + * @return a new {@link ClientConfigurationBuilder} instance. |
| 53 | + */ |
| 54 | + static ClientConfiguration create(String hostAndPort) { |
| 55 | + return new ClientConfigurationBuilder().connectedTo(hostAndPort).build(); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Creates a new {@link ClientConfiguration} instance configured to a single host given {@link InetSocketAddress}. |
| 60 | + * <p/> |
| 61 | + * For example given the endpoint http://localhost:9200 |
| 62 | + * |
| 63 | + * <pre class="code"> |
| 64 | + * ClientConfiguration configuration = ClientConfiguration |
| 65 | + * .create(InetSocketAddress.createUnresolved("localhost", 9200)); |
| 66 | + * </pre> |
| 67 | + * |
| 68 | + * @return a new {@link ClientConfigurationBuilder} instance. |
| 69 | + */ |
| 70 | + static ClientConfiguration create(InetSocketAddress socketAddress) { |
| 71 | + return new ClientConfigurationBuilder().connectedTo(socketAddress).build(); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Returns the configured endpoints. |
| 76 | + * |
| 77 | + * @return the configured endpoints. |
| 78 | + */ |
| 79 | + List<InetSocketAddress> getEndpoints(); |
| 80 | + |
| 81 | + /** |
| 82 | + * Obtain the {@link HttpHeaders} to be used by default. |
| 83 | + * |
| 84 | + * @return the {@link HttpHeaders} to be used by default. |
| 85 | + */ |
| 86 | + HttpHeaders getDefaultHeaders(); |
| 87 | + |
| 88 | + /** |
| 89 | + * Returns {@literal true} when the client should use SSL. |
| 90 | + * |
| 91 | + * @return {@literal true} when the client should use SSL. |
| 92 | + */ |
| 93 | + boolean useSsl(); |
| 94 | + |
| 95 | + /** |
| 96 | + * Returns the {@link SSLContext} to use. Can be {@link Optional#empty()} if unconfigured. |
| 97 | + * |
| 98 | + * @return the {@link SSLContext} to use. Can be {@link Optional#empty()} if unconfigured. |
| 99 | + */ |
| 100 | + Optional<SSLContext> getSslContext(); |
| 101 | + |
| 102 | + /** |
| 103 | + * @author Christoph Strobl |
| 104 | + */ |
| 105 | + interface ClientConfigurationBuilderWithRequiredEndpoint { |
| 106 | + |
| 107 | + /** |
| 108 | + * @param hostAndPort the {@literal host} and {@literal port} formatted as String {@literal host:port}. |
| 109 | + * @return the {@link MaybeSecureClientConfigurationBuilder}. |
| 110 | + */ |
| 111 | + default MaybeSecureClientConfigurationBuilder connectedTo(String hostAndPort) { |
| 112 | + return connectedTo(new String[] { hostAndPort }); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * @param hostAndPorts the list of {@literal host} and {@literal port} combinations formatted as String |
| 117 | + * {@literal host:port}. |
| 118 | + * @return the {@link MaybeSecureClientConfigurationBuilder}. |
| 119 | + */ |
| 120 | + MaybeSecureClientConfigurationBuilder connectedTo(String... hostAndPorts); |
| 121 | + |
| 122 | + /** |
| 123 | + * @param endpoint the {@literal host} and {@literal port}. |
| 124 | + * @return the {@link MaybeSecureClientConfigurationBuilder}. |
| 125 | + */ |
| 126 | + default MaybeSecureClientConfigurationBuilder connectedTo(InetSocketAddress endpoint) { |
| 127 | + return connectedTo(new InetSocketAddress[] { endpoint }); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * @param endpoints the list of {@literal host} and {@literal port} combinations. |
| 132 | + * @return the {@link MaybeSecureClientConfigurationBuilder}. |
| 133 | + */ |
| 134 | + MaybeSecureClientConfigurationBuilder connectedTo(InetSocketAddress... endpoints); |
| 135 | + |
| 136 | + /** |
| 137 | + * Obviously for testing. |
| 138 | + * |
| 139 | + * @return the {@link MaybeSecureClientConfigurationBuilder}. |
| 140 | + */ |
| 141 | + default MaybeSecureClientConfigurationBuilder connectedToLocalhost() { |
| 142 | + return connectedTo("localhost:9200"); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * @author Christoph Strobl |
| 148 | + */ |
| 149 | + interface MaybeSecureClientConfigurationBuilder extends ClientConfigurationBuilderWithOptionalDefaultHeaders { |
| 150 | + |
| 151 | + /** |
| 152 | + * Connect via {@literal https} <br /> |
| 153 | + * <strong>NOTE</strong> You need to leave out the protocol in |
| 154 | + * {@link ClientConfigurationBuilderWithRequiredEndpoint#connectedTo(String)}. |
| 155 | + * |
| 156 | + * @return the {@link ClientConfigurationBuilderWithOptionalDefaultHeaders}. |
| 157 | + */ |
| 158 | + ClientConfigurationBuilderWithOptionalDefaultHeaders usingSsl(); |
| 159 | + |
| 160 | + /** |
| 161 | + * Connect via {@literal https} using the given {@link SSLContext}.<br /> |
| 162 | + * <strong>NOTE</strong> You need to leave out the protocol in |
| 163 | + * {@link ClientConfigurationBuilderWithRequiredEndpoint#connectedTo(String)}. |
| 164 | + * |
| 165 | + * @return the {@link ClientConfigurationBuilderWithOptionalDefaultHeaders}. |
| 166 | + */ |
| 167 | + ClientConfigurationBuilderWithOptionalDefaultHeaders usingSsl(SSLContext sslContext); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * @author Christoph Strobl |
| 172 | + * @author Mark Paluch |
| 173 | + */ |
| 174 | + interface ClientConfigurationBuilderWithOptionalDefaultHeaders { |
| 175 | + |
| 176 | + /** |
| 177 | + * @param defaultHeaders must not be {@literal null}. |
| 178 | + * @return the {@link ClientConfigurationBuilderWithOptionalDefaultHeaders} |
| 179 | + */ |
| 180 | + ClientConfigurationBuilderWithOptionalDefaultHeaders withDefaultHeaders(HttpHeaders defaultHeaders); |
| 181 | + |
| 182 | + /** |
| 183 | + * Build the {@link ClientConfiguration} object. |
| 184 | + * |
| 185 | + * @return the {@link ClientConfiguration} object. |
| 186 | + */ |
| 187 | + ClientConfiguration build(); |
| 188 | + } |
| 189 | +} |
0 commit comments