Skip to content

Commit cf4f236

Browse files
authored
Shim option for Vonage Video API (#268)
1 parent f7dd370 commit cf4f236

File tree

13 files changed

+441
-288
lines changed

13 files changed

+441
-288
lines changed

build.gradle

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ dependencies {
2828
testImplementation 'io.jsonwebtoken:jjwt-impl:0.12.6'
2929
testImplementation 'io.jsonwebtoken:jjwt-jackson:0.12.6'
3030

31+
implementation 'com.vonage:jwt:2.0.0'
3132
implementation 'commons-lang:commons-lang:2.6'
3233
implementation 'commons-codec:commons-codec:1.17.1'
3334
implementation 'io.netty:netty-codec-http:4.1.114.Final'
@@ -68,6 +69,7 @@ javadoc {
6869
options {
6970
locale 'en_US'
7071
setMemberLevel JavadocMemberLevel.PUBLIC
72+
addBooleanOption('Xdoclint:none', true)
7173
}
7274
}
7375

@@ -96,6 +98,12 @@ java {
9698
withJavadocJar()
9799
}
98100

101+
test {
102+
testLogging {
103+
exceptionFormat = 'full'
104+
}
105+
}
106+
99107
publishing {
100108
publications {
101109
mavenJava(MavenPublication) {

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11-bin.zip
44
networkTimeout=10000
55
validateDistributionUrl=true
66
zipStoreBase=GRADLE_USER_HOME

src/main/java/com/opentok/CreatedSession.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@
77
*/
88
package com.opentok;
99

10-
import com.fasterxml.jackson.annotation.JsonCreator;
11-
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
12-
import com.fasterxml.jackson.annotation.JsonProperty;
10+
import com.fasterxml.jackson.annotation.*;
1311
import com.fasterxml.jackson.databind.ObjectMapper;
1412

1513
/**
1614
* Used internally.
1715
*/
1816
@JsonIgnoreProperties(ignoreUnknown = true)
17+
@JsonInclude(JsonInclude.Include.NON_NULL)
1918
public class CreatedSession {
2019

2120
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
@@ -26,6 +25,9 @@ public class CreatedSession {
2625
@JsonProperty("project_id")
2726
private String projectId;
2827

28+
@JsonProperty("application_id")
29+
private String applicationId;
30+
2931
@JsonProperty("partner_id")
3032
private String partnerId;
3133

@@ -55,6 +57,10 @@ public String getProjectId() {
5557
return projectId;
5658
}
5759

60+
public String getApplicationId() {
61+
return applicationId;
62+
}
63+
5864
public String getPartnerId() {
5965
return partnerId;
6066
}

src/main/java/com/opentok/Hls.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
/**
1111
* Represents HLS options for a live streaming broadcast. Pass this object
12-
* into the {@link BroadcastProperties.Builder#hls()} method. It is returned by the
12+
* into the {@link BroadcastProperties.Builder#hls(Hls)} method. It is returned by the
1313
* {@link BroadcastProperties#hls()} method.
1414
*/
1515
public class Hls {

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

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
import com.opentok.util.HttpClient;
2020
import com.opentok.util.HttpClient.ProxyAuthScheme;
2121
import org.apache.commons.lang.StringUtils;
22-
2322
import java.io.IOException;
2423
import java.io.UnsupportedEncodingException;
2524
import java.net.Proxy;
25+
import java.nio.file.Path;
2626
import java.util.List;
2727
import java.util.Map;
28+
import java.util.Objects;
29+
import java.util.UUID;
2830

2931
/**
3032
* Contains methods for creating OpenTok sessions, generating tokens, and working with archives.
@@ -37,9 +39,9 @@
3739
* Be sure to include the entire OpenTok server SDK on your web server.
3840
*/
3941
public class OpenTok {
40-
4142
private final int apiKey;
42-
private final String apiSecret;
43+
private final String apiSecret, applicationId;
44+
private final Path privateKeyPath;
4345
protected HttpClient client;
4446

4547
protected static final ObjectReader
@@ -55,21 +57,37 @@ public class OpenTok {
5557
connectReader = new ObjectMapper().readerFor(AudioConnector.class),
5658
captionReader = new ObjectMapper().readerFor(Caption.class);
5759

58-
static final String defaultApiUrl = "https://api.opentok.com";
59-
6060
/**
6161
* Creates an OpenTok object.
6262
*
6363
* @param apiKey Your OpenTok API key. (See your <a href="https://tokbox.com/account">Vonage Video API account page</a>.)
6464
* @param apiSecret Your OpenTok API secret. (See your <a href="https://tokbox.com/account">Vonage Video API account page</a>.)
6565
*/
6666
public OpenTok(int apiKey, String apiSecret) {
67-
this(apiKey, apiSecret, new HttpClient.Builder(apiKey, apiSecret).build());
67+
this(apiKey, apiSecret, null, null, new HttpClient.Builder(apiKey, apiSecret).build());
68+
}
69+
70+
/**
71+
* Creates an OpenTok object for use with the
72+
* <a href=https://developer.vonage.com/en/api/video>Vonage Video API</a>. This is intended as a short-term step
73+
* towards full migration to Vonage. See the
74+
* <a href=https://developer.vonage.com/en/video/transition-guides/server-sdks/java>Java SDK transition guide</a>
75+
* for details.
76+
*
77+
* @param applicationId Your Vonage application UUID with video capabilities enabled.
78+
* @param privateKeyPath Absolute path to the private key for your application.
79+
*
80+
* @since 4.15.0
81+
*/
82+
public OpenTok(String applicationId, Path privateKeyPath) {
83+
this(0, null, applicationId, privateKeyPath, new HttpClient.Builder(applicationId, privateKeyPath).build());
6884
}
6985

70-
private OpenTok(int apiKey, String apiSecret, HttpClient httpClient) {
86+
private OpenTok(int apiKey, String apiSecret, String applicationId, Path privateKeyPath, HttpClient httpClient) {
7187
this.apiKey = apiKey;
72-
this.apiSecret = apiSecret.trim();
88+
this.apiSecret = apiSecret != null ? apiSecret.trim() : null;
89+
this.applicationId = applicationId;
90+
this.privateKeyPath = privateKeyPath;
7391
this.client = httpClient;
7492
}
7593

@@ -85,7 +103,7 @@ private OpenTok(int apiKey, String apiSecret, HttpClient httpClient) {
85103
* import com.opentok.TokenOptions;
86104
*
87105
* class Test {
88-
* public static void main(String argv[]) throws OpenTokException {
106+
* public static void main(String args[]) throws OpenTokException {
89107
* int API_KEY = 0; // Replace with your OpenTok API key (see https://tokbox.com/account).
90108
* String API_SECRET = ""; // Replace with your OpenTok API secret.
91109
* OpenTok sdk = new OpenTok(API_KEY, API_SECRET);
@@ -123,22 +141,28 @@ private OpenTok(int apiKey, String apiSecret, HttpClient httpClient) {
123141
* @return The token string.
124142
*/
125143
public String generateToken(String sessionId, TokenOptions tokenOptions) throws OpenTokException {
126-
List<String> sessionIdParts;
144+
Session session;
127145
if (sessionId == null || sessionId.isEmpty()) {
128146
throw new InvalidArgumentException("Session not valid");
129147
}
130148

131-
try {
132-
sessionIdParts = Crypto.decodeSessionId(sessionId);
133-
} catch (UnsupportedEncodingException e) {
134-
throw new InvalidArgumentException("Session ID was not valid");
149+
if (privateKeyPath == null && apiSecret != null) {
150+
List<String> sessionIdParts;
151+
try {
152+
sessionIdParts = Crypto.decodeSessionId(sessionId);
153+
}
154+
catch (UnsupportedEncodingException e) {
155+
throw new InvalidArgumentException("Session ID was not valid");
156+
}
157+
if (!sessionIdParts.contains(Integer.toString(apiKey))) {
158+
throw new InvalidArgumentException("Session ID was not valid");
159+
}
160+
session = new Session(sessionId, apiKey, apiSecret);
135161
}
136-
if (!sessionIdParts.contains(Integer.toString(apiKey))) {
137-
throw new InvalidArgumentException("Session ID was not valid");
162+
else {
163+
session = new Session(sessionId, applicationId, privateKeyPath);
138164
}
139165

140-
// NOTE: kind of wasteful of a Session instance
141-
Session session = new Session(sessionId, apiKey, apiSecret);
142166
return session.generateToken(tokenOptions);
143167
}
144168

@@ -1036,15 +1060,11 @@ public void stopCaptions(String captionsId) throws OpenTokException {
10361060
* {@link OpenTok OpenTok()} constructor to build the OpenTok object.
10371061
*/
10381062
public static class Builder {
1039-
private int apiKey;
1040-
private String apiSecret;
1041-
private String apiUrl;
1042-
private String appendUserAgent;
1063+
private int apiKey, requestTimeout;
1064+
private String apiSecret, applicationId, apiUrl, appendUserAgent, principal, password;
1065+
private Path privateKeyPath;
10431066
private Proxy proxy;
10441067
private ProxyAuthScheme proxyAuthScheme;
1045-
private String principal;
1046-
private String password;
1047-
private int requestTimeout;
10481068

10491069
/**
10501070
* Constructs a new OpenTok.Builder object.
@@ -1060,6 +1080,13 @@ public Builder(int apiKey, String apiSecret) {
10601080
this.apiSecret = apiSecret;
10611081
}
10621082

1083+
public Builder(String applicationId, Path privateKeyPath) {
1084+
this.applicationId = UUID.fromString(
1085+
Objects.requireNonNull(applicationId, "Vonage Application ID is required")
1086+
).toString();
1087+
this.privateKeyPath = Objects.requireNonNull(privateKeyPath, "Private key path is required.");
1088+
}
1089+
10631090
/**
10641091
* Do not use. This method is used by Vonage for testing.
10651092
*/
@@ -1113,7 +1140,7 @@ public Builder appendToUserAgent(String appendUserAgent) {
11131140
* @return The OpenTok object.
11141141
*/
11151142
public OpenTok build() {
1116-
HttpClient.Builder clientBuilder = new HttpClient.Builder(apiKey, apiSecret);
1143+
HttpClient.Builder clientBuilder = new HttpClient.Builder(apiKey, apiSecret, applicationId, privateKeyPath);
11171144

11181145
if (apiUrl != null) {
11191146
clientBuilder.apiUrl(apiUrl);
@@ -1128,7 +1155,7 @@ public OpenTok build() {
11281155
clientBuilder.userAgent(DefaultUserAgent.DEFAULT_USER_AGENT+" "+appendUserAgent);
11291156
}
11301157

1131-
return new OpenTok(apiKey, apiSecret, clientBuilder.build());
1158+
return new OpenTok(apiKey, apiSecret, applicationId, privateKeyPath, clientBuilder.build());
11321159
}
11331160
}
11341161

0 commit comments

Comments
 (0)