camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [camel] branch rest-dsl-parser updated: CAMEL-12824: camel-route-parser - Add parser for rest-dsl
Date Fri, 21 Sep 2018 13:07:39 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


The following commit(s) were added to refs/heads/rest-dsl-parser by this push:
     new f0854fc  CAMEL-12824: camel-route-parser - Add parser for rest-dsl
f0854fc is described below

commit f0854fcd747e45aa8593e8e1274ff410b6f4e73d
Author: Claus Ibsen <claus.ibsen@gmail.com>
AuthorDate: Fri Sep 21 15:02:12 2018 +0200

    CAMEL-12824: camel-route-parser - Add parser for rest-dsl
---
 .../helper/CamelJavaRestDslParserHelper.java       | 33 ++++++++++++++
 .../parser/helper/CamelXmlRestDslParserHelper.java | 50 ++++++++++++++++++++++
 .../parser/model/RestConfigurationDetails.java     | 43 +++++++++++++++++++
 .../camel/parser/java/MyRestDslRouteBuilder.java   |  7 ++-
 .../camel/parser/java/RoasterJavaRestDslTest.java  | 12 +++++-
 .../apache/camel/parser/xml/XmlRestDslTest.java    | 20 ++++++---
 .../org/apache/camel/parser/xml/myrest.xml         |  5 +++
 7 files changed, 161 insertions(+), 9 deletions(-)

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 0b36844..d111a84 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
@@ -196,6 +196,30 @@ public final class CamelJavaRestDslParserHelper {
             node.setJsonDataFormat(extractValueFromFirstArgument(clazz, block, mi));
         } else if ("xmlDataFormat".equals(name)) {
             node.setXmlDataFormat(extractValueFromFirstArgument(clazz, block, mi));
+        } else if ("componentProperty".equals(name)) {
+            String key = extractValueFromFirstArgument(clazz, block, mi);
+            String value = extractValueFromSecondArgument(clazz, block, mi);
+            node.addComponentProperty(key, value);
+        } else if ("endpointProperty".equals(name)) {
+            String key = extractValueFromFirstArgument(clazz, block, mi);
+            String value = extractValueFromSecondArgument(clazz, block, mi);
+            node.addEndpointProperty(key, value);
+        } else if ("consumerProperty".equals(name)) {
+            String key = extractValueFromFirstArgument(clazz, block, mi);
+            String value = extractValueFromSecondArgument(clazz, block, mi);
+            node.addConsumerProperty(key, value);
+        } else if ("dataFormatProperty".equals(name)) {
+            String key = extractValueFromFirstArgument(clazz, block, mi);
+            String value = extractValueFromSecondArgument(clazz, block, mi);
+            node.addDataFormatProperty(key, value);
+        } else if ("apiProperty".equals(name)) {
+            String key = extractValueFromFirstArgument(clazz, block, mi);
+            String value = extractValueFromSecondArgument(clazz, block, mi);
+            node.addApiProperty(key, value);
+        } else if ("corsHeaderProperty".equals(name)) {
+            String key = extractValueFromFirstArgument(clazz, block, mi);
+            String value = extractValueFromSecondArgument(clazz, block, mi);
+            node.addCorsHeader(key, value);
         }
     }
 
@@ -208,6 +232,15 @@ public final class CamelJavaRestDslParserHelper {
         return null;
     }
 
