Skip to content

Commit b193615

Browse files
author
Madhav Chhura
committed
Merge branch 'master' of https://github.com/opentok/Opentok-Java-SDK into feature/jwt-authentication
* 'master' of https://github.com/opentok/Opentok-Java-SDK: OpenTokTest.java updated manual changes completed in styling File replaced with default formatting and required changes replace original files with required changes without extra formating test white space Pointing badge to master instead of latest commit styling changes applied styling and formating changes applied Default API Url updated in HttpClient builder issue -94 builder pattern updated updated TokBox constructor as per comment issue-94 # Conflicts: # src/main/java/com/opentok/OpenTok.java # src/main/java/com/opentok/util/HttpClient.java # src/test/java/com/opentok/test/OpenTokTest.java
2 parents 6af3638 + 6bd6461 commit b193615

File tree

6 files changed

+74
-21
lines changed

6 files changed

+74
-21
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# OpenTok Java SDK
22

3-
[![Build Status](https://travis-ci.org/opentok/Opentok-Java-SDK.svg)](https://travis-ci.org/opentok/Opentok-Java-SDK)
3+
[![Build Status](https://travis-ci.org/opentok/Opentok-Java-SDK.svg?branch=master)](https://travis-ci.org/opentok/Opentok-Java-SDK)
44

55
The OpenTok Java SDK lets you generate
66
[sessions](http://tokbox.com/opentok/tutorials/create-session/) and

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

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,25 +56,15 @@ public class OpenTok {
5656
* page.)
5757
*/
5858
public OpenTok(int apiKey, String apiSecret) {
59-
this(apiKey, apiSecret, defaultApiUrl);
60-
}
61-
62-
public OpenTok(int apiKey, String apiSecret, String apiUrl) {
63-
this(apiKey, apiSecret, apiUrl, null);
59+
this.apiKey = apiKey;
60+
this.apiSecret = apiSecret.trim();
61+
this.client = new HttpClient.Builder(apiKey, apiSecret).build();
6462
}
65-
66-
public OpenTok(int apiKey, String apiSecret, String apiUrl, Proxy proxy) {
63+
64+
private OpenTok(int apiKey, String apiSecret, HttpClient httpClient) {
6765
this.apiKey = apiKey;
6866
this.apiSecret = apiSecret.trim();
69-
if (apiUrl == null) {
70-
apiUrl = defaultApiUrl;
71-
}
72-
HttpClient.Builder clientBuilder = new HttpClient.Builder(apiKey, apiSecret)
73-
.apiUrl(apiUrl);
74-
if (proxy != null) {
75-
clientBuilder.proxy(proxy);
76-
}
77-
this.client = clientBuilder.build();
67+
this.client = httpClient;
7868
}
7969

8070
/**
@@ -300,7 +290,7 @@ private static String readXml(String xpathQuery, String xml) throws XPathExpress
300290
InputSource source = new InputSource(new StringReader(xml));
301291
return xpath.evaluate(xpathQuery, source);
302292
}
303-
293+
304294
/**
305295
* Gets an {@link Archive} object for the given archive ID.
306296
*
@@ -426,4 +416,39 @@ public Archive stopArchive(String archiveId) throws OpenTokException {
426416
public void deleteArchive(String archiveId) throws OpenTokException {
427417
this.client.deleteArchive(archiveId);
428418
}
419+
420+
public static class Builder {
421+
private int apiKey;
422+
private String apiSecret;
423+
private String apiUrl;
424+
private Proxy proxy;
425+
426+
public Builder(int apiKey, String apiSecret) {
427+
this.apiKey = apiKey;
428+
this.apiSecret = apiSecret;
429+
}
430+
431+
public Builder apiUrl(String apiUrl) {
432+
this.apiUrl = apiUrl;
433+
return this;
434+
}
435+
436+
public Builder proxy(Proxy proxy) {
437+
this.proxy = proxy;
438+
return this;
439+
}
440+
441+
public OpenTok build() {
442+
HttpClient.Builder clientBuilder = new HttpClient.Builder(apiKey, apiSecret);
443+
444+
if (this.apiUrl != null) {
445+
clientBuilder.apiUrl(this.apiUrl);
446+
}
447+
if (this.proxy != null) {
448+
clientBuilder.proxy(this.proxy);
449+
}
450+
451+
return new OpenTok(this.apiKey, this.apiSecret, clientBuilder.build());
452+
}
453+
}
429454
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* OpenTok Java SDK
3+
* Copyright (C) 2016 TokBox, Inc.
4+
* http://www.tokbox.com
5+
*
6+
* Licensed under The MIT License (MIT). See LICENSE file for more information.
7+
*/
8+
package com.opentok.constants;
9+
10+
public class DefaultApiUrl {
11+
public static final String DEFAULT_API_URI = "https://api.opentok.com";
12+
13+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.ning.http.client.filter.FilterException;
2626
import com.ning.http.client.filter.RequestFilter;
2727
import com.opentok.ArchiveProperties;
28+
import com.opentok.constants.DefaultApiUrl;
2829
import com.opentok.constants.Version;
2930
import com.opentok.exception.OpenTokException;
3031
import com.opentok.exception.RequestException;
@@ -275,9 +276,14 @@ public HttpClient build() {
275276
AsyncHttpClientConfig.Builder configBuilder = new AsyncHttpClientConfig.Builder()
276277
.setUserAgent("Opentok-Java-SDK/" + Version.VERSION + " JRE/" + System.getProperty("java.version"))
277278
.addRequestFilter(new TokenAuthRequestFilter(this.apiKey, this.apiSecret));
279+
if (this.apiUrl == null) {
280+
this.apiUrl=DefaultApiUrl.DEFAULT_API_URI;
281+
}
282+
278283
if (this.proxy != null) {
279284
configBuilder.setProxyServer(createProxyServer(this.proxy));
280285
}
286+
281287
this.config = configBuilder.build();
282288
// NOTE: not thread-safe, config could be modified by another thread here?
283289
HttpClient client = new HttpClient(this);

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/**
2+
* OpenTok Java SDK
3+
* Copyright (C) 2016 TokBox, Inc.
4+
* http://www.tokbox.com
5+
*
6+
* Licensed under The MIT License (MIT). See LICENSE file for more information.
7+
*/
18
package com.opentok.util;
29

310
import com.opentok.TokenOptions;

src/test/java/com/opentok/test/OpenTokTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public void setUp() throws OpenTokException {
9898
apiKey = anApiKey;
9999
apiSecret = anApiSecret;
100100
}
101-
sdk = new OpenTok(apiKey, apiSecret, apiUrl);
101+
sdk = new OpenTok.Builder(apiKey, apiSecret).apiUrl(apiUrl).build();
102102
token = sdk.generateToken(sessionId);
103103
}
104104

@@ -862,8 +862,10 @@ public void testCreateSessionWithProxy() throws OpenTokException, UnknownHostExc
862862
String sessionId = "SESSIONID";
863863
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(InetAddress.getLocalHost(), proxyingService.port()));
864864

865-
sdk = new OpenTok(apiKey, apiSecret, apiUrl, proxy);
866-
stubFor(post(urlEqualTo(SESSION_CREATE))
865+
866+
sdk = new OpenTok.Builder(apiKey, apiSecret).apiUrl(targetServiceBaseUrl).proxy(proxy).build();
867+
868+
stubFor(post(urlEqualTo("/session/create"))
867869
.willReturn(aResponse()
868870
.withStatus(200)
869871
.withHeader("Content-Type", "text/xml")

0 commit comments

Comments
 (0)