Skip to content

Commit 948d29b

Browse files
huw0sothawo
authored andcommitted
DATAES-650 - Add support for pathPrefix to configuration classes.
Original pull request: spring-projects#320.
1 parent b820c9a commit 948d29b

File tree

9 files changed

+115
-9
lines changed

9 files changed

+115
-9
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
*
3131
* @author Mark Paluch
3232
* @author Peter-Josef Meisch
33+
* @author Huw Ayling-Miller
3334
* @since 3.2
3435
*/
3536
public interface ClientConfiguration {
@@ -135,6 +136,14 @@ static ClientConfiguration create(InetSocketAddress socketAddress) {
135136
*/
136137
Duration getSocketTimeout();
137138

139+
/**
140+
* Returns the path prefix that should be prepended to HTTP(s) requests for Elasticsearch behind a proxy.
141+
*
142+
* @return the path prefix.
143+
* @since 4.0
144+
*/
145+
String getPathPrefix();
146+
138147
/**
139148
* @author Christoph Strobl
140149
*/
@@ -267,6 +276,15 @@ default TerminalClientConfigurationBuilder withSocketTimeout(long millis) {
267276
*/
268277
TerminalClientConfigurationBuilder withBasicAuth(String username, String password);
269278

279+
/**
280+
* Configure the path prefix that will be prepended to any HTTP(s) requests
281+
*
282+
* @param pathPrefix the pathPrefix.
283+
* @return the {@link TerminalClientConfigurationBuilder}
284+
* @since 4.0
285+
*/
286+
TerminalClientConfigurationBuilder withPathPrefix(String pathPrefix);
287+
270288
/**
271289
* Build the {@link ClientConfiguration} object.
272290
*

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
* @author Christoph Strobl
3838
* @author Mark Paluch
3939
* @author Peter-Josef Meisch
40+
* @author Huw Ayling-Miller
4041
* @since 3.2
4142
*/
4243
class ClientConfigurationBuilder
@@ -50,6 +51,7 @@ class ClientConfigurationBuilder
5051
private Duration soTimeout = Duration.ofSeconds(5);
5152
private String username;
5253
private String password;
54+
private String pathPrefix;
5355

5456
/*
5557
* (non-Javadoc)
@@ -156,6 +158,14 @@ public TerminalClientConfigurationBuilder withBasicAuth(String username, String
156158
return this;
157159
}
158160

161+
@Override
162+
public TerminalClientConfigurationBuilder withPathPrefix(String pathPrefix) {
163+
164+
this.pathPrefix = pathPrefix;
165+
166+
return this;
167+
}
168+
159169
/*
160170
* (non-Javadoc)
161171
* @see org.springframework.data.elasticsearch.client.ClientConfiguration.ClientConfigurationBuilderWithOptionalDefaultHeaders#build()
@@ -171,7 +181,7 @@ public ClientConfiguration build() {
171181
}
172182

173183
return new DefaultClientConfiguration(this.hosts, this.headers, this.useSsl, this.sslContext, this.soTimeout,
174-
this.connectTimeout);
184+
this.connectTimeout, this.pathPrefix);
175185
}
176186

177187
private static InetSocketAddress parse(String hostAndPort) {

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
*
3333
* @author Mark Paluch
3434
* @author Christoph Strobl
35+
* @author Huw Ayling-Miller
3536
* @since 3.2
3637
*/
3738
class DefaultClientConfiguration implements ClientConfiguration {
@@ -42,16 +43,18 @@ class DefaultClientConfiguration implements ClientConfiguration {
4243
private final @Nullable SSLContext sslContext;
4344
private final Duration soTimeout;
4445
private final Duration connectTimeout;
46+
private final String pathPrefix;
4547

4648
DefaultClientConfiguration(List<InetSocketAddress> hosts, HttpHeaders headers, boolean useSsl,
47-
@Nullable SSLContext sslContext, Duration soTimeout, Duration connectTimeout) {
49+
@Nullable SSLContext sslContext, Duration soTimeout, Duration connectTimeout, @Nullable String pathPrefix) {
4850

4951
this.hosts = Collections.unmodifiableList(new ArrayList<>(hosts));
5052
this.headers = new HttpHeaders(headers);
5153
this.useSsl = useSsl;
5254
this.sslContext = sslContext;
5355
this.soTimeout = soTimeout;
5456
this.connectTimeout = connectTimeout;
57+
this.pathPrefix = pathPrefix;
5558
}
5659

5760
/*
@@ -108,4 +111,13 @@ public Duration getSocketTimeout() {
108111
return this.soTimeout;
109112
}
110113

114+
/*
115+
* (non-Javadoc)
116+
* @see org.springframework.data.elasticsearch.client.ClientConfiguration#getPathPrefix()
117+
*/
118+
@Override
119+
public String getPathPrefix() {
120+
return this.pathPrefix;
121+
}
122+
111123
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
*
5353
* @author Christoph Strobl
5454
* @author Mark Paluch
55+
* @author Huw Ayling-Miller
5556
* @since 3.2
5657
*/
5758
public final class RestClients {
@@ -75,6 +76,11 @@ public static ElasticsearchRestClient create(ClientConfiguration clientConfigura
7576
HttpHost[] httpHosts = formattedHosts(clientConfiguration.getEndpoints(), clientConfiguration.useSsl()).stream()
7677
.map(HttpHost::create).toArray(HttpHost[]::new);
7778
RestClientBuilder builder = RestClient.builder(httpHosts);
79+
80+
if (clientConfiguration.getPathPrefix() != null) {
81+
builder.setPathPrefix(clientConfiguration.getPathPrefix());
82+
}
83+
7884
HttpHeaders headers = clientConfiguration.getDefaultHeaders();
7985

8086
if (!headers.isEmpty()) {

src/main/java/org/springframework/data/elasticsearch/client/reactive/DefaultReactiveElasticsearchClient.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123
* @author Christoph Strobl
124124
* @author Mark Paluch
125125
* @author Peter-Josef Meisch
126+
* @author Huw Ayling-Miller
126127
* @since 3.2
127128
* @see ClientConfiguration
128129
* @see ReactiveRestClients
@@ -217,6 +218,10 @@ private static WebClientProvider getWebClientProvider(ClientConfiguration client
217218
ReactorClientHttpConnector connector = new ReactorClientHttpConnector(httpClient);
218219
WebClientProvider provider = WebClientProvider.create(scheme, connector);
219220

221+
if (clientConfiguration.getPathPrefix() != null) {
222+
provider = provider.withPathPrefix(clientConfiguration.getPathPrefix());
223+
}
224+
220225
return provider.withDefaultHeaders(clientConfiguration.getDefaultHeaders());
221226
}
222227

src/main/java/org/springframework/data/elasticsearch/client/reactive/DefaultWebClientProvider.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
*
3333
* @author Mark Paluch
3434
* @author Christoph Strobl
35+
* @author Huw Ayling-Miller
3536
* @since 3.2
3637
*/
3738
class DefaultWebClientProvider implements WebClientProvider {
@@ -42,6 +43,7 @@ class DefaultWebClientProvider implements WebClientProvider {
4243
private final @Nullable ClientHttpConnector connector;
4344
private final Consumer<Throwable> errorListener;
4445
private final HttpHeaders headers;
46+
private final String pathPrefix;
4547

4648
/**
4749
* Create new {@link DefaultWebClientProvider} with empty {@link HttpHeaders} and no-op {@literal error listener}.
@@ -50,7 +52,7 @@ class DefaultWebClientProvider implements WebClientProvider {
5052
* @param connector can be {@literal null}.
5153
*/
5254
DefaultWebClientProvider(String scheme, @Nullable ClientHttpConnector connector) {
53-
this(scheme, connector, e -> {}, HttpHeaders.EMPTY);
55+
this(scheme, connector, e -> {}, HttpHeaders.EMPTY, null);
5456
}
5557

5658
/**
@@ -62,7 +64,7 @@ class DefaultWebClientProvider implements WebClientProvider {
6264
* @param headers must not be {@literal null}.
6365
*/
6466
private DefaultWebClientProvider(String scheme, @Nullable ClientHttpConnector connector,
65-
Consumer<Throwable> errorListener, HttpHeaders headers) {
67+
Consumer<Throwable> errorListener, HttpHeaders headers, @Nullable String pathPrefix) {
6668

6769
Assert.notNull(scheme, "Scheme must not be null! A common scheme would be 'http'.");
6870
Assert.notNull(errorListener, "ErrorListener must not be null! You may want use a no-op one 'e -> {}' instead.");
@@ -73,6 +75,7 @@ private DefaultWebClientProvider(String scheme, @Nullable ClientHttpConnector co
7375
this.connector = connector;
7476
this.errorListener = errorListener;
7577
this.headers = headers;
78+
this.pathPrefix = pathPrefix;
7679
}
7780

7881
/*
@@ -109,7 +112,7 @@ public WebClientProvider withDefaultHeaders(HttpHeaders headers) {
109112
merged.addAll(this.headers);
110113
merged.addAll(headers);
111114

112-
return new DefaultWebClientProvider(this.scheme, this.connector, errorListener, merged);
115+
return new DefaultWebClientProvider(this.scheme, this.connector, errorListener, merged, this.pathPrefix);
113116
}
114117

115118
/*
@@ -121,6 +124,15 @@ public Consumer<Throwable> getErrorListener() {
121124
return this.errorListener;
122125
}
123126

127+
/*
128+
* (non-Javadoc)
129+
* @see org.springframework.data.elasticsearch.client.reactive.WebClientProvider#getPathPrefix()
130+
*/
131+
@Override
132+
public String getPathPrefix() {
133+
return pathPrefix;
134+
}
135+
124136
/*
125137
* (non-Javadoc)
126138
* @see org.springframework.data.elasticsearch.client.reactive.WebClientProvider#withErrorListener(java.util.function.Consumer)
@@ -131,7 +143,18 @@ public WebClientProvider withErrorListener(Consumer<Throwable> errorListener) {
131143
Assert.notNull(errorListener, "Error listener must not be null.");
132144

133145
Consumer<Throwable> listener = this.errorListener.andThen(errorListener);
134-
return new DefaultWebClientProvider(this.scheme, this.connector, listener, this.headers);
146+
return new DefaultWebClientProvider(this.scheme, this.connector, listener, this.headers, this.pathPrefix);
147+
}
148+
149+
/*
150+
* (non-Javadoc)
151+
* @see org.springframework.data.elasticsearch.client.reactive.WebClientProvider#withPathPrefix(java.lang.String)
152+
*/
153+
@Override
154+
public WebClientProvider withPathPrefix(String pathPrefix) {
155+
Assert.notNull(pathPrefix, "pathPrefix must not be null.");
156+
157+
return new DefaultWebClientProvider(this.scheme, this.connector, this.errorListener, this.headers, pathPrefix);
135158
}
136159

137160
protected WebClient createWebClientForSocketAddress(InetSocketAddress socketAddress) {
@@ -142,7 +165,8 @@ protected WebClient createWebClientForSocketAddress(InetSocketAddress socketAddr
142165
builder = builder.clientConnector(connector);
143166
}
144167

145-
String baseUrl = String.format("%s://%s:%d", this.scheme, socketAddress.getHostString(), socketAddress.getPort());
168+
String baseUrl = String.format("%s://%s:%d%s", this.scheme, socketAddress.getHostString(), socketAddress.getPort(),
169+
pathPrefix == null ? "" : "/" + pathPrefix);
146170
return builder.baseUrl(baseUrl).filter((request, next) -> next.exchange(request).doOnError(errorListener)).build();
147171
}
148172
}

src/main/java/org/springframework/data/elasticsearch/client/reactive/WebClientProvider.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
*
3535
* @author Christoph Strobl
3636
* @author Mark Paluch
37+
* @author Huw Ayling-Miller
3738
* @since 3.2
3839
*/
3940
public interface WebClientProvider {
@@ -96,6 +97,14 @@ static WebClientProvider create(String scheme, @Nullable ClientHttpConnector con
9697
*/
9798
Consumer<Throwable> getErrorListener();
9899

100+
/**
101+
* Obtain the {@link String pathPrefix} to be used.
102+
*
103+
* @return the pathPrefix if set.
104+
* @since 4.0
105+
*/
106+
String getPathPrefix();
107+
99108
/**
100109
* Create a new instance of {@link WebClientProvider} applying the given headers by default.
101110
*
@@ -111,4 +120,13 @@ static WebClientProvider create(String scheme, @Nullable ClientHttpConnector con
111120
* @return new instance of {@link WebClientProvider}.
112121
*/
113122
WebClientProvider withErrorListener(Consumer<Throwable> errorListener);
123+
124+
/**
125+
* Create a new instance of {@link WebClientProvider} where HTTP requests are called with the given path prefix.
126+
*
127+
* @param pathPrefix Path prefix to add to requests
128+
* @return new instance of {@link WebClientProvider}
129+
* @since 4.0
130+
*/
131+
WebClientProvider withPathPrefix(String pathPrefix);
114132
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
*
3232
* @author Mark Paluch
3333
* @author Peter-Josef Meisch
34+
* @author Huw Ayling-Miller
3435
*/
3536
public class ClientConfigurationUnitTests {
3637

@@ -42,7 +43,7 @@ public void shouldCreateSimpleConfiguration() {
4243
assertThat(clientConfiguration.getEndpoints()).containsOnly(InetSocketAddress.createUnresolved("localhost", 9200));
4344
}
4445

45-
@Test // DATAES-488, DATAES-504
46+
@Test // DATAES-488, DATAES-504, DATAES-650
4647
public void shouldCreateCustomizedConfiguration() {
4748

4849
HttpHeaders headers = new HttpHeaders();
@@ -52,14 +53,16 @@ public void shouldCreateCustomizedConfiguration() {
5253
.connectedTo("foo", "bar") //
5354
.usingSsl() //
5455
.withDefaultHeaders(headers) //
55-
.withConnectTimeout(Duration.ofDays(1)).withSocketTimeout(Duration.ofDays(2)).build();
56+
.withConnectTimeout(Duration.ofDays(1)).withSocketTimeout(Duration.ofDays(2)) //
57+
.withPathPrefix("myPathPrefix").build();
5658

5759
assertThat(clientConfiguration.getEndpoints()).containsOnly(InetSocketAddress.createUnresolved("foo", 9200),
5860
InetSocketAddress.createUnresolved("bar", 9200));
5961
assertThat(clientConfiguration.useSsl()).isTrue();
6062
assertThat(clientConfiguration.getDefaultHeaders().get("foo")).containsOnly("bar");
6163
assertThat(clientConfiguration.getConnectTimeout()).isEqualTo(Duration.ofDays(1));
6264
assertThat(clientConfiguration.getSocketTimeout()).isEqualTo(Duration.ofDays(2));
65+
assertThat(clientConfiguration.getPathPrefix()).isEqualTo("myPathPrefix");
6366
}
6467

6568
@Test // DATAES-488, DATAES-504

src/test/java/org/springframework/data/elasticsearch/client/reactive/ReactiveMockClientTestsUtils.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,16 @@ public Consumer<Throwable> getErrorListener() {
263263
public WebClientProvider withErrorListener(Consumer<Throwable> errorListener) {
264264
throw new UnsupportedOperationException();
265265
}
266+
267+
@Override
268+
public String getPathPrefix() {
269+
return null;
270+
}
271+
272+
@Override
273+
public WebClientProvider withPathPrefix(String pathPrefix) {
274+
throw new UnsupportedOperationException();
275+
}
266276

267277
public Send when(String host) {
268278
InetSocketAddress inetSocketAddress = getInetSocketAddress(host);

0 commit comments

Comments
 (0)