+    private static String extractValueFromSecondArgument(JavaClassSource clazz, Block block,
MethodInvocation mi) {
+        List args = mi.arguments();
+        if (args != null && args.size() > 1) {
+            Expression exp = (Expression) args.get(1);
+            return getLiteralValue(clazz, block, exp);
+        }
+        return null;
+    }
+
     @SuppressWarnings("unchecked")
     private static FieldSource<JavaClassSource> getField(JavaClassSource clazz, Block
block, SimpleName ref) {
         String fieldName = ref.getIdentifier();
diff --git a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelXmlRestDslParserHelper.java
b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelXmlRestDslParserHelper.java
index 17c667b..0e83c1f 100644
--- a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelXmlRestDslParserHelper.java
+++ b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelXmlRestDslParserHelper.java
@@ -70,6 +70,56 @@ public final class CamelXmlRestDslParserHelper {
             detail.setXmlDataFormat(extractAttribute(map, "xmlDataFormat"));
         }
 
+        if ("componentProperty".equals(xmlNode.getNodeName())
+            && (xmlNode.getParentNode() != null && "restConfiguration".equals(xmlNode.getParentNode().getNodeName())))
{
+            NamedNodeMap map = xmlNode.getAttributes();
+            String key = extractAttribute(map, "key");
+            String value = extractAttribute(map, "value");
+            if (key != null && value != null) {
+                detail.addComponentProperty(key, value);
+            }
+        } else if ("endpointProperty".equals(xmlNode.getNodeName())
+            && (xmlNode.getParentNode() != null && "restConfiguration".equals(xmlNode.getParentNode().getNodeName())))
{
+            NamedNodeMap map = xmlNode.getAttributes();
+            String key = extractAttribute(map, "key");
+            String value = extractAttribute(map, "value");
+            if (key != null && value != null) {
+                detail.addEndpointProperty(key, value);
+            }
+        } else if ("consumerProperty".equals(xmlNode.getNodeName())
+            && (xmlNode.getParentNode() != null && "restConfiguration".equals(xmlNode.getParentNode().getNodeName())))
{
+            NamedNodeMap map = xmlNode.getAttributes();
+            String key = extractAttribute(map, "key");
+            String value = extractAttribute(map, "value");
+            if (key != null && value != null) {
+                detail.addConsumerProperty(key, value);
+            }
+        } else if ("dataFormatProperty".equals(xmlNode.getNodeName())
+            && (xmlNode.getParentNode() != null && "restConfiguration".equals(xmlNode.getParentNode().getNodeName())))
{
+            NamedNodeMap map = xmlNode.getAttributes();
+            String key = extractAttribute(map, "key");
+            String value = extractAttribute(map, "value");
+            if (key != null && value != null) {
+                detail.addDataFormatProperty(key, value);
+            }
+        } else if ("apiProperty".equals(xmlNode.getNodeName())
+            && (xmlNode.getParentNode() != null && "restConfiguration".equals(xmlNode.getParentNode().getNodeName())))
{
+            NamedNodeMap map = xmlNode.getAttributes();
+            String key = extractAttribute(map, "key");
+            String value = extractAttribute(map, "value");
+            if (key != null && value != null) {
+                detail.addApiProperty(key, value);
+            }
+        } else if ("corsHeaders".equals(xmlNode.getNodeName())
+            && (xmlNode.getParentNode() != null && "restConfiguration".equals(xmlNode.getParentNode().getNodeName())))
{
+            NamedNodeMap map = xmlNode.getAttributes();
+            String key = extractAttribute(map, "key");
+            String value = extractAttribute(map, "value");
+            if (key != null && value != null) {
+                detail.addCorsHeader(key, value);
+            }
+        }
+
         // walk the rest of the children
         NodeList children = xmlNode.getChildNodes();
         for (int i = 0; i < children.getLength(); i++) {
diff --git a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/RestConfigurationDetails.java
b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/RestConfigurationDetails.java
index c38cb65..b1db413 100644
--- a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/RestConfigurationDetails.java
+++ b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/RestConfigurationDetails.java
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.parser.model;
 
+import java.util.LinkedHashMap;
 import java.util.Map;
 
 public class RestConfigurationDetails {
@@ -325,4 +326,46 @@ public class RestConfigurationDetails {
     public void setCorsHeaders(Map<String, String> corsHeaders) {
         this.corsHeaders = corsHeaders;
     }
+
+    public void addComponentProperty(String key, String value) {
+        if (componentProperties == null) {
+            componentProperties = new LinkedHashMap<>();
+        }
+        componentProperties.put(key, value);
+    }
+
+    public void addEndpointProperty(String key, String value) {
+        if (endpointProperties == null) {
+            endpointProperties = new LinkedHashMap<>();
+        }
+        endpointProperties.put(key, value);
+    }
+
+    public void addConsumerProperty(String key, String value) {
+        if (consumerProperties == null) {
+            consumerProperties = new LinkedHashMap<>();
+        }
+        consumerProperties.put(key, value);
+    }
+
+    public void addDataFormatProperty(String key, String value) {
+        if (dataFormatProperties == null) {
+            dataFormatProperties = new LinkedHashMap<>();
+        }
+        dataFormatProperties.put(key, value);
+    }
+
+    public void addApiProperty(String key, String value) {
+        if (apiProperties == null) {
+            apiProperties = new LinkedHashMap<>();
+        }
+        apiProperties.put(key, value);
+    }
+
+    public void addCorsHeader(String key, String value) {
+        if (corsHeaders == null) {
+            corsHeaders = new LinkedHashMap<>();
+        }
+        corsHeaders.put(key, value);
+    }
 }
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 26957fa..1d914af 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
@@ -33,7 +33,12 @@ public class MyRestDslRouteBuilder extends RouteBuilder {
             .skipBindingOnErrorCode(true)
             .scheme("https")
             .hostNameResolver(RestHostNameResolver.allLocalIp)
-            .bindingMode(RestBindingMode.json);
+            .bindingMode(RestBindingMode.json)
+            .componentProperty("foo", "123")
+            .endpointProperty("pretty", "false")
+            .consumerProperty("bar", "456")
+            .corsHeaderProperty("key1", "value1")
+            .corsHeaderProperty("key2", "value2");
 
         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 34f1db0..dedf9ae 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
@@ -42,7 +42,7 @@ public class RoasterJavaRestDslTest extends CamelTestSupport {
         assertEquals(1, list.size());
         RestConfigurationDetails details = list.get(0);
         assertEquals("27", details.getLineNumber());
-        assertEquals("36", details.getLineNumberEnd());
+        assertEquals("41", 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());
@@ -56,6 +56,16 @@ public class RoasterJavaRestDslTest extends CamelTestSupport {
         assertEquals("true", details.getSkipBindingOnErrorCode());
         assertEquals("https", details.getScheme());
         assertEquals("allLocalIp", details.getHostNameResolver());
+
+        assertEquals(1, details.getComponentProperties().size());
+        assertEquals("123", details.getComponentProperties().get("foo"));
+        assertEquals(1, details.getEndpointProperties().size());
+        assertEquals("false", details.getEndpointProperties().get("pretty"));
+        assertEquals(1, details.getEndpointProperties().size());
+        assertEquals("456", details.getConsumerProperties().get("bar"));
+        assertEquals(2, details.getCorsHeaders().size());
+        assertEquals("value1", details.getCorsHeaders().get("key1"));
+        assertEquals("value2", details.getCorsHeaders().get("key2"));
     }
 
 }
diff --git a/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/xml/XmlRestDslTest.java
b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/xml/XmlRestDslTest.java
index 856c1e2..6acc2c6 100644
--- a/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/xml/XmlRestDslTest.java
+++ b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/xml/XmlRestDslTest.java
@@ -23,15 +23,12 @@ import java.util.List;
 import org.apache.camel.parser.XmlRestDslParser;
 import org.apache.camel.parser.model.RestConfigurationDetails;
 import org.junit.Test;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
 
 public class XmlRestDslTest {
 
-    private static final Logger LOG = LoggerFactory.getLogger(XmlParseTreeTest.class);
-
     @Test
     public void testXmlTree() throws Exception {
         InputStream is = new FileInputStream("src/test/resources/org/apache/camel/parser/xml/myrest.xml");
@@ -42,11 +39,11 @@ public class XmlRestDslTest {
         assertEquals(1, list.size());
         RestConfigurationDetails details = list.get(0);
         assertEquals("src/test/resources/org/apache/camel/camel/parser/xml/myrest.xml", details.getFileName());
-        assertEquals(null, details.getMethodName());
-        assertEquals(null, details.getClassName());
+        assertNull(details.getMethodName());
+        assertNull(details.getClassName());
 
         assertEquals("29", details.getLineNumber());
-        assertEquals("30", details.getLineNumberEnd());
+        assertEquals("35", details.getLineNumberEnd());
         assertEquals("1234", details.getPort());
         assertEquals("myapi", details.getContextPath());
         assertEquals("jetty", details.getComponent());
@@ -58,6 +55,15 @@ public class XmlRestDslTest {
         assertEquals("https", details.getScheme());
         assertEquals("allLocalIp", details.getHostNameResolver());
 
+        assertEquals(1, details.getComponentProperties().size());
+        assertEquals("123", details.getComponentProperties().get("foo"));
+        assertEquals(1, details.getEndpointProperties().size());
+        assertEquals("false", details.getEndpointProperties().get("pretty"));
+        assertEquals(1, details.getEndpointProperties().size());
+        assertEquals("456", details.getConsumerProperties().get("bar"));
+        assertEquals(2, details.getCorsHeaders().size());
+        assertEquals("value1", details.getCorsHeaders().get("key1"));
+        assertEquals("value2", details.getCorsHeaders().get("key2"));
     }
 
 }
diff --git a/tooling/camel-route-parser/src/test/resources/org/apache/camel/parser/xml/myrest.xml
b/tooling/camel-route-parser/src/test/resources/org/apache/camel/parser/xml/myrest.xml
index ef54669..370be9e 100644
--- a/tooling/camel-route-parser/src/test/resources/org/apache/camel/parser/xml/myrest.xml
+++ b/tooling/camel-route-parser/src/test/resources/org/apache/camel/parser/xml/myrest.xml
@@ -27,6 +27,11 @@
     <restConfiguration component="jetty" port="1234" bindingMode="json" apiComponent="swagger"
scheme="https"
                        contextPath="myapi" apiContextPath="myapi/swagger" hostNameResolver="allLocalIp"
apiHost="localhost"
                        skipBindingOnErrorCode="true">
+      <componentProperty key="foo" value="123"/>
+      <endpointProperty key="pretty" value="false"/>
+      <consumerProperty key="bar" value="456"/>
+      <corsHeaders key="key1" value="value1"/>
+      <corsHeaders key="key2" value="value2"/>
     </restConfiguration>
   </camelContext>
 


Mime
View raw message