Skip to content

Commit 2f4d168

Browse files
committed
Prefer JSON response while creating a session
1 parent 9230dfe commit 2f4d168

File tree

4 files changed

+111
-50
lines changed

4 files changed

+111
-50
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* OpenTok Java SDK
3+
* Copyright (C) 2016 TokBox, Inc.
4+
* http://www.tokbox.com
5+
*
6+
* Licensed under The MIT License (MIT). See LICENSE file for more information.
7+
*/
8+
package com.opentok;
9+
10+
import com.fasterxml.jackson.annotation.JsonCreator;
11+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
12+
import com.fasterxml.jackson.annotation.JsonProperty;
13+
import com.fasterxml.jackson.databind.ObjectMapper;
14+
15+
/**
16+
* Represents a generated OpenTok session via REST API.
17+
*/
18+
@JsonIgnoreProperties(ignoreUnknown = true)
19+
public class CreatedSession {
20+
21+
@JsonProperty("session_id")
22+
private String sessionId;
23+
24+
@JsonProperty("project_id")
25+
private String projectId;
26+
27+
@JsonProperty("partner_id")
28+
private String partner_id;
29+
30+
@JsonProperty("create_dt")
31+
private String createDt;
32+
33+
@JsonProperty("media_server_url")
34+
private String mediaServerURL;
35+
36+
protected CreatedSession() {
37+
}
38+
39+
@JsonCreator
40+
public static CreatedSession makeSession() {
41+
return new CreatedSession();
42+
}
43+
44+
public String getId() {
45+
return sessionId;
46+
}
47+
48+
public String getProjectId() {
49+
return projectId;
50+
}
51+
52+
public String getPartnerId() {
53+
return partner_id;
54+
}
55+
56+
public String getCreateDt() {
57+
return createDt;
58+
}
59+
60+
public String getMediaServerURL() {
61+
return mediaServerURL;
62+
}
63+
64+
@Override
65+
public String toString() {
66+
try {
67+
return new ObjectMapper().writeValueAsString(this);
68+
} catch (Exception e) {
69+
return "";
70+
}
71+
72+
}
73+
}

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

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,8 @@
1515
import com.opentok.exception.RequestException;
1616
import com.opentok.util.Crypto;
1717
import com.opentok.util.HttpClient;
18-
import com.opentok.util.TokenGenerator;
19-
import org.xml.sax.InputSource;
2018

