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/27 09:12:37 UTC

[camel] 04/05: CAMEL-14499: Optimize to avoid using camel-core-catalog for SendDynamicAware for camel-file/camel-ftp.

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

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

commit 9ff036334e80e8df9f65116b04d2da4efe437adf
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sun Sep 27 11:00:36 2020 +0200

    CAMEL-14499: Optimize to avoid using camel-core-catalog for SendDynamicAware for camel-file/camel-ftp.
---
 .../file/GenericFileSendDynamicAware.java          |  3 +-
 .../support/component/SendDynamicAwareSupport.java | 42 +++++++++++-----------
 .../java/org/apache/camel/util/URISupport.java     | 18 ++++++++++
 .../java/org/apache/camel/util/URISupportTest.java | 10 ++++++
 4 files changed, 51 insertions(+), 22 deletions(-)

diff --git a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileSendDynamicAware.java b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileSendDynamicAware.java
index 71566b9..0c5a210 100644
--- a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileSendDynamicAware.java
+++ b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileSendDynamicAware.java
@@ -55,8 +55,7 @@ public abstract class GenericFileSendDynamicAware extends SendDynamicAwareSuppor
         if (fileName || tempFileName || idempotentKey || move || moveFailed || preMove || moveExisting) {
             Map<String, Object> params = entry.getProperties();
 
-            // TODO: parseQuery should only have the query part, this is not correct
-            Map<String, Object> originalParams = URISupport.parseQuery(entry.getOriginalUri());
+            Map<String, Object> originalParams = URISupport.parseQuery(URISupport.extractQuery(entry.getOriginalUri()));
             if (fileName) {
                 Object val = originalParams.get("fileName");
                 if (val != null) {
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/component/SendDynamicAwareSupport.java b/core/camel-support/src/main/java/org/apache/camel/support/component/SendDynamicAwareSupport.java
index 485e7a4..c68b303 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/component/SendDynamicAwareSupport.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/component/SendDynamicAwareSupport.java
@@ -16,7 +16,6 @@
  */
 package org.apache.camel.support.component;
 
-import java.util.Collections;
 import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.Set;
@@ -25,6 +24,7 @@ import org.apache.camel.CamelContext;
 import org.apache.camel.Exchange;
 import org.apache.camel.ExtendedCamelContext;
 import org.apache.camel.catalog.RuntimeCamelCatalog;
+import org.apache.camel.spi.EndpointUriFactory;
 import org.apache.camel.spi.SendDynamicAware;
 import org.apache.camel.support.service.ServiceSupport;
 import org.apache.camel.util.URISupport;
@@ -61,25 +61,23 @@ public abstract class SendDynamicAwareSupport extends ServiceSupport implements
     @Override
     protected void doInit() throws Exception {
         if (isOnlyDynamicQueryParameters()) {
-            knownProperties = getCamelContext().adapt(ExtendedCamelContext.class).getEndpointUriFactory(getScheme()).propertyNames();
+            // optimize to eager load the list of known properties
+            EndpointUriFactory factory = getCamelContext().adapt(ExtendedCamelContext.class).getEndpointUriFactory(getScheme());
+            if (factory == null) {
+                throw new IllegalStateException("Cannot find EndpointUriFactory for component: " + getScheme());
+            }
+            knownProperties = factory.propertyNames();
         }
     }
 
     public Map<String, Object> endpointProperties(Exchange exchange, String uri) throws Exception {
         Map<String, Object> properties;
         if (isOnlyDynamicQueryParameters()) {
-            // optimize as we know its only query parameters that can be dynamic, and that there are no lenient properties
-            Map<String, Object> map;
-            int pos = uri.indexOf('?');
-            if (pos != -1) {
-                String query = uri.substring(pos + 1);
-                map = URISupport.parseQuery(query);
-            } else {
-                map = Collections.EMPTY_MAP;
-            }
-            if (map != null && isLenientProperties()) {
-                properties = new LinkedHashMap<>(map.size());
+            // optimize as we know its only query parameters that can be dynamic
+            Map<String, Object> map = URISupport.parseQuery(URISupport.extractQuery(uri));
+            if (map != null && !map.isEmpty() && isLenientProperties()) {
                 // okay so only add the known properties as they are the non lenient properties
+                properties = new LinkedHashMap<>();
                 map.forEach((k, v) -> {
                     if (knownProperties.contains(k)) {
                         properties.put(k, v);
@@ -99,13 +97,17 @@ public abstract class SendDynamicAwareSupport extends ServiceSupport implements
         Map<String, Object> properties;
         if (isOnlyDynamicQueryParameters()) {
             // optimize as we know its only query parameters that can be dynamic
-            Map<String, Object> map  = URISupport.parseQuery(uri);
-            properties = new LinkedHashMap<>();
-            map.forEach((k, v) -> {
-                if (!knownProperties.contains(k)) {
-                    properties.put(k, v.toString());
-                }
-            });
+            Map<String, Object> map = URISupport.parseQuery(URISupport.extractQuery(uri));
+            if (map != null && !map.isEmpty()) {
+                properties = new LinkedHashMap<>();
+                map.forEach((k, v) -> {
+                    if (!knownProperties.contains(k)) {
+                        properties.put(k, v.toString());
+                    }
+                });
+            } else {
+                properties = map;
+            }
         } else {
             RuntimeCamelCatalog catalog = exchange.getContext().adapt(ExtendedCamelContext.class).getRuntimeCamelCatalog();
             properties = new LinkedHashMap<>(catalog.endpointLenientProperties(uri));
diff --git a/core/camel-util/src/main/java/org/apache/camel/util/URISupport.java b/core/camel-util/src/main/java/org/apache/camel/util/URISupport.java
index 179753e..983c0df 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/URISupport.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/URISupport.java
@@ -116,6 +116,24 @@ public final class URISupport {
     }
 
     /**
+     * Extracts the query part of the given uri
+     *
+     * @param uri  the uri
+     * @return the query parameters or <tt>null</tt> if the uri has no query
+     */
+    public static String extractQuery(String uri) {
+        if (uri == null) {
+            return null;
+        }
+        int pos = uri.indexOf('?');
+        if (pos != -1) {
+            return uri.substring(pos + 1);
+        } else {
+            return null;
+        }
+    }
+
+    /**
      * Parses the query part of the uri (eg the parameters).
      * <p/>
      * The URI parameters will by default be URI encoded. However you can define a parameter values with the syntax:
diff --git a/core/camel-util/src/test/java/org/apache/camel/util/URISupportTest.java b/core/camel-util/src/test/java/org/apache/camel/util/URISupportTest.java
index 19d5384..2fd7e46 100644
--- a/core/camel-util/src/test/java/org/apache/camel/util/URISupportTest.java
+++ b/core/camel-util/src/test/java/org/apache/camel/util/URISupportTest.java
@@ -567,4 +567,14 @@ public class URISupportTest {
         assertThat(URISupport.joinPaths("/a/", null, null, null)).isEqualTo("/a/");
         assertThat(URISupport.joinPaths("a/", "/b", null, null)).isEqualTo("a/b");
     }
+
+    @Test
+    public void testExtractQuery() throws Exception {
+        assertEquals(null, URISupport.extractQuery(null));
+        assertEquals(null, URISupport.extractQuery(""));
+        assertEquals(null, URISupport.extractQuery("file:foo"));
+        assertEquals("recursive=true", URISupport.extractQuery("file:foo?recursive=true"));
+        assertEquals("recursive=true&delete=true", URISupport.extractQuery("file:foo?recursive=true&delete=true"));
+    }
+
 }