Repository: camel
Updated Branches:
  refs/heads/master 2fb649050 -> 753bf7410


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

Branch: refs/heads/master
Commit: 753bf741065d5311b399aeb4b06717d9fecd7bc9
Parents: 2fb6490
Author: Claus Ibsen <davscl...@apache.org>
Authored: Sun Aug 10 13:07:07 2014 +0200
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Sun Aug 10 13:07:07 2014 +0200

----------------------------------------------------------------------
 .../swagger/RestSwaggerApiDeclarationServlet.scala | 17 +++++++++++++++++
 .../src/main/webapp/WEB-INF/web.xml                | 11 ++++++++++-
 2 files changed, 27 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/753bf741/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 fb72242..c081254 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
@@ -40,6 +40,7 @@ class RestSwaggerApiDeclarationServlet extends HttpServlet {
   val reader = new RestSwaggerReader()
   var camel: CamelContext = null
   val swaggerConfig: SwaggerConfig = ConfigFactory.config
+  var cors: Boolean = false
 
   override def init(config: ServletConfig): Unit = {
     super.init(config)
@@ -61,6 +62,10 @@ class RestSwaggerApiDeclarationServlet extends HttpServlet {
     if (s != null) {
       swaggerConfig.setApiPath(s)
     }
+    s = config.getInitParameter("cors")
+    if (s != null) {
+      cors = "true".equalsIgnoreCase(s)
+    }
 
     val title = config.getInitParameter("api.title")
     val description = config.getInitParameter("api.description")
@@ -104,6 +109,12 @@ class RestSwaggerApiDeclarationServlet extends HttpServlet 
{
     val cookies = Map[String, String]()
     val headers = Map[String, List[String]]()
 
+    if (cors) {
+      response.addHeader("Access-Control-Allow-Headers", "Origin, Accept, 
X-Requested-With, Content-Type, Access-Control-Request-Method, 
Access-Control-Request-Headers")
+      response.addHeader("Access-Control-Allow-Methods", "GET, HEAD, POST, 
PUT, DELETE, TRACE, OPTIONS, CONNECT, PATCH")
+      response.addHeader("Access-Control-Allow-Origin", "*")
+    }
+
     if (camel != null) {
       val f = new SpecFilter
       val listings = RestApiListingCache.listing(camel, 
swaggerConfig).map(specs => {
@@ -142,6 +153,12 @@ class RestSwaggerApiDeclarationServlet extends HttpServlet 
{
     val headers = Map[String, List[String]]()
     val pathPart = docRoot
 
+    if (cors) {
+      response.addHeader("Access-Control-Allow-Headers", "Origin, Accept, 
X-Requested-With, Content-Type, Access-Control-Request-Method, 
Access-Control-Request-Headers")
+      response.addHeader("Access-Control-Allow-Methods", "GET, HEAD, POST, 
PUT, DELETE, TRACE, OPTIONS, CONNECT, PATCH")
+      response.addHeader("Access-Control-Allow-Origin", "*")
+    }
+
     if (camel != null) {
       val listings = RestApiListingCache.listing(camel, 
swaggerConfig).map(specs => {
           (for (spec <- specs.values) yield {

http://git-wip-us.apache.org/repos/asf/camel/blob/753bf741/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 7fd5d5f..6b43d03 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
@@ -44,7 +44,7 @@
     <servlet-name>CamelServlet</servlet-name>
     <display-name>Camel Http Transport Servlet</display-name>
     
<servlet-class>org.apache.camel.component.servlet.CamelHttpTransportServlet</servlet-class>
-    <load-on-startup>3</load-on-startup>
+    <load-on-startup>1</load-on-startup>
   </servlet>
 
   <!-- START SNIPPET: e1 -->
@@ -54,7 +54,16 @@
     
<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>
+    <init-param>
+      <param-name>api.path</param-name>
       <param-value>http://localhost:8080/api-docs</param-value>
     </init-param>
     <init-param>

Reply via email to