Skip to content

Commit f68d3bf

Browse files
author
Charley Robinson
committed
Initial import
0 parents  commit f68d3bf

14 files changed

+641
-0
lines changed

README

Whitespace-only changes.

Test.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import com.opentok.api.API_Config;
2+
import com.opentok.api.OpenTokSDK;
3+
import com.opentok.api.constants.SessionProperties;
4+
import com.opentok.api.constants.RoleConstants;
5+
import com.opentok.exception.OpenTokException;
6+
7+
class Test {
8+
public static void main(String argv[]) throws OpenTokException {
9+
OpenTokSDK sdk = new OpenTokSDK(API_Config.API_KEY,API_Config.API_SECRET);
10+
11+
//Generate a token
12+
String s = sdk.generate_token("session");
13+
System.out.println(s);
14+
15+
System.out.println(sdk.generate_token("session",RoleConstants.PUBLISHER));
16+
System.out.println(sdk.generate_token("session",RoleConstants.SUBSCRIBER));
17+
System.out.println(sdk.generate_token("session",RoleConstants.MODERATOR));
18+
19+
20+
21+
//Generate a basic session
22+
System.out.println(sdk.create_session().session_id);
23+
24+
System.out.println();
25+
26+
//Generate Session Properties for a session
27+
SessionProperties sp = new SessionProperties();
28+
sp.echoSuppression_enabled = true;
29+
30+
//Generate a session with a location hint and session properties
31+
System.out.println(sdk.create_session("127.0.0.1", sp).session_id);
32+
}
33+
}

build.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env sh
2+
find . -name \*.java -exec 'javac' '{}' ';'

com/opentok/api/API_Config.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
/*!
3+
* OpenTok Java Library
4+
* http://www.tokbox.com/
5+
*
6+
* Copyright 2010, TokBox, Inc.
7+
*
8+
*/
9+
package com.opentok.api;
10+
11+
public class API_Config {
12+
public static final int API_KEY = -1;
13+
14+
public static final String API_SECRET = "";
15+
16+
public static final String SDK_VERSION = "tbjava-@sdk_version@.@opentok.sdk.java.mod_time@";
17+
18+
public static final String API_URL = "@staging.api.url@";
19+
// Uncomment this line when you launch your app
20+
// public static final String API_URL = "@production.api.url@";
21+
}
22+

