diff --git a/src/main/resources/scala/api.mustache b/src/main/resources/scala/api.mustache index 25b8e75cd907..494b597c8be5 100644 --- a/src/main/resources/scala/api.mustache +++ b/src/main/resources/scala/api.mustache @@ -4,6 +4,9 @@ package {{package}} {{/imports}} import {{invokerPackage}}.ApiInvoker import {{invokerPackage}}.ApiException + +import java.io.File + import scala.collection.mutable.HashMap {{#operations}} @@ -19,7 +22,16 @@ class {{classname}} { val path = "{{path}}".replaceAll("\\{format\\}","json"){{#pathParams}}.replaceAll("\\{" + "{{baseName}}" + "\\}",apiInvoker.escapeString({{paramName}})) {{/pathParams}} - + + {{newline}} + val contentType = { + {{#bodyParam}}if({{bodyParam}} != null && {{bodyParam}}.isInstanceOf[File] ) + "multipart/form-data" + else "application/json" + {{/bodyParam}} + {{^bodyParam}}"application/json"{{/bodyParam}} + } + // query params val queryParams = new HashMap[String, String] val headerParams = new HashMap[String, String] @@ -39,7 +51,7 @@ class {{classname}} { {{/headerParams}} try { - apiInvoker.invokeApi(basePath, path, "{{httpMethod}}", queryParams.toMap, {{#bodyParam}}{{bodyParam}}{{/bodyParam}}{{^bodyParam}}None{{/bodyParam}}, headerParams.toMap) match { + apiInvoker.invokeApi(basePath, path, "{{httpMethod}}", queryParams.toMap, {{#bodyParam}}{{bodyParam}}{{/bodyParam}}{{^bodyParam}}None{{/bodyParam}}, headerParams.toMap, contentType) match { case s: String => {{#returnType}} Some(ApiInvoker.deserialize(s, "{{returnContainer}}", classOf[{{returnBaseType}}]).asInstanceOf[{{returnType}}]) {{/returnType}} diff --git a/src/main/resources/scala/apiInvoker.mustache b/src/main/resources/scala/apiInvoker.mustache index 21fd676f41e7..58c1c6123498 100644 --- a/src/main/resources/scala/apiInvoker.mustache +++ b/src/main/resources/scala/apiInvoker.mustache @@ -6,6 +6,10 @@ import com.sun.jersey.api.client.config.ClientConfig import com.sun.jersey.api.client.config.DefaultClientConfig import com.sun.jersey.api.client.filter.LoggingFilter +import com.sun.jersey.multipart.FormDataMultiPart +import com.sun.jersey.multipart.file.FileDataBodyPart + +import java.io.File import java.net.URLEncoder import javax.ws.rs.core.MediaType @@ -75,11 +79,11 @@ object ApiInvoker { } else null } - def invokeApi(host: String, path: String, method: String, queryParams: Map[String, String], body: AnyRef, headerParams: Map[String, String]) = { + def invokeApi(host: String, path: String, method: String, queryParams: Map[String, String], body: AnyRef, headerParams: Map[String, String], contentType: String) = { val client = getClient(host) val querystring = queryParams.filter(k => k._2 != null).map(k => (escapeString(k._1) + "=" + escapeString(k._2))).mkString("?", "&", "") - val builder = client.resource(host + path + querystring).`type`("application/json") + val builder = client.resource(host + path + querystring).`type`(contentType) headerParams.map(p => builder.header(p._1, p._2)) defaultHeaders.map(p => { @@ -94,7 +98,16 @@ object ApiInvoker { builder.get(classOf[ClientResponse]).asInstanceOf[ClientResponse] } case "POST" => { - builder.post(classOf[ClientResponse], serialize(body)) + if(body != null && body.isInstanceOf[File]) { + val file = body.asInstanceOf[File] + val form = new FormDataMultiPart() + form.field("filename", file.getName()) + form.bodyPart(new FileDataBodyPart("file", file, MediaType.MULTIPART_FORM_DATA_TYPE)) + builder.post(classOf[ClientResponse], form) + } + else { + builder.post(classOf[ClientResponse], serialize(body)) + } } case "PUT" => { builder.put(classOf[ClientResponse], serialize(body)) @@ -134,3 +147,4 @@ class ApiException extends Exception { this() } } + diff --git a/src/main/resources/scala/pom.mustache b/src/main/resources/scala/pom.mustache index db4d42b573ee..d66ac2dfe330 100644 --- a/src/main/resources/scala/pom.mustache +++ b/src/main/resources/scala/pom.mustache @@ -11,11 +11,6 @@ - - scala-tools.org - Scala-Tools Maven2 Repository - http://scala-tools.org/repo-releases - maven-mongodb-plugin-repo maven mongodb plugin repository @@ -161,6 +156,12 @@ ${jersey-version} compile + + com.sun.jersey.contribs + jersey-multipart + ${jersey-version} + compile + org.scala-lang scala-library @@ -196,4 +197,4 @@ 4.8.1 1.6.1 - \ No newline at end of file +