diff --git a/README.md b/README.md index e4a17b4..5295407 100644 --- a/README.md +++ b/README.md @@ -185,6 +185,16 @@ By default both the Java and Android clients use an `ExecutorService` to perform Note that once you've shut down the publish executor for a given client, there is no way to restart or replace that executor. You will need to build a new client. +### Using an HTTP proxy + +The KeenClient supports HTTP proxies via the `setProxy(String proxyHost, int proxyPort)` and `setProxy(Proxy proxy)` methods of a `KeenClient` instance. Simply use one of those methods after building a client like so: + +```java +KeenClient client = new JavaKeenClientBuilder().build(); +client.setProxy("secureproxy.example.com", 2570); +// now use the client object as you normally would +``` + ## Working with the Source ### Using IntelliJ IDEA or Android Studio diff --git a/core/src/main/java/io/keen/client/java/KeenClient.java b/core/src/main/java/io/keen/client/java/KeenClient.java index a4a53fa..5667190 100644 --- a/core/src/main/java/io/keen/client/java/KeenClient.java +++ b/core/src/main/java/io/keen/client/java/KeenClient.java @@ -5,6 +5,8 @@ import java.io.OutputStreamWriter; import java.io.StringReader; import java.io.StringWriter; +import java.net.InetSocketAddress; +import java.net.Proxy; import java.net.URL; import java.text.DateFormat; import java.text.SimpleDateFormat; @@ -585,6 +587,34 @@ public boolean isActive() { return isActive; } + /** + * Sets an HTTP proxy server configuration for this client. + * + * @param proxyHost The proxy hostname or IP address. + * @param proxyPort The proxy port number. + */ + public void setProxy(String proxyHost, int proxyPort) { + this.proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)); + } + + /** + * Sets an HTTP proxy server configuration for this client. + * + * @param proxy The Proxy object to set. + */ + public void setProxy(Proxy proxy) { + this.proxy = proxy; + } + + /** + * Gets the client Proxy. + * + * @return the proxy. + */ + public Proxy getProxy() { + return proxy; + } + ///// PROTECTED ABSTRACT BUILDER IMPLEMENTATION ///// /** @@ -983,6 +1013,7 @@ private enum ClientSingleton { private String baseUrl; private GlobalPropertiesEvaluator globalPropertiesEvaluator; private Map globalProperties; + private Proxy proxy; ///// PRIVATE METHODS ///// @@ -1195,7 +1226,7 @@ public void writeTo(OutputStream out) throws IOException { // Send the request. String writeKey = project.getWriteKey(); - Request request = new Request(url, "POST", writeKey, source); + Request request = new Request(url, "POST", writeKey, source, proxy); Response response = httpHandler.execute(request); // If logging is enabled, log the response. diff --git a/core/src/main/java/io/keen/client/java/http/Request.java b/core/src/main/java/io/keen/client/java/http/Request.java index d525765..480da0b 100644 --- a/core/src/main/java/io/keen/client/java/http/Request.java +++ b/core/src/main/java/io/keen/client/java/http/Request.java @@ -1,5 +1,6 @@ package io.keen.client.java.http; +import java.net.Proxy; import java.net.URL; /** @@ -16,14 +17,20 @@ public final class Request { public final String method; public final String authorization; public final OutputSource body; + public final Proxy proxy; ///// PUBLIC CONSTRUCTORS ///// public Request(URL url, String method, String authorization, OutputSource body) { + this(url, method, authorization, body, null); + } + + public Request(URL url, String method, String authorization, OutputSource body, Proxy proxy) { this.url = url; this.method = method; this.authorization = authorization; this.body = body; + this.proxy = proxy; } -} \ No newline at end of file +} diff --git a/core/src/main/java/io/keen/client/java/http/UrlConnectionHttpHandler.java b/core/src/main/java/io/keen/client/java/http/UrlConnectionHttpHandler.java index 42df6d8..5b2609e 100644 --- a/core/src/main/java/io/keen/client/java/http/UrlConnectionHttpHandler.java +++ b/core/src/main/java/io/keen/client/java/http/UrlConnectionHttpHandler.java @@ -43,7 +43,12 @@ public Response execute(Request request) throws IOException { * @throws IOException If there is an error opening the connection. */ protected HttpURLConnection openConnection(Request request) throws IOException { - HttpURLConnection result = (HttpURLConnection) request.url.openConnection(); + HttpURLConnection result; + if (request.proxy != null) { + result = (HttpURLConnection) request.url.openConnection(request.proxy); + } else { + result = (HttpURLConnection) request.url.openConnection(); + } result.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT); result.setReadTimeout(DEFAULT_READ_TIMEOUT); return result; diff --git a/core/src/test/java/io/keen/client/java/KeenClientTest.java b/core/src/test/java/io/keen/client/java/KeenClientTest.java index b6a4a07..0ea09d1 100644 --- a/core/src/test/java/io/keen/client/java/KeenClientTest.java +++ b/core/src/test/java/io/keen/client/java/KeenClientTest.java @@ -596,4 +596,16 @@ public void onFailure(Exception e) { } } + @Test + public void testProxy() throws Exception { + KeenClient testClient = new TestKeenClientBuilder().build(); + + testClient.setProxy("1.2.3.4", 1234); + assertNotNull(testClient.getProxy()); + assertEquals("/1.2.3.4:1234", testClient.getProxy().address().toString()); + + testClient.setProxy(null); + assertNull(testClient.getProxy()); + } + }