You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2020/09/25 18:45:00 UTC

[camel] 03/16: CAMEL-15567: components - Generate source code for creating endpoint uri via a map of properties. WIP

This is an automated email from the ASF dual-hosted git repository.

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

commit 566ce671d839dcce0a8fc41403d574266c84b85d
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Sep 25 08:55:20 2020 +0200

    CAMEL-15567: components - Generate source code for creating endpoint uri via a map of properties. WIP
---
 .../component/ahc/AhcEndpointUriAssembler.java     | 22 ++++++
 .../org/apache/camel/assembler/ahc-endpoint        |  2 +
 .../EventbridgeEndpointUriAssembler.java           | 22 ++++++
 .../camel/assembler/aws2-eventbridge-endpoint      |  2 +
 .../catalog/CustomEndpointUriAssemblerTest.java    | 80 ++++++++++++++++++++
 ...untimeCamelCatalogEndpointUriAssemblerTest.java | 16 +++-
 .../component/EndpointUriAssemblerSupport.java     | 86 ++++++----------------
 7 files changed, 167 insertions(+), 63 deletions(-)

diff --git a/components/camel-ahc/src/generated/java/org/apache/camel/component/ahc/AhcEndpointUriAssembler.java b/components/camel-ahc/src/generated/java/org/apache/camel/component/ahc/AhcEndpointUriAssembler.java
new file mode 100644
index 0000000..751a18a
--- /dev/null
+++ b/components/camel-ahc/src/generated/java/org/apache/camel/component/ahc/AhcEndpointUriAssembler.java
@@ -0,0 +1,22 @@
+/* Generated by camel build tools - do NOT edit this file! */
+package org.apache.camel.component.ahc;
+
+import java.net.URISyntaxException;
+import java.util.Map;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.spi.EndpointUriAssembler;
+import org.apache.camel.component.ahc.AhcComponent;
+
+/**
+ * Generated by camel build tools - do NOT edit this file!
+ */
+@SuppressWarnings("unchecked")
+public class AhcEndpointUriAssembler extends org.apache.camel.support.component.EndpointUriAssemblerSupport implements EndpointUriAssembler {
+
+    @Override
+    public String buildUri(CamelContext camelContext, String scheme, Map<String, String> parameters) throws URISyntaxException {
+        return null;
+    }
+}
+
diff --git a/components/camel-ahc/src/generated/resources/META-INF/services/org/apache/camel/assembler/ahc-endpoint b/components/camel-ahc/src/generated/resources/META-INF/services/org/apache/camel/assembler/ahc-endpoint
new file mode 100644
index 0000000..6fac227
--- /dev/null
+++ b/components/camel-ahc/src/generated/resources/META-INF/services/org/apache/camel/assembler/ahc-endpoint
@@ -0,0 +1,2 @@
+# Generated by camel build tools - do NOT edit this file!
+class=org.apache.camel.component.ahc.AhcEndpointUriAssembler
diff --git a/components/camel-aws2-eventbridge/src/generated/java/org/apache/camel/component/aws2/eventbridge/EventbridgeEndpointUriAssembler.java b/components/camel-aws2-eventbridge/src/generated/java/org/apache/camel/component/aws2/eventbridge/EventbridgeEndpointUriAssembler.java
new file mode 100644
index 0000000..088da23
--- /dev/null
+++ b/components/camel-aws2-eventbridge/src/generated/java/org/apache/camel/component/aws2/eventbridge/EventbridgeEndpointUriAssembler.java
@@ -0,0 +1,22 @@
+/* Generated by camel build tools - do NOT edit this file! */
+package org.apache.camel.component.aws2.eventbridge;
+
+import java.net.URISyntaxException;
+import java.util.Map;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.spi.EndpointUriAssembler;
+import org.apache.camel.component.aws2.eventbridge.EventbridgeComponent;
+
+/**
+ * Generated by camel build tools - do NOT edit this file!
+ */
+@SuppressWarnings("unchecked")
+public class EventbridgeEndpointUriAssembler extends org.apache.camel.support.component.EndpointUriAssemblerSupport implements EndpointUriAssembler {
+
+    @Override
+    public String buildUri(CamelContext camelContext, String scheme, Map<String, String> parameters) throws URISyntaxException {
+        return null;
+    }
+}
+
diff --git a/components/camel-aws2-eventbridge/src/generated/resources/META-INF/services/org/apache/camel/assembler/aws2-eventbridge-endpoint b/components/camel-aws2-eventbridge/src/generated/resources/META-INF/services/org/apache/camel/assembler/aws2-eventbridge-endpoint
new file mode 100644
index 0000000..0369ad0
--- /dev/null
+++ b/components/camel-aws2-eventbridge/src/generated/resources/META-INF/services/org/apache/camel/assembler/aws2-eventbridge-endpoint
@@ -0,0 +1,2 @@
+# Generated by camel build tools - do NOT edit this file!
+class=org.apache.camel.component.aws2.eventbridge.EventbridgeEndpointUriAssembler
diff --git a/core/camel-core/src/test/java/org/apache/camel/catalog/CustomEndpointUriAssemblerTest.java b/core/camel-core/src/test/java/org/apache/camel/catalog/CustomEndpointUriAssemblerTest.java
new file mode 100644
index 0000000..e92fe0d
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/catalog/CustomEndpointUriAssemblerTest.java
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.catalog;
+
+import java.net.URISyntaxException;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.spi.EndpointUriAssembler;
+import org.apache.camel.support.component.EndpointUriAssemblerSupport;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class CustomEndpointUriAssemblerTest extends ContextTestSupport {
+
+    @Test
+    public void testCustomAssemble() throws Exception {
+        EndpointUriAssembler assembler = new MyAssembler();
+
+        Map<String, String> params = new HashMap<>();
+        params.put("timerName", "foo");
+        params.put("period", "123");
+        params.put("repeatCount", "5");
+
+        String uri = assembler.buildUri(context, "timer", params);
+        Assertions.assertEquals("timer:foo?period=123&repeatCount=5", uri);
+    }
+
+    @Test
+    public void testCustomAssembleUnsorted() throws Exception {
+        EndpointUriAssembler assembler = new MyAssembler();
+
+        Map<String, String> params = new LinkedHashMap<>();
+        params.put("timerName", "foo");
+        params.put("repeatCount", "5");
+        params.put("period", "123");
+
+        String uri = assembler.buildUri(context, "timer", params);
+        Assertions.assertEquals("timer:foo?period=123&repeatCount=5", uri);
+    }
+
+    private class MyAssembler extends EndpointUriAssemblerSupport implements EndpointUriAssembler {
+
+        private static final String SYNTAX = "timer:timerName";
+
+        @Override
+        public String buildUri(CamelContext camelContext, String scheme, Map<String, String> parameters) throws URISyntaxException {
+            // begin from syntax
+            String uri = SYNTAX;
+
+            // TODO: optional path parameters that are missing
+
+            // append path parameters
+            uri = buildPathParameter(camelContext, SYNTAX, uri, "timerName", null, true, parameters);
+            // append remainder parameters
+            uri = buildQueryParameters(camelContext, uri, parameters);
+
+            return uri;
+        }
+
+    }
+
+}
diff --git a/core/camel-core/src/test/java/org/apache/camel/catalog/RuntimeCamelCatalogEndpointUriAssemblerTest.java b/core/camel-core/src/test/java/org/apache/camel/catalog/RuntimeCamelCatalogEndpointUriAssemblerTest.java
index 3bdf989..fbf620b 100644
--- a/core/camel-core/src/test/java/org/apache/camel/catalog/RuntimeCamelCatalogEndpointUriAssemblerTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/catalog/RuntimeCamelCatalogEndpointUriAssemblerTest.java
@@ -21,6 +21,7 @@ import java.util.Map;
 
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.ExtendedCamelContext;
+import org.apache.camel.catalog.impl.CamelCatalogEndpointUriAssembler;
 import org.apache.camel.spi.EndpointUriAssembler;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
