Skip to content

Commit 6f6554e

Browse files
committed
simplified XML processing and eliminated helper classes
1 parent 8287b1e commit 6f6554e

File tree

3 files changed

+22
-101
lines changed

3 files changed

+22
-101
lines changed

src/main/java/com/opentok/api/OpenTok.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@
1010

1111
package com.opentok.api;
1212

13+
import java.io.StringReader;
1314
import java.io.UnsupportedEncodingException;
1415
import java.util.Collection;
1516
import java.util.HashMap;
1617
import java.util.List;
1718
import java.util.Map;
1819

1920
import javax.xml.bind.DatatypeConverter;
21+
import javax.xml.xpath.XPath;
22+
import javax.xml.xpath.XPathExpressionException;
23+
import javax.xml.xpath.XPathFactory;
2024

2125
import com.opentok.api.constants.TokenOptions;
2226
import org.codehaus.jackson.JsonNode;
@@ -29,7 +33,7 @@
2933
import com.opentok.exception.OpenTokInvalidArgumentException;
3034
import com.opentok.exception.OpenTokRequestException;
3135
import com.opentok.exception.OpenTokSessionNotFoundException;
32-
import com.opentok.util.TokBoxXML;
36+
import org.xml.sax.InputSource;
3337

3438
/**
3539
* Contains methods for creating OpenTok sessions and generating tokens.
@@ -270,7 +274,7 @@ public String generateToken(String sessionId) throws OpenTokException {
270274
*/
271275
public Session createSession(SessionProperties properties) throws OpenTokException {
272276
Map<String, Collection<String>> params;
273-
Session session = null;
277+
String xpathQuery = "/sessions/Session/session_id";
274278

275279
// NOTE: doing this null check twice is kind of ugly
276280
if(properties != null) {
@@ -279,14 +283,18 @@ public Session createSession(SessionProperties properties) throws OpenTokExcepti
279283
params = null;
280284
}
281285

282-
TokBoxXML xmlResponse = new TokBoxXML(this.client.createSession(params));
286+
String xmlResponse = this.client.createSession(params);
283287

284288

285289
// 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);
290298
}
291299
}
292300

@@ -306,6 +314,13 @@ private static String repeatString(String str, int times){
306314
for(int i = 0;i < times;i++) ret.append(str);
307315
return ret.toString();
308316
}
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+
}
309324

310325
/**
311326
* Gets an {@link Archive} object for the given archive ID.

src/main/java/com/opentok/util/TokBoxUtils.java

Lines changed: 0 additions & 53 deletions
This file was deleted.

src/main/java/com/opentok/util/TokBoxXML.java

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)