|
18 | 18 |
|
19 | 19 | import javax.xml.bind.DatatypeConverter;
|
20 | 20 |
|
| 21 | +import com.opentok.api.constants.TokenOptions; |
21 | 22 | import org.codehaus.jackson.JsonNode;
|
22 | 23 | import org.codehaus.jackson.map.ObjectMapper;
|
23 | 24 | import org.codehaus.jackson.type.TypeReference;
|
@@ -144,87 +145,39 @@ public OpenTok(int apiKey, String apiSecret, String apiUrl) {
|
144 | 145 | *
|
145 | 146 | * @return The token string.
|
146 | 147 | */
|
147 |
| - public String generateToken(String sessionId, String role, long expireTime, String connectionData) throws OpenTokException { |
| 148 | + public String generateToken(String sessionId, TokenOptions tokenOptions) throws OpenTokException { |
148 | 149 |
|
149 | 150 | if(sessionId == null || sessionId == "") {
|
150 | 151 | throw new OpenTokInvalidArgumentException("Session not valid");
|
151 | 152 | }
|
152 |
| - String decodedSessionId = ""; |
153 |
| - try { |
154 |
| - String subSessionId = sessionId.substring(2); |
155 |
| - for (int i = 0; i<3; i++){ |
156 |
| - String newSessionId = subSessionId.concat(repeatString("=",i)); |
157 |
| - decodedSessionId = new String(DatatypeConverter.parseBase64Binary( |
158 |
| - newSessionId.replace('-', '+').replace('_', '/')), "ISO8859_1"); |
159 |
| - if (decodedSessionId.contains("~")){ |
160 |
| - break; |
161 |
| - } |
162 |
| - } |
163 |
| - } catch (UnsupportedEncodingException e) { |
164 |
| - throw new OpenTokSessionNotFoundException("Session not found"); |
165 |
| - } |
166 | 153 |
|
167 |
| - if(!decodedSessionId.split("~")[1].equals(String.valueOf(apiKey))) { |
168 |
| - throw new OpenTokSessionNotFoundException("Session not found"); |
169 |
| - } |
| 154 | + // TODO: use more succinct codec routines |
| 155 | +// String decodedSessionId = ""; |
| 156 | +// try { |
| 157 | +// String subSessionId = sessionId.substring(2); |
| 158 | +// for (int i = 0; i<3; i++){ |
| 159 | +// String newSessionId = subSessionId.concat(repeatString("=",i)); |
| 160 | +// decodedSessionId = new String(DatatypeConverter.parseBase64Binary( |
| 161 | +// newSessionId.replace('-', '+').replace('_', '/')), "ISO8859_1"); |
| 162 | +// if (decodedSessionId.contains("~")){ |
| 163 | +// break; |
| 164 | +// } |
| 165 | +// } |
| 166 | +// } catch (UnsupportedEncodingException e) { |
| 167 | +// throw new OpenTokSessionNotFoundException("Session not found"); |
| 168 | +// } |
| 169 | +// |
| 170 | +// if(!decodedSessionId.split("~")[1].equals(String.valueOf(apiKey))) { |
| 171 | +// throw new OpenTokSessionNotFoundException("Session not found"); |
| 172 | +// } |
170 | 173 |
|
171 | 174 | Session session = new Session(sessionId, apiKey, apiSecret);
|
172 |
| - return session.generateToken(role, expireTime, connectionData); |
| 175 | + return session.generateToken(tokenOptions); |
173 | 176 | }
|
174 | 177 |
|
175 |
| - |
176 |
| - |
177 |
| - /** |
178 |
| - * Generates the token for the given session. The role is set to publisher, the token expires in |
179 |
| - * 24 hours, and there is no connection data. |
180 |
| - * |
181 |
| - * @param sessionId The session ID. |
182 |
| - * |
183 |
| - * @see #generateToken(String sessionId, String role, long expireTime, String connectionData) |
184 |
| - */ |
185 | 178 | public String generateToken(String sessionId) throws OpenTokException {
|
186 |
| - return this.generateToken(sessionId, RoleConstants.PUBLISHER, 0, null); |
187 |
| - } |
188 |
| - |
189 |
| - |
190 |
| - /** |
191 |
| - * Generates the token for the given session and with the specified role. The token expires in |
192 |
| - * 24 hours, and there is no connection data. |
193 |
| - * |
194 |
| - * @param sessionId The session ID. |
195 |
| - * @param role The role assigned to the token. |
196 |
| - * |
197 |
| - * @see #generateToken(String sessionId, String role, long expireTime, String connectionData) |
198 |
| - */ |
199 |
| - public String generateToken(String sessionId, String role) throws OpenTokException { |
200 |
| - return this.generateToken(sessionId, role, 0, null); |
201 |
| - } |
202 |
| - |
203 |
| - /** |
204 |
| - * Generates the token for the given session, with the specified role and expiration time. |
205 |
| - * There is no connection data. |
206 |
| - * |
207 |
| - * @param sessionId The session ID. |
208 |
| - * @param role The role assigned to the token. |
209 |
| - * @param expireTime The expiration time, in seconds, since the UNIX epoch. Pass in 0 to use |
210 |
| - * the default expiration time of 24 hours after the token creation time. The maximum expiration |
211 |
| - * time is 30 days after the creation time. |
212 |
| - * |
213 |
| - * @see #generateToken(String sessionId, String role, long expireTime, String connectionData) |
214 |
| - */ |
215 |
| - public String generateToken(String sessionId, String role, Long expireTime) throws OpenTokException { |
216 |
| - return this.generateToken(sessionId, role, expireTime, null); |
217 |
| - } |
218 |
| - |
219 |
| - /** |
220 |
| - * Creates an OpenTok session and returns the session ID, with the default properties. The |
221 |
| - * session uses the OpenTok media server. And the session uses the first client connecting |
222 |
| - * to determine the location of OpenTok server to use. |
223 |
| - * |
224 |
| - * @see #createSession(SessionProperties) |
225 |
| - */ |
226 |
| - public Session createSession() throws OpenTokException { |
227 |
| - return createSession(null); |
| 179 | + // NOTE: should there be a static defaultTokenOptions? |
| 180 | + return generateToken(sessionId, new TokenOptions.Builder().build()); |
228 | 181 | }
|
229 | 182 |
|
230 | 183 | /**
|
@@ -337,6 +290,17 @@ public Session createSession(SessionProperties properties) throws OpenTokExcepti
|
337 | 290 | }
|
338 | 291 | }
|
339 | 292 |
|
| 293 | + /** |
| 294 | + * Creates an OpenTok session and returns the session ID, with the default properties. The |
| 295 | + * session uses the OpenTok media server. And the session uses the first client connecting |
| 296 | + * to determine the location of OpenTok server to use. |
| 297 | + * |
| 298 | + * @see #createSession(SessionProperties) |
| 299 | + */ |
| 300 | + public Session createSession() throws OpenTokException { |
| 301 | + return createSession(null); |
| 302 | + } |
| 303 | + |
340 | 304 | private static String repeatString(String str, int times){
|
341 | 305 | StringBuilder ret = new StringBuilder();
|
342 | 306 | for(int i = 0;i < times;i++) ret.append(str);
|
|
0 commit comments