From 05dad45172c9e344d22023832852ad6f40b522df Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 6 Jul 2016 14:43:30 +0200 Subject: [PATCH] Add proxy configuration for Jest client Closes gh-6332 --- .../jest/JestAutoConfiguration.java | 8 ++++ .../elasticsearch/jest/JestProperties.java | 39 +++++++++++++++++++ .../jest/JestAutoConfigurationTests.java | 15 +++++++ .../appendix-application-properties.adoc | 2 + 4 files changed, 64 insertions(+) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/jest/JestAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/jest/JestAutoConfiguration.java index 7335e865ee..4df795bf80 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/jest/JestAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/jest/JestAutoConfiguration.java @@ -20,6 +20,7 @@ import com.google.gson.Gson; import io.searchbox.client.JestClient; import io.searchbox.client.JestClientFactory; import io.searchbox.client.config.HttpClientConfig; +import org.apache.http.HttpHost; import org.springframework.beans.factory.ObjectProvider; 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.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.util.Assert; import org.springframework.util.StringUtils; /** @@ -69,6 +71,12 @@ public class JestAutoConfiguration { builder.defaultCredentials(this.properties.getUsername(), 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(); if (gson != null) { builder.gson(gson); diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/jest/JestProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/jest/JestProperties.java index d8bda7c6a3..9fcd257d35 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/jest/JestProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/jest/JestProperties.java @@ -55,6 +55,11 @@ public class JestProperties { */ private int readTimeout = 3000; + /** + * Proxy settings. + */ + private final Proxy proxy = new Proxy(); + public List getUris() { return this.uris; } @@ -95,4 +100,38 @@ public class JestProperties { 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; + } + + } + } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/jest/JestAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/jest/JestAutoConfigurationTests.java index 973d1bc6e2..3fb0d59305 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/jest/JestAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/jest/JestAutoConfigurationTests.java @@ -20,8 +20,11 @@ import com.google.gson.Gson; import io.searchbox.client.JestClient; import io.searchbox.client.http.JestHttpClient; import org.junit.After; +import org.junit.Rule; 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.test.util.EnvironmentTestUtils; import org.springframework.context.annotation.AnnotationConfigApplicationContext; @@ -38,6 +41,9 @@ import static org.mockito.Mockito.mock; */ public class JestAutoConfigurationTests { + @Rule + public ExpectedException thrown = ExpectedException.none(); + protected AnnotationConfigApplicationContext context; @After @@ -66,6 +72,15 @@ public class JestAutoConfigurationTests { 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) { load(null, environment); } diff --git a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index f1b8d0a2f5..8aac8dbee8 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -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]) spring.elasticsearch.jest.connection-timeout=3000 # Connection timeout in milliseconds. 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.uris=http://localhost:9200 # Comma-separated list of the Elasticsearch instances to use. spring.elasticsearch.jest.username= # Login user.