From 592d59ceb26d2c537691a262ef681537f0b7ac9b 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 --- .../codegen/languages/ScalaClientCodegen.java | 1 + .../main/resources/scala/apiInvoker.mustache | 28 +++++++++++++++---- .../src/main/resources/scala/pom.mustache | 7 +++++ 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/ScalaClientCodegen.java b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/ScalaClientCodegen.java index 6c845a9ccb7..697114f79bf 100644 --- a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/ScalaClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/ScalaClientCodegen.java @@ -43,6 +43,7 @@ public class ScalaClientCodegen extends DefaultCodegen implements CodegenConfig additionalProperties.put("groupId", groupId); additionalProperties.put("artifactId", artifactId); additionalProperties.put("artifactVersion", artifactVersion); + additionalProperties.put("asyncHttpClient", false); supportingFiles.add(new SupportingFile("pom.mustache", "", "pom.xml")); supportingFiles.add(new SupportingFile("apiInvoker.mustache", diff --git a/modules/swagger-codegen/src/main/resources/scala/apiInvoker.mustache b/modules/swagger-codegen/src/main/resources/scala/apiInvoker.mustache index 5c6d60d37b5..b8f588a9aa0 100644 --- a/modules/swagger-codegen/src/main/resources/scala/apiInvoker.mustache +++ b/modules/swagger-codegen/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/modules/swagger-codegen/src/main/resources/scala/pom.mustache b/modules/swagger-codegen/src/main/resources/scala/pom.mustache index ae3779d7805..1163b7d381a 100644 --- a/modules/swagger-codegen/src/main/resources/scala/pom.mustache +++ b/modules/swagger-codegen/src/main/resources/scala/pom.mustache @@ -165,6 +165,12 @@ jersey-multipart ${jersey-version} + + org.jfarcand + jersey-ahc-client + ${jersey-async-version} + compile + org.scala-lang scala-library @@ -204,6 +210,7 @@ 2.2 1.7 1.5.0-M1 + 1.0.5 1.0.0 2.4.2