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

acosentino pushed a commit to branch camel-3.7.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-3.7.x by this push:
     new 3382b4d  CAMEL-16303 - URI generation: if a part of the component name 
is equal to a path parameter the URI generation will fail
3382b4d is described below

commit 3382b4dd6de52024b15efdb081e3f386af1b52f2
Author: Andrea Cosentino <anco...@gmail.com>
AuthorDate: Thu Mar 4 15:33:17 2021 +0100

    CAMEL-16303 - URI generation: if a part of the component name is equal to a 
path parameter the URI generation will fail
---
 .../component/EndpointUriFactorySupport.java       | 48 ++++++++++++++++++++--
 .../java/org/apache/camel/util/StringHelper.java   | 45 ++++++++++++++++++++
 2 files changed, 90 insertions(+), 3 deletions(-)

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 f09c593..47b8d57 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
@@ -24,6 +24,7 @@ import org.apache.camel.CamelContext;
 import org.apache.camel.CamelContextAware;
 import org.apache.camel.spi.EndpointUriFactory;
 import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.StringHelper;
 import org.apache.camel.util.URISupport;
 
 /**
@@ -55,9 +56,15 @@ public abstract class EndpointUriFactorySupport implements 
CamelContextAware, En
         }
         if (ObjectHelper.isNotEmpty(obj)) {
             String str = 
camelContext.getTypeConverter().convertTo(String.class, obj);
-            uri = uri.replace(name, str);
+            int occurence = StringHelper.countOccurence(uri, name);
+            if (occurence > 1) {
+                uri = StringHelper.replaceFromSecondOccurence(uri, name, str);
+            } else {
+                uri = uri.replace(name, str);
+            }
         } else {
-            // the option is optional and we have no default or value for it, 
so we need to remove it from the syntax
+            // the option is optional and we have no default or value for it, 
so we need to
+            // remove it from the syntax
             int pos = uri.indexOf(name);
             if (pos != -1) {
                 // remove from syntax
@@ -91,7 +98,8 @@ public abstract class EndpointUriFactorySupport implements 
CamelContextAware, En
         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)
+            // (this is not correct but lets deal with this as the 
camel-catalog handled
+            // this)
             boolean questionMark = uri.indexOf('?') != -1;
             if (questionMark) {
                 uri = uri + "&" + query;
@@ -101,4 +109,38 @@ public abstract class EndpointUriFactorySupport implements 
CamelContextAware, En
         }
         return uri;
     }
+
+    private int countOccurence(String str, String findStr) {
+        int lastIndex = 0;
+        int count = 0;
+
+        while (lastIndex != -1) {
+
+            lastIndex = str.indexOf(findStr, lastIndex);
+
+            if (lastIndex != -1) {
+                count++;
+                lastIndex += findStr.length();
+            }
+        }
+        return count;
+    }
+
+    private String replaceFromSecondOccurence(String str, String name, String 
repl) {
+        int index = str.indexOf(name);
+        boolean replace = false;
+
+        while (index != -1) {
+            String tempString = str.substring(index);
+            if (replace) {
+                tempString = tempString.replaceFirst(name, repl);
+                str = str.substring(0, index) + tempString;
+                replace = false;
+            } else {
+                replace = true;
+            }
+            index = str.indexOf(name, index + 1);
+        }
+        return str;
+    }
 }
diff --git 
a/core/camel-util/src/main/java/org/apache/camel/util/StringHelper.java 
b/core/camel-util/src/main/java/org/apache/camel/util/StringHelper.java
index 8199401..3e9da32 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/StringHelper.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/StringHelper.java
@@ -1009,4 +1009,49 @@ public final class StringHelper {
         return Pattern.compile(regex).splitAsStream(text);
     }
 
+    /**
+     * Returns the occurence of a search string in to a string
+     *
+     * @param  text   the text
+     * @param  search the string to search
+     * @return        an integer reporting the number of occurence of the 
searched string in to the text
+     */
+    public static int countOccurence(String text, String search) {
+        int lastIndex = 0;
+        int count = 0;
+        while (lastIndex != -1) {
+            lastIndex = text.indexOf(search, lastIndex);
+            if (lastIndex != -1) {
+                count++;
+                lastIndex += search.length();
+            }
+        }
+        return count;
+    }
+
+    /**
+     * Replaces a string in to a text starting from his second occurence
+     *
+     * @param  text   the text
+     * @param  search the string to search
+     * @param  repl   the replacement for the string
+     * @return        the string with the replacement
+     */
+    public static String replaceFromSecondOccurence(String text, String 
search, String repl) {
+        int index = text.indexOf(search);
+        boolean replace = false;
+
+        while (index != -1) {
+            String tempString = text.substring(index);
+            if (replace) {
+                tempString = tempString.replaceFirst(search, repl);
+                text = text.substring(0, index) + tempString;
+                replace = false;
+            } else {
+                replace = true;
+            }
+            index = text.indexOf(search, index + 1);
+        }
+        return text;
+    }
 }

Reply via email to