Skip to content

Commit 03fd603

Browse files
authored
DATAES-700 - Enable proxy support for RestClient.
Original PR: spring-projects#348
1 parent 35e9b69 commit 03fd603

File tree

7 files changed

+111
-9
lines changed

7 files changed

+111
-9
lines changed

pom.xml

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,24 @@
249249
<scope>test</scope>
250250
</dependency>
251251

252+
<dependency>
253+
<groupId>com.github.tomakehurst</groupId>
254+
<artifactId>wiremock-jre8</artifactId>
255+
<version>2.25.1</version>
256+
<scope>test</scope>
257+
<exclusions>
258+
<!-- these exclusions are needed because of Elasticsearch JarHell-->
259+
<exclusion>
260+
<groupId>commons-logging</groupId>
261+
<artifactId>commons-logging</artifactId>
262+
</exclusion>
263+
<exclusion>
264+
<groupId>org.ow2.asm</groupId>
265+
<artifactId>asm</artifactId>
266+
</exclusion>
267+
</exclusions>
268+
</dependency>
269+
252270
<!-- Upgrade xbean to 4.5 to prevent incompatibilities due to ASM versions -->
253271
<dependency>
254272
<groupId>org.apache.xbean</groupId>
@@ -259,8 +277,8 @@
259277

260278
<dependency>
261279
<groupId>javax.servlet</groupId>
262-
<artifactId>servlet-api</artifactId>
263-
<version>3.0-alpha-1</version>
280+
<artifactId>javax.servlet-api</artifactId>
281+
<version>3.1.0</version>
264282
<scope>test</scope>
265283
</dependency>
266284

src/main/java/org/springframework/data/elasticsearch/client/ClientConfiguration.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,14 @@ static ClientConfiguration create(InetSocketAddress socketAddress) {
153153
*/
154154
String getPathPrefix();
155155

156+
/**
157+
* returns an optionally set proxy in the form host:port
158+
*
159+
* @return the optional proxy
160+
* @since 4.0
161+
*/
162+
Optional<String> getProxy();
163+
156164
/**
157165
* @author Christoph Strobl
158166
*/
@@ -221,8 +229,8 @@ interface MaybeSecureClientConfigurationBuilder extends TerminalClientConfigurat
221229
TerminalClientConfigurationBuilder usingSsl(SSLContext sslContext);
222230

223231
/**
224-
* Connect via {@literal https} using the givens {@link SSLContext} and HostnameVerifier {@link HostnameVerifier} .<br />
225-
*
232+
* Connect via {@literal https} using the givens {@link SSLContext} and HostnameVerifier {@link HostnameVerifier}
233+
* .<br />
226234
* <strong>NOTE</strong> You need to leave out the protocol in
227235
* {@link ClientConfigurationBuilderWithRequiredEndpoint#connectedTo(String)}.
228236
*
@@ -304,6 +312,12 @@ default TerminalClientConfigurationBuilder withSocketTimeout(long millis) {
304312
*/
305313
TerminalClientConfigurationBuilder withPathPrefix(String pathPrefix);
306314

315+
/**
316+
* @param proxy a proxy formatted as String {@literal host:port}.
317+
* @return the {@link MaybeSecureClientConfigurationBuilder}.
318+
*/
319+
MaybeSecureClientConfigurationBuilder withProxy(String proxy);
320+
307321
/**
308322
* Build the {@link ClientConfiguration} object.
309323
*

src/main/java/org/springframework/data/elasticsearch/client/ClientConfigurationBuilder.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class ClientConfigurationBuilder
5555
private String username;
5656
private String password;
5757
private String pathPrefix;
58+
private String proxy;
5859

5960
/*
6061
* (non-Javadoc)
@@ -83,6 +84,13 @@ public MaybeSecureClientConfigurationBuilder connectedTo(InetSocketAddress... en
8384
return this;
8485
}
8586

87+
@Override
88+
public MaybeSecureClientConfigurationBuilder withProxy(String proxy) {
89+
Assert.hasLength(proxy, "proxy must not be null or empty");
90+
this.proxy = proxy;
91+
return this;
92+
}
93+
8694
/*
8795
* (non-Javadoc)
8896
* @see org.springframework.data.elasticsearch.client.ClientConfiguration.MaybeSecureClientConfigurationBuilder#usingSsl()
@@ -199,8 +207,8 @@ public ClientConfiguration build() {
199207
headers.setBasicAuth(username, password);
200208
}
201209

202-
return new DefaultClientConfiguration(this.hosts, this.headers, this.useSsl, this.sslContext, this.soTimeout,
203-
this.connectTimeout, this.pathPrefix, this.hostnameVerifier);
210+
return new DefaultClientConfiguration(hosts, headers, useSsl, sslContext, soTimeout, connectTimeout, pathPrefix,
211+
hostnameVerifier, proxy);
204212
}
205213

206214
private static InetSocketAddress parse(String hostAndPort) {

src/main/java/org/springframework/data/elasticsearch/client/DefaultClientConfiguration.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,11 @@ class DefaultClientConfiguration implements ClientConfiguration {
4646
private final Duration connectTimeout;
4747
private final String pathPrefix;
4848
private final @Nullable HostnameVerifier hostnameVerifier;
49+
private final String proxy;
4950

5051
DefaultClientConfiguration(List<InetSocketAddress> hosts, HttpHeaders headers, boolean useSsl,
51-
@Nullable SSLContext sslContext, Duration soTimeout, Duration connectTimeout, @Nullable String pathPrefix, @Nullable HostnameVerifier hostnameVerifier) {
52+
@Nullable SSLContext sslContext, Duration soTimeout, Duration connectTimeout, @Nullable String pathPrefix,
53+
@Nullable HostnameVerifier hostnameVerifier, String proxy) {
5254

5355
this.hosts = Collections.unmodifiableList(new ArrayList<>(hosts));
5456
this.headers = new HttpHeaders(headers);
@@ -58,6 +60,7 @@ class DefaultClientConfiguration implements ClientConfiguration {
5860
this.connectTimeout = connectTimeout;
5961
this.pathPrefix = pathPrefix;
6062
this.hostnameVerifier = hostnameVerifier;
63+
this.proxy = proxy;
6164
}
6265

6366
/*
@@ -132,4 +135,12 @@ public String getPathPrefix() {
132135
return this.pathPrefix;
133136
}
134137

138+
/*
139+
* (non-Javadoc)
140+
* @see org.springframework.data.elasticsearch.client.ClientConfiguration#getProxy()
141+
*/
142+
@Override
143+
public Optional<String> getProxy() {
144+
return Optional.ofNullable(proxy);
145+
}
135146
}

