Rest DSL. camel-swagger work in progress.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/3f821b09 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/3f821b09 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/3f821b09 Branch: refs/heads/master Commit: 3f821b0975470a9aebe03029dd9d4d765b209e5d Parents: 8b3d645 Author: Claus Ibsen <davscl...@apache.org> Authored: Sun Aug 10 10:36:30 2014 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Sun Aug 10 10:39:59 2014 +0200 ---------------------------------------------------------------------- .../component/swagger/RestApiListingCache.scala | 11 ++--- .../RestSwaggerApiDeclarationServlet.scala | 11 +++-- .../component/swagger/RestSwaggerReader.scala | 47 +++++++++++++++++--- 3 files changed, 53 insertions(+), 16 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/3f821b09/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/RestApiListingCache.scala ---------------------------------------------------------------------- diff --git a/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/RestApiListingCache.scala b/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/RestApiListingCache.scala index 494e620..65abddf 100644 --- a/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/RestApiListingCache.scala +++ b/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/RestApiListingCache.scala @@ -18,8 +18,10 @@ package org.apache.camel.component.swagger import com.wordnik.swagger.core.util.ReaderUtil import com.wordnik.swagger.config.SwaggerConfig +import com.wordnik.swagger.model.ApiListing + import org.apache.camel.CamelContext -import com.wordnik.swagger.model.{Operation, ApiListing} + import scala.collection.mutable.ListBuffer // to iterate Java list using for loop @@ -36,10 +38,9 @@ object RestApiListingCache extends ReaderUtil { val rests = camel.getRestDefinitions.asScala for (rest <- rests) { - // TODO: this can be nicer - val option = reader.read(rest, config) - if (option.get != null) { - listings += option.get + val some = reader.read(rest, config) + if (!some.isEmpty) { + listings += some.get } } http://git-wip-us.apache.org/repos/asf/camel/blob/3f821b09/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/RestSwaggerApiDeclarationServlet.scala ---------------------------------------------------------------------- diff --git a/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/RestSwaggerApiDeclarationServlet.scala b/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/RestSwaggerApiDeclarationServlet.scala index 11af449..fb72242 100644 --- a/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/RestSwaggerApiDeclarationServlet.scala +++ b/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/RestSwaggerApiDeclarationServlet.scala @@ -43,7 +43,6 @@ class RestSwaggerApiDeclarationServlet extends HttpServlet { override def init(config: ServletConfig): Unit = { super.init(config) - LOG.info("init") // configure swagger options var s = config.getInitParameter("api.version") @@ -99,13 +98,12 @@ class RestSwaggerApiDeclarationServlet extends HttpServlet { * Renders the resource listing which is the overview of all the apis */ def renderResourceListing(request: HttpServletRequest, response: HttpServletResponse) = { - LOG.info("renderResourceListing") + LOG.trace("renderResourceListing") val queryParams = Map[String, List[String]]() val cookies = Map[String, String]() val headers = Map[String, List[String]]() - LOG.info("renderResourceListing camel -> {}", camel) if (camel != null) { val f = new SpecFilter val listings = RestApiListingCache.listing(camel, swaggerConfig).map(specs => { @@ -123,7 +121,7 @@ class RestSwaggerApiDeclarationServlet extends HttpServlet { List(), swaggerConfig.info ) - LOG.info("renderResourceListing write response -> {}", resourceListing) + LOG.debug("renderResourceListing write response -> {}", resourceListing) response.getOutputStream.write(JsonSerializer.asJson(resourceListing).getBytes("utf-8")) } else { response.setStatus(204) @@ -134,6 +132,8 @@ class RestSwaggerApiDeclarationServlet extends HttpServlet { * Renders the api listing of a single resource */ def renderApiDeclaration(request: HttpServletRequest, response: HttpServletResponse) = { + LOG.trace("renderApiDeclaration") + val route = request.getPathInfo val docRoot = request.getPathInfo val f = new SpecFilter @@ -142,7 +142,6 @@ class RestSwaggerApiDeclarationServlet extends HttpServlet { val headers = Map[String, List[String]]() val pathPart = docRoot - LOG.info("renderApiDeclaration camel -> {}", camel) if (camel != null) { val listings = RestApiListingCache.listing(camel, swaggerConfig).map(specs => { (for (spec <- specs.values) yield { @@ -151,7 +150,7 @@ class RestSwaggerApiDeclarationServlet extends HttpServlet { }).toList.flatten listings.size match { case 1 => { - LOG.info("renderResourceListing write response -> {}", listings.head) + LOG.debug("renderResourceListing write response -> {}", listings.head) response.getOutputStream.write(JsonSerializer.asJson(listings.head).getBytes("utf-8")) } case _ => response.setStatus(404) http://git-wip-us.apache.org/repos/asf/camel/blob/3f821b09/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/RestSwaggerReader.scala ---------------------------------------------------------------------- diff --git a/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/RestSwaggerReader.scala b/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/RestSwaggerReader.scala index 39f0f56..f9fe70f 100644 --- a/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/RestSwaggerReader.scala +++ b/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/RestSwaggerReader.scala @@ -6,7 +6,7 @@ * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -25,7 +25,7 @@ import com.wordnik.swagger.core.SwaggerSpec import org.apache.camel.model.rest.{VerbDefinition, RestDefinition} import org.apache.camel.util.FileUtil - import org.slf4j.LoggerFactory +import org.slf4j.LoggerFactory import scala.collection.mutable.ListBuffer @@ -48,11 +48,20 @@ class RestSwaggerReader { } } - // TOOD: register classloader + // TODO: add parameters to operations + // - {id} is a path type, and required + // - type/typeList/outType/outTypeList is body type and required def read(rest: RestDefinition, config: SwaggerConfig): Option[ApiListing] = { - val resourcePath = rest.getPath + var resourcePath = rest.getPath + // resource path must start with slash + if (resourcePath == null) { + resourcePath = "" + } + if (!resourcePath.startsWith("/")) { + resourcePath = "/" + resourcePath + } // create a list of apis val apis = new ListBuffer[ApiDescription] @@ -82,8 +91,13 @@ class RestSwaggerReader { } path = verb.getUri + + // the method must be in upper case var method = verb.asVerb().toUpperCase(Locale.US) + // create an unique nickname using the method and paths + var nickName = createNickname(verb.asVerb(), buildUrl(resourcePath, path)) + var responseType = verb.getOutType match { case e: String => e case _ => "java.lang.Void" @@ -104,7 +118,7 @@ class RestSwaggerReader { "", "", responseType, - "", + nickName, 0, produces, consumes, @@ -154,4 +168,27 @@ class RestSwaggerReader { else None } + def createNickname(method: String, absPath : String): String = { + val s = method + "/" + absPath + val arr = s.split("\\/") + val r = arr.foldLeft("") { + (a, b) => a + toTitleCase(sanitizeNickname(b)) + } + // first char should be lower + r.charAt(0).toLower + r.substring(1) + } + + def toTitleCase(s: String): String = { + if (s.size > 0) { + s.charAt(0).toUpper + s.substring(1) + } else { + s + } + } + + def sanitizeNickname(s: String): String = { + // nick name must only be alpha chars + s.replaceAll("\\W", "") + } + }