21-
import javax.xml.xpath.XPath;
22-
import javax.xml.xpath.XPathExpressionException;
23-
import javax.xml.xpath.XPathFactory;
2419
import java.io.IOException;
25-
import java.io.StringReader;
2620
import java.io.UnsupportedEncodingException;
2721
import java.net.Proxy;
2822
import java.util.Collection;
@@ -48,6 +42,8 @@ public class OpenTok {
4842
.readerFor(Archive.class);
4943
static protected ObjectReader archiveListReader = new ObjectMapper()
5044
.readerFor(ArchiveList.class);
45+
static protected ObjectReader createdSessionReader = new ObjectMapper()
46+
.readerFor(CreatedSession[].class);
5147
static final String defaultApiUrl = "https://api.opentok.com";
5248

5349
/**
@@ -238,25 +234,19 @@ public String generateToken(String sessionId) throws OpenTokException {
238234
* session. You will use this session ID in the client SDKs to identify the session.
239235
*/
240236
public Session createSession(SessionProperties properties) throws OpenTokException {
241-
Map<String, Collection<String>> params;
242-
String xpathQuery = "/sessions/Session/session_id";
237+
final SessionProperties _properties = properties != null ? properties : new SessionProperties.Builder().build();
238+
final Map<String, Collection<String>> params = _properties.toMap();
239+
final String response = this.client.createSession(params);
243240

244-
// NOTE: doing this null check twice is kind of ugly
245-
params = properties != null ? properties.toMap() :
246-
new SessionProperties.Builder().build().toMap();
247-
248-
String xmlResponse = this.client.createSession(params);
249-
250-
251-
// NOTE: doing this null check twice is kind of ugly
252241
try {
253-
if (properties != null) {
254-
return new Session(readXml(xpathQuery, xmlResponse), apiKey, apiSecret, properties);
255-
} else {
256-
return new Session(readXml(xpathQuery, xmlResponse), apiKey, apiSecret);
242+
CreatedSession[] sessions = createdSessionReader.readValue(response);
243+
// A bit ugly, but API response should include an array with one session
244+
if (sessions.length != 1) {
245+
throw new OpenTokException(String.format("Unexpected number of sessions created %d", sessions.length));
257246
}
258-
} catch (XPathExpressionException e) {
259-
throw new OpenTokException("Cannot create session. Could not read the response: " + xmlResponse);
247+
return new Session(sessions[0].getId(), apiKey, apiSecret, _properties);
248+
} catch (IOException e) {
249+
throw new OpenTokException("Cannot create session. Could not read the response: " + response);
260250
}
261251
}
262252

@@ -302,13 +292,6 @@ public Session createSession() throws OpenTokException {
302292
return createSession(null);
303293
}
304294

305-
private static String readXml(String xpathQuery, String xml) throws XPathExpressionException {
306-
XPathFactory xpathFactory = XPathFactory.newInstance();
307-
XPath xpath = xpathFactory.newXPath();
308-
InputSource source = new InputSource(new StringReader(xml));
309-
return xpath.evaluate(xpathQuery, source);
310-
}
311-
312295
/**
313296
* Gets an {@link Archive} object for the given archive ID.
314297
*

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public String createSession(Map<String, Collection<String>> params) throws Reque
4848

4949
Future<Response> request = this.preparePost(this.apiUrl + "/session/create")
5050
.setFormParams(paramsString)
51+
.addHeader("Accept", "application/json") // XML version is deprecated
5152
.execute();
5253

5354
try {

src/test/java/com/opentok/test/OpenTokTest.java

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,11 @@ public void testCreateDefaultSession() throws OpenTokException {
103103
stubFor(post(urlEqualTo(SESSION_CREATE))
104104
.willReturn(aResponse()
105105
.withStatus(200)
106-
.withHeader("Content-Type", "text/xml")
107-
.withBody("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><sessions><Session><" +
108-
"session_id>" + sessionId + "</session_id><partner_id>123456</partner_id><create_dt>" +
109-
"Mon Mar 17 00:41:31 PDT 2014</create_dt></Session></sessions>")));
106+
.withHeader("Content-Type", "application/json")
107+
.withBody("[{\"session_id\":\"" + sessionId + "\",\"project_id\":\"00000000\"," +
108+
"\"partner_id\":\"123456\"," +
109+
"\"create_dt\":\"Mon Mar 17 00:41:31 PDT 2014\"," +
110+
"\"media_server_url\":\"\"}]")));
110111

111112
Session session = sdk.createSession();
112113

@@ -131,10 +132,11 @@ public void testCreateRoutedSession() throws OpenTokException {
131132
stubFor(post(urlEqualTo(SESSION_CREATE))
132133
.willReturn(aResponse()
133134
.withStatus(200)
134-
.withHeader("Content-Type", "text/xml")
135-
.withBody("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><sessions><Session><" +
136-
"session_id>" + sessionId + "</session_id><partner_id>123456</partner_id><create_dt>" +
137-
"Mon Mar 17 00:41:31 PDT 2014</create_dt></Session></sessions>")));
135+
.withHeader("Content-Type", "application/json")
136+
.withBody("[{\"session_id\":\"" + sessionId + "\",\"project_id\":\"00000000\"," +
137+
"\"partner_id\":\"123456\"," +
138+
"\"create_dt\":\"Mon Mar 17 00:41:31 PDT 2014\"," +
139+
"\"media_server_url\":\"\"}]")));
138140

139141
SessionProperties properties = new SessionProperties.Builder()
140142
.mediaMode(MediaMode.ROUTED)
@@ -162,10 +164,11 @@ public void testCreateLocationHintSession() throws OpenTokException {
162164
stubFor(post(urlEqualTo(SESSION_CREATE))
163165
.willReturn(aResponse()
164166
.withStatus(200)
165-
.withHeader("Content-Type", "text/xml")
166-
.withBody("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><sessions><Session><" +
167-
"session_id>" + sessionId + "</session_id><partner_id>123456</partner_id><create_dt>" +
168-
"Mon Mar 17 00:41:31 PDT 2014</create_dt></Session></sessions>")));
167+
.withHeader("Content-Type", "application/json")
168+
.withBody("[{\"session_id\":\"" + sessionId + "\",\"project_id\":\"00000000\"," +
169+
"\"partner_id\":\"123456\"," +
170+
"\"create_dt\":\"Mon Mar 17 00:41:31 PDT 2014\"," +
171+
"\"media_server_url\":\"\"}]")));
169172

170173
SessionProperties properties = new SessionProperties.Builder()
171174
.location(locationHint)
@@ -193,10 +196,11 @@ public void testCreateAlwaysArchivedSession() throws OpenTokException {
193196
stubFor(post(urlEqualTo(SESSION_CREATE))
194197
.willReturn(aResponse()
195198
.withStatus(200)
196-
.withHeader("Content-Type", "text/xml")
197-
.withBody("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><sessions><Session><" +
198-
"session_id>" + sessionId + "</session_id><partner_id>123456</partner_id><create_dt>" +
199-
"Mon Mar 17 00:41:31 PDT 2014</create_dt></Session></sessions>")));
199+
.withHeader("Content-Type", "application/json")
200+
.withBody("[{\"session_id\":\"" + sessionId + "\",\"project_id\":\"00000000\"," +
201+
"\"partner_id\":\"123456\"," +
202+
"\"create_dt\":\"Mon Mar 17 00:41:31 PDT 2014\"," +
203+
"\"media_server_url\":\"\"}]")));
200204

201205
SessionProperties properties = new SessionProperties.Builder()
202206
.archiveMode(ArchiveMode.ALWAYS)
@@ -885,11 +889,11 @@ public void testCreateSessionWithProxy() throws OpenTokException, UnknownHostExc
885889
stubFor(post(urlEqualTo("/session/create"))
886890
.willReturn(aResponse()
887891
.withStatus(200)
888-
.withHeader("Content-Type", "text/xml")
889-
.withBody("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><sessions><Session><" +
890-
"session_id>" + sessionId + "</session_id><partner_id>123456</partner_id><create_dt>" +
891-
"Mon Mar 17 00:41:31 PDT 2014</create_dt></Session></sessions>")
892-
));
892+
.withHeader("Content-Type", "application/json")
893+
.withBody("[{\"session_id\":\"" + sessionId + "\",\"project_id\":\"00000000\"," +
894+
"\"partner_id\":\"123456\"," +
895+
"\"create_dt\":\"Mon Mar 17 00:41:31 PDT 2014\"," +
896+
"\"media_server_url\":\"\"}]")));
893897

894898
Session session = sdk.createSession();
895899

0 commit comments

Comments
 (0)