Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/main/java/io/keen/client/java/KeenConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.keen.client.java;

import java.net.InetSocketAddress;
import java.net.Proxy;

/**
* KeenConfig
*
Expand All @@ -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;
}
}
16 changes: 15 additions & 1 deletion src/main/java/io/keen/client/java/KeenHttpRequestRunnable.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -21,12 +22,18 @@ class KeenHttpRequestRunnable implements Runnable {
private final String eventCollection;
private final Map<String, Object> event;
private final AddEventCallback callback;
private final Proxy proxy;

KeenHttpRequestRunnable(KeenClient keenClient, String eventCollection, Map<String, Object> event, AddEventCallback callback) {
this(keenClient,eventCollection,event,callback,KeenConfig.getProxy());
}

KeenHttpRequestRunnable(KeenClient keenClient, String eventCollection, Map<String, Object> event, AddEventCallback callback, Proxy proxy) {
this.keenClient = keenClient;
this.eventCollection = eventCollection;
this.event = event;
this.callback = callback;
this.proxy = proxy;
}

@Override
Expand Down Expand Up @@ -56,7 +63,7 @@ HttpURLConnection sendEvent(String eventCollection, Map<String, Object> 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");
Expand All @@ -72,6 +79,13 @@ HttpURLConnection sendEvent(String eventCollection, Map<String, Object> 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
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/io/keen/client/java/KeenConfigTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
62 changes: 62 additions & 0 deletions src/test/java/io/keen/client/java/KeenHttpRequestRunnableTest.java
Original file line number Diff line number Diff line change
@@ -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;
}
};
}