com/opentok/api/OpenTokSDK.java

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
2+
/*!
3+
* OpenTok Java Library
4+
* http://www.tokbox.com/
5+
*
6+
* Copyright 2010, TokBox, Inc.
7+
*
8+
* Last modified: @opentok.sdk.java.mod_time@
9+
*/
10+
11+
package com.opentok.api;
12+
13+
import java.net.URLEncoder;
14+
import java.util.Arrays;
15+
import java.util.HashMap;
16+
import java.util.Iterator;
17+
import java.util.List;
18+
import java.util.Random;
19+
import java.util.Map;
20+
21+
import com.opentok.api.constants.SessionProperties;
22+
import com.opentok.api.constants.RoleConstants;
23+
import com.opentok.exception.OpenTokException;
24+
import com.opentok.util.Base64;
25+
import com.opentok.util.GenerateMac;
26+
import com.opentok.util.TokBoxXML;
27+
import com.opentok.api.OpenTokSession;
28+
29+
public class OpenTokSDK {
30+
31+
protected int api_key;
32+
protected String api_secret;
33+
34+
public OpenTokSDK(int api_key, String api_secret) {
35+
this.api_key = api_key;
36+
this.api_secret = api_secret.trim();
37+
}
38+
39+
/**
40+
*
41+
* Generate a token which is passed to the JS API to enable widgets to connect to the Opentok api.
42+
*
43+
* @session_id: Specify a session_id to make this token only valid for that session_id.
44+
* @role: One of the constants defined in RoleConstants. Default is publisher, look in the documentation to learn more about roles.
45+
* @expire_time: Integer timestamp. You can override the default token expire time of 24h by choosing an explicit expire time. Can be up to 7d after create_time.
46+
*/
47+
public String generate_token(String session_id, String role, Long expire_time, String connection_data) throws OpenTokException {
48+
Long create_time = new Long(System.currentTimeMillis() / 1000).longValue();
49+
StringBuilder data_string_builder = new StringBuilder();
50+
//Build the string
51+
Random random = new Random();
52+
int nonce = random.nextInt();
53+
data_string_builder.append("session_id=");
54+
data_string_builder.append(session_id);
55+
data_string_builder.append("&create_time=");
56+
data_string_builder.append(create_time);
57+
data_string_builder.append("&nonce=");
58+
data_string_builder.append(nonce);
59+
data_string_builder.append("&role=");
60+
data_string_builder.append(role);
61+
if(expire_time != null) {
62+
data_string_builder.append("&expire_time=");
63+
data_string_builder.append(expire_time);
64+
}
65+
if (connection_data != null) {
66+
data_string_builder.append("&connection_data=");
67+
data_string_builder.append(connection_data);
68+
}
69+
70+
71+
StringBuilder token_string_builder = new StringBuilder();
72+
try {
73+
token_string_builder.append("T1==");
74+
75+
StringBuilder inner_builder = new StringBuilder();
76+
inner_builder.append("partner_id=");
77+
inner_builder.append(this.api_key);
78+
79+
inner_builder.append("&sdk_version=");
80+
inner_builder.append(API_Config.SDK_VERSION);
81+
82+
inner_builder.append("&sig=");
83+
84+
inner_builder.append(GenerateMac.calculateRFC2104HMAC(data_string_builder.toString(),
85+
this.api_secret));
86+
inner_builder.append(":");
87+
inner_builder.append(data_string_builder.toString());
88+
89+
token_string_builder.append(Base64.encode(inner_builder.toString()));
90+
91+
}catch (java.security.SignatureException e) {
92+
throw new OpenTokException(e.getMessage());
93+
}
94+
95+
return token_string_builder.toString();
96+
}
97+
98+
/**
99+
* Creates a new session.
100+
* @location: IP address to geolocate the call around.
101+
* @session_properties: Optional array, keys are defined in SessionPropertyConstants
102+
*/
103+
104+
public OpenTokSession create_session(String location, SessionProperties properties) throws OpenTokException {
105+
Map<String, String> params;
106+
if(properties != null)
107+
params = properties.to_map();
108+
else
109+
params = new HashMap<String, String>();
110+
111+
return this.create_session(location, params);
112+
}
113+
114+
/**
115+
* Overloaded functions
116+
* These work the same as those defined above, but with optional params filled in with defaults
117+
*/
118+
119+
public String generate_token(String session_id) throws OpenTokException {
120+
return this.generate_token(session_id, RoleConstants.PUBLISHER, null, null);
121+
}
122+
123+
124+
public String generate_token(String session_id, String role) throws OpenTokException {
125+
return this.generate_token(session_id, role, null, null);
126+
}
127+
128+
public String generate_token(String session_id, String role, Long expire_time) throws OpenTokException {
129+
return this.generate_token(session_id, role, null, null);
130+
}
131+
132+
public OpenTokSession create_session() throws OpenTokException {
133+
return create_session(null, new HashMap<String, String>());
134+
}
135+
136+
137+
public OpenTokSession create_session(String location) throws OpenTokException {
138+
return create_session(location, new HashMap<String, String>());
139+
}
140+
141+
public OpenTokSession create_session(String location, Map<String, String> params) throws OpenTokException {
142+
params.put("location", location);
143+
TokBoxXML xmlResponse = this.do_request("/session/create", params);
144+
if(xmlResponse.hasElement("error", "Errors")) {
145+
throw new OpenTokException("Unable to create session");
146+
}
147+
String session_id = xmlResponse.getElementValue("session_id", "Session");
148+
return new OpenTokSession(session_id);
149+
}
150+
151+
protected TokBoxXML do_request(String url, Map<String, String> params) throws OpenTokException {
152+
TokBoxNetConnection n = new TokBoxNetConnection();
153+
Map<String, String> headers = new HashMap<String, String>();
154+
headers.put("X-TB-PARTNER-AUTH", this.api_key + ":" + this.api_secret);
155+
156+
return new TokBoxXML(n.request(API_Config.API_URL + url, params, headers));
157+
}
158+
159+
protected static String join(List<String> s, String delimiter) throws java.io.UnsupportedEncodingException{
160+
if (s.isEmpty()) return "";
161+
Iterator<String> iter = s.iterator();
162+
StringBuffer buffer = new StringBuffer(URLEncoder.encode(iter.next(),"UTF-8"));
163+
while (iter.hasNext()) buffer.append(delimiter).append(URLEncoder.encode(iter.next(),"UTF-8"));
164+
return buffer.toString();
165+
}
166+
}

