Add proxy configuration for Jest client

Closes gh-6332
pull/6323/merge
Stephane Nicoll 8 years ago
parent 5c44c77287
commit 05dad45172

@ -20,6 +20,7 @@ import com.google.gson.Gson;
import io.searchbox.client.JestClient; import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory; import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig; import io.searchbox.client.config.HttpClientConfig;
import org.apache.http.HttpHost;
import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureAfter;
@ -30,6 +31,7 @@ import org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
/** /**
@ -69,6 +71,12 @@ public class JestAutoConfiguration {
builder.defaultCredentials(this.properties.getUsername(), builder.defaultCredentials(this.properties.getUsername(),
this.properties.getPassword()); this.properties.getPassword());
} }
String proxyHost = this.properties.getProxy().getHost();
if (StringUtils.hasText(proxyHost)) {
Integer proxyPort = this.properties.getProxy().getPort();
Assert.notNull(proxyPort, "Proxy port must not be null");
builder.proxy(new HttpHost(proxyHost, proxyPort));
}
Gson gson = this.gsonProvider.getIfUnique(); Gson gson = this.gsonProvider.getIfUnique();
if (gson != null) { if (gson != null) {
builder.gson(gson); builder.gson(gson);

@ -55,6 +55,11 @@ public class JestProperties {
*/ */
private int readTimeout = 3000; private int readTimeout = 3000;
/**
* Proxy settings.
*/
private final Proxy proxy = new Proxy();
public List<String> getUris() { public List<String> getUris() {
return this.uris; return this.uris;
} }
@ -95,4 +100,38 @@ public class JestProperties {
this.readTimeout = readTimeout; this.readTimeout = readTimeout;
} }
public Proxy getProxy() {
return this.proxy;
}
public static class Proxy {
/**
* Proxy host the HTTP client should use.
*/
private String host;
/**
* Proxy port the HTTP client should use.
*/
private Integer port;
public String getHost() {
return this.host;
}
public void setHost(String host) {
this.host = host;
}
public Integer getPort() {
return this.port;
}
public void setPort(Integer port) {
this.port = port;
}
}
} }

@ -20,8 +20,11 @@ import com.google.gson.Gson;
import io.searchbox.client.JestClient; import io.searchbox.client.JestClient;
import io.searchbox.client.http.JestHttpClient; import io.searchbox.client.http.JestHttpClient;
import org.junit.After; import org.junit.After;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration; import org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration;
import org.springframework.boot.test.util.EnvironmentTestUtils; import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@ -38,6 +41,9 @@ import static org.mockito.Mockito.mock;
*/ */
public class JestAutoConfigurationTests { public class JestAutoConfigurationTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
protected AnnotationConfigApplicationContext context; protected AnnotationConfigApplicationContext context;
@After @After
@ -66,6 +72,15 @@ public class JestAutoConfigurationTests {
assertThat(client.getGson()).isSameAs(this.context.getBean("customGson")); assertThat(client.getGson()).isSameAs(this.context.getBean("customGson"));
} }
@Test
public void proxyHostWithoutPort() {
this.thrown.expect(BeanCreationException.class);
this.thrown.expectMessage("Proxy port must not be null");
load("spring.elasticsearch.jest.uris=http://localhost:9200",
"spring.elasticsearch.jest.proxy.host=proxy.example.com");
}
private void load(String... environment) { private void load(String... environment) {
load(null, environment); load(null, environment);
} }

@ -634,6 +634,8 @@ content into your application; rather pick only the properties that you need.
# JEST (Elasticsearch HTTP client) ({sc-spring-boot-autoconfigure}/jest/JestProperties.{sc-ext}[JestProperties]) # JEST (Elasticsearch HTTP client) ({sc-spring-boot-autoconfigure}/jest/JestProperties.{sc-ext}[JestProperties])
spring.elasticsearch.jest.connection-timeout=3000 # Connection timeout in milliseconds. spring.elasticsearch.jest.connection-timeout=3000 # Connection timeout in milliseconds.
spring.elasticsearch.jest.password= # Login password. spring.elasticsearch.jest.password= # Login password.
spring.elasticsearch.jest.proxy.host= # Proxy host the HTTP client should use.
spring.elasticsearch.jest.proxy.port= # Proxy port the HTTP client should use.
spring.elasticsearch.jest.read-timeout=3000 # Read timeout in milliseconds. spring.elasticsearch.jest.read-timeout=3000 # Read timeout in milliseconds.
spring.elasticsearch.jest.uris=http://localhost:9200 # Comma-separated list of the Elasticsearch instances to use. spring.elasticsearch.jest.uris=http://localhost:9200 # Comma-separated list of the Elasticsearch instances to use.
spring.elasticsearch.jest.username= # Login user. spring.elasticsearch.jest.username= # Login user.

Loading…
Cancel
Save