Skip to content

Commit 8b96cc7

Browse files
committed
adds java.net.Proxy option to OpenTok constructor, fixes #53
1 parent 618edf2 commit 8b96cc7

File tree

2 files changed

+56
-7
lines changed

2 files changed

+56
-7
lines changed

src/main/java/com/opentok/OpenTok.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.io.IOException;
1111
import java.io.StringReader;
1212
import java.io.UnsupportedEncodingException;
13+
import java.net.Proxy;
1314
import java.util.Collection;
1415
import java.util.List;
1516
import java.util.Map;
@@ -50,6 +51,7 @@ public class OpenTok {
5051
.reader(Archive.class);
5152
static protected ObjectReader archiveListReader = new ObjectMapper()
5253
.reader(ArchiveList.class);
54+
static final String defaultApiUrl = "https://api.opentok.com";
5355

5456
/**
5557
* Creates an OpenTok object.
@@ -60,15 +62,25 @@ public class OpenTok {
6062
* page.)
6163
*/
6264
public OpenTok(int apiKey, String apiSecret) {
63-
this(apiKey, apiSecret, "https://api.opentok.com");
65+
this(apiKey, apiSecret, defaultApiUrl);
6466
}
6567

6668
public OpenTok(int apiKey, String apiSecret, String apiUrl) {
69+
this(apiKey, apiSecret, apiUrl, null);
70+
}
71+
72+
public OpenTok(int apiKey, String apiSecret, String apiUrl, Proxy proxy) {
6773
this.apiKey = apiKey;
6874
this.apiSecret = apiSecret.trim();
69-
this.client = new HttpClient.Builder(apiKey, apiSecret)
70-
.apiUrl(apiUrl)
71-
.build();
75+
if (apiUrl == null) {
76+
apiUrl = defaultApiUrl;
77+
}
78+
HttpClient.Builder clientBuilder = new HttpClient.Builder(apiKey, apiSecret)
79+
.apiUrl(apiUrl);
80+
if (proxy != null) {
81+
clientBuilder.proxy(proxy);
82+
}
83+
this.client = clientBuilder.build();
7284
}
7385

7486
/**

src/main/java/com/opentok/util/HttpClient.java

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
package com.opentok.util;
99

1010
import java.io.IOException;
11+
import java.net.InetSocketAddress;
12+
import java.net.Proxy;
13+
import java.net.SocketAddress;
1114
import java.util.Collection;
1215
import java.util.Map;
1316
import java.util.concurrent.ExecutionException;
@@ -285,6 +288,7 @@ public String deleteArchive(String archiveId) throws RequestException {
285288
public static class Builder {
286289
private final int apiKey;
287290
private final String apiSecret;
291+
private Proxy proxy;
288292
private String apiUrl;
289293
private AsyncHttpClientConfig config;
290294

@@ -298,15 +302,48 @@ public Builder apiUrl(String apiUrl) {
298302
return this;
299303
}
300304

305+
public Builder proxy(Proxy proxy) {
306+
this.proxy = proxy;
307+
return this;
308+
}
309+
301310
public HttpClient build() {
302-
this.config = new AsyncHttpClientConfig.Builder()
311+
AsyncHttpClientConfig.Builder configBuilder = new AsyncHttpClientConfig.Builder()
303312
.setUserAgent("Opentok-Java-SDK/" + Version.VERSION + " JRE/" + System.getProperty("java.version"))
304-
.addRequestFilter(new PartnerAuthRequestFilter(this.apiKey, this.apiSecret))
305-
.build();
313+
.addRequestFilter(new PartnerAuthRequestFilter(this.apiKey, this.apiSecret));
314+
if (this.proxy != null) {
315+
configBuilder.setProxyServer(createProxyServer(this.proxy));
316+
}
317+
this.config = configBuilder.build();
306318
// NOTE: not thread-safe, config could be modified by another thread here?
307319
HttpClient client = new HttpClient(this);
308320
return client;
309321
}
322+
323+
// credit: https://github.com/AsyncHttpClient/async-http-client/blob/b52a8de5d6a862b5d1652d62f87ce774cbcff156/src/main/java/com/ning/http/client/ProxyServer.java#L99-L127
324+
static ProxyServer createProxyServer(final Proxy proxy) {
325+
if (proxy.type().equals(Proxy.Type.DIRECT)) {
326+
return null;
327+
}
328+
329+
if (!proxy.type().equals(Proxy.Type.HTTP)) {
330+
throw new IllegalArgumentException("Only DIRECT and HTTP Proxies are supported!");
331+
}
332+
333+
final SocketAddress sa = proxy.address();
334+
335+
if (!(sa instanceof InetSocketAddress)) {
336+
throw new IllegalArgumentException("Only Internet Address sockets are supported!");
337+
}
338+
339+
InetSocketAddress isa = (InetSocketAddress) sa;
340+
341+
if (isa.isUnresolved()) {
342+
return new ProxyServer(isa.getHostName(), isa.getPort());
343+
} else {
344+
return new ProxyServer(isa.getAddress().getHostAddress(), isa.getPort());
345+
}
346+
}
310347
}
311348

312349
static class PartnerAuthRequestFilter implements RequestFilter {

0 commit comments

Comments
 (0)