You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2014/08/10 15:48:11 UTC

[1/2] git commit: Rest DSL. camel-swagger work in progress.

Repository: camel
Updated Branches:
  refs/heads/master 753bf7410 -> 7113dee32


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/0472b3e2
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/0472b3e2
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/0472b3e2

Branch: refs/heads/master
Commit: 0472b3e264e3f9a6519b48bf151bd1b3f76ede35
Parents: 753bf74
Author: Claus Ibsen <da...@apache.org>
Authored: Sun Aug 10 15:26:45 2014 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sun Aug 10 15:26:45 2014 +0200

----------------------------------------------------------------------
 .../component/swagger/RestSwaggerReader.scala   | 81 ++++++++++++++++----
 1 file changed, 67 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/0472b3e2/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 7522595..aa48d61 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
@@ -19,7 +19,7 @@ package org.apache.camel.component.swagger
 import java.util.Locale
 
 import com.wordnik.swagger.config.SwaggerConfig
-import com.wordnik.swagger.model.{ApiDescription, Operation, ApiListing}
+import com.wordnik.swagger.model._
 import com.wordnik.swagger.core.util.ModelUtil
 import com.wordnik.swagger.core.SwaggerSpec
 
@@ -28,6 +28,11 @@ import org.apache.camel.util.FileUtil
 import org.slf4j.LoggerFactory
 
 import scala.collection.mutable.ListBuffer
+import com.wordnik.swagger.model.Parameter
+import com.wordnik.swagger.model.ApiDescription
+import scala.Some
+import com.wordnik.swagger.model.Operation
+import com.wordnik.swagger.model.ApiListing
 
 // to iterate Java list using for loop
 import scala.collection.JavaConverters._
