11
11
import java .net .InetSocketAddress ;
12
12
import java .net .Proxy ;
13
13
import java .net .SocketAddress ;
14
+ import java .util .ArrayList ;
14
15
import java .util .Collection ;
16
+ import java .util .HashMap ;
17
+ import java .util .List ;
15
18
import java .util .Map ;
19
+ import java .util .Map .Entry ;
16
20
import java .util .concurrent .ExecutionException ;
17
21
import java .util .concurrent .Future ;
18
22
23
+ import org .asynchttpclient .AsyncHttpClientConfig ;
24
+ import org .asynchttpclient .DefaultAsyncHttpClient ;
25
+ import org .asynchttpclient .DefaultAsyncHttpClientConfig ;
26
+ import org .asynchttpclient .Realm ;
27
+ import org .asynchttpclient .Realm .AuthScheme ;
28
+ import org .asynchttpclient .RequestBuilder ;
29
+ import org .asynchttpclient .Response ;
30
+ import org .asynchttpclient .filter .FilterContext ;
31
+ import org .asynchttpclient .filter .FilterException ;
32
+ import org .asynchttpclient .filter .RequestFilter ;
33
+ import org .asynchttpclient .proxy .ProxyServer ;
34
+
19
35
import com .fasterxml .jackson .core .JsonProcessingException ;
20
36
import com .fasterxml .jackson .databind .ObjectMapper ;
21
37
import com .fasterxml .jackson .databind .node .JsonNodeFactory ;
22
38
import com .fasterxml .jackson .databind .node .ObjectNode ;
23
- import com .ning .http .client .*;
24
- import com .ning .http .client .filter .FilterContext ;
25
- import com .ning .http .client .filter .FilterException ;
26
- import com .ning .http .client .filter .RequestFilter ;
27
39
import com .opentok .ArchiveProperties ;
28
40
import com .opentok .constants .DefaultApiUrl ;
29
41
import com .opentok .constants .Version ;
30
42
import com .opentok .exception .OpenTokException ;
31
43
import com .opentok .exception .RequestException ;
32
44
33
- public class HttpClient extends AsyncHttpClient {
45
+ public class HttpClient extends DefaultAsyncHttpClient {
34
46
35
47
private final String apiUrl ;
36
48
private final int apiKey ;
@@ -44,10 +56,16 @@ private HttpClient(Builder builder) {
44
56
public String createSession (Map <String , Collection <String >> params ) throws RequestException {
45
57
String responseString = null ;
46
58
Response response = null ;
47
- FluentStringsMap paramsString = new FluentStringsMap ().addAll (params );
59
+ Map <String , List <String >> paramsWithList = null ;
60
+ if (params != null ) {
61
+ paramsWithList = new HashMap <>();
62
+ for (Entry <String , Collection <String >> entry : params .entrySet ()) {
63
+ paramsWithList .put (entry .getKey (), new ArrayList <>(entry .getValue ()));
64
+ }
65
+ }
48
66
49
67
Future <Response > request = this .preparePost (this .apiUrl + "/session/create" )
50
- .setFormParams (paramsString )
68
+ .setFormParams (paramsWithList )
51
69
.addHeader ("Accept" , "application/json" ) // XML version is deprecated
52
70
.execute ();
53
71
@@ -61,7 +79,7 @@ public String createSession(Map<String, Collection<String>> params) throws Reque
61
79
throw new RequestException ("Could not create an OpenTok Session. The server response was invalid." +
62
80
" response code: " + response .getStatusCode ());
63
81
}
64
- } catch (InterruptedException | ExecutionException | IOException e ) {
82
+ } catch (InterruptedException | ExecutionException e ) {
65
83
throw new RequestException ("Could not create an OpenTok Session" , e );
66
84
}
67
85
return responseString ;
@@ -89,7 +107,7 @@ public String getArchive(String archiveId) throws RequestException {
89
107
throw new RequestException ("Could not get an OpenTok Archive. The server response was invalid." +
90
108
" response code: " + response .getStatusCode ());
91
109
}
92
- } catch (InterruptedException | ExecutionException | IOException e ) {
110
+ } catch (InterruptedException | ExecutionException e ) {
93
111
throw new RequestException ("Could not get an OpenTok Archive" , e );
94
112
}
95
113
@@ -126,7 +144,7 @@ public String getArchives(int offset, int count) throws RequestException {
126
144
throw new RequestException ("Could not get an OpenTok Archive. The server response was invalid." +
127
145
" response code: " + response .getStatusCode ());
128
146
}
129
- } catch (InterruptedException | ExecutionException | IOException e ) {
147
+ } catch (InterruptedException | ExecutionException e ) {
130
148
throw new RequestException ("Could not get OpenTok Archives" , e );
131
149
}
132
150
@@ -150,7 +168,7 @@ public String getArchives(String sessionId) throws RequestException {
150
168
throw new RequestException ("Could not get an OpenTok Archive. The server response was invalid."
151
169
+ " response code: " + response .getStatusCode ());
152
170
}
153
- } catch (InterruptedException | ExecutionException | IOException e ) {
171
+ } catch (InterruptedException | ExecutionException e ) {
154
172
throw new RequestException ("Could not get OpenTok Archives" , e );
155
173
}
156
174
}
@@ -202,7 +220,7 @@ public String startArchive(String sessionId, ArchiveProperties properties)
202
220
throw new RequestException ("Could not start an OpenTok Archive. The server response was invalid." +
203
221
" response code: " + response .getStatusCode ());
204
222
}
205
- } catch (InterruptedException | ExecutionException | IOException e ) {
223
+ } catch (InterruptedException | ExecutionException e ) {
206
224
throw new RequestException ("Could not start an OpenTok Archive." , e );
207
225
}
208
226
return responseString ;
@@ -238,7 +256,7 @@ public String stopArchive(String archiveId) throws RequestException {
238
256
throw new RequestException ("Could not stop an OpenTok Archive. The server response was invalid." +
239
257
" response code: " + response .getStatusCode ());
240
258
}
241
- } catch (InterruptedException | ExecutionException | IOException e ) {
259
+ } catch (InterruptedException | ExecutionException e ) {
242
260
throw new RequestException ("Could not stop an OpenTok Archive." , e );
243
261
}
244
262
return responseString ;
@@ -266,17 +284,28 @@ public String deleteArchive(String archiveId) throws RequestException {
266
284
throw new RequestException ("Could not get an OpenTok Archive. The server response was invalid." +
267
285
" response code: " + response .getStatusCode ());
268
286
}
269
- } catch (InterruptedException | ExecutionException | IOException e ) {
287
+ } catch (InterruptedException | ExecutionException e ) {
270
288
throw new RequestException ("Could not delete an OpenTok Archive. archiveId = " + archiveId , e );
271
289
}
272
290
273
291
return responseString ;
274
292
}
293
+
294
+ public static enum ProxyAuthScheme {
295
+ BASIC ,
296
+ DIGEST ,
297
+ NTLM ,
298
+ SPNEGO ,
299
+ KERBEROS
300
+ }
275
301
276
302
public static class Builder {
277
303
private final int apiKey ;
278
304
private final String apiSecret ;
279
305
private Proxy proxy ;
306
+ private ProxyAuthScheme proxyAuthScheme ;
307
+ private String principal ;
308
+ private String password ;
280
309
private String apiUrl ;
281
310
private AsyncHttpClientConfig config ;
282
311
@@ -291,20 +320,28 @@ public Builder apiUrl(String apiUrl) {
291
320
}
292
321
293
322
public Builder proxy (Proxy proxy ) {
323
+ proxy (proxy , null , null , null );
324
+ return this ;
325
+ }
326
+
327
+ public Builder proxy (Proxy proxy , ProxyAuthScheme proxyAuthScheme , String principal , String password ) {
294
328
this .proxy = proxy ;
329
+ this .proxyAuthScheme = proxyAuthScheme ;
330
+ this .principal = principal ;
331
+ this .password = password ;
295
332
return this ;
296
333
}
297
334
298
335
public HttpClient build () {
299
- AsyncHttpClientConfig .Builder configBuilder = new AsyncHttpClientConfig .Builder ()
336
+ DefaultAsyncHttpClientConfig .Builder configBuilder = new DefaultAsyncHttpClientConfig .Builder ()
300
337
.setUserAgent ("Opentok-Java-SDK/" + Version .VERSION + " JRE/" + System .getProperty ("java.version" ))
301
338
.addRequestFilter (new TokenAuthRequestFilter (this .apiKey , this .apiSecret ));
302
339
if (this .apiUrl == null ) {
303
340
this .apiUrl =DefaultApiUrl .DEFAULT_API_URI ;
304
341
}
305
342
306
343
if (this .proxy != null ) {
307
- configBuilder .setProxyServer (createProxyServer (this .proxy ));
344
+ configBuilder .setProxyServer (createProxyServer (this .proxy , this . proxyAuthScheme , this . principal , this . password ));
308
345
}
309
346
310
347
this .config = configBuilder .build ();
@@ -314,7 +351,7 @@ public HttpClient build() {
314
351
}
315
352
316
353
// credit: https://github.com/AsyncHttpClient/async-http-client/blob/b52a8de5d6a862b5d1652d62f87ce774cbcff156/src/main/java/com/ning/http/client/ProxyServer.java#L99-L127
317
- static ProxyServer createProxyServer (final Proxy proxy ) {
354
+ static ProxyServer createProxyServer (final Proxy proxy , ProxyAuthScheme proxyAuthScheme , String principal , String password ) {
318
355
switch (proxy .type ()) {
319
356
case DIRECT :
320
357
return null ;
@@ -329,9 +366,37 @@ static ProxyServer createProxyServer(final Proxy proxy) {
329
366
}
330
367
331
368
InetSocketAddress isa = (InetSocketAddress ) sa ;
332
-
369
+
333
370
final String isaHost = isa .isUnresolved () ? isa .getHostName () : isa .getAddress ().getHostAddress ();
334
- return new ProxyServer (isaHost , isa .getPort ());
371
+ ProxyServer .Builder builder = new ProxyServer .Builder (isaHost , isa .getPort ());
372
+
373
+ if (principal != null ) {
374
+ Realm .AuthScheme authScheme = null ;
375
+ switch (proxyAuthScheme ) {
376
+ case BASIC :
377
+ authScheme = AuthScheme .BASIC ;
378
+ break ;
379
+ case DIGEST :
380
+ authScheme = AuthScheme .DIGEST ;
381
+ break ;
382
+ case NTLM :
383
+ authScheme = AuthScheme .NTLM ;
384
+ break ;
385
+ case KERBEROS :
386
+ authScheme = AuthScheme .KERBEROS ;
387
+ break ;
388
+ case SPNEGO :
389
+ authScheme = AuthScheme .SPNEGO ;
390
+ break ;
391
+ }
392
+
393
+ Realm .Builder rb = new Realm .Builder (principal , password );
394
+ rb .setScheme (authScheme );
395
+
396
+ builder .setRealm (rb .build ());
397
+ }
398
+
399
+ return builder .build ();
335
400
}
336
401
}
337
402
@@ -346,9 +411,10 @@ public TokenAuthRequestFilter(int apiKey, String apiSecret) {
346
411
this .apiSecret = apiSecret ;
347
412
}
348
413
349
- public FilterContext filter (FilterContext ctx ) throws FilterException {
414
+ @ Override
415
+ public <T > FilterContext <T > filter (FilterContext <T > ctx ) throws FilterException {
350
416
try {
351
- return new FilterContext .FilterContextBuilder (ctx )
417
+ return new FilterContext .FilterContextBuilder < T > (ctx )
352
418
.request (new RequestBuilder (ctx .getRequest ())
353
419
.addHeader (authHeader , TokenGenerator .generateToken (apiKey , apiSecret ))
354
420
.build ())
0 commit comments