camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [1/2] camel git commit: CAMEL-7800: camel-swagger-java - work in progress
Date Fri, 18 Sep 2015 12:02:49 GMT
Repository: camel
Updated Branches:
  refs/heads/swagger 5b5b834b4 -> 112c58e54


CAMEL-7800: camel-swagger-java - 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/64852d26
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/64852d26
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/64852d26

Branch: refs/heads/swagger
Commit: 64852d26e781b5805f4f4c755605600a575f6d33
Parents: 5b5b834
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Fri Sep 18 13:46:59 2015 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Fri Sep 18 13:46:59 2015 +0200

----------------------------------------------------------------------
 .../RestOperationResponseMsgDefinition.java     | 35 ++++++-----
 .../camel/component/rest/FromRestGetTest.java   |  4 +-
 components/camel-swagger-java/pom.xml           | 61 ++++++++++++++------
 .../apache/camel/swagger/RestSwaggerReader.java | 11 +++-
 examples/camel-example-swagger-java/pom.xml     |  4 --
 .../src/main/resources/camel-config-xml.xml     | 16 +++--
 .../src/main/webapp/home.html                   | 14 ++---
 7 files changed, 91 insertions(+), 54 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/64852d26/camel-core/src/main/java/org/apache/camel/model/rest/RestOperationResponseMsgDefinition.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/rest/RestOperationResponseMsgDefinition.java
b/camel-core/src/main/java/org/apache/camel/model/rest/RestOperationResponseMsgDefinition.java
index ddd08bb..7705e96 100644
--- a/camel-core/src/main/java/org/apache/camel/model/rest/RestOperationResponseMsgDefinition.java
+++ b/camel-core/src/main/java/org/apache/camel/model/rest/RestOperationResponseMsgDefinition.java
@@ -28,8 +28,6 @@ import org.apache.camel.spi.Metadata;
  * To specify the rest operation response messages using Swagger.
  * <p/>
  * This maps to the Swagger Response Message Object.
- * see com.wordnik.swagger.model.ResponseMessage
- * and https://github.com/swagger-api/swagger-spec/blob/master/versions/1.2.md#525-response-message-object.
  */
 @Metadata(label = "rest")
 @XmlRootElement(name = "responseMessage")
