camel-commits mailing list archives

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


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

Branch: refs/heads/swagger
Commit: 3e0fb3c58f178aaad3bd371050cbd9c9fe789bfc
Parents: 112c58e
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Fri Sep 18 15:27:53 2015 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Fri Sep 18 15:27:53 2015 +0200

----------------------------------------------------------------------
 .../RestSwaggerApiDeclarationServlet.java       | 28 ++++++------------
 .../apache/camel/swagger/RestSwaggerReader.java | 30 +++++++++++++++-----
 .../swagger/RestSwaggerReaderModelTest.java     |  5 +---
 .../camel/swagger/RestSwaggerReaderTest.java    |  6 +---
 .../src/main/resources/camel-config-xml.xml     | 15 ++++++++++
 5 files changed, 49 insertions(+), 35 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/3e0fb3c5/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerApiDeclarationServlet.java
----------------------------------------------------------------------
diff --git a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerApiDeclarationServlet.java
b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerApiDeclarationServlet.java
index 68d621b..dc1b938 100644
--- a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerApiDeclarationServlet.java
+++ b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerApiDeclarationServlet.java
@@ -29,6 +29,7 @@ import javax.servlet.http.HttpServletResponse;
 import com.fasterxml.jackson.annotation.JsonInclude;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.SerializationFeature;
+import io.swagger.core.filter.SpecFilter;
 import io.swagger.jaxrs.config.BeanConfig;
 import io.swagger.models.Contact;
 import io.swagger.models.Info;
@@ -144,21 +145,14 @@ public abstract class RestSwaggerApiDeclarationServlet extends HttpServlet
{
                     route = route.substring(contextId.length());
                 }
 
-
-                // TODO: implement these
-                if (!route.equals("") && !route.equals("/")) {
-                    // render overview if the route is empty or is the root path
-                    // renderApiDeclaration(request, response, contextId, route);
-                } else {
-                    renderResourceListing(request, response, contextId);
-                }
+                renderResourceListing(request, response, contextId, route);
             }
         } catch (Exception e) {
             LOG.warn("Error rendering swagger due " + e.getMessage(), e);
         }
     }
 
