19
19
import com .opentok .util .HttpClient ;
20
20
import com .opentok .util .HttpClient .ProxyAuthScheme ;
21
21
import org .apache .commons .lang .StringUtils ;
22
-
23
22
import java .io .IOException ;
24
23
import java .io .UnsupportedEncodingException ;
25
24
import java .net .Proxy ;
25
+ import java .nio .file .Path ;
26
26
import java .util .List ;
27
27
import java .util .Map ;
28
+ import java .util .Objects ;
29
+ import java .util .UUID ;
28
30
29
31
/**
30
32
* Contains methods for creating OpenTok sessions, generating tokens, and working with archives.
37
39
* Be sure to include the entire OpenTok server SDK on your web server.
38
40
*/
39
41
public class OpenTok {
40
-
41
42
private final int apiKey ;
42
- private final String apiSecret ;
43
+ private final String apiSecret , applicationId ;
44
+ private final Path privateKeyPath ;
43
45
protected HttpClient client ;
44
46
45
47
protected static final ObjectReader
@@ -55,21 +57,37 @@ public class OpenTok {
55
57
connectReader = new ObjectMapper ().readerFor (AudioConnector .class ),
56
58
captionReader = new ObjectMapper ().readerFor (Caption .class );
57
59
58
- static final String defaultApiUrl = "https://api.opentok.com" ;
59
-
60
60
/**
61
61
* Creates an OpenTok object.
62
62
*
63
63
* @param apiKey Your OpenTok API key. (See your <a href="https://tokbox.com/account">Vonage Video API account page</a>.)
64
64
* @param apiSecret Your OpenTok API secret. (See your <a href="https://tokbox.com/account">Vonage Video API account page</a>.)
65
65
*/
66
66
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 ());
68
84
}
69
85
70
- private OpenTok (int apiKey , String apiSecret , HttpClient httpClient ) {
86
+ private OpenTok (int apiKey , String apiSecret , String applicationId , Path privateKeyPath , HttpClient httpClient ) {
71
87
this .apiKey = apiKey ;
72
- this .apiSecret = apiSecret .trim ();
88
+ this .apiSecret = apiSecret != null ? apiSecret .trim () : null ;
89
+ this .applicationId = applicationId ;
90
+ this .privateKeyPath = privateKeyPath ;
73
91
this .client = httpClient ;
74
92
}
75
93
@@ -85,7 +103,7 @@ private OpenTok(int apiKey, String apiSecret, HttpClient httpClient) {
85
103
* import com.opentok.TokenOptions;
86
104
*
87
105
* class Test {
88
- * public static void main(String argv []) throws OpenTokException {
106
+ * public static void main(String args []) throws OpenTokException {
89
107
* int API_KEY = 0; // Replace with your OpenTok API key (see https://tokbox.com/account).
90
108
* String API_SECRET = ""; // Replace with your OpenTok API secret.
91
109
* OpenTok sdk = new OpenTok(API_KEY, API_SECRET);
@@ -123,22 +141,28 @@ private OpenTok(int apiKey, String apiSecret, HttpClient httpClient) {
123
141
* @return The token string.
124
142
*/
125
143
public String generateToken (String sessionId , TokenOptions tokenOptions ) throws OpenTokException {
126
- List < String > sessionIdParts ;
144
+ Session session ;
127
145
if (sessionId == null || sessionId .isEmpty ()) {
128
146
throw new InvalidArgumentException ("Session not valid" );
129
147
}
130
148
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 );
135
161
}
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 );
138
164
}
139
165
140
- // NOTE: kind of wasteful of a Session instance
141
- Session session = new Session (sessionId , apiKey , apiSecret );
142
166
return session .generateToken (tokenOptions );
143
167
}
144
168
@@ -1036,15 +1060,11 @@ public void stopCaptions(String captionsId) throws OpenTokException {
1036
1060
* {@link OpenTok OpenTok()} constructor to build the OpenTok object.
1037
1061
*/
1038
1062
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 ;
1043
1066
private Proxy proxy ;
1044
1067
private ProxyAuthScheme proxyAuthScheme ;
1045
- private String principal ;
1046
- private String password ;
1047
- private int requestTimeout ;
1048
1068
1049
1069
/**
1050
1070
* Constructs a new OpenTok.Builder object.
@@ -1060,6 +1080,13 @@ public Builder(int apiKey, String apiSecret) {
1060
1080
this .apiSecret = apiSecret ;
1061
1081
}
1062
1082
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
+
1063
1090
/**
1064
1091
* Do not use. This method is used by Vonage for testing.
1065
1092
*/
@@ -1113,7 +1140,7 @@ public Builder appendToUserAgent(String appendUserAgent) {
1113
1140
* @return The OpenTok object.
1114
1141
*/
1115
1142
public OpenTok build () {
1116
- HttpClient .Builder clientBuilder = new HttpClient .Builder (apiKey , apiSecret );
1143
+ HttpClient .Builder clientBuilder = new HttpClient .Builder (apiKey , apiSecret , applicationId , privateKeyPath );
1117
1144
1118
1145
if (apiUrl != null ) {
1119
1146
clientBuilder .apiUrl (apiUrl );
@@ -1128,7 +1155,7 @@ public OpenTok build() {
1128
1155
clientBuilder .userAgent (DefaultUserAgent .DEFAULT_USER_AGENT +" " +appendUserAgent );
1129
1156
}
1130
1157
1131
- return new OpenTok (apiKey , apiSecret , clientBuilder .build ());
1158
+ return new OpenTok (apiKey , apiSecret , applicationId , privateKeyPath , clientBuilder .build ());
1132
1159
}
1133
1160
}
1134
1161
0 commit comments