Skip to content

Commit 6f853e6

Browse files
committed
implement getArchive, add tests #11
* Jackson has been upgraded to 2.x * Archive objects are immutable * Jackson annotations enable deserialization using reflection
1 parent 57460d0 commit 6f853e6

File tree

5 files changed

+141
-103
lines changed

5 files changed

+141
-103
lines changed

build.gradle

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ dependencies {
3030
testCompile group: 'com.github.tomakehurst', name: 'wiremock', version: '1.45'
3131
testCompile group: 'commons-lang', name: 'commons-lang', version: '2.6'
3232
compile group: 'com.ning', name: 'async-http-client', version: '1.6.1'
33-
compile group: 'org.codehaus.jackson', name: 'jackson-mapper-asl', version: '1.9.13'
34-
compile group: 'org.codehaus.jackson', name: 'jackson-core-asl', version: '1.9.13'
33+
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.3.1'
3534
compile group: 'commons-validator', name: 'commons-validator', version: '1.4.0'
3635
compile group: 'commons-codec', name: 'commons-codec', version: '1.9'
3736
// TODO: find out how to initialize these dependencies properly, or remove them

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

Lines changed: 44 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package com.opentok;
22

3-
import java.util.UUID;
4-
5-
import org.codehaus.jackson.map.ObjectMapper;
3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import com.fasterxml.jackson.annotation.JsonValue;
6+
import com.fasterxml.jackson.databind.ObjectMapper;
67

78
/**
89
* Represents an archive of an OpenTok session.
@@ -11,166 +12,131 @@ public class Archive {
1112
/**
1213
* Defines values returned by the {@link Archive#getStatus} method.
1314
*/
14-
public enum ArchiveState {
15+
public enum Status {
1516
/**
16-
* The archive file is available for download from the OpenTok cloud. You can get the URL of
17+
* The archive file is AVAILABLE for download from the OpenTok cloud. You can get the URL of
1718
* the download file by calling the {@link Archive#getUrl} method.
1819
*/
19-
available,
20+
AVAILABLE,
2021
/**
21-
* The archive file has been deleted.
22+
* The archive file has been DELETED.
2223
*/
23-
deleted,
24+
DELETED,
2425
/**
25-
* The recording of the archive failed.
26+
* The recording of the archive FAILED.
2627
*/
27-
failed,
28+
FAILED,
2829
/**
29-
* The archive recording has started and is in progress.
30+
* The archive recording has STARTED and is in progress.
3031
*/
31-
started,
32+
STARTED,
3233
/**
33-
* The archive recording has stopped, but the file is not available.
34+
* The archive recording has STOPPED, but the file is not AVAILABLE.
3435
*/
35-
stopped,
36+
STOPPED,
3637
/**
37-
* The archive file is available at the target S3 bucket you specified using the
38+
* The archive file is AVAILABLE at the target S3 bucket you specified using the
3839
* REST API.
3940
*/
40-
uploaded,
41-
/**
42-
* The archive status is unknown.
43-
*/
44-
unknown
41+
UPLOADED;
42+
43+
@JsonValue public String toString() {
44+
return super.toString().toLowerCase();
45+
}
4546
}
4647

47-
private long createdAt = System.currentTimeMillis();
48-
private int duration = 0;
49-
private UUID id;
50-
private String name;
51-
private int partnerId;
52-
private String reason = "";
53-
private String sessionId;
54-
private int size = 0;
55-
private ArchiveState status = ArchiveState.unknown;
56-
private String url;
48+
@JsonProperty private long createdAt;
49+
@JsonProperty private int duration = 0;
50+
@JsonProperty private String id;
51+
@JsonProperty private String name;
52+
@JsonProperty private int partnerId;
53+
@JsonProperty private String reason;
54+
@JsonProperty private String sessionId;
55+
@JsonProperty private int size = 0;
56+
@JsonProperty private Status status;
57+
@JsonProperty private String url;
5758

5859
protected Archive() {
5960
}
6061

62+
@JsonCreator
63+
public static Archive makeArchive() {
64+
return new Archive();
65+
}
66+
6167
/**
6268
* The time at which the archive was created, in milliseconds since the UNIX epoch.
6369
*/
6470
public long getCreatedAt() {
6571
return createdAt;
6672
}
6773

68-
public void setCreatedAt(long createdAt) {
69-
this.createdAt = createdAt;
70-
}
71-
7274
/**
7375
* The duration of the archive, in milliseconds.
7476
*/
7577
public int getDuration() {
7678
return duration;
7779
}
7880

79-
public void setDuration(int duration) {
80-
this.duration = duration;
81-
}
82-
8381
/**
8482
* The archive ID.
8583
*/
86-
public UUID getId() {
84+
public String getId() {
8785
return id;
8886
}
8987

90-
public void setId(UUID id) {
91-
this.id = id;
92-
}
93-
9488
/**
9589
* The name of the archive.
9690
*/
9791
public String getName() {
9892
return name;
9993
}
10094

101-
public void setName(String name) {
102-
this.name = name;
103-
}
104-
10595
/**
10696
* The OpenTok API key associated with the archive.
10797
*/
10898
public int getPartnerId() {
10999
return partnerId;
110100
}
111101

112-
public void setPartnerId(int partnerId) {
113-
this.partnerId = partnerId;
114-
}
115-
116102
/**
117-
* For archives with the status "stopped", this can be set to "90 mins exceeded", "failure", "session ended",
118-
* or "user initiated". For archives with the status "failed", this can be set to "system failure".
103+
* For archives with the status "STOPPED", this can be set to "90 mins exceeded", "failure", "session ended",
104+
* or "user initiated". For archives with the status "FAILED", this can be set to "system failure".
119105
*/
120106
public String getReason() {
121107
return reason;
122108
}
123109

124-
public void setReason(String reason) {
125-
this.reason = reason;
126-
}
127-
128110
/**
129111
* The session ID of the OpenTok session associated with this archive.
130112
*/
131113
public String getSessionId() {
132114
return sessionId;
133115
}
134116

135-
public void setSessionId(String sessionId) {
136-
this.sessionId = sessionId;
137-
}
138-
139117
/**
140118
* The size of the MP4 file. For archives that have not been generated, this value is set to 0.
141119
*/
142120
public int getSize() {
143121
return size;
144122
}
145123

146-
public void setSize(int size) {
147-
this.size = size;
148-
}
149-
150124
/**
151-
* The status of the archive, as defined by the {@link ArchiveState} enum.
125+
* The status of the archive, as defined by the {@link com.opentok.Archive.Status} enum.
152126
*/
153-
public ArchiveState getStatus() {
127+
public Status getStatus() {
154128
return status;
155129
}
156130

157-
public void setStatus(ArchiveState status) {
158-
this.status = status;
159-
}
160-
161131
/**
162-
* The download URL of the available MP4 file. This is only set for an archive with the status set to "available";
163-
* for other archives, (including archives wit the status "uploaded") this method returns null. The download URL is
164-
* obfuscated, and the file is only available from the URL for 10 minutes. To generate a new URL, call
132+
* The download URL of the AVAILABLE MP4 file. This is only set for an archive with the status set to "AVAILABLE";
133+
* for other archives, (including archives wit the status "UPLOADED") this method returns null. The download URL is
134+
* obfuscated, and the file is only AVAILABLE from the URL for 10 minutes. To generate a new URL, call
165135
* the {@link com.opentok.OpenTok#listArchives()} or {@link com.opentok.OpenTok#getArchive(String)} method.
166136
*/
167137
public String getUrl() {
168138
return url;
169139
}
170-
171-
public void setUrl(String url) {
172-
this.url = url;
173-
}
174140

175141
// public Archive stop() throws OpenTokException {
176142
// HashMap<String, String> headers = new HashMap<String, String>();

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

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@
2222

2323
import com.opentok.exception.OpenTokException;
2424
import com.opentok.exception.InvalidArgumentException;
25+
import com.opentok.exception.RequestException;
2526
import com.opentok.util.Crypto;
2627
import com.opentok.util.HttpClient;
28+
29+
import com.fasterxml.jackson.databind.ObjectMapper;
2730
import org.xml.sax.InputSource;
2831

2932
/**
@@ -231,7 +234,7 @@ public String generateToken(String sessionId) throws OpenTokException {
231234
* "disabled" (the default) &mdash; The session's streams will all be relayed using the OpenTok media server.
232235
* <br><br>
233236
* <i>In OpenTok v2:</i> The <a href="http://www.tokbox.com/blog/mantis-next-generation-cloud-technology-for-webrtc/">OpenTok
234-
* media server</a> provides benefits not available in peer-to-peer sessions. For example, the OpenTok media server can
237+
* media server</a> provides benefits not AVAILABLE in peer-to-peer sessions. For example, the OpenTok media server can
235238
* decrease bandwidth usage in multiparty sessions. Also, the OpenTok server can improve the quality of the user experience
236239
* through <a href="http://www.tokbox.com/blog/quality-of-experience-and-traffic-shaping-the-next-step-with-mantis/">dynamic
237240
* traffic shaping</a>. For information on pricing, see the <a href="http://www.tokbox.com/pricing">OpenTok pricing page</a>.
@@ -302,16 +305,16 @@ private static String readXml(String xpathQuery, String xml) throws XPathExpress
302305
* @param archiveId The archive ID.
303306
* @return The {@link Archive} object.
304307
*/
305-
// public Archive getArchive(String archiveId) throws OpenTokException {
306-
// ObjectMapper mapper = new ObjectMapper();
307-
// String archive = HttpClient.makeGetRequest("/v2/partner/" + this.apiKey + "/archive/" + archiveId);
308-
// try {
309-
// return mapper.readValue(archive, Archive.class);
310-
// } catch (Exception e) {
311-
// throw new RequestException(500, "Exception mapping json: " + e.getMessage());
312-
// }
313-
//
314-
// }
308+
public Archive getArchive(String archiveId) throws OpenTokException {
309+
ObjectMapper mapper = new ObjectMapper();
310+
String archive = this.client.getArchive(archiveId);
311+
try {
312+
return mapper.readValue(archive, Archive.class);
313+
} catch (Exception e) {
314+
throw new RequestException("Exception mapping json: " + e.getMessage());
315+
}
316+
317+
}
315318

316319
/**
317320
* Returns a List of {@link Archive} objects, representing archives that are both
@@ -329,8 +332,8 @@ private static String readXml(String xpathQuery, String xml) throws XPathExpress
329332
* Returns a List of {@link Archive} objects, representing archives that are both
330333
* both completed and in-progress, for your API key.
331334
*
332-
* @param offset The index offset of the first archive. 0 is offset of the most recently started archive.
333-
* 1 is the offset of the archive that started prior to the most recent archive.
335+
* @param offset The index offset of the first archive. 0 is offset of the most recently STARTED archive.
336+
* 1 is the offset of the archive that STARTED prior to the most recent archive.
334337
* @param count The number of archives to be returned. The maximum number of archives returned is 1000.
335338
* @return A List of {@link Archive} objects.
336339
*/
@@ -385,7 +388,7 @@ private static String readXml(String xpathQuery, String xml) throws XPathExpress
385388
* session being archived.
386389
*
387390
* @param archiveId The archive ID of the archive you want to stop recording.
388-
* @return The Archive object corresponding to the archive being stopped.
391+
* @return The Archive object corresponding to the archive being STOPPED.
389392
*/
390393
// public Archive stopArchive(String archiveId) throws OpenTokException {
391394
// HashMap<String, String> headers = new HashMap<String, String>();
@@ -403,8 +406,8 @@ private static String readXml(String xpathQuery, String xml) throws XPathExpress
403406
/**
404407
* Deletes an OpenTok archive.
405408
* <p>
406-
* You can only delete an archive which has a status of "available" or "uploaded". Deleting an archive
407-
* removes its record from the list of archives. For an "available" archive, it also removes the archive
409+
* You can only delete an archive which has a status of "AVAILABLE" or "UPLOADED". Deleting an archive
410+
* removes its record from the list of archives. For an "AVAILABLE" archive, it also removes the archive
408411
* file, making it unavailable for download.
409412
*
410413
* @param archiveId The archive ID of the archive you want to delete.

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ private HttpClient(Builder builder) {
2828
}
2929

3030
public String createSession(Map<String, Collection<String>> params) {
31-
Future request = null;
31+
Future<Response> request = null;
3232
String responseString = null;
3333
Response response = null;
3434
FluentStringsMap paramsString = new FluentStringsMap().addAll(params);
@@ -43,7 +43,7 @@ public String createSession(Map<String, Collection<String>> params) {
4343
}
4444

4545
try {
46-
response = (Response)request.get();
46+
response = request.get();
4747
// TODO: check response code
4848
responseString = response.getResponseBody();
4949
} catch (InterruptedException e) {
@@ -59,6 +59,36 @@ public String createSession(Map<String, Collection<String>> params) {
5959
return responseString;
6060
}
6161

62+
public String getArchive(String archiveId) {
63+
String responseString = null;
64+
Future<Response> request = null;
65+
String url = this.apiUrl+"/v2/partner/"+this.apiKey+"/archive/"+archiveId;
66+
67+
try {
68+
request = this.prepareGet(url).execute();
69+
} catch (IOException e) {
70+
// TODO: throw OpenTokException
71+
e.printStackTrace();
72+
}
73+
74+
try {
75+
Response response = request.get();
76+
// TODO: check response code
77+
responseString = response.getResponseBody();
78+
} catch (InterruptedException e) {
79+
// TODO: throw OpenTokException
80+
e.printStackTrace();
81+
} catch (ExecutionException e) {
82+
// TODO: throw OpenTokException
83+
e.printStackTrace();
84+
} catch (IOException e) {
85+
// TODO: throw OpenTokException
86+
e.printStackTrace();
87+
}
88+
89+
return responseString;
90+
}
91+
6292
// protected static String makeDeleteRequest(String resource) throws OpenTokException {
6393
// BoundRequestBuilder get = client.prepareDelete(apiUrl + resource);
6494
// addCommonHeaders(get);

0 commit comments

Comments
 (0)