Skip to content

Commit f1d3ab1

Browse files
authored
Merge pull request #119 from juandebravo/feature/prefer-json-to-create-a-session
Prefer JSON format response while creating an Opentok session
2 parents 366ca65 + 87d0243 commit f1d3ab1

25 files changed

+153
-95
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ apply plugin: 'maven'
77
apply plugin: 'signing'
88
apply plugin: 'idea'
99
apply plugin: 'eclipse'
10+
apply plugin: 'license'
1011

1112
group = 'com.tokbox'
1213
archivesBaseName = 'opentok-server-sdk'
@@ -49,7 +50,6 @@ dependencies {
4950
//compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.5'
5051
}
5152

52-
apply plugin: 'license'
5353
license {
5454
header rootProject.file('codequality/HEADER')
5555
ext.year = Calendar.getInstance().get(Calendar.YEAR)

src/main/java/com/opentok/Archive.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* OpenTok Java SDK
3-
* Copyright (C) 2016 TokBox, Inc.
3+
* Copyright (C) 2017 TokBox, Inc.
44
* http://www.tokbox.com
55
*
66
* Licensed under The MIT License (MIT). See LICENSE file for more information.
@@ -11,7 +11,7 @@
1111
import com.fasterxml.jackson.databind.ObjectMapper;
1212

1313
/**
14-
* Represents an archive of an OpenTok session.
14+
* Represents an archive of an OpenTok session.
1515
*/
1616
@JsonIgnoreProperties(ignoreUnknown=true)
1717
public class Archive {
@@ -160,7 +160,7 @@ public String getSessionId() {
160160
return sessionId;
161161
}
162162

163-
/**
163+
/**
164164
* The size of the MP4 file. For archives that have not been generated, this value is set to 0.
165165
*/
166166
public int getSize() {
@@ -176,7 +176,7 @@ public Status getStatus() {
176176

177177
/**
178178
* The download URL of the available MP4 file. This is only set for an archive with the status
179-
* set to Status.AVAILABLE; for other archives, (including archives with the status of
179+
* set to Status.AVAILABLE; for other archives, (including archives with the status of
180180
* Status.UPLOADED) this method returns null. The download URL is obfuscated, and the file
181181
* is only available from the URL for 10 minutes. To generate a new URL, call the
182182
* {@link com.opentok.OpenTok#listArchives()} or {@link com.opentok.OpenTok#getArchive(String)}
@@ -201,7 +201,7 @@ public boolean hasAudio() {
201201
}
202202

203203
/**
204-
* The output mode to be generated for this archive: <code>composed</code> or <code>individual</code>.
204+
* The output mode to be generated for this archive: <code>composed</code> or <code>individual</code>.
205205
*/
206206
public OutputMode getOutputMode() {
207207
return outputMode;
@@ -214,7 +214,7 @@ public String toString() {
214214
} catch (Exception e) {
215215
return "";
216216
}
217-
217+
218218
}
219219

220-
}
220+
}

src/main/java/com/opentok/ArchiveList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* OpenTok Java SDK
3-
* Copyright (C) 2016 TokBox, Inc.
3+
* Copyright (C) 2017 TokBox, Inc.
44
* http://www.tokbox.com
55
*
66
* Licensed under The MIT License (MIT). See LICENSE file for more information.

src/main/java/com/opentok/ArchiveMode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* OpenTok Java SDK
3-
* Copyright (C) 2016 TokBox, Inc.
3+
* Copyright (C) 2017 TokBox, Inc.
44
* http://www.tokbox.com
55
*
66
* Licensed under The MIT License (MIT). See LICENSE file for more information.

src/main/java/com/opentok/ArchiveProperties.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* OpenTok Java SDK
3-
* Copyright (C) 2016 TokBox, Inc.
3+
* Copyright (C) 2017 TokBox, Inc.
44
* http://www.tokbox.com
55
*
66
* Licensed under The MIT License (MIT). See LICENSE file for more information.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* OpenTok Java SDK
3+
* Copyright (C) 2017 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+
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
22+
23+
@JsonProperty("session_id")
24+
private String sessionId;
25+
26+
@JsonProperty("project_id")
27+
private String projectId;
28+
29+
@JsonProperty("partner_id")
30+
private String partnerId;
31+
32+
@JsonProperty("create_dt")
33+
private String createDt;
34+
35+
@JsonProperty("media_server_url")
36+
private String mediaServerURL;
37+
38+
protected CreatedSession() {
39+
}
40+
41+
@JsonCreator
42+
public static CreatedSession makeSession() {
43+
return new CreatedSession();
44+
}
45+
46+
public String getId() {
47+
return sessionId;
48+
}
49+
50+
public String getProjectId() {
51+
return projectId;
52+
}
53+
54+
public String getPartnerId() {
55+
return partnerId;
56+
}
57+
58+
public String getCreateDt() {
59+
return createDt;
60+
}
61+
62+
public String getMediaServerURL() {
63+
return mediaServerURL;
64+
}
65+
66+
@Override
67+
public String toString() {
68+
try {
69+
return OBJECT_MAPPER.writeValueAsString(this);
70+
} catch (Exception e) {
71+
return "";
72+
}
73+
74+
}
75+
}

src/main/java/com/opentok/MediaMode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* OpenTok Java SDK
3-
* Copyright (C) 2016 TokBox, Inc.
3+
* Copyright (C) 2017 TokBox, Inc.
44
* http://www.tokbox.com
55
*
66
* Licensed under The MIT License (MIT). See LICENSE file for more information.

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

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* OpenTok Java SDK
3-
* Copyright (C) 2016 TokBox, Inc.
3+
* Copyright (C) 2017 TokBox, Inc.
44
* http://www.tokbox.com
55
*
66
* Licensed under The MIT License (MIT). See LICENSE file for more information.
@@ -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/Role.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* OpenTok Java SDK
3-
* Copyright (C) 2016 TokBox, Inc.
3+
* Copyright (C) 2017 TokBox, Inc.
44
* http://www.tokbox.com
55
*
66
* Licensed under The MIT License (MIT). See LICENSE file for more information.

src/main/java/com/opentok/Session.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* OpenTok Java SDK
3-
* Copyright (C) 2016 TokBox, Inc.
3+
* Copyright (C) 2017 TokBox, Inc.
44
* http://www.tokbox.com
55
*
66
* Licensed under The MIT License (MIT). See LICENSE file for more information.

0 commit comments

Comments
 (0)