@@ -36,18 +41,6 @@ class RestSwaggerReader {
 
   private val LOG = LoggerFactory.getLogger(classOf[RestSwaggerReader])
 
-  def buildUrl(path1: String, path2: String): String = {
-    val s1 = FileUtil.stripTrailingSeparator(path1)
-    val s2 = FileUtil.stripLeadingSeparator(path2)
-    if (s1 != null && s2 != null) {
-      s1 + "/" + s2
-    } else if (path1 != null) {
-      path1
-    } else {
-      path2
-    }
-  }
-
   // TODO: add parameters to operations
   // - {id} is a path type, and required
   // - type/outType is body type and required
@@ -114,6 +107,8 @@ class RestSwaggerReader {
         case _ => List()
       }
 
+
+
       operations += Operation(
         method,
         "",
@@ -125,7 +120,7 @@ class RestSwaggerReader {
         consumes,
         List(),
         List(),
-        List(),
+        createParameters(verb, buildUrl(resourcePath, path)),
         List(),
         None)
     }
@@ -169,6 +164,52 @@ class RestSwaggerReader {
     else None
   }
 
+  def createParameters(verb: VerbDefinition, absPath : String): List[Parameter] = {
+    val parameters = new ListBuffer[Parameter]
+
+    // each {} is a parameter
+    val arr = absPath.split("\\/")
+    for (a <- arr) {
+      if (a.startsWith("{") && a.endsWith("}")) {
+        var key = a.substring(1, a.length - 1)
+
+        parameters += Parameter(
+          key,
+          None,
+          None,
+          true,
+          false,
+          "string",
+          AnyAllowableValues,
+          "path",
+          None
+        )
+      }
+    }
+
+    // if we have input type then its a body parameter
+    if (verb.getType != null) {
+      var bodyType = verb.getType
+      if (bodyType.endsWith("[]")) {
+        bodyType = "List[" + bodyType.substring(0, bodyType.length - 2) + "]"
+      }
+
+      parameters += Parameter(
+        "body",
+        None,
+        None,
+        true,
+        false,
+        bodyType,
+        AnyAllowableValues,
+        "body",
+        None
+      )
+    }
+
+    parameters.toList
+  }
+
   def createNickname(method: String, absPath : String): String = {
     val s = method + "/" + absPath
     val arr = s.split("\\/")
@@ -192,4 +233,16 @@ class RestSwaggerReader {
     s.replaceAll("\\W", "")
   }
 
+  def buildUrl(path1: String, path2: String): String = {
+    val s1 = FileUtil.stripTrailingSeparator(path1)
+    val s2 = FileUtil.stripLeadingSeparator(path2)
+    if (s1 != null && s2 != null) {
+      s1 + "/" + s2
+    } else if (path1 != null) {
+      path1
+    } else {
+      path2
+    }
+  }
+
 }


[2/2] git commit: Rest DSL. camel-swagger work in progress.

Posted by da...@apache.org.
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/7113dee3
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/7113dee3
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/7113dee3

Branch: refs/heads/master
Commit: 7113dee32c80e1f39298446e9c9bd1d988fe9494
Parents: 0472b3e
Author: Claus Ibsen <da...@apache.org>
Authored: Sun Aug 10 15:48:00 2014 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sun Aug 10 15:48:00 2014 +0200

----------------------------------------------------------------------
 .../swagger/RestSwaggerCorsFilter.scala         | 43 ++++++++++++++++++++
 .../src/main/webapp/WEB-INF/web.xml             | 16 +++++---
 2 files changed, 54 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/7113dee3/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/RestSwaggerCorsFilter.scala
----------------------------------------------------------------------
diff --git a/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/RestSwaggerCorsFilter.scala b/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/RestSwaggerCorsFilter.scala
new file mode 100644
index 0000000..a3bfa26
--- /dev/null
+++ b/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/RestSwaggerCorsFilter.scala
@@ -0,0 +1,43 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (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
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.swagger
+
+import javax.servlet._
+import javax.servlet.http.HttpServletResponse
+
+class RestSwaggerCorsFilter extends Filter {
+
+  override def init(config: FilterConfig): Unit = {
+    // noop
+  }
+
+  override def destroy(): Unit = {
+    // noop
+  }
+
+  override def doFilter(request: ServletRequest, response: ServletResponse, chain: FilterChain): Unit = {
+    val res = response.asInstanceOf[HttpServletResponse]
+
+    res.setHeader("Access-Control-Allow-Origin", "*")
+    res.setHeader("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, CONNECT, PATCH")
+    res.setHeader("Access-Control-Max-Age", "3600")
+    res.setHeader("Access-Control-Allow-Headers", "Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers")
+
+    chain.doFilter(request, response)
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/7113dee3/examples/camel-example-servlet-rest-tomcat/src/main/webapp/WEB-INF/web.xml
----------------------------------------------------------------------
diff --git a/examples/camel-example-servlet-rest-tomcat/src/main/webapp/WEB-INF/web.xml b/examples/camel-example-servlet-rest-tomcat/src/main/webapp/WEB-INF/web.xml
index 6b43d03..ee0c9f1 100755
--- a/examples/camel-example-servlet-rest-tomcat/src/main/webapp/WEB-INF/web.xml
+++ b/examples/camel-example-servlet-rest-tomcat/src/main/webapp/WEB-INF/web.xml
@@ -54,11 +54,6 @@
     <servlet-class>org.apache.camel.component.swagger.RestSwaggerApiDeclarationServlet</servlet-class>
     <load-on-startup>2</load-on-startup>
     <init-param>
-      <!-- enable cors so people can use swagger ui to browse the apis -->
-      <param-name>cors</param-name>
-      <param-value>true</param-value>
-    </init-param>
-    <init-param>
       <param-name>base.path</param-name>
       <param-value>http://localhost:8080/rest</param-value>
     </init-param>
@@ -93,4 +88,15 @@
     <url-pattern>/rest/*</url-pattern>
   </servlet-mapping>
 
+  <!-- enable CORS filter so people can use swagger ui to browse and test the apis -->
+  <filter>
+    <filter-name>RestSwaggerCorsFilter</filter-name>
+    <filter-class>org.apache.camel.component.swagger.RestSwaggerCorsFilter</filter-class>
+  </filter>
+
+  <filter-mapping>
+    <filter-name>RestSwaggerCorsFilter</filter-name>
+    <url-pattern>/*</url-pattern>
+  </filter-mapping>
+
 </web-app>
\ No newline at end of file