com/opentok/api/OpenTokSession.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
/*!
3+
* OpenTok Java Library
4+
* http://www.tokbox.com/
5+
*
6+
* Copyright 2010, TokBox, Inc.
7+
*
8+
*/
9+
package com.opentok.api;
10+
11+
public class OpenTokSession {
12+
13+
public String session_id;
14+
15+
public OpenTokSession(String session_id) {
16+
this.session_id = session_id;
17+
}
18+
19+
public String getSessionId() {
20+
return this.session_id;
21+
}
22+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
2+
/*!
3+
* OpenTok Java Library
4+
* http://www.tokbox.com/
5+
*
6+
* Copyright 2010, TokBox, Inc.
7+
*
8+
*/
9+
package com.opentok.api;
10+
import java.util.*;
11+
import java.net.*;
12+
import java.io.*;
13+
14+
15+
class TokBoxNetConnection {
16+
17+
18+
public String request(String reqString, Map<String, String> paramList, Map<String, String> headers){
19+
20+
StringBuilder returnString = new StringBuilder();
21+
22+
URL url = null;
23+
HttpURLConnection conn = null;
24+
BufferedReader br = null;
25+
OutputStreamWriter wr = null;
26+
BufferedWriter bufWriter = null;
27+
28+
try {
29+
30+
StringBuilder dataString = new StringBuilder();
31+
32+
for(Iterator<String> i = paramList.keySet().iterator(); i.hasNext(); ) {
33+
String key = i.next();
34+
String value = paramList.get(key);
35+
36+
if(null != value) {
37+
value = URLEncoder.encode(paramList.get(key), "UTF-8").replaceAll("\\+", "%20");
38+
dataString.append(URLEncoder.encode(key, "UTF-8")).append("=").append(value).append("&");
39+
}
40+
}
41+
url = new URL(reqString);
42+
conn = (HttpURLConnection) url.openConnection();
43+
44+
conn.setDoOutput(true);
45+
conn.setDoInput(true);
46+
conn.setUseCaches(false);
47+
48+
conn.setRequestMethod("POST");
49+
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
50+
conn.setRequestProperty("Content-Length", Integer.toString(dataString.toString().length()));
51+
conn.setRequestProperty("Accept-Charset", "utf-8");
52+
conn.setRequestProperty("Accept", "text/html, application/xhtml+xml,application/xml");
53+
54+
for(Iterator<String> i = headers.keySet().iterator(); i.hasNext(); ) {
55+
String key = i.next();
56+
String value = headers.get(key);
57+
conn.setRequestProperty(key, value);
58+
}
59+
60+
wr = new OutputStreamWriter(conn.getOutputStream(), "UTF8");
61+
bufWriter = new BufferedWriter( wr );
62+
bufWriter.write(dataString.toString());
63+
bufWriter.flush();
64+
65+
br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF8"));
66+
67+
String str;
68+
while(null != ((str = br.readLine())))
69+
{
70+
returnString.append(str);
71+
returnString.append("\n");
72+
}
73+
} catch(IOException e) {
74+
e.printStackTrace();
75+
} finally {
76+
try {
77+
if(null != conn) {
78+
conn.disconnect();
79+
}
80+
81+
if(null != wr) {
82+
wr.close();
83+
}
84+
85+
if(null != bufWriter) {
86+
bufWriter.close();
87+
}
88+
89+
if(null != br) {
90+
br.close();
91+
}
92+
}
93+
catch(IOException e) {
94+
e.printStackTrace();
95+
}
96+
}
97+
98+
return returnString.toString();
99+
}
100+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
/*!
3+
* OpenTok Java Library
4+
* http://www.tokbox.com/
5+
*
6+
* Copyright 2010, TokBox, Inc.
7+
*/
8+
9+
package com.opentok.api.constants;
10+
///List of valid roles for a token
11+
public class RoleConstants {
12+
public static final String SUBSCRIBER = "subscriber"; //Can only subscribe
13+
public static final String PUBLISHER = "publisher"; //Can publish, subscribe, and signal
14+
public static final String MODERATOR = "moderator"; //Can do the above along with forceDisconnect and forceUnpublish
15+
}

0 commit comments

Comments
 (0)