-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathWS.scala
557 lines (473 loc) · 16.7 KB
/
WS.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
package play.api.libs.ws
import java.io.File
import scala.concurrent.{Future, Promise}
import play.api.libs.iteratee._
import play.api.libs.iteratee.Input._
import play.api.http.{ Writeable, ContentTypeOf }
import com.ning.http.client.{
AsyncHttpClient,
AsyncHttpClientConfig,
RequestBuilderBase,
FluentCaseInsensitiveStringsMap,
HttpResponseBodyPart,
HttpResponseHeaders,
HttpResponseStatus,
Response => AHCResponse,
PerRequestConfig
}
import collection.immutable.TreeMap
import play.core.utils.CaseInsensitiveOrdered
import com.ning.http.util.AsyncHttpProviderUtils
/**
* Asynchronous API to to query web services, as an http client.
*
* Usage example:
* {{{
* WS.url("http://example.com/feed").get()
* WS.url("http://example.com/item").post("content")
* }}}
*
* The value returned is a Future[Response],
* and you should use Play's asynchronous mechanisms to use this response.
*
*/
object WS {
import com.ning.http.client.Realm.{ AuthScheme, RealmBuilder }
import javax.net.ssl.SSLContext
private var clientHolder: Option[AsyncHttpClient] = None
/**
* resets the underlying AsyncHttpClient
*/
def resetClient(): Unit = {
clientHolder.map { clientRef =>
clientRef.close()
}
clientHolder = None
}
/**
* retrieves or creates underlying HTTP client.
*/
def client =
clientHolder.getOrElse {
val playConfig = play.api.Play.maybeApplication.map(_.configuration)
val asyncHttpConfig = new AsyncHttpClientConfig.Builder()
.setConnectionTimeoutInMs(playConfig.flatMap(_.getMilliseconds("ws.timeout")).getOrElse(120000L).toInt)
.setRequestTimeoutInMs(playConfig.flatMap(_.getMilliseconds("ws.timeout")).getOrElse(120000L).toInt)
.setFollowRedirects(playConfig.flatMap(_.getBoolean("ws.followRedirects")).getOrElse(true))
.setUseProxyProperties(playConfig.flatMap(_.getBoolean("ws.useProxyProperties")).getOrElse(true))
playConfig.flatMap(_.getString("ws.useragent")).map { useragent =>
asyncHttpConfig.setUserAgent(useragent)
}
if (playConfig.flatMap(_.getBoolean("ws.acceptAnyCertificate")).getOrElse(false) == false) {
asyncHttpConfig.setSSLContext(SSLContext.getDefault)
}
val innerClient = new AsyncHttpClient(asyncHttpConfig.build())
clientHolder = Some(innerClient)
innerClient
}
/**
* Prepare a new request. You can then construct it by chaining calls.
*
* @param url the URL to request
*/
def url(url: String): WSRequestHolder = WSRequestHolder(url, Map(), Map(), None, None, None, None, None)
/**
* A WS Request.
*/
class WSRequest(_method: String, _auth: Option[Tuple3[String, String, AuthScheme]], _calc: Option[SignatureCalculator]) extends RequestBuilderBase[WSRequest](classOf[WSRequest], _method, false) {
import scala.collection.JavaConverters._
def getStringData = body.getOrElse("")
protected var body: Option[String] = None
override def setBody(s: String) = { this.body = Some(s); super.setBody(s)}
protected var calculator: Option[SignatureCalculator] = _calc
protected var headers: Map[String, Seq[String]] = Map()
protected var _url: String = null
//this will do a java mutable set hence the {} response
_auth.map(data => auth(data._1, data._2, data._3)).getOrElse({})
/**
* Add http auth headers. Defaults to HTTP Basic.
*/
private def auth(username: String, password: String, scheme: AuthScheme = AuthScheme.BASIC): WSRequest = {
this.setRealm((new RealmBuilder())
.setScheme(scheme)
.setPrincipal(username)
.setPassword(password)
.setUsePreemptiveAuth(true)
.build())
}
/**
* Return the current headers of the request being constructed
*/
def allHeaders: Map[String, Seq[String]] = {
mapAsScalaMapConverter(request.asInstanceOf[com.ning.http.client.Request].getHeaders()).asScala.map(e => e._1 -> e._2.asScala.toSeq).toMap
}
/**
* Return the current query string parameters
*/
def queryString: Map[String, Seq[String]] = {
mapAsScalaMapConverter(request.asInstanceOf[com.ning.http.client.Request].getParams()).asScala.map(e => e._1 -> e._2.asScala.toSeq).toMap
}
/**
* Retrieve an HTTP header.
*/
def header(name: String): Option[String] = headers.get(name).flatMap(_.headOption)
/**
* The HTTP method.
*/
def method: String = _method
/**
* The URL
*/
def url: String = _url
private def ningHeadersToMap(headers: java.util.Map[String, java.util.Collection[String]]) =
mapAsScalaMapConverter(headers).asScala.map(e => e._1 -> e._2.asScala.toSeq).toMap
private def ningHeadersToMap(headers: FluentCaseInsensitiveStringsMap) = {
val res = mapAsScalaMapConverter(headers).asScala.map(e => e._1 -> e._2.asScala.toSeq).toMap
//todo: wrap the case insensitive ning map instead of creating a new one (unless perhaps immutabilty is important)
TreeMap(res.toSeq: _*)(CaseInsensitiveOrdered)
}
private[libs] def execute: Future[Response] = {
import com.ning.http.client.AsyncCompletionHandler
var result = Promise[Response]()
calculator.map(_.sign(this))
WS.client.executeRequest(this.build(), new AsyncCompletionHandler[AHCResponse]() {
override def onCompleted(response: AHCResponse) = {
result.success(Response(response))
response
}
override def onThrowable(t: Throwable) = {
result.failure(t)
}
})
result.future
}
/**
* Set an HTTP header.
*/
override def setHeader(name: String, value: String) = {
headers = headers + (name -> List(value))
super.setHeader(name, value)
}
/**
* Add an HTTP header (used for headers with multiple values).
*/
override def addHeader(name: String, value: String) = {
headers = headers + (name -> (headers.get(name).getOrElse(List()) :+ value))
super.addHeader(name, value)
}
/**
* Defines the request headers.
*/
override def setHeaders(hdrs: FluentCaseInsensitiveStringsMap) = {
headers = ningHeadersToMap(hdrs)
super.setHeaders(hdrs)
}
/**
* Defines the request headers.
*/
override def setHeaders(hdrs: java.util.Map[String, java.util.Collection[String]]) = {
headers = ningHeadersToMap(hdrs)
super.setHeaders(hdrs)
}
/**
* Defines the request headers.
*/
def setHeaders(hdrs: Map[String, Seq[String]]) = {
headers = hdrs
hdrs.foreach(header => header._2.foreach(value =>
super.addHeader(header._1, value)
))
this
}
/**
* Defines the query string.
*/
def setQueryString(queryString: Map[String, Seq[String]]) = {
for ((key, values) <- queryString; value <- values) {
this.addQueryParameter(key, value)
}
this
}
/**
* Defines the URL.
*/
override def setUrl(url: String) = {
_url = url
super.setUrl(url)
}
private[libs] def executeStream[A](consumer: ResponseHeaders => Iteratee[Array[Byte], A]): Future[Iteratee[Array[Byte], A]] = {
import com.ning.http.client.AsyncHandler
var doneOrError = false
calculator.map(_.sign(this))
var statusCode = 0
val iterateeP = Promise[Iteratee[Array[Byte], A]]()
var iteratee: Iteratee[Array[Byte], A] = null
WS.client.executeRequest(this.build(), new AsyncHandler[Unit]() {
import com.ning.http.client.AsyncHandler.STATE
override def onStatusReceived(status: HttpResponseStatus) = {
statusCode = status.getStatusCode()
STATE.CONTINUE
}
override def onHeadersReceived(h: HttpResponseHeaders) = {
val headers = h.getHeaders()
iteratee = consumer(ResponseHeaders(statusCode, ningHeadersToMap(headers)))
STATE.CONTINUE
}
override def onBodyPartReceived(bodyPart: HttpResponseBodyPart) = {
if (!doneOrError) {
iteratee = iteratee.pureFlatFold {
case Step.Done(a, e) => {
doneOrError = true
val it = Done(a, e)
iterateeP.success(it)
it
}
case Step.Cont(k) => {
k(El(bodyPart.getBodyPartBytes()))
}
case Step.Error(e, input) => {
doneOrError = true
val it = Error(e, input)
iterateeP.success(it)
it
}
}
STATE.CONTINUE
} else {
iteratee = null
// Must close underlying connection, otherwise async http client will drain the stream
bodyPart.closeUnderlyingConnection()
STATE.ABORT
}
}
override def onCompleted() = {
Option(iteratee).map(iterateeP.success(_))
}
override def onThrowable(t: Throwable) = {
iterateeP.failure(t)
}
})
iterateeP.future
}
}
/**
* A WS Request builder.
*/
case class WSRequestHolder(url: String,
headers: Map[String, Seq[String]],
queryString: Map[String, Seq[String]],
calc: Option[SignatureCalculator],
auth: Option[Tuple3[String, String, AuthScheme]],
followRedirects: Option[Boolean],
timeout: Option[Int],
virtualHost: Option[String]) {
/**
* sets the signature calculator for the request
* @param calc
*/
def sign(calc: SignatureCalculator): WSRequestHolder = this.copy(calc = Some(calc))
/**
* sets the authentication realm
* @param calc
*/
def withAuth(username: String, password: String, scheme: AuthScheme): WSRequestHolder =
this.copy(auth = Some((username, password, scheme)))
/**
* adds any number of HTTP headers
* @param hdrs
*/
def withHeaders(hdrs: (String, String)*): WSRequestHolder = {
val headers = hdrs.foldLeft(this.headers)((m, hdr) =>
if (m.contains(hdr._1)) m.updated(hdr._1, m(hdr._1) :+ hdr._2)
else (m + (hdr._1 -> Seq(hdr._2)))
)
this.copy(headers = headers)
}
/**
* adds any number of query string parameters to the
*/
def withQueryString(parameters: (String, String)*): WSRequestHolder =
this.copy(queryString = parameters.foldLeft(queryString) {
case (m, (k, v)) => m + (k -> (v +: m.get(k).getOrElse(Nil)))
})
/**
* Sets whether redirects (301, 302) should be followed automatically
*/
def withFollowRedirects(follow: Boolean): WSRequestHolder =
this.copy(followRedirects = Some(follow))
/**
* Sets the request timeout in milliseconds
*/
def withTimeout(timeout: Int): WSRequestHolder =
this.copy(timeout = Some(timeout))
def withVirtualHost(vh: String): WSRequestHolder = {
this.copy(virtualHost = Some(vh))
}
/**
* performs a get with supplied body
*/
def get(): Future[Response] = prepare("GET").execute
/**
* performs a get with supplied body
* @param consumer that's handling the response
*/
def get[A](consumer: ResponseHeaders => Iteratee[Array[Byte], A]): Future[Iteratee[Array[Byte], A]] =
prepare("GET").executeStream(consumer)
/**
* Perform a POST on the request asynchronously.
*/
def post[T](body: T)(implicit wrt: Writeable[T], ct: ContentTypeOf[T]): Future[Response] = prepare("POST", body).execute
/**
* Perform a POST on the request asynchronously.
* Request body won't be chunked
*/
def post(body: File): Future[Response] = prepare("POST", body).execute
/**
* performs a POST with supplied body
* @param consumer that's handling the response
*/
def postAndRetrieveStream[A, T](body: T)(consumer: ResponseHeaders => Iteratee[Array[Byte], A])(implicit wrt: Writeable[T], ct: ContentTypeOf[T]): Future[Iteratee[Array[Byte], A]] = prepare("POST", body).executeStream(consumer)
/**
* Perform a PUT on the request asynchronously.
*/
def put[T](body: T)(implicit wrt: Writeable[T], ct: ContentTypeOf[T]): Future[Response] = prepare("PUT", body).execute
/**
* Perform a PUT on the request asynchronously.
* Request body won't be chunked
*/
def put(body: File): Future[Response] = prepare("PUT", body).execute
/**
* performs a PUT with supplied body
* @param consumer that's handling the response
*/
def putAndRetrieveStream[A, T](body: T)(consumer: ResponseHeaders => Iteratee[Array[Byte], A])(implicit wrt: Writeable[T], ct: ContentTypeOf[T]): Future[Iteratee[Array[Byte], A]] = prepare("PUT", body).executeStream(consumer)
/**
* Perform a DELETE on the request asynchronously.
*/
def delete(): Future[Response] = prepare("DELETE").execute
/**
* Perform a HEAD on the request asynchronously.
*/
def head(): Future[Response] = prepare("HEAD").execute
/**
* Perform a OPTIONS on the request asynchronously.
*/
def options(): Future[Response] = prepare("OPTIONS").execute
/**
* Execute an arbitrary method on the request asynchronously.
*
* @param method The method to execute
*/
def execute(method: String): Future[Response] = prepare(method).execute
private[play] def prepare(method: String) = {
val request = new WSRequest(method, auth, calc).setUrl(url)
.setHeaders(headers)
.setQueryString(queryString)
followRedirects.map(request.setFollowRedirects(_))
timeout.map { t: Int =>
val config = new PerRequestConfig()
config.setRequestTimeoutInMs(t)
request.setPerRequestConfig(config)
}
virtualHost.map { v =>
request.setVirtualHost(v)
}
request
}
private[play] def prepare(method: String, body: File) = {
import com.ning.http.client.generators.FileBodyGenerator
import java.nio.ByteBuffer
val bodyGenerator = new FileBodyGenerator(body);
val request = new WSRequest(method, auth, calc).setUrl(url)
.setHeaders(headers)
.setQueryString(queryString)
.setBody(bodyGenerator)
followRedirects.map(request.setFollowRedirects(_))
timeout.map { t: Int =>
val config = new PerRequestConfig()
config.setRequestTimeoutInMs(t)
request.setPerRequestConfig(config)
}
virtualHost.map { v =>
request.setVirtualHost(v)
}
request
}
private[play] def prepare[T](method: String, body: T)(implicit wrt: Writeable[T], ct: ContentTypeOf[T]) = {
val request = new WSRequest(method, auth, calc).setUrl(url)
.setHeaders(Map("Content-Type" -> Seq(ct.mimeType.getOrElse("text/plain"))) ++ headers)
.setQueryString(queryString)
.setBody(wrt.transform(body))
followRedirects.map(request.setFollowRedirects(_))
timeout.map { t: Int =>
val config = new PerRequestConfig()
config.setRequestTimeoutInMs(t)
request.setPerRequestConfig(config)
}
virtualHost.map { v =>
request.setVirtualHost(v)
}
request
}
}
}
/**
* A WS HTTP response.
*/
case class Response(ahcResponse: AHCResponse) {
import scala.xml._
import play.api.libs.json._
/**
* Get the underlying response object.
*/
def getAHCResponse = ahcResponse
/**
* The response status code.
*/
def status: Int = ahcResponse.getStatusCode()
/**
* The response status message.
*/
def statusText: String = ahcResponse.getStatusText()
/**
* Get a response header.
*/
def header(key: String): Option[String] = Option(ahcResponse.getHeader(key))
/**
* The response body as String.
*/
lazy val body: String = {
// RFC-2616#3.7.1 states that any text/* mime type should default to ISO-8859-1 charset if not
// explicitly set, while Plays default encoding is UTF-8. So, use UTF-8 if charset is not explicitly
// set and content type is not text/*, otherwise default to ISO-8859-1
val contentType = Option(ahcResponse.getContentType).getOrElse("application/octet-stream")
val charset = Option(AsyncHttpProviderUtils.parseCharset(contentType)).getOrElse {
if (contentType.startsWith("text/"))
AsyncHttpProviderUtils.DEFAULT_CHARSET
else
"utf-8"
}
ahcResponse.getResponseBody(charset)
}
/**
* The response body as Xml.
*/
lazy val xml: Elem = XML.loadString(body)
/**
* The response body as Json.
*/
lazy val json: JsValue = Json.parse(ahcResponse.getResponseBodyAsBytes)
}
/**
* An HTTP response header (the body has not been retrieved yet)
*/
case class ResponseHeaders(status: Int, headers: Map[String, Seq[String]])
/**
* Sign a WS call.
*/
trait SignatureCalculator {
/**
* Sign it.
*/
def sign(request: WS.WSRequest)
}