src/main/java/org/springframework/data/elasticsearch/client/RestClients.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ public static ElasticsearchRestClient create(ClientConfiguration clientConfigura
124124

125125
clientBuilder.setDefaultRequestConfig(requestConfigBuilder.build());
126126

127+
clientConfiguration.getProxy().map(HttpHost::create).ifPresent(clientBuilder::setProxy);
128+
127129
return clientBuilder;
128130
});
129131

src/test/java/org/springframework/data/elasticsearch/client/ClientConfigurationUnitTests.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void shouldCreateSimpleConfiguration() {
4545
assertThat(clientConfiguration.getEndpoints()).containsOnly(InetSocketAddress.createUnresolved("localhost", 9200));
4646
}
4747

48-
@Test // DATAES-488, DATAES-504, DATAES-650
48+
@Test // DATAES-488, DATAES-504, DATAES-650, DATAES-700
4949
public void shouldCreateCustomizedConfiguration() {
5050

5151
HttpHeaders headers = new HttpHeaders();
@@ -56,7 +56,8 @@ public void shouldCreateCustomizedConfiguration() {
5656
.usingSsl() //
5757
.withDefaultHeaders(headers) //
5858
.withConnectTimeout(Duration.ofDays(1)).withSocketTimeout(Duration.ofDays(2)) //
59-
.withPathPrefix("myPathPrefix").build();
59+
.withPathPrefix("myPathPrefix") //
60+
.withProxy("localhost:8080").build();
6061

6162
assertThat(clientConfiguration.getEndpoints()).containsOnly(InetSocketAddress.createUnresolved("foo", 9200),
6263
InetSocketAddress.createUnresolved("bar", 9200));
@@ -65,6 +66,7 @@ public void shouldCreateCustomizedConfiguration() {
6566
assertThat(clientConfiguration.getConnectTimeout()).isEqualTo(Duration.ofDays(1));
6667
assertThat(clientConfiguration.getSocketTimeout()).isEqualTo(Duration.ofDays(2));
6768
assertThat(clientConfiguration.getPathPrefix()).isEqualTo("myPathPrefix");
69+
assertThat(clientConfiguration.getProxy()).contains("localhost:8080");
6870
}
6971

7072
@Test // DATAES-488, DATAES-504
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.springframework.data.elasticsearch.client;
2+
3+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
4+
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
5+
6+
import java.io.IOException;
7+
8+
import org.elasticsearch.client.RequestOptions;
9+
import org.elasticsearch.client.RestHighLevelClient;
10+
import org.junit.jupiter.api.Test;
11+
12+
import com.github.tomakehurst.wiremock.WireMockServer;
13+
import com.github.tomakehurst.wiremock.client.WireMock;
14+
15+
/**
16+
* @author Peter-Josef Meisch
17+
*/
18+
public class RestClientsTest {
19+
20+
@Test // DATAES-700
21+
void shouldUseConfiguredProxy() throws IOException {
22+
23+
WireMockServer wireMockServer = new WireMockServer(options() //
24+
.dynamicPort() //
25+
.usingFilesUnderDirectory("src/test/resources/wiremock-mappings")); // needed, otherwise Wiremock goes to
26+
// test/resources/mappings
27+
wireMockServer.start();
28+
try {
29+
WireMock.configureFor(wireMockServer.port());
30+
31+
ClientConfigurationBuilder configurationBuilder = new ClientConfigurationBuilder();
32+
ClientConfiguration clientConfiguration = configurationBuilder //
33+
.connectedTo("localhost:9200")//
34+
.withProxy("localhost:" + wireMockServer.port()) //
35+
.build();
36+
37+
RestHighLevelClient restClient = RestClients.create(clientConfiguration).rest();
38+
restClient.ping(RequestOptions.DEFAULT);
39+
40+
verify(headRequestedFor(urlEqualTo("/")));
41+
42+
} finally {
43+
wireMockServer.shutdown();
44+
}
45+
}
46+
47+
}

0 commit comments

Comments
 (0)