-    private void renderResourceListing(HttpServletRequest request, HttpServletResponse response,
String contextId) throws Exception {
+    private void renderResourceListing(HttpServletRequest request, HttpServletResponse response,
String contextId, String route) throws Exception {
         LOG.trace("renderResourceListing");
 
         if (cors) {
@@ -169,17 +163,13 @@ public abstract class RestSwaggerApiDeclarationServlet extends HttpServlet
{
 
         List<RestDefinition> rests = getRestDefinitions(contextId);
         if (rests != null) {
+            // read the rest-dsl into swagger model
+            Swagger swagger = reader.read(rests, route, swaggerConfig, new DefaultClassResolver());
 
-            // TODO: combine the rests
-
-            for (RestDefinition rest : rests) {
-                Swagger swagger = reader.read(rest, swaggerConfig, new DefaultClassResolver());
-
-                ObjectMapper mapper = new ObjectMapper();
-                mapper.enable(SerializationFeature.INDENT_OUTPUT);
-                mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
-                mapper.writeValue(response.getOutputStream(), swagger);
-            }
+            ObjectMapper mapper = new ObjectMapper();
+            mapper.enable(SerializationFeature.INDENT_OUTPUT);
+            mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
+            mapper.writeValue(response.getOutputStream(), swagger);
         } else {
             response.setStatus(204);
         }

http://git-wip-us.apache.org/repos/asf/camel/blob/3e0fb3c5/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 1ad8c1f..fcfb057 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
@@ -50,6 +50,7 @@ import org.apache.camel.model.rest.RestOperationResponseMsgDefinition;
 import org.apache.camel.model.rest.RestParamType;
 import org.apache.camel.model.rest.VerbDefinition;
 import org.apache.camel.spi.ClassResolver;
+import org.apache.camel.util.ObjectHelper;
 
 /**
  * A Camel REST-DSL swagger reader that parse the rest-dsl into a swagger model representation.
@@ -57,16 +58,35 @@ import org.apache.camel.spi.ClassResolver;
 public class RestSwaggerReader {
 
     /**
-     * Read the REST-DSL definition and parse that as a Swagger model representation
+     * Read the REST-DSL definition's and parse that as a Swagger model representation
      *
-     * @param rest              the rest-dsl
+     * @param rests             the rest-dsl
+     * @param route             optional route path to filter the rest-dsl to only include
from the chose route
      * @param config            the swagger configuration
      * @param classResolver     class resolver to use
      * @return the swagger model
      */
-    public Swagger read(RestDefinition rest, BeanConfig config, ClassResolver classResolver)
{
+    public Swagger read(List<RestDefinition> rests, String route, BeanConfig config,
ClassResolver classResolver) {
         Swagger swagger = new Swagger();
 
+        for (RestDefinition rest : rests) {
+
+            if (ObjectHelper.isNotEmpty(route) && !route.equals("/")) {
+                // filter by route
+                if (!rest.getPath().equals(route)) {
+                    continue;
+                }
+            }
+
+            parse(swagger, rest, classResolver);
+        }
+
+        // configure before returning
+        swagger = config.configure(swagger);
+        return swagger;
+    }
+
+    private void parse(Swagger swagger, RestDefinition rest, ClassResolver classResolver)
{
         List<VerbDefinition> verbs = new ArrayList<>(rest.getVerbs());
         // must sort the verbs by uri so we group them together when an uri has multiple
operations
         Collections.sort(verbs, new VerbOrdering());
@@ -207,10 +227,6 @@ public class RestSwaggerReader {
             // add path
             swagger.path(opPath, path);
         }
-
-        // configure before returning
-        swagger = config.configure(swagger);
-        return swagger;
     }
 
     private Model asModel(String typeName, Swagger swagger) {

http://git-wip-us.apache.org/repos/asf/camel/blob/3e0fb3c5/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderModelTest.java
----------------------------------------------------------------------
diff --git a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderModelTest.java
b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderModelTest.java
index 301421f..4d26438 100644
--- a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderModelTest.java
+++ b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderModelTest.java
@@ -65,9 +65,6 @@ public class RestSwaggerReaderModelTest extends CamelTestSupport {
 
     @Test
     public void testReaderRead() throws Exception {
-        RestDefinition rest = context.getRestDefinitions().get(0);
-        assertNotNull(rest);
-
         BeanConfig config = new BeanConfig();
         config.setHost("localhost:8080");
         config.setSchemes(new String[]{"http"});
@@ -77,7 +74,7 @@ public class RestSwaggerReaderModelTest extends CamelTestSupport {
         config.setLicenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html");
         RestSwaggerReader reader = new RestSwaggerReader();
 
-        Swagger swagger = reader.read(rest, config, new DefaultClassResolver());
+        Swagger swagger = reader.read(context.getRestDefinitions(), null, config, new DefaultClassResolver());
         assertNotNull(swagger);
 
         ObjectMapper mapper = new ObjectMapper();

http://git-wip-us.apache.org/repos/asf/camel/blob/3e0fb3c5/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderTest.java
----------------------------------------------------------------------
diff --git a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderTest.java
b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderTest.java
index b7c9de3..20becb4 100644
--- a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderTest.java
+++ b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderTest.java
@@ -24,7 +24,6 @@ import io.swagger.models.Swagger;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.impl.DefaultClassResolver;
 import org.apache.camel.impl.JndiRegistry;
-import org.apache.camel.model.rest.RestDefinition;
 import org.apache.camel.model.rest.RestParamType;
 import org.apache.camel.test.junit4.CamelTestSupport;
 import org.junit.Test;
@@ -60,16 +59,13 @@ public class RestSwaggerReaderTest extends CamelTestSupport {
 
     @Test
     public void testReaderRead() throws Exception {
-        RestDefinition rest = context.getRestDefinitions().get(0);
-        assertNotNull(rest);
-
         BeanConfig config = new BeanConfig();
         config.setHost("localhost:8080");
         config.setSchemes(new String[]{"http"});
         config.setBasePath("/api");
         RestSwaggerReader reader = new RestSwaggerReader();
 
-        Swagger swagger = reader.read(rest, config, new DefaultClassResolver());
+        Swagger swagger = reader.read(context.getRestDefinitions(), null, config, new DefaultClassResolver());
         assertNotNull(swagger);
 
         ObjectMapper mapper = new ObjectMapper();

http://git-wip-us.apache.org/repos/asf/camel/blob/3e0fb3c5/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 be888b4..ecdeed7 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
@@ -75,6 +75,21 @@
       </get>
     </rest>
 
+    <!-- defines the rest services using the context-path /echo -->
+    <rest path="/echo" consumes="application/text" produces="application/text">
+      <description>Echo rest service</description>
+
+      <get uri="/ping">
+        <description>A ping service</description>
+        <route>
+          <transform>
+            <constant>pong</constant>
+          </transform>
+        </route>
+      </get>
+
+    </rest>
+
   </camelContext>
 
 </beans>
\ No newline at end of file


Mime
View raw message