From 2386e394aed056d44fbd45ab4abf2dd771872540 Mon Sep 17 00:00:00 2001 From: Luca Milanesio Date: Mon, 9 Feb 2015 18:46:09 +0000 Subject: [PATCH] Support for HTTP Async client transport for Jersey on Scala When generating Scala client, the ApiInvoker is a class with overridable options, avoiding the need of public vars to be manually modified. Additionally it is possible to plug the AsyncHttp protocol for Jersey in order to achieve Netty support and maximum scalability. NOTE: Support for Scala 2.10.0-3 removed as largely obsolete --- build.sbt | 2 +- sbt | 2 +- src/main/resources/scala/apiInvoker.mustache | 28 +++++++++++++++---- src/main/resources/scala/pom.mustache | 7 +++++ .../swagger/codegen/BasicScalaGenerator.scala | 3 +- 5 files changed, 34 insertions(+), 8 deletions(-) diff --git a/build.sbt b/build.sbt index bcc9df96b82..a221718800e 100644 --- a/build.sbt +++ b/build.sbt @@ -13,7 +13,7 @@ javacOptions ++= Seq("-target", "1.6", "-source", "1.6", "-Xlint:unchecked", "-X scalacOptions ++= Seq("-optimize", "-unchecked", "-deprecation", "-Xcheckinit", "-encoding", "utf8") -crossScalaVersions := Seq("2.10.0", "2.10.1", "2.10.2", "2.10.3", "2.10.4", "2.11.0", "2.11.1") +crossScalaVersions := Seq("2.10.4", "2.11.0", "2.11.1") scalaVersion := "2.10.4" diff --git a/sbt b/sbt index 491debf4fb3..82ebbfc72ab 100755 --- a/sbt +++ b/sbt @@ -128,7 +128,7 @@ declare -r default_jvm_opts="-Dfile.encoding=UTF8 -XX:MaxPermSize=256m -Xms512m declare -r noshare_opts="-Dsbt.global.base=project/.sbtboot -Dsbt.boot.directory=project/.boot -Dsbt.ivy.home=project/.ivy" declare -r latest_28="2.8.2" declare -r latest_29="2.9.3" -declare -r latest_210="2.10.0" +declare -r latest_210="2.10.4" declare -r script_path=$(get_script_path "$BASH_SOURCE") declare -r script_dir="$(dirname $script_path)" diff --git a/src/main/resources/scala/apiInvoker.mustache b/src/main/resources/scala/apiInvoker.mustache index 5c6d60d37b5..b8f588a9aa0 100644 --- a/src/main/resources/scala/apiInvoker.mustache +++ b/src/main/resources/scala/apiInvoker.mustache @@ -35,10 +35,12 @@ object ScalaJsonUtil { } } -object ApiInvoker { - val mapper = ScalaJsonUtil.getJsonMapper - val defaultHeaders: HashMap[String, String] = HashMap() - val hostMap: HashMap[String, Client] = HashMap() +class ApiInvoker(val mapper: ObjectMapper = ScalaJsonUtil.getJsonMapper, + httpHeaders: HashMap[String, String] = HashMap(), + hostMap: HashMap[String, Client] = HashMap(), + asyncHttpClient: Boolean = false) { + + var defaultHeaders: HashMap[String, String] = httpHeaders def escape(value: String): String = { URLEncoder.encode(value, "utf-8").replaceAll("\\+", "%20") @@ -151,14 +153,30 @@ object ApiInvoker { hostMap.contains(host) match { case true => hostMap(host) case false => { - val client = Client.create() + val client = newClient(host) // client.addFilter(new LoggingFilter()) hostMap += host -> client client } } } + + def newClient(host: String): Client = asyncHttpClient match { + case true => { + import org.sonatype.spice.jersey.client.ahc.config.DefaultAhcConfig + import org.sonatype.spice.jersey.client.ahc.AhcHttpClient + + val config: DefaultAhcConfig = new DefaultAhcConfig() + AhcHttpClient.create(config) + } + case _ => Client.create() + } } +object ApiInvoker extends ApiInvoker(mapper = ScalaJsonUtil.getJsonMapper, + httpHeaders = HashMap(), + hostMap = HashMap(), + asyncHttpClient = {{asyncHttpClient}}) + class ApiException(val code: Int, msg: String) extends RuntimeException(msg) diff --git a/src/main/resources/scala/pom.mustache b/src/main/resources/scala/pom.mustache index d5ad2d05a09..77ebf74d252 100644 --- a/src/main/resources/scala/pom.mustache +++ b/src/main/resources/scala/pom.mustache @@ -162,6 +162,12 @@ ${jersey-version} compile + + org.jfarcand + jersey-ahc-client + ${jersey-async-version} + compile + org.scala-lang scala-library @@ -221,6 +227,7 @@ 1.7 + 1.0.5 4.8.1 1.0.0 4.8.1 diff --git a/src/main/scala/com/wordnik/swagger/codegen/BasicScalaGenerator.scala b/src/main/scala/com/wordnik/swagger/codegen/BasicScalaGenerator.scala index e4850a61544..c9652971764 100644 --- a/src/main/scala/com/wordnik/swagger/codegen/BasicScalaGenerator.scala +++ b/src/main/scala/com/wordnik/swagger/codegen/BasicScalaGenerator.scala @@ -212,7 +212,8 @@ class BasicScalaGenerator extends BasicGenerator { additionalParams ++= Map( "artifactId" -> "scala-client", "artifactVersion" -> "1.0.0", - "groupId" -> "com.wordnik") + "groupId" -> "com.wordnik", + "asyncHttpClient" -> "false") // supporting classes override def supportingFiles = List(