/*!
* OpenTok Java Library
* http://www.tokbox.com/
*
* Copyright 2010, TokBox, Inc.
*
* Last modified: 2011-08-12
*/
package tokbox.com.opentok.api;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Map;
import tokbox.com.opentok.api.constants.SessionProperties;
import tokbox.com.opentok.api.constants.RoleConstants;
import tokbox.com.opentok.exception.OpenTokException;
import tokbox.com.opentok.util.Base64;
import tokbox.com.opentok.util.GenerateMac;
import tokbox.com.opentok.util.TokBoxXML;
import tokbox.com.opentok.api.OpenTokSession;
public class OpenTokSDK {
protected final int api_key;
protected final String api_secret;
public OpenTokSDK(final int api_key, final String api_secret) {
this.api_key = api_key;
this.api_secret = api_secret.trim();
}
/**
*
* Generate a token which is passed to the JS API to enable widgets to connect to the Opentok api.
*
* @session_id: Specify a session_id to make this token only valid for that session_id.
* @role: One of the constants defined in RoleConstants. Default is publisher, look in the documentation to learn more about roles.
* @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.
*/
public String generate_token(final String session_id, final String role, final Long expire_time, final String connection_data) throws OpenTokException {
final Long create_time = new Long(System.currentTimeMillis() / 1000).longValue();
final StringBuilder data_string_builder = new StringBuilder();
//Build the string
final Random random = new Random();
final int nonce = random.nextInt();
data_string_builder.append("session_id=");
data_string_builder.append(session_id);
data_string_builder.append("&create_time=");
data_string_builder.append(create_time);
data_string_builder.append("&nonce=");
data_string_builder.append(nonce);
data_string_builder.append("&role=");
data_string_builder.append(role);
if(expire_time != null) {
data_string_builder.append("&expire_time=");
data_string_builder.append(expire_time);
}
if (connection_data != null) {
data_string_builder.append("&connection_data=");
data_string_builder.append(connection_data);
}
final StringBuilder token_string_builder = new StringBuilder();
try {
token_string_builder.append("T1==");
final StringBuilder inner_builder = new StringBuilder();
inner_builder.append("partner_id=");
inner_builder.append(this.api_key);
inner_builder.append("&sdk_version=");
inner_builder.append(API_Config.SDK_VERSION);
inner_builder.append("&sig=");
inner_builder.append(GenerateMac.calculateRFC2104HMAC(data_string_builder.toString(),
this.api_secret));
inner_builder.append(":");
inner_builder.append(data_string_builder.toString());
token_string_builder.append(Base64.encode(inner_builder.toString()));
}catch (java.security.SignatureException e) {
throw new OpenTokException(e.getMessage());
}
return token_string_builder.toString();
}
/**
* Creates a new session.
* @location: IP address to geolocate the call around.
* @session_properties: Optional array, keys are defined in SessionPropertyConstants
*/
public OpenTokSession create_session(final String location, final SessionProperties properties) throws OpenTokException {
Map<String, String> params;
if(properties != null){
params = properties.to_map();
}
else{
params = new HashMap<String, String>();
}
return this.create_session(location, params);
}
/**
* Overloaded functions
* These work the same as those defined above, but with optional params filled in with defaults
*/
public String generate_token(final String session_id) throws OpenTokException {
return this.generate_token(session_id, RoleConstants.PUBLISHER, null, null);
}
public String generate_token(final String session_id, final String role) throws OpenTokException {
return this.generate_token(session_id, role, null, null);
}
public String generate_token(final String session_id, final String role, final Long expire_time) throws OpenTokException {
return this.generate_token(session_id, role, null, null);
}
public OpenTokSession create_session() throws OpenTokException {
return create_session(null, new HashMap<String, String>());
}
public OpenTokSession create_session(final String location) throws OpenTokException {
return create_session(location, new HashMap<String, String>());
}
public OpenTokSession create_session(final String location, final Map<String, String> params) throws OpenTokException {
params.put("location", location);
final TokBoxXML xmlResponse = this.do_request("/session/create", params);
if(xmlResponse.hasElement("error", "Errors")) {
throw new OpenTokException("Unable to create session");
}
final String session_id = xmlResponse.getElementValue("session_id", "Session");
return new OpenTokSession(session_id);
}
protected TokBoxXML do_request(final String url, final Map<String, String> params) throws OpenTokException {
final TokBoxNetConnection n = new TokBoxNetConnection();
final Map<String, String> headers = new HashMap<String, String>();
headers.put("X-TB-PARTNER-AUTH", this.api_key + ":" + this.api_secret);
return new TokBoxXML(n.request(API_Config.API_URL + url, params, headers));
}
protected static String join(final List<String> s, final String delimiter) throws java.io.UnsupportedEncodingException{
if (s.isEmpty()){
return "";
}
final Iterator<String> iter = s.iterator();
final StringBuffer buffer = new StringBuffer(URLEncoder.encode(iter.next(),"UTF-8"));
while (iter.hasNext()){
buffer.append(delimiter).append(URLEncoder.encode(iter.next(),"UTF-8"));
}
return buffer.toString();
}
}