From 4613656e1fcfe44d61c29b27ad081ceb881dfdc1 Mon Sep 17 00:00:00 2001 From: Simon Murtha Smith Date: Wed, 8 Oct 2014 22:42:34 -0700 Subject: [PATCH 1/3] Add HTTP Proxy support. Lots of ideas and code from #13, thanks for the starting point! --- .../java/io/keen/client/java/KeenClient.java | 32 ++++++++++++++++++- .../io/keen/client/java/http/Request.java | 9 +++++- .../java/http/UrlConnectionHttpHandler.java | 7 +++- .../io/keen/client/java/KeenClientTest.java | 12 +++++++ 4 files changed, 57 insertions(+), 3 deletions(-) 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..1af6bd2 100644 --- a/core/src/main/java/io/keen/client/java/KeenClient.java +++ b/core/src/main/java/io/keen/client/java/KeenClient.java @@ -6,6 +6,8 @@ import java.io.StringReader; import java.io.StringWriter; import java.net.URL; +import java.net.Proxy; +import java.net.InetSocketAddress; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -50,6 +52,8 @@ */ public class KeenClient { + private Proxy proxy; + ///// PUBLIC STATIC METHODS ///// /** @@ -585,6 +589,32 @@ 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)); + } + + /** + * Gets the client Proxy. + * + * @return the proxy. + */ + public Proxy getProxy() { + return proxy; + } + + /** + * Clears the proxy, such that a proxy no longer be used. + */ + public void clearProxy() { + proxy = null; + } + ///// PROTECTED ABSTRACT BUILDER IMPLEMENTATION ///// /** @@ -1195,7 +1225,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..90e2318 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,6 +1,7 @@ package io.keen.client.java.http; import java.net.URL; +import java.net.Proxy; /** * Encapsulates an HTTP request. @@ -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..f8fd91f 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.clearProxy(); + assertNull(testClient.getProxy()); + } + } From bfb7cadf0ec8ed71d4ec15987f19a2ac48cbebde Mon Sep 17 00:00:00 2001 From: Simon Murtha Smith Date: Thu, 9 Oct 2014 19:39:52 -0700 Subject: [PATCH 2/3] Organize imports + variables, add setProxy(Proxy). --- .../java/io/keen/client/java/KeenClient.java | 23 ++++++++++--------- .../io/keen/client/java/http/Request.java | 2 +- .../io/keen/client/java/KeenClientTest.java | 2 +- 3 files changed, 14 insertions(+), 13 deletions(-) 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 1af6bd2..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,9 +5,9 @@ import java.io.OutputStreamWriter; import java.io.StringReader; import java.io.StringWriter; -import java.net.URL; -import java.net.Proxy; import java.net.InetSocketAddress; +import java.net.Proxy; +import java.net.URL; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -52,8 +52,6 @@ */ public class KeenClient { - private Proxy proxy; - ///// PUBLIC STATIC METHODS ///// /** @@ -599,6 +597,15 @@ 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. * @@ -608,13 +615,6 @@ public Proxy getProxy() { return proxy; } - /** - * Clears the proxy, such that a proxy no longer be used. - */ - public void clearProxy() { - proxy = null; - } - ///// PROTECTED ABSTRACT BUILDER IMPLEMENTATION ///// /** @@ -1013,6 +1013,7 @@ private enum ClientSingleton { private String baseUrl; private GlobalPropertiesEvaluator globalPropertiesEvaluator; private Map globalProperties; + private Proxy proxy; ///// PRIVATE METHODS ///// 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 90e2318..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,7 +1,7 @@ package io.keen.client.java.http; -import java.net.URL; import java.net.Proxy; +import java.net.URL; /** * Encapsulates an HTTP request. 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 f8fd91f..0ea09d1 100644 --- a/core/src/test/java/io/keen/client/java/KeenClientTest.java +++ b/core/src/test/java/io/keen/client/java/KeenClientTest.java @@ -604,7 +604,7 @@ public void testProxy() throws Exception { assertNotNull(testClient.getProxy()); assertEquals("/1.2.3.4:1234", testClient.getProxy().address().toString()); - testClient.clearProxy(); + testClient.setProxy(null); assertNull(testClient.getProxy()); } From 8726525f0aa24f96b35b7edfbcd9f153276f6cfa Mon Sep 17 00:00:00 2001 From: Simon Murtha Smith Date: Wed, 15 Oct 2014 18:21:49 -0400 Subject: [PATCH 3/3] Add proxy info to readme. --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) 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