Skip to content

Commit 0efe4eb

Browse files
committed
Merge pull request #71 from aoberoi/archiving-updates
adds ArchiveMode
2 parents e5d93eb + 2ce970e commit 0efe4eb

File tree

4 files changed

+130
-7
lines changed

4 files changed

+130
-7
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ method. The `properties` parameter is optional and it is used to specify two thi
7272

7373
* Whether the session uses the OpenTok Media Router
7474
* A location hint for the OpenTok server.
75+
* Whether the session is automatically archived.
7576

7677
An instance can be initialized using the `com.opentok.SessionProperties.Builder` class.
7778
The `sessionId` property of the returned `com.opentok.Session` instance, which you can read using
@@ -80,6 +81,7 @@ the `getSessionId()` method, is useful to get an identifier that can be saved to
8081

8182
```java
8283
import com.opentok.MediaMode;
84+
import com.opentok.ArchiveMode;
8385
import com.opentok.Session;
8486
import com.opentok.SessionProperties;
8587

@@ -96,6 +98,12 @@ Session session = opentok.createSession(new SessionProperties.Builder()
9698
.location("12.34.56.78")
9799
.build());
98100

101+
// A session that is automatically archived (it must used the routed media mode)
102+
Session session = opentok.createSession(new SessionProperties.Builder()
103+
.mediaMode(MediaMode.ROUTED)
104+
.archiveMode(ArchiveMode.ALWAYS)
105+
.build());
106+
99107
// Store this sessionId in the database for later use:
100108
String sessionId = session.getSessionId();
101109
```
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* OpenTok Java SDK
3+
* Copyright (C) 2015 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+
/**
11+
* Defines values for the archiveMode parameter of the
12+
* {@link SessionProperties.Builder#archiveMode(ArchiveMode archiveMode)} method.
13+
*/
14+
public enum ArchiveMode {
15+
16+
/**
17+
* The session is not archived automatically. To archive the session, you can call the
18+
* OpenTok.StartArchive() method.
19+
*/
20+
MANUAL ("manual"),
21+
22+
/**
23+
* The session is archived automatically (as soon as there are clients publishing streams
24+
* to the session).
25+
*/
26+
ALWAYS ("always");
27+
28+
private String serialized;
29+
30+
private ArchiveMode(String s) {
31+
serialized = s;
32+
}
33+
34+
@Override
35+
public String toString() {
36+
return serialized;
37+
}
38+
}

src/main/java/com/opentok/SessionProperties.java

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ public class SessionProperties {
2727

2828
private String location = null;
2929
private MediaMode mediaMode;
30+
private ArchiveMode archiveMode;
3031

3132
private SessionProperties(Builder builder) {
3233
this.location = builder.location;
3334
this.mediaMode = builder.mediaMode;
35+
this.archiveMode = builder.archiveMode;
3436
}
3537

3638
/**
@@ -41,6 +43,7 @@ private SessionProperties(Builder builder) {
4143
public static class Builder {
4244
private String location = null;
4345
private MediaMode mediaMode = MediaMode.RELAYED;
46+
private ArchiveMode archiveMode = ArchiveMode.MANUAL;
4447

4548

4649
/**
@@ -102,12 +105,31 @@ public Builder mediaMode(MediaMode mediaMode) {
102105
return this;
103106
}
104107

108+
/**
109+
* Call this method to determine whether the session will be automatically archived (<code>ArchiveMode.ALWAYS</code>)
110+
* or not (<code>ArchiveMode.MANUAL</code>).
111+
*
112+
* Using an always archived session also requires the routed media mode (<code>MediaMode.ROUTED</code>).
113+
*
114+
* @param archiveMode
115+
*
116+
* @return The SessionProperties.Builder object with the archive mode setting.
117+
*/
118+
public Builder archiveMode(ArchiveMode archiveMode) {
119+
this.archiveMode = archiveMode;
120+
return this;
121+
}
122+
105123
/**
106124
* Builds the SessionProperties object.
107125
*
108126
* @return The SessionProperties object.
109127
*/
110128
public SessionProperties build() {
129+
// Would throw in this case, but would introduce a backwards incompatible change.
130+
//if (this.archiveMode == ArchiveMode.ALWAYS && this.mediaMode != MediaMode.ROUTED) {
131+
// throw new InvalidArgumentException("A session with always archive mode must also have the routed media mode.");
132+
//}
111133
return new SessionProperties(this);
112134
}
113135
}
@@ -127,6 +149,15 @@ public MediaMode mediaMode() {
127149
return mediaMode;
128150
}
129151

