11package io .udash
22package rest .jetty
33
4- import com .avsystem .commons ._
4+ import com .avsystem .commons .*
55import com .avsystem .commons .annotation .explicitGenerics
6- import io .udash .rest .raw ._
6+ import io .udash .rest .raw .*
77import io .udash .utils .URLEncoder
88import monix .eval .Task
9- import org .eclipse .jetty .client .{BufferingResponseListener , BytesRequestContent , HttpClient , Result , StringRequestContent }
9+ import monix .execution .Callback
10+ import org .eclipse .jetty .client .*
1011import org .eclipse .jetty .http .{HttpCookie , HttpHeader , MimeTypes }
1112
1213import java .nio .charset .Charset
13- import scala .concurrent .duration . _
14- import scala .util .{ Failure , Success }
14+ import scala .concurrent .CancellationException
15+ import scala .concurrent . duration . *
1516
1617object JettyRestClient {
1718 final val DefaultMaxResponseLength = 2 * 1024 * 1024
@@ -31,55 +32,57 @@ object JettyRestClient {
3132 maxResponseLength : Int = DefaultMaxResponseLength ,
3233 timeout : Duration = DefaultTimeout
3334 ): RawRest .HandleRequest =
34- request => Task .async { callback =>
35- val path = baseUrl + PlainValue .encodePath(request.parameters.path)
36- val httpReq = client.newRequest( baseUrl).method (request.method.name )
35+ request => Task (client.newRequest(baseUrl).method(request.method.name)).flatMap { httpReq =>
36+ Task .async { ( callback : Callback [ Throwable , RestResponse ]) =>
37+ val path = baseUrl + PlainValue .encodePath (request.parameters.path )
3738
38- httpReq.path(path)
39- request.parameters.query.entries.foreach {
40- case (name, PlainValue (value)) => httpReq.param(name, value)
41- }
42- request.parameters.headers.entries.foreach {
43- case (name, PlainValue (value)) => httpReq.headers(headers => headers.add(name, value))
44- }
45- request.parameters.cookies.entries.foreach {
46- case (name, PlainValue (value)) => httpReq.cookie(HttpCookie .build(
47- URLEncoder .encode(name, spaceAsPlus = true ), URLEncoder .encode(value, spaceAsPlus = true )).build())
48- }
49-
50- request.body match {
51- case HttpBody .Empty =>
52- case tb : HttpBody .Textual =>
53- httpReq.body(new StringRequestContent (tb.contentType, tb.content, Charset .forName(tb.charset)))
54- case bb : HttpBody .Binary =>
55- httpReq.body(new BytesRequestContent (bb.contentType, bb.bytes))
56- }
39+ httpReq.path(path)
40+ request.parameters.query.entries.foreach {
41+ case (name, PlainValue (value)) => httpReq.param(name, value)
42+ }
43+ request.parameters.headers.entries.foreach {
44+ case (name, PlainValue (value)) => httpReq.headers(headers => headers.add(name, value))
45+ }
46+ request.parameters.cookies.entries.foreach {
47+ case (name, PlainValue (value)) => httpReq.cookie(HttpCookie .build(
48+ URLEncoder .encode(name, spaceAsPlus = true ), URLEncoder .encode(value, spaceAsPlus = true )).build())
49+ }
5750
58- timeout match {
59- case fd : FiniteDuration => httpReq.timeout(fd.length, fd.unit)
60- case _ =>
61- }
51+ request.body match {
52+ case HttpBody .Empty =>
53+ case tb : HttpBody .Textual =>
54+ httpReq.body(new StringRequestContent (tb.contentType, tb.content, Charset .forName(tb.charset)))
55+ case bb : HttpBody .Binary =>
56+ httpReq.body(new BytesRequestContent (bb.contentType, bb.bytes))
57+ }
6258
63- httpReq.send(new BufferingResponseListener (maxResponseLength) {
64- override def onComplete (result : Result ): Unit =
65- if (result.isSucceeded) {
66- val httpResp = result.getResponse
67- val contentTypeOpt = httpResp.getHeaders.get(HttpHeader .CONTENT_TYPE ).opt
68- val charsetOpt = contentTypeOpt.map(MimeTypes .getCharsetFromContentType)
69- val body = (contentTypeOpt, charsetOpt) match {
70- case (Opt (contentType), Opt (charset)) =>
71- HttpBody .textual(getContentAsString, MimeTypes .getContentTypeWithoutCharset(contentType), charset)
72- case (Opt (contentType), Opt .Empty ) =>
73- HttpBody .binary(getContent, contentType)
74- case _ =>
75- HttpBody .Empty
76- }
77- val headers = httpResp.getHeaders.asScala.iterator.map(h => (h.getName, PlainValue (h.getValue))).toList
78- val response = RestResponse (httpResp.getStatus, IMapping (headers), body)
79- callback(Success (response))
80- } else {
81- callback(Failure (result.getFailure))
59+ timeout match {
60+ case fd : FiniteDuration => httpReq.timeout(fd.length, fd.unit)
61+ case _ =>
8262 }
83- })
63+
64+ httpReq.send(new BufferingResponseListener (maxResponseLength) {
65+ override def onComplete (result : Result ): Unit =
66+ if (result.isSucceeded) {
67+ val httpResp = result.getResponse
68+ val contentTypeOpt = httpResp.getHeaders.get(HttpHeader .CONTENT_TYPE ).opt
69+ val charsetOpt = contentTypeOpt.map(MimeTypes .getCharsetFromContentType)
70+ val body = (contentTypeOpt, charsetOpt) match {
71+ case (Opt (contentType), Opt (charset)) =>
72+ HttpBody .textual(getContentAsString, MimeTypes .getContentTypeWithoutCharset(contentType), charset)
73+ case (Opt (contentType), Opt .Empty ) =>
74+ HttpBody .binary(getContent, contentType)
75+ case _ =>
76+ HttpBody .Empty
77+ }
78+ val headers = httpResp.getHeaders.asScala.iterator.map(h => (h.getName, PlainValue (h.getValue))).toList
79+ val response = RestResponse (httpResp.getStatus, IMapping (headers), body)
80+ callback(Success (response))
81+ } else {
82+ callback(Failure (result.getFailure))
83+ }
84+ })
85+ }
86+ .doOnCancel(Task (httpReq.abort(new CancellationException (" Request cancelled" ))))
8487 }
8588}
0 commit comments