@@ -39,8 +37,9 @@ public class RestOperationResponseMsgDefinition {
     @XmlTransient
     private VerbDefinition verb;
 
-    @XmlAttribute(required = true)
-    private int code;
+    @XmlAttribute
+    @Metadata(defaultValue = "200")
+    private String code;
 
     @XmlAttribute(required = true)
     private String message;
@@ -56,14 +55,11 @@ public class RestOperationResponseMsgDefinition {
     public RestOperationResponseMsgDefinition() {
     }
 
-    public int getCode() {
-        return code != 0 ? code : 200;
+    public String getCode() {
+        return code != null ? code : "200";
     }
 
-    /**
-     * Sets the Swagger Operation's ResponseMessage code
-     */
-    public void setCode(int code) {
+    public void setCode(String code) {
         this.code = code;
     }
 
@@ -71,9 +67,6 @@ public class RestOperationResponseMsgDefinition {
         return responseModel != null ? responseModel : "";
     }
 
-    /**
-     * Sets the Swagger Operation's ResponseMessage responseModel
-     */
     public void setResponseModel(String responseModel) {
         this.responseModel = responseModel;
     }
@@ -82,23 +75,29 @@ public class RestOperationResponseMsgDefinition {
         return message != null ? message : "success";
     }
 
-    /**
-     * Sets the Swagger Operation's ResponseMessage message
-     */
     public void setMessage(String message) {
         this.message = message;
     }
 
     /**
-     * The return code
+     * The response code such as a HTTP status code.
      */
     public RestOperationResponseMsgDefinition code(int code) {
+        setCode("" + code);
+        return this;
+    }
+
+    /**
+     * The response code such as a HTTP status code. Can use <tt>general</tt>,
or other words
+     * to indicate general error responses that do not map to a specific HTTP status code.
+     */
+    public RestOperationResponseMsgDefinition code(String code) {
         setCode(code);
         return this;
     }
 
     /**
-     * The return message
+     * The response message (description)
      */
     public RestOperationResponseMsgDefinition message(String msg) {
         setMessage(msg);

http://git-wip-us.apache.org/repos/asf/camel/blob/64852d26/camel-core/src/test/java/org/apache/camel/component/rest/FromRestGetTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/component/rest/FromRestGetTest.java
b/camel-core/src/test/java/org/apache/camel/component/rest/FromRestGetTest.java
index b82d9a0..3297ce0 100644
--- a/camel-core/src/test/java/org/apache/camel/component/rest/FromRestGetTest.java
+++ b/camel-core/src/test/java/org/apache/camel/component/rest/FromRestGetTest.java
@@ -79,7 +79,8 @@ public class FromRestGetTest extends ContextTestSupport {
         assertEquals("acc1", rest.getVerbs().get(0).getParams().get(0).getAccess());
         assertEquals("acc2", rest.getVerbs().get(0).getParams().get(1).getAccess());
 
-        assertEquals(300, rest.getVerbs().get(0).getResponseMsgs().get(0).getCode());
+        assertEquals("300", rest.getVerbs().get(0).getResponseMsgs().get(0).getCode());
+        assertEquals("error", rest.getVerbs().get(0).getResponseMsgs().get(1).getCode());
         assertEquals("test msg", rest.getVerbs().get(0).getResponseMsgs().get(0).getMessage());
         assertEquals(Integer.class.getCanonicalName(), rest.getVerbs().get(0).getResponseMsgs().get(0).getResponseModel());
 
@@ -115,6 +116,7 @@ public class FromRestGetTest extends ContextTestSupport {
                         .defaultValue("b").allowMultiple(true).name("header_letter").required(false).access("acc2")
                         .endParam()
                         .responseMessage().code(300).message("test msg").responseModel(Integer.class).endResponseMessage()
+                        .responseMessage().code("error").message("does not work").endResponseMessage()
                         .to("direct:bye")
                         .post().to("mock:update");
 

http://git-wip-us.apache.org/repos/asf/camel/blob/64852d26/components/camel-swagger-java/pom.xml
----------------------------------------------------------------------
diff --git a/components/camel-swagger-java/pom.xml b/components/camel-swagger-java/pom.xml
index 5e0177a..742a9e5 100644
--- a/components/camel-swagger-java/pom.xml
+++ b/components/camel-swagger-java/pom.xml
@@ -40,24 +40,66 @@
       <artifactId>camel-core</artifactId>
     </dependency>
 
+    <!-- align to use versions from Camel BOM -->
+    <dependency>
+      <groupId>com.fasterxml.jackson.core</groupId>
+      <artifactId>jackson-annotations</artifactId>
+      <version>${jackson2-version}</version>
+    </dependency>
+    <dependency>
+      <groupId>com.fasterxml.jackson.core</groupId>
+      <artifactId>jackson-databind</artifactId>
+      <version>${jackson2-version}</version>
+    </dependency>
+    <dependency>
+      <groupId>com.fasterxml.jackson.datatype</groupId>
+      <artifactId>jackson-datatype-joda</artifactId>
+      <version>${jackson2-version}</version>
+    </dependency>
+    <dependency>
+      <groupId>com.fasterxml.jackson.dataformat</groupId>
+      <artifactId>jackson-dataformat-yaml</artifactId>
+      <version>${jackson2-version}</version>
+    </dependency>
+    <dependency>
+      <groupId>com.fasterxml.jackson.dataformat</groupId>
+      <artifactId>jackson-dataformat-xml</artifactId>
+      <version>${jackson2-version}</version>
+    </dependency>
+    <dependency>
+      <groupId>com.fasterxml.jackson.jaxrs</groupId>
+      <artifactId>jackson-jaxrs-json-provider</artifactId>
+      <version>${jackson2-version}</version>
+    </dependency>
+    <dependency>
+      <groupId>com.google.guava</groupId>
+      <artifactId>guava</artifactId>
+      <version>${google-guava-version}</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.commons</groupId>
+      <artifactId>commons-lang3</artifactId>
+      <version>${commons-lang3-version}</version>
+    </dependency>
+
+    <!-- swagger -->
     <dependency>
       <groupId>io.swagger</groupId>
       <artifactId>swagger-core</artifactId>
       <version>${swagger-java-version}</version>
     </dependency>
-
     <dependency>
       <groupId>io.swagger</groupId>
       <artifactId>swagger-annotations</artifactId>
       <version>${swagger-java-version}</version>
     </dependency>
-
     <dependency>
       <groupId>io.swagger</groupId>
       <artifactId>swagger-jaxrs</artifactId>
       <version>${swagger-java-version}</version>
     </dependency>
 
+    <!-- servlet api -->
     <dependency>
       <groupId>javax.servlet</groupId>
       <artifactId>servlet-api</artifactId>
@@ -72,21 +114,6 @@
       <scope>test</scope>
     </dependency>
     <dependency>
-      <groupId>org.apache.camel</groupId>
-      <artifactId>camel-jetty8</artifactId>
-      <scope>test</scope>
-    </dependency>
-    <dependency>
-      <groupId>org.apache.camel</groupId>
-      <artifactId>camel-http-common</artifactId>
-      <scope>test</scope>
-    </dependency>
-    <dependency>
-      <groupId>org.apache.camel</groupId>
-      <artifactId>camel-jetty-common</artifactId>
-      <scope>test</scope>
-    </dependency>
-    <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <scope>test</scope>

http://git-wip-us.apache.org/repos/asf/camel/blob/64852d26/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerReader.java
----------------------------------------------------------------------
diff --git a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerReader.java
b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerReader.java
index a83c03d..8f12dfe 100644
--- a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerReader.java
+++ b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerReader.java
@@ -158,6 +158,7 @@ public class RestSwaggerReader {
                     parameter.setDescription(param.getDescription());
                     parameter.setRequired(param.getRequired());
 
+                    // set type on parameter
                     if (parameter instanceof SerializableParameter) {
                         SerializableParameter sp = (SerializableParameter) parameter;
 
@@ -166,6 +167,7 @@ public class RestSwaggerReader {
                         }
                     }
 
+                    // set schema on body parameter
                     if (parameter instanceof BodyParameter) {
                         BodyParameter bp = (BodyParameter) parameter;
 
@@ -181,6 +183,7 @@ public class RestSwaggerReader {
                 }
             }
 
+            // if we have an out type then set that as response message
             if (verb.getOutType() != null) {
                 Response response = new Response();
                 Property prop = modelTypeAsProperty(verb.getOutType(), swagger);
@@ -189,13 +192,17 @@ public class RestSwaggerReader {
                 op.addResponse("200", response);
             }
 
+            // enrich with configured response messages from the rest-dsl
             for (RestOperationResponseMsgDefinition msg : verb.getResponseMsgs()) {
-                Response response = op.getResponses().get("" + msg.getCode());
+                Response response = null;
+                if (op.getResponses() != null) {
+                    response = op.getResponses().get(msg.getCode());
+                }
                 if (response == null) {
                     response = new Response();
                 }
                 response.setDescription(msg.getMessage());
-                op.addResponse("" + msg.getCode(), response);
+                op.addResponse(msg.getCode(), response);
             }
 
             // add path

http://git-wip-us.apache.org/repos/asf/camel/blob/64852d26/examples/camel-example-swagger-java/pom.xml
----------------------------------------------------------------------
diff --git a/examples/camel-example-swagger-java/pom.xml b/examples/camel-example-swagger-java/pom.xml
index 09b0ba7..7552a65 100755
--- a/examples/camel-example-swagger-java/pom.xml
+++ b/examples/camel-example-swagger-java/pom.xml
@@ -53,10 +53,6 @@
       <groupId>org.apache.camel</groupId>
       <artifactId>camel-servlet</artifactId>
     </dependency>
-    <dependency>
-      <groupId>org.apache.camel</groupId>
-      <artifactId>camel-metrics</artifactId>
-    </dependency>
 
     <!-- swagger api -->
     <dependency>

http://git-wip-us.apache.org/repos/asf/camel/blob/64852d26/examples/camel-example-swagger-java/src/main/resources/camel-config-xml.xml
----------------------------------------------------------------------
diff --git a/examples/camel-example-swagger-java/src/main/resources/camel-config-xml.xml b/examples/camel-example-swagger-java/src/main/resources/camel-config-xml.xml
index 8ec9b0b..be888b4 100755
--- a/examples/camel-example-swagger-java/src/main/resources/camel-config-xml.xml
+++ b/examples/camel-example-swagger-java/src/main/resources/camel-config-xml.xml
@@ -23,9 +23,6 @@
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
 
-  <!-- use camel-metrics route policy to gather metrics for all routes -->
-  <bean id="metricsRoutePolicyFactory" class="org.apache.camel.component.metrics.routepolicy.MetricsRoutePolicyFactory"/>
-
   <!-- a bean for user services -->
   <bean id="userService" class="org.apache.camel.example.rest.UserService"/>
 
@@ -50,7 +47,17 @@
       <get uri="/{id}" outType="org.apache.camel.example.rest.User">
         <description>Find user by id</description>
         <param name="id" type="path" description="The id of the user to get" dataType="integer"/>
-        <to uri="bean:userService?method=getUser(${header.id})"/>
+        <responseMessage message="The user that was found"/>
+        <responseMessage code="404" message="User not found"/>
+        <route>
+          <to uri="bean:userService?method=getUser(${header.id})"/>
+          <filter>
+            <simple>${body} == null</simple>
+            <setHeader headerName="Exchange.HTTP_RESPONSE_CODE">
+              <constant>404</constant>
+            </setHeader>
+          </filter>
+        </route>
       </get>
 
       <!-- this is a rest PUT to create/update an user -->
@@ -63,6 +70,7 @@
       <!-- this is a rest GET to find all users -->
       <get uri="/findAll" outType="org.apache.camel.example.rest.User[]">
         <description>Find all users</description>
+        <responseMessage message="All the users"/>
         <to uri="bean:userService?method=listUsers"/>
       </get>
     </rest>

http://git-wip-us.apache.org/repos/asf/camel/blob/64852d26/examples/camel-example-swagger-java/src/main/webapp/home.html
----------------------------------------------------------------------
diff --git a/examples/camel-example-swagger-java/src/main/webapp/home.html b/examples/camel-example-swagger-java/src/main/webapp/home.html
index b9c4e2a..71e50c8 100644
--- a/examples/camel-example-swagger-java/src/main/webapp/home.html
+++ b/examples/camel-example-swagger-java/src/main/webapp/home.html
@@ -18,7 +18,7 @@
 <head>
     <meta charset="utf-8"/>
     <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
-    <title>Apache Camel Servlet REST and Apache Tomcat example</title>
+    <title>Apache Camel Swagger Java Example</title>
     <link rel="stylesheet" href="css/foundation.min.css"/>
     <script src="js/vendor/modernizr.js"></script>
 </head>
@@ -26,23 +26,21 @@
 <body>
 <div class="row">
     <div class="large-12 columns">
-        <h2>Apache Camel Example using Servlet REST & Web Container</h2>
+        <h2>Apache Camel Example using Swagger Java</h2>
     </div>
 </div>
 
 <div class="row">
     <div class="large-12 columns">
-        This example shows how to use Servlet REST to define REST endpoints in Camel routes
using the Rest DSL
+        This example shows how to use Servlet REST to define REST endpoints in Camel routes
using the Rest DSL,
+        and using camel-swagger-java to expose the REST service APIs.
 
         <br/>
 
-        This example is implemented in both the Java and XML DSLs. By default the Java DSL
is in use.
-        You can change this in the src/main/webapps/WEB-INF/web.xml file
+        This example is implemented in XML DSL.
 
         <br/>
-        For Java DSL the routes are defined in Java code, in the <tt>org.apache.camel.example.rest.UserRouteBuilder</tt>
-        class.
-        And for XML DSL the routes are define in XML code, in the <tt>src/main/resources/camel-config-xml.xml</tt>
file.
+        The XML DSL the routes are define in XML code, in the <tt>src/main/resources/camel-config-xml.xml</tt>
file.
 
         <p/>
         There is a <i>user</i> REST service that supports the following operations


Mime
View raw message