152+
/**
153+
* Defines whether the session will be automatically archived (<code>ArchiveMode.ALWAYS</code>)
154+
* or not (<code>ArchiveMode.MANUAL</code>). See
155+
* {@link com.opentok.SessionProperties.Builder#archiveMode(ArchiveMode archiveMode)}
156+
*/
157+
public ArchiveMode archiveMode() {
158+
return archiveMode;
159+
}
160+
130161
/**
131162
* Returns the session properties as a Map.
132163
*/
@@ -137,9 +168,14 @@ public Map<String, Collection<String>> toMap() {
137168
valueList.add(location);
138169
params.put("location", valueList);
139170
}
140-
ArrayList<String> valueList = new ArrayList<String>();
141-
valueList.add(mediaMode.toString());
142-
params.put("p2p.preference", valueList);
171+
172+
ArrayList<String> mediaModeValueList = new ArrayList<String>();
173+
mediaModeValueList.add(mediaMode.toString());
174+
params.put("p2p.preference", mediaModeValueList);
175+
176+
ArrayList<String> archiveModeValueList = new ArrayList<String>();
177+
archiveModeValueList.add(archiveMode.toString());
178+
params.put("archiveMode", archiveModeValueList);
143179

144180
return params;
145181
}

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

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,13 @@ public void testCreateDefaultSession() throws OpenTokException {
8282
assertEquals(this.apiKey, session.getApiKey());
8383
assertEquals(sessionId, session.getSessionId());
8484
assertEquals(MediaMode.RELAYED, session.getProperties().mediaMode());
85+
assertEquals(ArchiveMode.MANUAL, session.getProperties().archiveMode());
8586
assertNull(session.getProperties().getLocation());
8687

8788
verify(postRequestedFor(urlMatching("/session/create"))
8889
.withRequestBody(matching(".*p2p.preference=enabled.*"))
89-
.withHeader("X-TB-PARTNER-AUTH", matching(this.apiKey+":"+this.apiSecret))
90+
.withRequestBody(matching(".*archiveMode=manual.*"))
91+
.withHeader("X-TB-PARTNER-AUTH", matching(this.apiKey + ":" + this.apiSecret))
9092
.withHeader("User-Agent", matching(".*Opentok-Java-SDK/"+ Version.VERSION+".*")));
9193
}
9294

@@ -149,13 +151,52 @@ public void testCreateLocationHintSession() throws OpenTokException {
149151
.withHeader("User-Agent", matching(".*Opentok-Java-SDK/"+ Version.VERSION+".*")));
150152
}
151153

154+
@Test
155+
public void testCreateAlwaysArchivedSession() throws OpenTokException {
156+
String sessionId = "SESSIONID";
157+
String locationHint = "12.34.56.78";
158+
stubFor(post(urlEqualTo("/session/create"))
159+
.willReturn(aResponse()
160+
.withStatus(200)
161+
.withHeader("Content-Type", "text/xml")
162+
.withBody("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><sessions><Session><" +
163+
"session_id>" + sessionId + "</session_id><partner_id>123456</partner_id><create_dt>" +
164+
"Mon Mar 17 00:41:31 PDT 2014</create_dt></Session></sessions>")));
165+
166+
SessionProperties properties = new SessionProperties.Builder()
167+
.archiveMode(ArchiveMode.ALWAYS)
168+
.build();
169+
Session session = sdk.createSession(properties);
170+
171+
assertNotNull(session);
172+
assertEquals(this.apiKey, session.getApiKey());
173+
assertEquals(sessionId, session.getSessionId());
174+
assertEquals(ArchiveMode.ALWAYS, session.getProperties().archiveMode());
175+
176+
177+
verify(postRequestedFor(urlMatching("/session/create"))
178+
// TODO: this is a pretty bad way to verify, ideally we can decode the body and then query the object
179+
.withRequestBody(matching(".*archiveMode=always.*"))
180+
.withHeader("X-TB-PARTNER-AUTH", matching(this.apiKey + ":" + this.apiSecret))
181+
.withHeader("User-Agent", matching(".*Opentok-Java-SDK/" + Version.VERSION + ".*")));
182+
}
183+
152184
@Test(expected = InvalidArgumentException.class)
153185
public void testCreateBadSession() throws OpenTokException {
154-
SessionProperties properties = new SessionProperties.Builder()
155-
.location("NOT A VALID IP")
156-
.build();
186+
SessionProperties properties = new SessionProperties.Builder()
187+
.location("NOT A VALID IP")
188+
.build();
157189
}
158190

191+
// This is not part of the API because it would introduce a backwards incompatible change.
192+
// @Test(expected = InvalidArgumentException.class)
193+
// public void testCreateInvalidAlwaysArchivedAndRelayedSession() throws OpenTokException {
194+
// SessionProperties properties = new SessionProperties.Builder()
195+
// .mediaMode(MediaMode.RELAYED)
196+
// .archiveMode(ArchiveMode.ALWAYS)
197+
// .build();
198+
// }
199+
159200
// TODO: test session creation conditions that result in errors
160201

161202
@Test

0 commit comments

Comments
 (0)