Skip to content

Commit 390d7e8

Browse files
mp911dechristophstrobl
authored andcommitted
DATAES-488 - Polishing.
Convert spaces to tabs for pom.xml. Switch reactive dependencies to optional. Remove unused commonscollections property. Use managed versions for reactor and Spring dependencies. Introduce WebClientProvider to avoid reinstantiation of WebClient instances. Introduce ClientConfiguration to encapsulate common Elasticsearch client configuration properties. Split ElasticsearchClients into RestClients and ReactiveRestClients to avoid mandatory dependency on WebFlux/Project Reactor. Adapt tests and code referring to WebClient creation. Extract response body as byte array instead of Flux of DataBuffer to avoid chunking and to parse an entire response. Encapsulate hostAndPort string used across configuration/HostProvider with InetSocketAddress. Add parser for InetSocketAddress. Original Pull Request: spring-projects#226
1 parent 691a8c5 commit 390d7e8

29 files changed

+1952
-863
lines changed

pom.xml

Lines changed: 283 additions & 275 deletions
Large diffs are not rendered by default.
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
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+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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.ArrayList;
20+
import java.util.Arrays;
21+
import java.util.List;
22+
import java.util.stream.Collectors;
23+
24+
import javax.net.ssl.SSLContext;
25+
26+
import org.springframework.data.elasticsearch.client.ClientConfiguration.ClientConfigurationBuilderWithOptionalDefaultHeaders;
27+
import org.springframework.data.elasticsearch.client.ClientConfiguration.ClientConfigurationBuilderWithRequiredEndpoint;
28+
import org.springframework.data.elasticsearch.client.ClientConfiguration.MaybeSecureClientConfigurationBuilder;
29+
import org.springframework.http.HttpHeaders;
30+
import org.springframework.lang.Nullable;
31+
import org.springframework.util.Assert;
32+
33+
/**
34+
* Default builder implementation for {@link ClientConfiguration}.
35+
*
36+
* @author Christoph Strobl
37+
* @author Mark Paluch
38+
* @since 4.0
39+
*/
40+
class ClientConfigurationBuilder
41+
implements ClientConfigurationBuilderWithRequiredEndpoint, MaybeSecureClientConfigurationBuilder {
42+
43+
private List<InetSocketAddress> hosts = new ArrayList<>();
44+
private HttpHeaders headers = HttpHeaders.EMPTY;
45+
private boolean useSsl;
46+
private @Nullable SSLContext sslContext;
47+
48+
/*
49+
* (non-Javadoc)
50+
* @see org.springframework.data.elasticsearch.client.ClientConfiguration.ClientConfigurationBuilderWithRequiredEndpoint#connectedTo(java.lang.String[])
51+
*/
52+
@Override
53+
public MaybeSecureClientConfigurationBuilder connectedTo(String... hostAndPorts) {
54+
55+
Assert.notEmpty(hostAndPorts, "At least one host is required");
56+
57+
this.hosts.addAll(Arrays.stream(hostAndPorts).map(ClientConfigurationBuilder::parse).collect(Collectors.toList()));
58+
return this;
59+
}
60+
61+
/*
62+
* (non-Javadoc)
63+
* @see org.springframework.data.elasticsearch.client.ClientConfiguration.ClientConfigurationBuilderWithRequiredEndpoint#connectedTo(java.net.InetSocketAddress[])
64+
*/
65+
@Override
66+
public MaybeSecureClientConfigurationBuilder connectedTo(InetSocketAddress... endpoints) {
67+
68+
Assert.notEmpty(endpoints, "At least one endpoint is required");
69+
70+
this.hosts.addAll(Arrays.asList(endpoints));
71+
72+
return this;
73+
}
74+
75+
/*
76+
* (non-Javadoc)
77+
* @see org.springframework.data.elasticsearch.client.ClientConfiguration.MaybeSecureClientConfigurationBuilder#usingSsl()
78+
*/
79+
@Override
80+
public ClientConfigurationBuilderWithOptionalDefaultHeaders usingSsl() {
81+
82+
this.useSsl = true;
83+
return this;
84+
}
85+
86+
/*
87+
* (non-Javadoc)
88+
* @see org.springframework.data.elasticsearch.client.ClientConfiguration.MaybeSecureClientConfigurationBuilder#usingSsl(javax.net.ssl.SSLContext)
89+
*/
90+
@Override
91+
public ClientConfigurationBuilderWithOptionalDefaultHeaders usingSsl(SSLContext sslContext) {
92+
93+
Assert.notNull(sslContext, "SSL Context must not be null");
94+
95+
this.useSsl = true;
96+
this.sslContext = sslContext;
97+
return this;
98+
}
99+
100+
/*
101+
* (non-Javadoc)
102+
* @see org.springframework.data.elasticsearch.client.ClientConfiguration.ClientConfigurationBuilderWithOptionalDefaultHeaders#withDefaultHeaders(org.springframework.http.HttpHeaders)
103+
*/
104+
@Override
105+
public ClientConfigurationBuilderWithOptionalDefaultHeaders withDefaultHeaders(HttpHeaders defaultHeaders) {
106+
107+
Assert.notNull(defaultHeaders, "Default HTTP headers must not be null");
108+
109+
this.headers = defaultHeaders;
110+
return this;
111+
}
112+
113+
/*
114+
* (non-Javadoc)
115+
* @see org.springframework.data.elasticsearch.client.ClientConfiguration.ClientConfigurationBuilderWithOptionalDefaultHeaders#build()
116+
*/
117+
@Override
118+
public ClientConfiguration build() {
119+
return new DefaultClientConfiguration(this.hosts, this.headers, this.useSsl, this.sslContext);
120+
}
121+
122+
private static InetSocketAddress parse(String hostAndPort) {
123+
return InetSocketAddressParser.parse(hostAndPort, ElasticsearchHost.DEFAULT_PORT);
124+
}
125+
}

0 commit comments

Comments
 (0)