|
| 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 | +} |
0 commit comments