diff --git a/src/main/java/io/keen/client/java/KeenConfig.java b/src/main/java/io/keen/client/java/KeenConfig.java index 9bd8453..f33d109 100644 --- a/src/main/java/io/keen/client/java/KeenConfig.java +++ b/src/main/java/io/keen/client/java/KeenConfig.java @@ -1,5 +1,8 @@ package io.keen.client.java; +import java.net.InetSocketAddress; +import java.net.Proxy; + /** * KeenConfig * @@ -10,9 +13,30 @@ */ public class KeenConfig { + private static Proxy proxy; /** * How many threads to use to send events to Keen IO. Change this to 0 if you want to manage your own threads. */ public static int NUM_THREADS_FOR_HTTP_REQUESTS = 3; + /** + * Call this to set a HTTP proxy server configuration for all Keen IO communication. + * + * @param proxyUrl The proxy hostname or IP address. + * @param proxyPort The proxy port number. + */ + public static void setProxy(String proxyUrl, int proxyPort) { + proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyUrl, proxyPort)); + } + + static Proxy getProxy() { + return proxy; + } + + /** + * Call this to clean the currently configured HTTP Proxy server configuration. + */ + public static void clearProxy() { + proxy = null; + } } diff --git a/src/main/java/io/keen/client/java/KeenHttpRequestRunnable.java b/src/main/java/io/keen/client/java/KeenHttpRequestRunnable.java index 2c4309b..d3ac1ff 100644 --- a/src/main/java/io/keen/client/java/KeenHttpRequestRunnable.java +++ b/src/main/java/io/keen/client/java/KeenHttpRequestRunnable.java @@ -4,6 +4,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; +import java.net.Proxy; import java.net.URL; import java.util.Map; @@ -21,12 +22,18 @@ class KeenHttpRequestRunnable implements Runnable { private final String eventCollection; private final Map event; private final AddEventCallback callback; + private final Proxy proxy; KeenHttpRequestRunnable(KeenClient keenClient, String eventCollection, Map event, AddEventCallback callback) { + this(keenClient,eventCollection,event,callback,KeenConfig.getProxy()); + } + + KeenHttpRequestRunnable(KeenClient keenClient, String eventCollection, Map event, AddEventCallback callback, Proxy proxy) { this.keenClient = keenClient; this.eventCollection = eventCollection; this.event = event; this.callback = callback; + this.proxy = proxy; } @Override @@ -56,7 +63,7 @@ HttpURLConnection sendEvent(String eventCollection, Map event) t URL url = new URL(urlString); // set up the POST - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + HttpURLConnection connection = openConnection(url); connection.setRequestMethod("POST"); connection.setRequestProperty("Accept", "application/json"); connection.setRequestProperty("Content-Type", "application/json"); @@ -72,6 +79,13 @@ HttpURLConnection sendEvent(String eventCollection, Map event) t return connection; } + HttpURLConnection openConnection(URL url) throws IOException { + if(proxy == null){ + return (HttpURLConnection) url.openConnection(); + } + return (HttpURLConnection)url.openConnection(proxy); + } + static void handleResult(InputStream input, int responseCode, AddEventCallback callback) { if (responseCode == 201) { // event add worked diff --git a/src/test/java/io/keen/client/java/KeenConfigTest.java b/src/test/java/io/keen/client/java/KeenConfigTest.java new file mode 100644 index 0000000..f55e1b9 --- /dev/null +++ b/src/test/java/io/keen/client/java/KeenConfigTest.java @@ -0,0 +1,37 @@ +package io.keen.client.java; + +import org.junit.After; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +public class KeenConfigTest { + + @After + public void tearDown() throws Exception { + KeenConfig.clearProxy(); + } + + @Test + public void proxyStartOffEmpty() throws Exception { + assertNull(KeenConfig.getProxy()); + } + + @Test + public void canConfigureProxyObject() throws Exception { + + KeenConfig.setProxy("foo", 8080); + + assertEquals("foo:8080", KeenConfig.getProxy().address().toString()); + } + + @Test + public void canClearProxy() throws Exception { + KeenConfig.setProxy("foo", 8080); + + KeenConfig.clearProxy(); + + assertNull(KeenConfig.getProxy()); + } +} diff --git a/src/test/java/io/keen/client/java/KeenHttpRequestRunnableTest.java b/src/test/java/io/keen/client/java/KeenHttpRequestRunnableTest.java new file mode 100644 index 0000000..d41a4fb --- /dev/null +++ b/src/test/java/io/keen/client/java/KeenHttpRequestRunnableTest.java @@ -0,0 +1,62 @@ +package io.keen.client.java; + +import org.junit.Test; + +import java.io.IOException; +import java.net.*; + +import static org.junit.Assert.*; + +public class KeenHttpRequestRunnableTest { + + private Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("/", 8080)); + + @Test + public void willUseProxyIfConfigured() throws Exception { + + KeenHttpRequestRunnable request = new KeenHttpRequestRunnable(null,null, null, null, proxy); + + MockHandler handler = new MockHandler(); + + URL url = new URL("foo", "bar", 99, "/foobar", handler); + + request.openConnection(url); + + assertTrue(handler.calledWithProxy); + assertEquals(proxy, handler.openedProxy); + } + + @Test + public void noProxyConfiguredWillNotUseAProxy() throws Exception { + + KeenHttpRequestRunnable request = new KeenHttpRequestRunnable(null,null, null, null, null); + + MockHandler handler = new MockHandler(); + + URL url = new URL("foo", "bar", 99, "/foobar", handler); + + request.openConnection(url); + + assertTrue(handler.calledWithoutProxy); + assertNull(handler.openedProxy); + } + + class MockHandler extends URLStreamHandler { + boolean calledWithProxy; + boolean calledWithoutProxy; + Proxy openedProxy; + + @Override + protected URLConnection openConnection(URL u) throws IOException { + calledWithoutProxy = true; + return null; + } + + @Override + protected URLConnection openConnection(URL u, Proxy p) throws IOException { + calledWithProxy = true; + openedProxy = p; + return null; + } + }; +}