15
15
import com .ning .http .client .filter .RequestFilter ;
16
16
17
17
import com .opentok .constants .Version ;
18
+ import com .opentok .exception .OpenTokException ;
19
+ import com .opentok .exception .RequestException ;
18
20
19
21
public class HttpClient extends AsyncHttpClient {
20
22
@@ -27,7 +29,7 @@ private HttpClient(Builder builder) {
27
29
this .apiUrl = builder .apiUrl ;
28
30
}
29
31
30
- public String createSession (Map <String , Collection <String >> params ) {
32
+ public String createSession (Map <String , Collection <String >> params ) throws RequestException {
31
33
Future <Response > request = null ;
32
34
String responseString = null ;
33
35
Response response = null ;
@@ -38,58 +40,73 @@ public String createSession(Map<String, Collection<String>> params) {
38
40
.setParameters (paramsString )
39
41
.execute ();
40
42
} catch (IOException e ) {
41
- // TODO: throw OpenTokException
42
- e .printStackTrace ();
43
+ throw new RequestException ("Could not create an OpenTok Session" , e );
43
44
}
44
45
45
46
try {
46
47
response = request .get ();
47
- // TODO: check response code
48
- responseString = response .getResponseBody ();
48
+ switch (response .getStatusCode ()) {
49
+ case 200 :
50
+ responseString = response .getResponseBody ();
51
+ break ;
52
+ default :
53
+ throw new RequestException ("Could not create an OpenTok Session. The server response was invalid." +
54
+ " response code: " + response .getStatusCode ());
55
+ }
56
+
57
+ // if we only wanted Java 7 and above, we could DRY this into one catch clause
49
58
} catch (InterruptedException e ) {
50
- // TODO: throw OpenTokException
51
- e .printStackTrace ();
59
+ throw new RequestException ("Could not create an OpenTok Session" , e );
52
60
} catch (ExecutionException e ) {
53
- // TODO: throw OpenTokException
54
- e .printStackTrace ();
61
+ throw new RequestException ("Could not create an OpenTok Session" , e );
55
62
} catch (IOException e ) {
56
- // TODO: throw OpenTokException
57
- e .printStackTrace ();
63
+ throw new RequestException ("Could not create an OpenTok Session" , e );
58
64
}
59
65
return responseString ;
60
66
}
61
67
62
- public String getArchive (String archiveId ) {
68
+ public String getArchive (String archiveId ) throws RequestException {
63
69
String responseString = null ;
64
70
Future <Response > request = null ;
65
71
String url = this .apiUrl + "/v2/partner/" + this .apiKey + "/archive/" + archiveId ;
66
72
67
73
try {
68
74
request = this .prepareGet (url ).execute ();
69
75
} catch (IOException e ) {
70
- // TODO: throw OpenTokException
71
- e .printStackTrace ();
76
+ throw new RequestException ("Could not get an OpenTok Archive" , e );
72
77
}
73
78
74
79
try {
75
80
Response response = request .get ();
76
- // TODO: check response code
77
- responseString = response .getResponseBody ();
81
+ switch (response .getStatusCode ()) {
82
+ case 200 :
83
+ responseString = response .getResponseBody ();
84
+ break ;
85
+ case 400 :
86
+ throw new RequestException ("Could not get an OpenTok Archive. The archiveId was invalid. " +
87
+ "archiveId: " + archiveId );
88
+ case 403 :
89
+ throw new RequestException ("Could not get an OpenTok Archive. The request was not authorized." );
90
+ case 500 :
91
+ throw new RequestException ("Could not get an OpenTok Archive. A server error occurred." );
92
+ default :
93
+ throw new RequestException ("Could not get an OpenTok Archive. The server response was invalid." +
94
+ " response code: " + response .getStatusCode ());
95
+ }
96
+
97
+ // if we only wanted Java 7 and above, we could DRY this into one catch clause
78
98
} catch (InterruptedException e ) {
79
- // TODO: throw OpenTokException
80
- e .printStackTrace ();
99
+ throw new RequestException ("Could not get an OpenTok Archive" , e );
81
100
} catch (ExecutionException e ) {
82
- // TODO: throw OpenTokException
83
- e .printStackTrace ();
101
+ throw new RequestException ("Could not get an OpenTok Archive" , e );
84
102
} catch (IOException e ) {
85
- // TODO: throw OpenTokException
86
- e .printStackTrace ();
103
+ throw new RequestException ("Could not get an OpenTok Archive" , e );
87
104
}
88
105
89
106
return responseString ;
90
107
}
91
108
92
- public String getArchives (int offset , int count ) {
109
+ public String getArchives (int offset , int count ) throws RequestException {
93
110
String responseString = null ;
94
111
Future <Response > request = null ;
95
112
// TODO: maybe use a StringBuilder?
@@ -107,29 +124,37 @@ public String getArchives(int offset, int count) {
107
124
try {
108
125
request = this .prepareGet (url ).execute ();
109
126
} catch (IOException e ) {
110
- // TODO: throw OpenTokException
111
- e .printStackTrace ();
127
+ throw new RequestException ("Could not get OpenTok Archives" , e );
112
128
}
113
129
114
130
try {
115
131
Response response = request .get ();
116
- // TODO: check response code
117
- responseString = response .getResponseBody ();
132
+ switch (response .getStatusCode ()) {
133
+ case 200 :
134
+ responseString = response .getResponseBody ();
135
+ break ;
136
+ case 403 :
137
+ throw new RequestException ("Could not get OpenTok Archives. The request was not authorized." );
138
+ case 500 :
139
+ throw new RequestException ("Could not get OpenTok Archives. A server error occurred." );
140
+ default :
141
+ throw new RequestException ("Could not get an OpenTok Archive. The server response was invalid." +
142
+ " response code: " + response .getStatusCode ());
143
+ }
144
+
145
+ // if we only wanted Java 7 and above, we could DRY this into one catch clause
118
146
} catch (InterruptedException e ) {
119
- // TODO: throw OpenTokException
120
- e .printStackTrace ();
147
+ throw new RequestException ("Could not get OpenTok Archives" , e );
121
148
} catch (ExecutionException e ) {
122
- // TODO: throw OpenTokException
123
- e .printStackTrace ();
149
+ throw new RequestException ("Could not get OpenTok Archives" , e );
124
150
} catch (IOException e ) {
125
- // TODO: throw OpenTokException
126
- e .printStackTrace ();
151
+ throw new RequestException ("Could not get OpenTok Archives" , e );
127
152
}
128
153
129
154
return responseString ;
130
155
}
131
156
132
- public String startArchive (String sessionId , String name ) {
157
+ public String startArchive (String sessionId , String name ) throws OpenTokException , RequestException {
133
158
String responseString = null ;
134
159
Future <Response > request = null ;
135
160
String requestBody = null ;
@@ -145,37 +170,50 @@ public String startArchive(String sessionId, String name) {
145
170
try {
146
171
requestBody = mapper .writeValueAsString (jsonBody );
147
172
} catch (JsonProcessingException e ) {
148
- // TODO: throw OpenTokException
149
- e .printStackTrace ();
173
+ throw new OpenTokException ("Could not start an OpenTok Archive. The JSON body encoding failed." , e );
150
174
}
151
175
try {
152
176
request = this .preparePost (url )
153
177
.setBody (requestBody )
154
178
.setHeader ("Content-Type" , "application/json" )
155
179
.execute ();
156
180
} catch (IOException e ) {
157
- // TODO: throw OpenTokException
158
- e .printStackTrace ();
181
+ throw new RequestException ("Could not start an OpenTok Archive." , e );
159
182
}
160
183
161
184
try {
162
185
Response response = request .get ();
163
- // TODO: check response code
164
- responseString = response .getResponseBody ();
186
+ switch (response .getStatusCode ()) {
187
+ case 200 :
188
+ responseString = response .getResponseBody ();
189
+ break ;
190
+ case 403 :
191
+ throw new RequestException ("Could not start an OpenTok Archive. The request was not authorized." );
192
+ case 404 :
193
+ throw new RequestException ("Could not start an OpenTok Archive. The sessionId does not exist. " +
194
+ "sessionId = " + sessionId );
195
+ case 409 :
196
+ throw new RequestException ("Could not start an OpenTok Archive. The session is either " +
197
+ "peer-to-peer or already recording. sessionId = " + sessionId );
198
+ case 500 :
199
+ throw new RequestException ("Could not start an OpenTok Archive. A server error occurred." );
200
+ default :
201
+ throw new RequestException ("Could not start an OpenTok Archive. The server response was invalid." +
202
+ " response code: " + response .getStatusCode ());
203
+ }
204
+
205
+ // if we only wanted Java 7 and above, we could DRY this into one catch clause
165
206
} catch (InterruptedException e ) {
166
- // TODO: throw OpenTokException
167
- e .printStackTrace ();
207
+ throw new RequestException ("Could not start an OpenTok Archive." , e );
168
208
} catch (ExecutionException e ) {
169
- // TODO: throw OpenTokException
170
- e .printStackTrace ();
209
+ throw new RequestException ("Could not start an OpenTok Archive." , e );
171
210
} catch (IOException e ) {
172
- // TODO: throw OpenTokException
173
- e .printStackTrace ();
211
+ throw new RequestException ("Could not start an OpenTok Archive." , e );
174
212
}
175
213
return responseString ;
176
214
}
177
215
178
- public String stopArchive (String archiveId ) {
216
+ public String stopArchive (String archiveId ) throws RequestException {
179
217
String responseString = null ;
180
218
Future <Response > request = null ;
181
219
// TODO: maybe use a StringBuilder?
@@ -184,52 +222,81 @@ public String stopArchive(String archiveId) {
184
222
try {
185
223
request = this .preparePost (url ).execute ();
186
224
} catch (IOException e ) {
187
- // TODO: throw OpenTokException
188
- e .printStackTrace ();
225
+ throw new RequestException ("Could not stop an OpenTok Archive. archiveId = " + archiveId , e );
189
226
}
190
227
191
228
try {
192
229
Response response = request .get ();
193
- // TODO: check response code
194
- responseString = response .getResponseBody ();
230
+ switch (response .getStatusCode ()) {
231
+ case 200 :
232
+ responseString = response .getResponseBody ();
233
+ break ;
234
+ case 400 :
235
+ // NOTE: the REST api spec talks about sessionId and action, both of which aren't required.
236
+ // see: https://github.com/opentok/OpenTok-2.0-archiving-samples/blob/master/REST-API.md#stop_archive
237
+ throw new RequestException ("Could not stop an OpenTok Archive." );
238
+ case 403 :
239
+ throw new RequestException ("Could not stop an OpenTok Archive. The request was not authorized." );
240
+ case 404 :
241
+ throw new RequestException ("Could not stop an OpenTok Archive. The archiveId does not exist. " +
242
+ "archiveId = " + archiveId );
243
+ case 409 :
244
+ throw new RequestException ("Could not stop an OpenTok Archive. The archive is not being recorded. " +
245
+ "archiveId = " + archiveId );
246
+ case 500 :
247
+ throw new RequestException ("Could not stop an OpenTok Archive. A server error occurred." );
248
+ default :
249
+ throw new RequestException ("Could not stop an OpenTok Archive. The server response was invalid." +
250
+ " response code: " + response .getStatusCode ());
251
+ }
252
+
253
+ // if we only wanted Java 7 and above, we could DRY this into one catch clause
195
254
} catch (InterruptedException e ) {
196
- // TODO: throw OpenTokException
197
- e .printStackTrace ();
255
+ throw new RequestException ("Could not stop an OpenTok Archive." , e );
198
256
} catch (ExecutionException e ) {
199
- // TODO: throw OpenTokException
200
- e .printStackTrace ();
257
+ throw new RequestException ("Could not stop an OpenTok Archive." , e );
201
258
} catch (IOException e ) {
202
- // TODO: throw OpenTokException
203
- e .printStackTrace ();
259
+ throw new RequestException ("Could not stop an OpenTok Archive." , e );
204
260
}
205
261
return responseString ;
206
262
}
207
263
208
- public String deleteArchive (String archiveId ) {
264
+ public String deleteArchive (String archiveId ) throws RequestException {
209
265
String responseString = null ;
210
266
Future <Response > request = null ;
211
267
String url = this .apiUrl + "/v2/partner/" + this .apiKey + "/archive/" + archiveId ;
212
268
213
269
try {
214
270
request = this .prepareDelete (url ).execute ();
215
271
} catch (IOException e ) {
216
- // TODO: throw OpenTokException
217
- e .printStackTrace ();
272
+ throw new RequestException ("Could not delete an OpenTok Archive. archiveId = " + archiveId , e );
218
273
}
219
274
220
275
try {
221
276
Response response = request .get ();
222
- // TODO: check response code
223
- responseString = response .getResponseBody ();
277
+ switch (response .getStatusCode ()) {
278
+ case 204 :
279
+ responseString = response .getResponseBody ();
280
+ break ;
281
+ case 403 :
282
+ throw new RequestException ("Could not delete an OpenTok Archive. The request was not authorized." );
283
+ case 409 :
284
+ throw new RequestException ("Could not delete an OpenTok Archive. The status was not \" uploaded\" ," +
285
+ " \" available\" , or \" deleted\" . archiveId = " + archiveId );
286
+ case 500 :
287
+ throw new RequestException ("Could not delete an OpenTok Archive. A server error occurred." );
288
+ default :
289
+ throw new RequestException ("Could not get an OpenTok Archive. The server response was invalid." +
290
+ " response code: " + response .getStatusCode ());
291
+ }
292
+
293
+ // if we only wanted Java 7 and above, we could DRY this into one catch clause
224
294
} catch (InterruptedException e ) {
225
- // TODO: throw OpenTokException
226
- e .printStackTrace ();
295
+ throw new RequestException ("Could not delete an OpenTok Archive. archiveId = " + archiveId , e );
227
296
} catch (ExecutionException e ) {
228
- // TODO: throw OpenTokException
229
- e .printStackTrace ();
297
+ throw new RequestException ("Could not delete an OpenTok Archive. archiveId = " + archiveId , e );
230
298
} catch (IOException e ) {
231
- // TODO: throw OpenTokException
232
- e .printStackTrace ();
299
+ throw new RequestException ("Could not delete an OpenTok Archive. archiveId = " + archiveId , e );
233
300
}
234
301
235
302
return responseString ;
0 commit comments