1919import com .opentok .util .HttpClient ;
2020import com .opentok .util .HttpClient .ProxyAuthScheme ;
2121import org .apache .commons .lang .StringUtils ;
22-
2322import java .io .IOException ;
2423import java .io .UnsupportedEncodingException ;
2524import java .net .Proxy ;
25+ import java .nio .file .Path ;
2626import java .util .List ;
2727import 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.
3739 * Be sure to include the entire OpenTok server SDK on your web server.
3840 */
3941public 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