camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [camel] 02/02: CAMEL-12824: camel-route-parser - Add parser for rest-dsl
Date Thu, 20 Sep 2018 09:41:11 GMT
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch rest-dsl-parser
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 9ace61230ef846c82ce87421e5ede81ef08d6baa
Author: Claus Ibsen <claus.ibsen@gmail.com>
AuthorDate: Thu Sep 20 11:35:45 2018 +0200

    CAMEL-12824: camel-route-parser - Add parser for rest-dsl
---
 .../org/apache/camel/parser/RestDslParser.java     |  3 ++
 .../helper/CamelJavaRestDslParserHelper.java       | 62 +++++++++++++++++++---
 .../camel/parser/java/MyRestDslRouteBuilder.java   | 13 ++++-
 .../camel/parser/java/RoasterJavaRestDslTest.java  | 14 +++--
 4 files changed, 81 insertions(+), 11 deletions(-)

diff --git a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/RestDslParser.java
b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/RestDslParser.java
index fb29182..e8dd599 100644
--- a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/RestDslParser.java
+++ b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/RestDslParser.java
@@ -33,6 +33,9 @@ import org.jboss.forge.roaster.model.source.MethodSource;
  */
 public final class RestDslParser {
 
+    // TODO: add support for rest services (eg rest().get() ...)
+    // TODO: add XML rest-dsl parser also
+
     private RestDslParser() {
     }
 
diff --git a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaRestDslParserHelper.java
b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaRestDslParserHelper.java
index 38cc0c1..0b36844 100644
--- a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaRestDslParserHelper.java
+++ b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaRestDslParserHelper.java
@@ -82,6 +82,11 @@ public final class CamelJavaRestDslParserHelper {
                             if (line > -1) {
                                 node.setLineNumber("" + line);
                             }
+                            pos = exp.getStartPosition() + exp.getLength();
+                            line = findLineNumber(fullyQualifiedFileName, pos);
+                            if (line > -1) {
+                                node.setLineNumberEnd("" + line);
+                            }
                             node.setFileName(fullyQualifiedFileName);
                             node.setClassName(clazz.getQualifiedName());
                             node.setMethodName(configureMethod.getName());
@@ -138,11 +143,59 @@ public final class CamelJavaRestDslParserHelper {
     private void doParseRestConfiguration(RestConfigurationDetails node, String fullyQualifiedFileName,
                                           JavaClassSource clazz, MethodSource<JavaClassSource>
configureMethod, Block block,
                                           MethodInvocation mi) {
+
+        // end line number is the first node in the method chain we parse
+        if (node.getLineNumberEnd() == null) {
+            int pos = mi.getStartPosition() + mi.getLength();
+            int line = findLineNumber(fullyQualifiedFileName, pos);
+            if (line > -1) {
+                node.setLineNumberEnd("" + line);
+            }
+        }
+
         String name = mi.getName().getIdentifier();
-        if ("port".equals(name)) {
+        if ("component".equals(name)) {
+            node.setComponent(extractValueFromFirstArgument(clazz, block, mi));
+        } else if ("apiComponent".equals(name)) {
+            node.setApiComponent(extractValueFromFirstArgument(clazz, block, mi));
+        } else if ("producerComponent".equals(name)) {
+            node.setProducerComponent(extractValueFromFirstArgument(clazz, block, mi));
+        } else if ("scheme".equals(name)) {
+            node.setScheme(extractValueFromFirstArgument(clazz, block, mi));
+        } else if ("host".equals(name)) {
+            node.setHost(extractValueFromFirstArgument(clazz, block, mi));
+        } else if ("apiHost".equals(name)) {
+            node.setApiHost(extractValueFromFirstArgument(clazz, block, mi));
+        } else if ("port".equals(name)) {
             node.setPort(extractValueFromFirstArgument(clazz, block, mi));
+        } else if ("producerApiDoc".equals(name)) {
+            node.setProducerApiDoc(extractValueFromFirstArgument(clazz, block, mi));
         } else if ("contextPath".equals(name)) {
             node.setContextPath(extractValueFromFirstArgument(clazz, block, mi));
+        } else if ("apiContextPath".equals(name)) {
+            node.setApiContextPath(extractValueFromFirstArgument(clazz, block, mi));
+        } else if ("apiContextRouteId".equals(name)) {
+            node.setApiContextRouteId(extractValueFromFirstArgument(clazz, block, mi));
+        } else if ("apiContextIdPattern".equals(name)) {
+            node.setApiContextIdPattern(extractValueFromFirstArgument(clazz, block, mi));
+        } else if ("apiContextListening".equals(name)) {
+            node.setApiContextListening(extractValueFromFirstArgument(clazz, block, mi));
+        } else if ("apiVendorExtension".equals(name)) {
+            node.setApiVendorExtension(extractValueFromFirstArgument(clazz, block, mi));
+        } else if ("hostNameResolver".equals(name)) {
+            node.setHostNameResolver(extractValueFromFirstArgument(clazz, block, mi));
+        } else if ("bindingMode".equals(name)) {
+            node.setBindingMode(extractValueFromFirstArgument(clazz, block, mi));
+        } else if ("skipBindingOnErrorCode".equals(name)) {
+            node.setSkipBindingOnErrorCode(extractValueFromFirstArgument(clazz, block, mi));
+        } else if ("clientRequestValidation".equals(name)) {
+            node.setClientRequestValidation(extractValueFromFirstArgument(clazz, block, mi));
+        } else if ("enableCORS".equals(name)) {
+            node.setEnableCORS(extractValueFromFirstArgument(clazz, block, mi));
+        } else if ("jsonDataFormat".equals(name)) {
+            node.setJsonDataFormat(extractValueFromFirstArgument(clazz, block, mi));
+        } else if ("xmlDataFormat".equals(name)) {
+            node.setXmlDataFormat(extractValueFromFirstArgument(clazz, block, mi));
         }
     }
 
@@ -234,13 +287,10 @@ public final class CamelJavaRestDslParserHelper {
             return "{{" + name + "}}";
         }
 
-        // if its a qualified name (usually a constant field in another class)
-        // then add a dummy value as we cannot find the field value in other classes and
maybe even outside the
-        // source code we have access to
+        // if its a qualified name, then its an enum where we should grab the simple name
         if (expression instanceof QualifiedName) {
             QualifiedName qn = (QualifiedName) expression;
-            String name = qn.getFullyQualifiedName();
-            return "{{" + name + "}}";
+            return qn.getName().getIdentifier();
         }
 
         if (expression instanceof SimpleName) {
diff --git a/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/MyRestDslRouteBuilder.java
b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/MyRestDslRouteBuilder.java
index b73f77d..26957fa 100644
--- a/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/MyRestDslRouteBuilder.java
+++ b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/MyRestDslRouteBuilder.java
@@ -17,12 +17,23 @@
 package org.apache.camel.parser.java;
 
 import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.model.rest.RestBindingMode;
+import org.apache.camel.model.rest.RestHostNameResolver;
 
 public class MyRestDslRouteBuilder extends RouteBuilder {
 
     @Override
     public void configure() throws Exception {
-        restConfiguration().contextPath("myapi").port(1234);
+        restConfiguration()
+            .contextPath("myapi").port(1234)
+            .component("jetty")
+            .apiComponent("swagger")
+            .apiHost("localhost")
+            .apiContextPath("myapi/swagger")
+            .skipBindingOnErrorCode(true)
+            .scheme("https")
+            .hostNameResolver(RestHostNameResolver.allLocalIp)
+            .bindingMode(RestBindingMode.json);
 
         rest()
             .get("/foo")
diff --git a/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaRestDslTest.java
b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaRestDslTest.java
index 81a19a4..34f1db0 100644
--- a/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaRestDslTest.java
+++ b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaRestDslTest.java
@@ -25,13 +25,9 @@ import org.apache.camel.test.junit4.CamelTestSupport;
 import org.jboss.forge.roaster.Roaster;
 import org.jboss.forge.roaster.model.source.JavaClassSource;
 import org.junit.Test;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 public class RoasterJavaRestDslTest extends CamelTestSupport {
 
-    private static final Logger LOG = LoggerFactory.getLogger(RoasterJavaRestDslTest.class);
-
     @Override
     public boolean isDumpRouteCoverage() {
         return false;
@@ -45,11 +41,21 @@ public class RoasterJavaRestDslTest extends CamelTestSupport {
             "src/test/java/org/apache/camel/parser/java/MyRestDslRouteBuilder.java", true);
         assertEquals(1, list.size());
         RestConfigurationDetails details = list.get(0);
+        assertEquals("27", details.getLineNumber());
+        assertEquals("36", details.getLineNumberEnd());
         assertEquals("src/test/java/org/apache/camel/parser/java/MyRestDslRouteBuilder.java",
details.getFileName());
         assertEquals("configure", details.getMethodName());
         assertEquals("org.apache.camel.parser.java.MyRestDslRouteBuilder", details.getClassName());
         assertEquals("1234", details.getPort());
         assertEquals("myapi", details.getContextPath());
+        assertEquals("jetty", details.getComponent());
+        assertEquals("json", details.getBindingMode());
+        assertEquals("swagger", details.getApiComponent());
+        assertEquals("myapi/swagger", details.getApiContextPath());
+        assertEquals("localhost", details.getApiHost());
+        assertEquals("true", details.getSkipBindingOnErrorCode());
+        assertEquals("https", details.getScheme());
+        assertEquals("allLocalIp", details.getHostNameResolver());
     }
 
 }


Mime
View raw message