Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Commit 8cd80ec

Browse files
committed
Merge pull request #19 from launchdarkly/pk/proxy-support
added ability to pass in HTTP proxy config
2 parents bf2035c + 5e3c105 commit 8cd80ec

File tree

5 files changed

+116
-2
lines changed

5 files changed

+116
-2
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ repositories {
1111

1212
allprojects {
1313
group = 'com.launchdarkly'
14-
version = "0.9.0"
14+
version = "0.10.0-SNAPSHOT"
1515
sourceCompatibility = 1.6
1616
targetCompatibility = 1.6
1717
}

src/main/java/com/launchdarkly/client/EventProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class Consumer implements Runnable {
6161

6262
Consumer(LDConfig config) {
6363
this.config = config;
64-
client = HttpClients.createDefault();
64+
client = HttpClients.custom().setProxy(config.proxyHost).build();
6565
}
6666

6767
@Override

src/main/java/com/launchdarkly/client/LDClient.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ protected CloseableHttpClient createClient() {
8888
RequestConfig requestConfig = RequestConfig.custom()
8989
.setConnectTimeout(config.connectTimeout)
9090
.setSocketTimeout(config.socketTimeout)
91+
.setProxy(config.proxyHost)
9192
.build();
9293
client = CachingHttpClients.custom()
9394
.setCacheConfig(cacheConfig)

src/main/java/com/launchdarkly/client/LDConfig.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.launchdarkly.client;
22

3+
import org.apache.http.HttpHost;
34
import org.apache.http.client.methods.HttpGet;
45
import org.apache.http.client.methods.HttpPost;
56
import org.apache.http.client.utils.URIBuilder;
@@ -27,13 +28,15 @@ public final class LDConfig {
2728
final int connectTimeout;
2829
final int socketTimeout;
2930
final int flushInterval;
31+
final HttpHost proxyHost;
3032

3133
protected LDConfig(Builder builder) {
3234
this.baseURI = builder.baseURI;
3335
this.capacity = builder.capacity;
3436
this.connectTimeout = builder.connectTimeout;
3537
this.socketTimeout = builder.socketTimeout;
3638
this.flushInterval = builder.flushInterval;
39+
this.proxyHost = builder.proxyHost();
3740
}
3841

3942
/**
@@ -54,6 +57,9 @@ public static class Builder{
5457
private int socketTimeout = DEFAULT_SOCKET_TIMEOUT;
5558
private int capacity = DEFAULT_CAPACITY;
5659
private int flushInterval = DEFAULT_FLUSH_INTERVAL;
60+
private String proxyHost;
61+
private int proxyPort = -1;
62+
private String proxyScheme;
5763

5864
/**
5965
* Creates a builder with all configuration parameters set to the default
@@ -151,6 +157,63 @@ public Builder capacity(int capacity) {
151157
return this;
152158
}
153159

160+
/**
161+
* Set the host to use as an HTTP proxy for making connections to LaunchDarkly. If this is not set, but
162+
* {@link #proxyPort(int)} or {@link #proxyScheme(String)} are specified, this will default to <code>localhost</code>.
163+
* <p>
164+
* If none of {@link #proxyHost(String)}, {@link #proxyPort(int)} or {@link #proxyScheme(String)} are specified,
165+
* a proxy will not be used, and {@link LDClient} will connect to LaunchDarkly directly.
166+
* </p>
167+
* @param host
168+
* @return
169+
*/
170+
public Builder proxyHost(String host) {
171+
this.proxyHost = host;
172+
return this;
173+
}
174+
175+
/**
176+
* Set the port to use for an HTTP proxy for making connections to LaunchDarkly. If not set (but {@link #proxyHost(String)}
177+
* or {@link #proxyScheme(String)} are specified, the default port for the scheme will be used.
178+
*
179+
* <p>
180+
* If none of {@link #proxyHost(String)}, {@link #proxyPort(int)} or {@link #proxyScheme(String)} are specified,
181+
* a proxy will not be used, and {@link LDClient} will connect to LaunchDarkly directly.
182+
* </p>
183+
* @param port
184+
* @return
185+
*/
186+
public Builder proxyPort(int port) {
187+
this.proxyPort = port;
188+
return this;
189+
}
190+
191+
/**
192+
* Set the scheme to use for an HTTP proxy for making connections to LaunchDarkly. If not set (but {@link #proxyHost(String)}
193+
* or {@link #proxyPort(int)} are specified, the default <code>https</code> scheme will be used.
194+
*
195+
* <p>
196+
* If none of {@link #proxyHost(String)}, {@link #proxyPort(int)} or {@link #proxyScheme(String)} are specified,
197+
* a proxy will not be used, and {@link LDClient} will connect to LaunchDarkly directly.
198+
* </p>
199+
* @param scheme
200+
* @return
201+
*/
202+
public Builder proxyScheme(String scheme) {
203+
this.proxyScheme = scheme;
204+
return this;
205+
}
206+
207+
HttpHost proxyHost() {
208+
if (this.proxyHost == null && this.proxyPort == -1 && this.proxyScheme == null) {
209+
return null;
210+
} else {
211+
String hostname = this.proxyHost == null ? "localhost" : this.proxyHost;
212+
String scheme = this.proxyScheme == null ? "https" : this.proxyScheme;
213+
return new HttpHost(hostname, this.proxyPort, scheme);
214+
}
215+
}
216+
154217
/**
155218
* Build the configured {@link com.launchdarkly.client.LDConfig} object
156219
* @return the {@link com.launchdarkly.client.LDConfig} configured by this builder

src/test/java/com/launchdarkly/client/LDConfigTest.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.launchdarkly.client;
22

33
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNull;
45

56
import org.junit.Test;
67

@@ -31,4 +32,53 @@ public void testSocketTimeoutSpecifiedInMilliseconds() {
3132

3233
assertEquals(3000, config.socketTimeout);
3334
}
35+
36+
@Test
37+
public void testNoProxyConfigured() {
38+
LDConfig config = new LDConfig.Builder().build();
39+
40+
assertNull(config.proxyHost);
41+
}
42+
43+
@Test
44+
public void testOnlyProxyPortConfiguredHasPort() {
45+
LDConfig config = new LDConfig.Builder().proxyPort(1234).build();
46+
47+
assertEquals(1234, config.proxyHost.getPort());
48+
}
49+
50+
@Test
51+
public void testOnlyProxyPortConfiguredHasDefaultHost() {
52+
LDConfig config = new LDConfig.Builder().proxyPort(1234).build();
53+
54+
assertEquals("localhost", config.proxyHost.getHostName());
55+
}
56+
57+
@Test
58+
public void testOnlyProxyPortConfiguredHasDefaultScheme() {
59+
LDConfig config = new LDConfig.Builder().proxyPort(1234).build();
60+
61+
assertEquals("https", config.proxyHost.getSchemeName());
62+
}
63+
64+
@Test
65+
public void testOnlyProxyHostConfiguredHasDefaultPort() {
66+
LDConfig config = new LDConfig.Builder().proxyHost("myproxy.example.com").build();
67+
68+
assertEquals(-1, config.proxyHost.getPort());
69+
}
70+
71+
@Test
72+
public void testProxyHostConfiguredHasHost() {
73+
LDConfig config = new LDConfig.Builder().proxyHost("myproxy.example.com").build();
74+
75+
assertEquals("myproxy.example.com", config.proxyHost.getHostName());
76+
}
77+
78+
@Test
79+
public void testProxySchemeConfiguredHasScheme() {
80+
LDConfig config = new LDConfig.Builder().proxyScheme("http").build();
81+
82+
assertEquals("http", config.proxyHost.getSchemeName());
83+
}
3484
}

0 commit comments

Comments
 (0)