@@ -28,7 +29,7 @@ import org.junit.jupiter.api.Test;
 public class RuntimeCamelCatalogEndpointUriAssemblerTest extends ContextTestSupport {
 
     @Test
-    public void testAssemble() throws Exception {
+    public void testLookupAssemble() throws Exception {
         EndpointUriAssembler assembler = context.adapt(ExtendedCamelContext.class).getEndpointUriAssembler("timer");
 
         Map<String, String> params = new HashMap<>();
@@ -39,4 +40,17 @@ public class RuntimeCamelCatalogEndpointUriAssemblerTest extends ContextTestSupp
         String uri = assembler.buildUri(context, "timer", params);
         Assertions.assertEquals("timer:foo?period=123&repeatCount=5", uri);
     }
+
+    @Test
+    public void testRuntimeAssemble() throws Exception {
+        EndpointUriAssembler assembler = new CamelCatalogEndpointUriAssembler();
+
+        Map<String, String> params = new HashMap<>();
+        params.put("timerName", "foo");
+        params.put("period", "123");
+        params.put("repeatCount", "5");
+
+        String uri = assembler.buildUri(context, "timer", params);
+        Assertions.assertEquals("timer:foo?period=123&repeatCount=5", uri);
+    }
 }
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/component/EndpointUriAssemblerSupport.java b/core/camel-support/src/main/java/org/apache/camel/support/component/EndpointUriAssemblerSupport.java
index 95ea8c1..b74fbae 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/component/EndpointUriAssemblerSupport.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/component/EndpointUriAssemblerSupport.java
@@ -16,12 +16,13 @@
  */
 package org.apache.camel.support.component;
 
-import java.util.List;
+import java.net.URISyntaxException;
+import java.util.Map;
+import java.util.TreeMap;
 
 import org.apache.camel.CamelContext;
-import org.apache.camel.NoSuchBeanException;
-import org.apache.camel.support.EndpointHelper;
-import org.apache.camel.util.TimeUtils;
+import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.URISupport;
 
 /**
  * Base class used by Camel Package Maven Plugin when it generates source code for fast endpoint uri assembler via
@@ -29,66 +30,27 @@ import org.apache.camel.util.TimeUtils;
  */
 public abstract class EndpointUriAssemblerSupport {
 
-    /**
-     * Converts the property to the expected type
-     *
-     * @param  camelContext the camel context
-     * @param  type         the expected type
-     * @param  value        the value
-     * @return              the value converted to the expected type
-     */
-    public static <T> T property(CamelContext camelContext, Class<T> type, Object value) {
-        // if the type is not string based and the value is a bean reference, then we need to lookup
-        // the bean from the registry
-        if (value instanceof String && String.class != type) {
-            String text = value.toString();
-
-            if (EndpointHelper.isReferenceParameter(text)) {
-                Object obj;
-                // special for a list where we refer to beans which can be either a list or a single element
-                // so use Object.class as type
-                if (type == List.class) {
-                    obj = EndpointHelper.resolveReferenceListParameter(camelContext, text, Object.class);
-                } else {
-                    obj = EndpointHelper.resolveReferenceParameter(camelContext, text, type);
-                }
-                if (obj == null) {
-                    // no bean found so throw an exception
-                    throw new NoSuchBeanException(text, type.getName());
-                }
-                value = obj;
-            } else if (type == long.class || type == Long.class || type == int.class || type == Integer.class) {
-                Object obj = null;
-                // string to long/int then it may be a duration where we can convert the value to milli seconds
-                // it may be a time pattern, such as 5s for 5 seconds = 5000
-                try {
-                    long num = TimeUtils.toMilliSeconds(text);
-                    if (type == int.class || type == Integer.class) {
-                        // need to cast to int
-                        obj = (int) num;
-                    } else {
-                        obj = num;
-                    }
-                } catch (IllegalArgumentException e) {
-                    // ignore
-                }
-                if (obj != null) {
-                    value = obj;
-                }
-            }
+    protected String buildPathParameter(CamelContext camelContext, String syntax, String uri, String name, String defaultValue, boolean required, Map<String, String> parameters) {
+        String obj = parameters.remove(name);
+        if (ObjectHelper.isEmpty(obj)) {
+            obj = defaultValue;
         }
-
-        // special for boolean values with string values as we only want to accept "true" or "false"
-        if ((type == Boolean.class || type == boolean.class) && value instanceof String) {
-            String text = (String) value;
-            if (!text.equalsIgnoreCase("true") && !text.equalsIgnoreCase("false")) {
-                throw new IllegalArgumentException(
-                        "Cannot convert the String value: " + value + " to type: " + type
-                                                   + " as the value is not true or false");
-            }
+        if (ObjectHelper.isEmpty(obj) && required) {
+            throw new IllegalArgumentException("Option " + name + " is required when creating endpoint uri with syntax " + syntax);
         }
-
-        return camelContext.getTypeConverter().convertTo(type, value);
+        if (ObjectHelper.isNotEmpty(obj)) {
+            uri = uri.replace(name, obj);
+        }
+        return uri;
     }
 
+    protected String buildQueryParameters(CamelContext camelContext, String uri, Map<String, String> parameters) throws URISyntaxException {
+        // we want sorted parameters
+        Map map = new TreeMap(parameters);
+        String query = URISupport.createQueryString(map);
+        if (ObjectHelper.isNotEmpty(query)) {
+            uri = uri + "?" + query;
+        }
+        return uri;
+    }
 }