-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #280 from UdashFramework/udash-rest-jetty
rest-jetty module with Jetty based REST client
- Loading branch information
Showing
12 changed files
with
215 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 0 additions & 36 deletions
36
rest/.jvm/src/test/scala/io/udash/rest/HttpRestCallTest.scala
This file was deleted.
Oops, something went wrong.
22 changes: 22 additions & 0 deletions
22
rest/.jvm/src/test/scala/io/udash/rest/ServletBasedRestApiTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.udash | ||
package rest | ||
|
||
import org.eclipse.jetty.server.Server | ||
import org.eclipse.jetty.servlet.{ServletHandler, ServletHolder} | ||
|
||
import scala.concurrent.duration._ | ||
|
||
abstract class ServletBasedRestApiTest extends RestApiTest with UsesHttpServer { | ||
override implicit def patienceConfig: PatienceConfig = PatienceConfig(10.seconds) | ||
|
||
def maxPayloadSize: Int = 1024 * 1024 | ||
def serverTimeout: FiniteDuration = 10.seconds | ||
|
||
protected def setupServer(server: Server): Unit = { | ||
val servlet = new RestServlet(serverHandle, serverTimeout, maxPayloadSize) | ||
val holder = new ServletHolder(servlet) | ||
val handler = new ServletHandler | ||
handler.addServletWithMapping(holder, "/api/*") | ||
server.setHandler(handler) | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
rest/.jvm/src/test/scala/io/udash/rest/SttpRestCallTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package io.udash | ||
package rest | ||
|
||
import com.softwaremill.sttp.SttpBackend | ||
import io.udash.rest.raw.HttpErrorException | ||
import io.udash.rest.raw.RawRest.HandleRequest | ||
|
||
import scala.concurrent.Future | ||
import scala.concurrent.duration._ | ||
|
||
trait SttpClientRestTest extends ServletBasedRestApiTest { | ||
implicit val backend: SttpBackend[Future, Nothing] = SttpRestClient.defaultBackend() | ||
|
||
def clientHandle: HandleRequest = | ||
SttpRestClient.asHandleRequest(s"$baseUrl/api") | ||
|
||
override protected def afterAll(): Unit = { | ||
backend.close() | ||
super.afterAll() | ||
} | ||
} | ||
|
||
class SttpRestCallTest extends SttpClientRestTest with RestApiTestScenarios { | ||
def port: Int = 9090 | ||
|
||
test("too large binary request") { | ||
val future = proxy.binaryEcho(Array.fill[Byte](maxPayloadSize + 1)(5)) | ||
val exception = future.failed.futureValue | ||
assert(exception == HttpErrorException(413, "Payload is larger than maximum 1048576 bytes (1048577)")) | ||
} | ||
} | ||
|
||
class ServletTimeoutTest extends SttpClientRestTest { | ||
def port: Int = 9091 | ||
override def serverTimeout: FiniteDuration = 1.millisecond | ||
|
||
test("rest method timeout") { | ||
val exception = proxy.neverGet.failed.futureValue | ||
assert(exception == HttpErrorException(500, "server operation timed out")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
rest/jetty/src/main/scala/io/udash/rest/jetty/JettyRestClient.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package io.udash | ||
package rest.jetty | ||
|
||
import java.net.HttpCookie | ||
import java.nio.charset.Charset | ||
|
||
import com.avsystem.commons._ | ||
import com.avsystem.commons.annotation.explicitGenerics | ||
import io.udash.rest.raw._ | ||
import org.eclipse.jetty.client.HttpClient | ||
import org.eclipse.jetty.client.api.Result | ||
import org.eclipse.jetty.client.util.{BufferingResponseListener, BytesContentProvider, StringContentProvider} | ||
import org.eclipse.jetty.http.{HttpHeader, MimeTypes} | ||
|
||
import scala.util.{Failure, Success} | ||
import scala.concurrent.duration._ | ||
|
||
object JettyRestClient { | ||
final val DefaultMaxResponseLength = 2 * 1024 * 1024 | ||
final val DefaultTimeout = 10.seconds | ||
|
||
@explicitGenerics def apply[RestApi: RawRest.AsRealRpc : RestMetadata]( | ||
client: HttpClient, | ||
baseUri: String, | ||
maxResponseLength: Int = DefaultMaxResponseLength, | ||
timeout: Duration = DefaultTimeout | ||
): RestApi = | ||
RawRest.fromHandleRequest[RestApi](asHandleRequest(client, baseUri, maxResponseLength, timeout)) | ||
|
||
def asHandleRequest( | ||
client: HttpClient, | ||
baseUrl: String, | ||
maxResponseLength: Int = DefaultMaxResponseLength, | ||
timeout: Duration = DefaultTimeout | ||
): RawRest.HandleRequest = | ||
RawRest.safeHandle { request => | ||
callback => | ||
val path = baseUrl + PlainValue.encodePath(request.parameters.path) | ||
val httpReq = client.newRequest(baseUrl).method(request.method.name) | ||
|
||
httpReq.path(path) | ||
request.parameters.query.entries.foreach { | ||
case (name, PlainValue(value)) => httpReq.param(name, value) | ||
} | ||
request.parameters.headers.entries.foreach { | ||
case (name, PlainValue(value)) => httpReq.header(name, value) | ||
} | ||
request.parameters.cookies.entries.foreach { | ||
case (name, PlainValue(value)) => httpReq.cookie(new HttpCookie(name, value)) | ||
} | ||
|
||
request.body match { | ||
case HttpBody.Empty => | ||
case tb: HttpBody.Textual => | ||
httpReq.content(new StringContentProvider(tb.contentType, tb.content, Charset.forName(tb.charset))) | ||
case bb: HttpBody.Binary => | ||
httpReq.content(new BytesContentProvider(bb.contentType, bb.bytes)) | ||
} | ||
|
||
timeout match { | ||
case fd: FiniteDuration => httpReq.timeout(fd.length, fd.unit) | ||
case _ => | ||
} | ||
|
||
httpReq.send(new BufferingResponseListener(maxResponseLength) { | ||
override def onComplete(result: Result): Unit = | ||
if (result.isSucceeded) { | ||
val httpResp = result.getResponse | ||
val contentTypeOpt = httpResp.getHeaders.get(HttpHeader.CONTENT_TYPE).opt | ||
val charsetOpt = contentTypeOpt.map(MimeTypes.getCharsetFromContentType) | ||
val body = (contentTypeOpt, charsetOpt) match { | ||
case (Opt(contentType), Opt(charset)) => | ||
HttpBody.textual(getContentAsString, MimeTypes.getContentTypeWithoutCharset(contentType), charset) | ||
case (Opt(contentType), Opt.Empty) => | ||
HttpBody.binary(getContent, contentType) | ||
case _ => | ||
HttpBody.Empty | ||
} | ||
val headers = httpResp.getHeaders.iterator.asScala.map(h => (h.getName, PlainValue(h.getValue))).toList | ||
val response = RestResponse(httpResp.getStatus, IMapping(headers), body) | ||
callback(Success(response)) | ||
} else { | ||
callback(Failure(result.getFailure)) | ||
} | ||
}) | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
rest/jetty/src/test/scala/io/udash/rest/jetty/JettyRestCallTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package io.udash | ||
package rest.jetty | ||
|
||
import io.udash.rest.raw.RawRest.HandleRequest | ||
import io.udash.rest.{RestApiTestScenarios, ServletBasedRestApiTest} | ||
import org.eclipse.jetty.client.HttpClient | ||
|
||
final class JettyRestCallTest extends ServletBasedRestApiTest with RestApiTestScenarios { | ||
def port: Int = 9092 | ||
val client: HttpClient = new HttpClient | ||
|
||
def clientHandle: HandleRequest = | ||
JettyRestClient.asHandleRequest(client, s"$baseUrl/api", maxPayloadSize) | ||
|
||
override protected def beforeAll(): Unit = { | ||
super.beforeAll() | ||
client.start() | ||
} | ||
|
||
override protected def afterAll(): Unit = { | ||
client.stop() | ||
super.afterAll() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters