10
10
11
11
package com .opentok .api ;
12
12
13
+ import java .io .StringReader ;
13
14
import java .io .UnsupportedEncodingException ;
14
15
import java .util .Collection ;
15
16
import java .util .HashMap ;
16
17
import java .util .List ;
17
18
import java .util .Map ;
18
19
19
20
import javax .xml .bind .DatatypeConverter ;
21
+ import javax .xml .xpath .XPath ;
22
+ import javax .xml .xpath .XPathExpressionException ;
23
+ import javax .xml .xpath .XPathFactory ;
20
24
21
25
import com .opentok .api .constants .TokenOptions ;
22
26
import org .codehaus .jackson .JsonNode ;
29
33
import com .opentok .exception .OpenTokInvalidArgumentException ;
30
34
import com .opentok .exception .OpenTokRequestException ;
31
35
import com .opentok .exception .OpenTokSessionNotFoundException ;
32
- import com . opentok . util . TokBoxXML ;
36
+ import org . xml . sax . InputSource ;
33
37
34
38
/**
35
39
* Contains methods for creating OpenTok sessions and generating tokens.
@@ -270,7 +274,7 @@ public String generateToken(String sessionId) throws OpenTokException {
270
274
*/
271
275
public Session createSession (SessionProperties properties ) throws OpenTokException {
272
276
Map <String , Collection <String >> params ;
273
- Session session = null ;
277
+ String xpathQuery = "/sessions/Session/session_id" ;
274
278
275
279
// NOTE: doing this null check twice is kind of ugly
276
280
if (properties != null ) {
@@ -279,14 +283,18 @@ public Session createSession(SessionProperties properties) throws OpenTokExcepti
279
283
params = null ;
280
284
}
281
285
282
- TokBoxXML xmlResponse = new TokBoxXML ( this .client .createSession (params ) );
286
+ String xmlResponse = this .client .createSession (params );
283
287
284
288
285
289
// NOTE: doing this null check twice is kind of ugly
286
- if (properties != null ) {
287
- return new Session (xmlResponse .getElementValue ("session_id" , "Session" ), apiKey , apiSecret , properties );
288
- } else {
289
- return new Session (xmlResponse .getElementValue ("session_id" , "Session" ), apiKey , apiSecret );
290
+ try {
291
+ if (properties != null ) {
292
+ return new Session (readXml (xpathQuery , xmlResponse ), apiKey , apiSecret , properties );
293
+ } else {
294
+ return new Session (readXml (xpathQuery , xmlResponse ), apiKey , apiSecret );
295
+ }
296
+ } catch (XPathExpressionException e ) {
297
+ throw new OpenTokException ("Cannot create session. Could not read the response: " +xmlResponse );
290
298
}
291
299
}
292
300
@@ -306,6 +314,13 @@ private static String repeatString(String str, int times){
306
314
for (int i = 0 ;i < times ;i ++) ret .append (str );
307
315
return ret .toString ();
308
316
}
317
+
318
+ private static String readXml (String xpathQuery , String xml ) throws XPathExpressionException {
319
+ XPathFactory xpathFactory = XPathFactory .newInstance ();
320
+ XPath xpath = xpathFactory .newXPath ();
321
+ InputSource source = new InputSource (new StringReader (xml ));
322
+ return xpath .evaluate (xpathQuery , source );
323
+ }
309
324
310
325
/**
311
326
* Gets an {@link Archive} object for the given archive ID.
0 commit comments