You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by lb...@apache.org on 2020/10/21 14:33:44 UTC

[camel] 01/02: CAMEL-15722: endpoint uri builder: add an option to URL encode the returned uri or not

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

lburgazzoli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 35b17c47cb14bc1e266a4a469e78d6c2ab25f211
Author: Luca Burgazzoli <lb...@gmail.com>
AuthorDate: Wed Oct 21 12:11:26 2020 +0200

    CAMEL-15722: endpoint uri builder: add an option to URL encode the returned uri or not
---
 .../org/apache/camel/spi/EndpointUriFactory.java   | 14 +++-
 .../catalog/CustomEndpointUriFactoryTest.java      | 74 +++++++++++++++++++---
 .../component/EndpointUriFactorySupport.java       |  4 +-
 .../packaging/EndpointUriFactoryGenerator.java     |  4 +-
 4 files changed, 83 insertions(+), 13 deletions(-)

diff --git a/core/camel-api/src/main/java/org/apache/camel/spi/EndpointUriFactory.java b/core/camel-api/src/main/java/org/apache/camel/spi/EndpointUriFactory.java
index 1eb4d99..35530e6 100644
--- a/core/camel-api/src/main/java/org/apache/camel/spi/EndpointUriFactory.java
+++ b/core/camel-api/src/main/java/org/apache/camel/spi/EndpointUriFactory.java
@@ -42,7 +42,19 @@ public interface EndpointUriFactory extends CamelContextAware {
      * @param  properties endpoint options
      * @return            the constructed endpoint uri
      */
-    String buildUri(String scheme, Map<String, Object> properties) throws URISyntaxException;
+    default String buildUri(String scheme, Map<String, Object> properties) throws URISyntaxException {
+        return buildUri(scheme, properties, true);
+    }
+
+    /**
+     * Assembles an endpoint uri for the given component name with the given parameters.
+     *
+     * @param  scheme     the component name
+     * @param  properties endpoint options
+     * @param  encode     whether to URL encode the returned uri or not
+     * @return            the constructed endpoint uri
+     */
+    String buildUri(String scheme, Map<String, Object> properties, boolean encode) throws URISyntaxException;
 
     /**
      * Returns all the names this endpoint supports.
diff --git a/core/camel-core/src/test/java/org/apache/camel/catalog/CustomEndpointUriFactoryTest.java b/core/camel-core/src/test/java/org/apache/camel/catalog/CustomEndpointUriFactoryTest.java
index a6eafaf..97bb0c7 100644
--- a/core/camel-core/src/test/java/org/apache/camel/catalog/CustomEndpointUriFactoryTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/catalog/CustomEndpointUriFactoryTest.java
@@ -215,6 +215,27 @@ public class CustomEndpointUriFactoryTest extends ContextTestSupport {
     }
 
     @Test
+    public void testCQLAssembler() throws Exception {
+        EndpointUriFactory assembler = new MyCQLAssembler();
+        assembler.setCamelContext(context);
+
+        Map<String, Object> params = new LinkedHashMap<>();
+        params.put("host", "localhost");
+        params.put("keyspace", "test");
+        params.put("cql", "insert into test_data(id, text) values (now(), ?)");
+
+        Assertions.assertEquals(
+                "cql:localhost/test?cql=insert+into+test_data%28id%2C+text%29+values+%28now%28%29%2C+%3F%29",
+                assembler.buildUri("cql", new LinkedHashMap<>(params)));
+        Assertions.assertEquals(
+                "cql:localhost/test?cql=insert+into+test_data%28id%2C+text%29+values+%28now%28%29%2C+%3F%29",
+                assembler.buildUri("cql", new LinkedHashMap<>(params), true));
+        Assertions.assertEquals(
+                "cql:localhost/test?cql=insert into test_data(id, text) values (now(), ?)",
+                assembler.buildUri("cql", new LinkedHashMap<>(params), false));
+    }
+
+    @Test
     public void testJmsSecrets() throws Exception {
         EndpointUriFactory assembler = new MyJmsxAssembler();
         assembler.setCamelContext(context);
@@ -239,7 +260,7 @@ public class CustomEndpointUriFactoryTest extends ContextTestSupport {
         }
 
         @Override
-        public String buildUri(String scheme, Map<String, Object> properties)
+        public String buildUri(String scheme, Map<String, Object> properties, boolean encode)
                 throws URISyntaxException {
             // begin from syntax
             String uri = SYNTAX;
@@ -248,7 +269,7 @@ public class CustomEndpointUriFactoryTest extends ContextTestSupport {
             uri = buildPathParameter(SYNTAX, uri, "name", null, true, properties);
             uri = buildPathParameter(SYNTAX, uri, "port", 8080, false, properties);
             // append remainder parameters
-            uri = buildQueryParameters(uri, properties);
+            uri = buildQueryParameters(uri, properties, encode);
 
             return uri;
         }
@@ -280,7 +301,7 @@ public class CustomEndpointUriFactoryTest extends ContextTestSupport {
         }
 
         @Override
-        public String buildUri(String scheme, Map<String, Object> properties)
+        public String buildUri(String scheme, Map<String, Object> properties, boolean encode)
                 throws URISyntaxException {
             // begin from syntax
             String uri = SYNTAX;
@@ -290,7 +311,7 @@ public class CustomEndpointUriFactoryTest extends ContextTestSupport {
             uri = buildPathParameter(SYNTAX, uri, "path", null, false, properties);
             uri = buildPathParameter(SYNTAX, uri, "port", 8080, false, properties);
             // append remainder parameters
-            uri = buildQueryParameters(uri, properties);
+            uri = buildQueryParameters(uri, properties, encode);
 
             return uri;
         }
@@ -322,13 +343,13 @@ public class CustomEndpointUriFactoryTest extends ContextTestSupport {
         }
 
         @Override
-        public String buildUri(String scheme, Map<String, Object> properties)
+        public String buildUri(String scheme, Map<String, Object> properties, boolean encode)
                 throws URISyntaxException {
 
             String uri = SYNTAX;
             uri = buildPathParameter(SYNTAX, uri, "destinationType", "queue", false, properties);
             uri = buildPathParameter(SYNTAX, uri, "destinationName", null, true, properties);
-            uri = buildQueryParameters(uri, properties);
+            uri = buildQueryParameters(uri, properties, encode);
 
             return uri;
         }
@@ -359,11 +380,11 @@ public class CustomEndpointUriFactoryTest extends ContextTestSupport {
         }
 
         @Override
-        public String buildUri(String scheme, Map<String, Object> properties) throws URISyntaxException {
+        public String buildUri(String scheme, Map<String, Object> properties, boolean encode) throws URISyntaxException {
             String uri = SYNTAX;
             uri = buildPathParameter(SYNTAX, uri, "destinationType", "queue", false, properties);
             uri = buildPathParameter(SYNTAX, uri, "destinationName", null, true, properties);
-            uri = buildQueryParameters(uri, properties);
+            uri = buildQueryParameters(uri, properties, encode);
 
             return uri;
         }
@@ -385,4 +406,41 @@ public class CustomEndpointUriFactoryTest extends ContextTestSupport {
 
     }
 
+    private static class MyCQLAssembler extends EndpointUriFactorySupport implements EndpointUriFactory {
+        private static final String SYNTAX = "cql:host/keyspace";
+
+        @Override
+        public boolean isEnabled(String scheme) {
+            return "cql".equals(scheme);
+        }
+
+        @Override
+        public String buildUri(String scheme, Map<String, Object> properties, boolean encode)
+                throws URISyntaxException {
+
+            String uri = SYNTAX;
+            uri = buildPathParameter(SYNTAX, uri, "host", null, true, properties);
+            uri = buildPathParameter(SYNTAX, uri, "keyspace", null, true, properties);
+            uri = buildQueryParameters(uri, properties, encode);
+
+            return uri;
+        }
+
+        @Override
+        public Set<String> propertyNames() {
+            return Collections.emptySet();
+        }
+
+        @Override
+        public Set<String> secretPropertyNames() {
+            return Collections.emptySet();
+        }
+
+        @Override
+        public boolean isLenientProperties() {
+            return false;
+        }
+
+    }
+
 }
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/component/EndpointUriFactorySupport.java b/core/camel-support/src/main/java/org/apache/camel/support/component/EndpointUriFactorySupport.java
index bcc3811..f09c593 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/component/EndpointUriFactorySupport.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/component/EndpointUriFactorySupport.java
@@ -74,7 +74,7 @@ public abstract class EndpointUriFactorySupport implements CamelContextAware, En
         return uri;
     }
 
-    protected String buildQueryParameters(String uri, Map<String, Object> parameters)
+    protected String buildQueryParameters(String uri, Map<String, Object> parameters, boolean encode)
             throws URISyntaxException {
         // we want sorted parameters
         Map<String, Object> map = new TreeMap<>(parameters);
@@ -88,7 +88,7 @@ public abstract class EndpointUriFactorySupport implements CamelContextAware, En
             }
         }
 
-        String query = URISupport.createQueryString(map);
+        String query = URISupport.createQueryString(map, encode);
         if (ObjectHelper.isNotEmpty(query)) {
             // there may be a ? sign in the context path then use & instead
             // (this is not correct but lets deal with this as the camel-catalog handled this)
diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/EndpointUriFactoryGenerator.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/EndpointUriFactoryGenerator.java
index 228dcd32..b1f2662 100644
--- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/EndpointUriFactoryGenerator.java
+++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/EndpointUriFactoryGenerator.java
@@ -81,7 +81,7 @@ public final class EndpointUriFactoryGenerator {
         w.write("    }\n");
         w.write("\n");
         w.write("    @Override\n");
-        w.write("    public String buildUri(String scheme, Map<String, Object> properties) throws URISyntaxException {\n");
+        w.write("    public String buildUri(String scheme, Map<String, Object> properties, boolean encode) throws URISyntaxException {\n");
         w.write("        String syntax = scheme + BASE;\n");
         w.write("        String uri = syntax;\n");
         w.write("\n");
@@ -91,7 +91,7 @@ public final class EndpointUriFactoryGenerator {
             w.write("        uri = buildPathParameter(syntax, uri, \"" + option.getName() + "\", "
                     + defaultValue(option) + ", " + option.isRequired() + ", copy);\n");
         }
-        w.write("        uri = buildQueryParameters(uri, copy);\n");
+        w.write("        uri = buildQueryParameters(uri, copy, encode);\n");
         w.write("        return uri;\n");
         w.write("    }\n");
         w.write("\n");