You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2023/06/20 07:43:56 UTC

[camel] branch main updated (1edebbc9ebf -> e63792ff182)

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

orpiske pushed a change to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


    from 1edebbc9ebf Upgrade spring
     new 2b695defc15 [CAMEL-19456] The invocation of the removeRoute() method is too slow when using RAW().
     new 440731dfd4f don't use * import and License add
     new fdd41be0d1a Decode the parameters
     new 4e8d18d3d0f The return value of the getDecodeQuery method has been changed from query to uri.
     new f9e821f03de don't use * import
     new e63792ff182 Change to Checkstyle format

The 6 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../camel/impl/engine/AbstractCamelContext.java    | 35 +++++++++++-------
 .../camel/impl/engine/DefaultCamelContextTest.java | 36 ++++++++++++++++++
 .../java/org/apache/camel/impl/engine/MyBean.java  | 11 +++---
 .../java/org/apache/camel/util/URISupport.java     | 43 ++++++++++++++++++++++
 .../java/org/apache/camel/util/URISupportTest.java | 26 +++++++++++++
 5 files changed, 132 insertions(+), 19 deletions(-)
 copy components/camel-aws/camel-aws-xray/src/main/java/org/apache/camel/component/aws/xray/decorators/http/AhcSegmentDecorator.java => core/camel-core/src/test/java/org/apache/camel/impl/engine/MyBean.java (78%)


[camel] 02/06: don't use * import and License add

Posted by or...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 440731dfd4f86937384a4f489ea27c4fc226c2bc
Author: luke.me <lu...@kakaoenterprise.com>
AuthorDate: Thu Jun 15 16:34:23 2023 +0900

    don't use * import and License add
---
 .../apache/camel/impl/engine/AbstractCamelContext.java   |  8 +++++++-
 .../test/java/org/apache/camel/impl/engine/MyBean.java   | 16 ++++++++++++++++
 .../apache/camel/util/UnsafeUriCharactersDecoder.java    | 16 ++++++++++++++++
 3 files changed, 39 insertions(+), 1 deletion(-)

diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
index 5982d6001d6..197a7d7a818 100644
--- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
+++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
@@ -175,7 +175,13 @@ import org.apache.camel.support.jsse.SSLContextParameters;
 import org.apache.camel.support.service.BaseService;
 import org.apache.camel.support.service.ServiceHelper;
 import org.apache.camel.support.startup.DefaultStartupStepRecorder;
-import org.apache.camel.util.*;
+import org.apache.camel.util.IOHelper;
+import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.StopWatch;
+import org.apache.camel.util.StringHelper;
+import org.apache.camel.util.TimeUtils;
+import org.apache.camel.util.URISupport;
+import org.apache.camel.util.UnsafeUriCharactersDecoder;
 import org.apache.camel.vault.VaultConfiguration;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/engine/MyBean.java b/core/camel-core/src/test/java/org/apache/camel/impl/engine/MyBean.java
index d6e9faf43e2..3b7f7faa042 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/engine/MyBean.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/engine/MyBean.java
@@ -1,3 +1,19 @@
+/*
+ * 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.impl.engine;
 
 @org.apache.camel.spi.annotations.Component(value = "MyBean")
diff --git a/core/camel-util/src/main/java/org/apache/camel/util/UnsafeUriCharactersDecoder.java b/core/camel-util/src/main/java/org/apache/camel/util/UnsafeUriCharactersDecoder.java
index 6fa7af5a49b..49756536b15 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/UnsafeUriCharactersDecoder.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/UnsafeUriCharactersDecoder.java
@@ -1,3 +1,19 @@
+/*
+ * 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.util;
 
 


[camel] 03/06: Decode the parameters

Posted by or...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit fdd41be0d1a1bbe19d53fcc711be70943b8cef20
Author: luke.me <lu...@kakaoenterprise.com>
AuthorDate: Sun Jun 18 03:58:35 2023 +0900

    Decode the parameters
---
 .../camel/impl/engine/AbstractCamelContext.java    | 14 ++---
 .../camel/impl/engine/DefaultCamelContextTest.java | 13 ++--
 .../java/org/apache/camel/util/URISupport.java     | 37 ++++++++---
 .../camel/util/UnsafeUriCharactersDecoder.java     | 71 ----------------------
 .../java/org/apache/camel/util/URISupportTest.java | 26 ++++++++
 5 files changed, 70 insertions(+), 91 deletions(-)

diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
index 197a7d7a818..82cede02b93 100644
--- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
+++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
@@ -181,7 +181,6 @@ import org.apache.camel.util.StopWatch;
 import org.apache.camel.util.StringHelper;
 import org.apache.camel.util.TimeUtils;
 import org.apache.camel.util.URISupport;
-import org.apache.camel.util.UnsafeUriCharactersDecoder;
 import org.apache.camel.vault.VaultConfiguration;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -703,10 +702,6 @@ public abstract class AbstractCamelContext extends BaseService
         removeEndpoints(endpoint.getEndpointUri());
     }
 
-    public String unsafeUriCharactersDecodeWithOutPercent(String uri){
-        return UnsafeUriCharactersDecoder.decode(uri);
-    }
-
     @Override
     public Collection<Endpoint> removeEndpoints(String uri) throws Exception {
         Collection<Endpoint> answer = new ArrayList<>();
@@ -715,8 +710,11 @@ public abstract class AbstractCamelContext extends BaseService
             answer.add(oldEndpoint);
             stopServices(oldEndpoint);
         } else {
-            String decodeUri = unsafeUriCharactersDecodeWithOutPercent(uri);
-            oldEndpoint = endpoints.remove(getEndpointKey(decodeUri));
+            String decodeQuery = URISupport.getDecodeQuery(uri);
+            if(decodeQuery != null) {
+                String decodeUri = StringHelper.before(uri, "?") + "?" + decodeQuery;
+                oldEndpoint = endpoints.remove(getEndpointKey(decodeUri));
+            }
             if(oldEndpoint != null){
                 answer.add(oldEndpoint);
                 stopServices(oldEndpoint);
@@ -728,7 +726,7 @@ public abstract class AbstractCamelContext extends BaseService
                         try {
                             stopServices(oldEndpoint);
                         } catch (Exception e) {
-                            LOG.warn("Error stopping endpoint " + oldEndpoint + ". This exception will be ignored.", e);
+                            LOG.warn("Error stopping endpoint {}. This exception will be ignored.", oldEndpoint, e);
                         }
                         answer.add(oldEndpoint);
                         toRemove.add(entry.getKey());
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/engine/DefaultCamelContextTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/engine/DefaultCamelContextTest.java
index 6322df0088c..eda6076b268 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/engine/DefaultCamelContextTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/engine/DefaultCamelContextTest.java
@@ -40,6 +40,8 @@ import org.apache.camel.support.CamelContextHelper;
 import org.apache.camel.support.DefaultUuidGenerator;
 import org.apache.camel.support.NormalizedUri;
 import org.apache.camel.support.service.ServiceSupport;
+import org.apache.camel.util.StringHelper;
+import org.apache.camel.util.URISupport;
 import org.junit.jupiter.api.Test;
 
 import static org.junit.jupiter.api.Assertions.*;
@@ -430,15 +432,18 @@ public class DefaultCamelContextTest extends TestSupport {
         });
         ctx.start();
 
-
         EndpointRegistry<NormalizedUri> endpoints = ctx.getEndpointRegistry();
         Map<String, RouteService> routeServices = ctx.getRouteServices();
         Set<Endpoint> routeEndpoints =  routeServices.get("rawRoute").gatherEndpoints();
+
         for(Endpoint endpoint : routeEndpoints) {
             Endpoint oldEndpoint = endpoints.remove(ctx.getEndpointKey(endpoint.getEndpointUri()));
-            if(oldEndpoint == null){
-                oldEndpoint = endpoints.remove(ctx.getEndpointKey(ctx.unsafeUriCharactersDecodeWithOutPercent(endpoint.getEndpointUri())));
-            }else {
+            if(oldEndpoint == null) {
+                String decodeQuery = URISupport.getDecodeQuery(endpoint.getEndpointUri());
+                String decodeUri = StringHelper.before(endpoint.getEndpointUri(), "?") + "?" + decodeQuery;
+                oldEndpoint = endpoints.remove(ctx.getEndpointKey(decodeUri));
+
+            } else {
                 assertNotNull(oldEndpoint);
             }
             assertNotNull(oldEndpoint);
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 2d3b16fd911..4e7c1625e32 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
@@ -22,14 +22,7 @@ import java.net.URISyntaxException;
 import java.net.URLEncoder;
 import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
 import java.util.regex.Pattern;
 
 import static org.apache.camel.util.CamelURIParser.URI_ALREADY_NORMALIZED;
@@ -753,6 +746,34 @@ public final class URISupport {
         return rc;
     }
 
+    public static String getDecodeQuery(final String uri) {
+        try {
+            URI u = new URI(uri);
+            String query = URISupport.prepareQuery(u);
+            if(query == null){
+                return null;
+            }else {
+                Map<String, Object> parameters = URISupport.parseQuery(query, false, false);
+                if (parameters.size() == 1) {
+                    // only 1 parameter need to create new query string
+                    query = URISupport.createQueryString(parameters);
+                    return query;
+                } else {
+                    // reorder parameters a..z
+                    final Set<String> keySet = parameters.keySet();
+                    final String[] parametersArray = keySet.toArray(new String[keySet.size()]);
+                    Arrays.sort(parametersArray);
+
+                    // build uri object with sorted parameters
+                    query = URISupport.createQueryString(parametersArray, parameters, true);
+                    return query;
+                }
+            }
+        }catch(URISyntaxException ex){
+            return null;
+        }
+    }
+
     public static String pathAndQueryOf(final URI uri) {
         final String path = uri.getPath();
 
diff --git a/core/camel-util/src/main/java/org/apache/camel/util/UnsafeUriCharactersDecoder.java b/core/camel-util/src/main/java/org/apache/camel/util/UnsafeUriCharactersDecoder.java
deleted file mode 100644
index 49756536b15..00000000000
--- a/core/camel-util/src/main/java/org/apache/camel/util/UnsafeUriCharactersDecoder.java
+++ /dev/null
@@ -1,71 +0,0 @@
-/*
- * 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.util;
-
-
-import java.util.HashMap;
-import java.util.Map;
-
-public final class UnsafeUriCharactersDecoder {
-    private static final Map<String,String> unsafeStringsRfc1738;
-
-    static {
-        unsafeStringsRfc1738 = new HashMap<>();
-        unsafeStringsRfc1738.put("%22","\"");
-        unsafeStringsRfc1738.put("%3C","<");
-        unsafeStringsRfc1738.put("%3E",">");
-        unsafeStringsRfc1738.put("%7B","{");
-        unsafeStringsRfc1738.put("%7D","}");
-        unsafeStringsRfc1738.put("%7C","|");
-        unsafeStringsRfc1738.put("%5C","\\\\");
-        unsafeStringsRfc1738.put("%5E","^");
-        unsafeStringsRfc1738.put("%7E","~");
-        unsafeStringsRfc1738.put("%5B","[");
-        unsafeStringsRfc1738.put("%5D","]");
-        unsafeStringsRfc1738.put("%60","`");
-        unsafeStringsRfc1738.put("%20"," ");
-        unsafeStringsRfc1738.put("%23","#");
-    }
-
-    public static String decode(String uri){
-        int len = uri.length();
-        StringBuilder sb = new StringBuilder(len > 500 ? len / 2 : len);
-        for (int i = 0; i < len; i++) {
-            char ch = uri.charAt(i);
-            if (ch == '%') {
-                char next = i + 1 < len ? uri.charAt(i + 1) : ' ';
-                char next2 = i + 2 < len ? uri.charAt(i + 2) : ' ';
-                String encodedString = String.valueOf(ch) + next + next2;
-                if (isHexDigit(next) && isHexDigit(next2) && unsafeStringsRfc1738.containsKey(encodedString.toUpperCase())) {
-                    i = i + 2;
-                    sb.append(unsafeStringsRfc1738.get(encodedString));
-                } else {
-                    sb.append(ch);
-                }
-            } else {
-                sb.append(ch);
-            }
-        }
-        return sb.toString();
-    }
-
-    private static boolean isHexDigit(char ch) {
-        // 0..9 A..F a..f
-        return ch >= 48 && ch <= 57 || ch >= 65 && ch <= 70 || ch >= 97 && ch <= 102;
-    }
-
-}
\ No newline at end of file
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 1170222ab64..974fe5d5bd5 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
@@ -29,6 +29,7 @@ import org.junit.jupiter.api.Test;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertNotSame;
@@ -612,4 +613,29 @@ public class URISupportTest {
         assertEquals("hey=foo&hey=bar&hey=3&hey=true&hey=baz", URISupport.buildMultiValueQuery("hey", list));
     }
 
+    @Test
+    public void testGetDecodeQuery() throws Exception{
+        String out = URISupport.normalizeUri("smtp://localhost?username=davsclaus&password=secret");
+        String enc = UnsafeUriCharactersEncoder.encode(out);
+        String dec = StringHelper.before(enc,"?") + "?" + URISupport.getDecodeQuery(enc);
+        assertEquals(out, dec);
+
+        out = URISupport.normalizeUri("smtp://localhost?password=secret&username=davsclaus");
+        assertEquals(out, dec);
+
+        out = URISupport.normalizeUri("http://localhost?username=davsclaus&password=RAW(#@a)");
+        enc = UnsafeUriCharactersEncoder.encode(out);
+        assertNotEquals(out, enc);
+
+        dec = StringHelper.before(enc,"?") + "?" + URISupport.getDecodeQuery(enc);
+        assertEquals(out, dec);
+
+        out = URISupport.normalizeUri("bean://MyBean?method=RAW(addString(%22#@a%23, test))");
+        enc = UnsafeUriCharactersEncoder.encode(out);
+        assertNotEquals(out, enc);
+
+        dec = StringHelper.before(enc,"?") + "?" + URISupport.getDecodeQuery(enc);
+        assertEquals(out, dec);
+
+    }
 }


[camel] 05/06: don't use * import

Posted by or...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit f9e821f03de2a536ed48c5925b195e687df6e366
Author: luke.me <lu...@kakaoenterprise.com>
AuthorDate: Sun Jun 18 16:03:35 2023 +0900

    don't use * import
---
 .../src/main/java/org/apache/camel/util/URISupport.java          | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

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 ed724de74e9..40cc7b92e2a 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
@@ -22,7 +22,14 @@ import java.net.URISyntaxException;
 import java.net.URLEncoder;
 import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
-import java.util.*;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
 import java.util.regex.Pattern;
 
 import static org.apache.camel.util.CamelURIParser.URI_ALREADY_NORMALIZED;


[camel] 06/06: Change to Checkstyle format

Posted by or...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit e63792ff182eefe3dd312c86b11b2b907d8c32d0
Author: luke.me <lu...@kakaoenterprise.com>
AuthorDate: Mon Jun 19 16:23:26 2023 +0900

    Change to Checkstyle format
---
 .../java/org/apache/camel/impl/engine/AbstractCamelContext.java   | 4 ++--
 .../org/apache/camel/impl/engine/DefaultCamelContextTest.java     | 8 +++-----
 .../src/main/java/org/apache/camel/util/URISupport.java           | 8 ++++----
 .../src/test/java/org/apache/camel/util/URISupportTest.java       | 4 ++--
 4 files changed, 11 insertions(+), 13 deletions(-)

diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
index 0439da8df0c..e22568d641e 100644
--- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
+++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
@@ -711,10 +711,10 @@ public abstract class AbstractCamelContext extends BaseService
             stopServices(oldEndpoint);
         } else {
             String decodeUri = URISupport.getDecodeQuery(uri);
-            if(decodeUri != null) {
+            if (decodeUri != null) {
                 oldEndpoint = endpoints.remove(getEndpointKey(decodeUri));
             }
-            if(oldEndpoint != null){
+            if (oldEndpoint != null) {
                 answer.add(oldEndpoint);
                 stopServices(oldEndpoint);
             } else {
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/engine/DefaultCamelContextTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/engine/DefaultCamelContextTest.java
index 336f681ccab..1de5a3a9e8d 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/engine/DefaultCamelContextTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/engine/DefaultCamelContextTest.java
@@ -40,7 +40,6 @@ import org.apache.camel.support.CamelContextHelper;
 import org.apache.camel.support.DefaultUuidGenerator;
 import org.apache.camel.support.NormalizedUri;
 import org.apache.camel.support.service.ServiceSupport;
-import org.apache.camel.util.StringHelper;
 import org.apache.camel.util.URISupport;
 import org.junit.jupiter.api.Test;
 
@@ -434,11 +433,11 @@ public class DefaultCamelContextTest extends TestSupport {
 
         EndpointRegistry<NormalizedUri> endpoints = ctx.getEndpointRegistry();
         Map<String, RouteService> routeServices = ctx.getRouteServices();
-        Set<Endpoint> routeEndpoints =  routeServices.get("rawRoute").gatherEndpoints();
+        Set<Endpoint> routeEndpoints = routeServices.get("rawRoute").gatherEndpoints();
 
-        for(Endpoint endpoint : routeEndpoints) {
+        for (Endpoint endpoint : routeEndpoints) {
             Endpoint oldEndpoint = endpoints.remove(ctx.getEndpointKey(endpoint.getEndpointUri()));
-            if(oldEndpoint == null) {
+            if (oldEndpoint == null) {
                 String decodeUri = URISupport.getDecodeQuery(endpoint.getEndpointUri());
                 oldEndpoint = endpoints.remove(ctx.getEndpointKey(decodeUri));
 
@@ -450,7 +449,6 @@ public class DefaultCamelContextTest extends TestSupport {
 
     }
 
-
     private static class MyService extends ServiceSupport implements CamelContextAware {
 
         private CamelContext camelContext;
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 40cc7b92e2a..9e1df36550c 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
@@ -753,7 +753,7 @@ public final class URISupport {
         return rc;
     }
 
-    private static String makeUri(String uriWithoutQuery, String query){
+    private static String makeUri(String uriWithoutQuery, String query) {
         int len = uriWithoutQuery.length();
         if (query != null) {
             len += 1 + query.length();
@@ -772,9 +772,9 @@ public final class URISupport {
             URI u = new URI(uri);
             String query = URISupport.prepareQuery(u);
             String uriWithoutQuery = URISupport.stripQuery(uri);
-            if(query == null){
+            if (query == null) {
                 return uriWithoutQuery;
-            }else {
+            } else {
                 Map<String, Object> parameters = URISupport.parseQuery(query, false, false);
                 if (parameters.size() == 1) {
                     // only 1 parameter need to create new query string
@@ -791,7 +791,7 @@ public final class URISupport {
                     return makeUri(uriWithoutQuery, query);
                 }
             }
-        }catch(URISyntaxException ex){
+        } catch (URISyntaxException ex) {
             return null;
         }
     }
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 832206e15d4..14a4cdb5856 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
@@ -29,8 +29,8 @@ import org.junit.jupiter.api.Test;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertNotEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertNotSame;
 import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -614,7 +614,7 @@ public class URISupportTest {
     }
 
     @Test
-    public void testGetDecodeQuery() throws Exception{
+    public void testGetDecodeQuery() throws Exception {
         String out = URISupport.normalizeUri("smtp://localhost?username=davsclaus&password=secret");
         String enc = UnsafeUriCharactersEncoder.encode(out);
         String dec = URISupport.getDecodeQuery(enc);


[camel] 01/06: [CAMEL-19456] The invocation of the removeRoute() method is too slow when using RAW().

Posted by or...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 2b695defc151dff11cac5d6e4f0302ff94beece6
Author: luke.me <lu...@kakaoenterprise.com>
AuthorDate: Thu Jun 15 16:04:58 2023 +0900

    [CAMEL-19456] The invocation of the removeRoute() method is too slow when using RAW().
---
 .../camel/impl/engine/AbstractCamelContext.java    | 44 +++++++++--------
 .../camel/impl/engine/DefaultCamelContextTest.java | 34 +++++++++++++
 .../java/org/apache/camel/impl/engine/MyBean.java  |  8 ++++
 .../camel/util/UnsafeUriCharactersDecoder.java     | 55 ++++++++++++++++++++++
 4 files changed, 122 insertions(+), 19 deletions(-)

diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
index c67cadb0387..5982d6001d6 100644
--- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
+++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
@@ -175,12 +175,7 @@ import org.apache.camel.support.jsse.SSLContextParameters;
 import org.apache.camel.support.service.BaseService;
 import org.apache.camel.support.service.ServiceHelper;
 import org.apache.camel.support.startup.DefaultStartupStepRecorder;
-import org.apache.camel.util.IOHelper;
-import org.apache.camel.util.ObjectHelper;
-import org.apache.camel.util.StopWatch;
-import org.apache.camel.util.StringHelper;
-import org.apache.camel.util.TimeUtils;
-import org.apache.camel.util.URISupport;
+import org.apache.camel.util.*;
 import org.apache.camel.vault.VaultConfiguration;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -702,6 +697,10 @@ public abstract class AbstractCamelContext extends BaseService
         removeEndpoints(endpoint.getEndpointUri());
     }
 
+    public String unsafeUriCharactersDecodeWithOutPercent(String uri){
+        return UnsafeUriCharactersDecoder.decode(uri);
+    }
+
     @Override
     public Collection<Endpoint> removeEndpoints(String uri) throws Exception {
         Collection<Endpoint> answer = new ArrayList<>();
@@ -710,21 +709,28 @@ public abstract class AbstractCamelContext extends BaseService
             answer.add(oldEndpoint);
             stopServices(oldEndpoint);
         } else {
-            List<NormalizedUri> toRemove = new ArrayList<>();
-            for (Map.Entry<NormalizedUri, Endpoint> entry : endpoints.entrySet()) {
-                oldEndpoint = entry.getValue();
-                if (EndpointHelper.matchEndpoint(this, oldEndpoint.getEndpointUri(), uri)) {
-                    try {
-                        stopServices(oldEndpoint);
-                    } catch (Exception e) {
-                        LOG.warn("Error stopping endpoint {}. This exception will be ignored.", oldEndpoint, e);
+            String decodeUri = unsafeUriCharactersDecodeWithOutPercent(uri);
+            oldEndpoint = endpoints.remove(getEndpointKey(decodeUri));
+            if(oldEndpoint != null){
+                answer.add(oldEndpoint);
+                stopServices(oldEndpoint);
+            } else {
+                List<NormalizedUri> toRemove = new ArrayList<>();
+                for (Map.Entry<NormalizedUri, Endpoint> entry : endpoints.entrySet()) {
+                    oldEndpoint = entry.getValue();
+                    if (EndpointHelper.matchEndpoint(this, oldEndpoint.getEndpointUri(), uri)) {
+                        try {
+                            stopServices(oldEndpoint);
+                        } catch (Exception e) {
+                            LOG.warn("Error stopping endpoint " + oldEndpoint + ". This exception will be ignored.", e);
+                        }
+                        answer.add(oldEndpoint);
+                        toRemove.add(entry.getKey());
                     }
-                    answer.add(oldEndpoint);
-                    toRemove.add(entry.getKey());
                 }
-            }
-            for (NormalizedUri key : toRemove) {
-                endpoints.remove(key);
+                for (NormalizedUri key : toRemove) {
+                    endpoints.remove(key);
+                }
             }
         }
 
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/engine/DefaultCamelContextTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/engine/DefaultCamelContextTest.java
index 5e236feec55..6322df0088c 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/engine/DefaultCamelContextTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/engine/DefaultCamelContextTest.java
@@ -18,6 +18,7 @@ package org.apache.camel.impl.engine;
 
 import java.util.Collection;
 import java.util.Iterator;
+import java.util.Map;
 import java.util.Set;
 
 import org.apache.camel.CamelContext;
@@ -37,6 +38,7 @@ import org.apache.camel.spi.EndpointRegistry;
 import org.apache.camel.spi.UuidGenerator;
 import org.apache.camel.support.CamelContextHelper;
 import org.apache.camel.support.DefaultUuidGenerator;
+import org.apache.camel.support.NormalizedUri;
 import org.apache.camel.support.service.ServiceSupport;
 import org.junit.jupiter.api.Test;
 
@@ -413,6 +415,38 @@ public class DefaultCamelContextTest extends TestSupport {
         assertNull(ctx.hasService(MyService.class));
     }
 
+    @Test
+    public void testRemoveRoute() throws Exception {
+        DefaultCamelContext ctx = new DefaultCamelContext(false);
+
+        ctx.disableJMX();
+        ctx.getRegistry().bind("MyBean", MyBean.class);
+
+        ctx.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start").routeId("rawRoute").to("bean:MyBean?method=RAW(addString('aa a',${body}))");
+            }
+        });
+        ctx.start();
+
+
+        EndpointRegistry<NormalizedUri> endpoints = ctx.getEndpointRegistry();
+        Map<String, RouteService> routeServices = ctx.getRouteServices();
+        Set<Endpoint> routeEndpoints =  routeServices.get("rawRoute").gatherEndpoints();
+        for(Endpoint endpoint : routeEndpoints) {
+            Endpoint oldEndpoint = endpoints.remove(ctx.getEndpointKey(endpoint.getEndpointUri()));
+            if(oldEndpoint == null){
+                oldEndpoint = endpoints.remove(ctx.getEndpointKey(ctx.unsafeUriCharactersDecodeWithOutPercent(endpoint.getEndpointUri())));
+            }else {
+                assertNotNull(oldEndpoint);
+            }
+            assertNotNull(oldEndpoint);
+        }
+
+    }
+
+
     private static class MyService extends ServiceSupport implements CamelContextAware {
 
         private CamelContext camelContext;
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/engine/MyBean.java b/core/camel-core/src/test/java/org/apache/camel/impl/engine/MyBean.java
new file mode 100644
index 00000000000..d6e9faf43e2
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/engine/MyBean.java
@@ -0,0 +1,8 @@
+package org.apache.camel.impl.engine;
+
+@org.apache.camel.spi.annotations.Component(value = "MyBean")
+public class MyBean {
+    public String addString(String source, String dst) throws Exception {
+        return source + dst;
+    }
+}
diff --git a/core/camel-util/src/main/java/org/apache/camel/util/UnsafeUriCharactersDecoder.java b/core/camel-util/src/main/java/org/apache/camel/util/UnsafeUriCharactersDecoder.java
new file mode 100644
index 00000000000..6fa7af5a49b
--- /dev/null
+++ b/core/camel-util/src/main/java/org/apache/camel/util/UnsafeUriCharactersDecoder.java
@@ -0,0 +1,55 @@
+package org.apache.camel.util;
+
+
+import java.util.HashMap;
+import java.util.Map;
+
+public final class UnsafeUriCharactersDecoder {
+    private static final Map<String,String> unsafeStringsRfc1738;
+
+    static {
+        unsafeStringsRfc1738 = new HashMap<>();
+        unsafeStringsRfc1738.put("%22","\"");
+        unsafeStringsRfc1738.put("%3C","<");
+        unsafeStringsRfc1738.put("%3E",">");
+        unsafeStringsRfc1738.put("%7B","{");
+        unsafeStringsRfc1738.put("%7D","}");
+        unsafeStringsRfc1738.put("%7C","|");
+        unsafeStringsRfc1738.put("%5C","\\\\");
+        unsafeStringsRfc1738.put("%5E","^");
+        unsafeStringsRfc1738.put("%7E","~");
+        unsafeStringsRfc1738.put("%5B","[");
+        unsafeStringsRfc1738.put("%5D","]");
+        unsafeStringsRfc1738.put("%60","`");
+        unsafeStringsRfc1738.put("%20"," ");
+        unsafeStringsRfc1738.put("%23","#");
+    }
+
+    public static String decode(String uri){
+        int len = uri.length();
+        StringBuilder sb = new StringBuilder(len > 500 ? len / 2 : len);
+        for (int i = 0; i < len; i++) {
+            char ch = uri.charAt(i);
+            if (ch == '%') {
+                char next = i + 1 < len ? uri.charAt(i + 1) : ' ';
+                char next2 = i + 2 < len ? uri.charAt(i + 2) : ' ';
+                String encodedString = String.valueOf(ch) + next + next2;
+                if (isHexDigit(next) && isHexDigit(next2) && unsafeStringsRfc1738.containsKey(encodedString.toUpperCase())) {
+                    i = i + 2;
+                    sb.append(unsafeStringsRfc1738.get(encodedString));
+                } else {
+                    sb.append(ch);
+                }
+            } else {
+                sb.append(ch);
+            }
+        }
+        return sb.toString();
+    }
+
+    private static boolean isHexDigit(char ch) {
+        // 0..9 A..F a..f
+        return ch >= 48 && ch <= 57 || ch >= 65 && ch <= 70 || ch >= 97 && ch <= 102;
+    }
+
+}
\ No newline at end of file


[camel] 04/06: The return value of the getDecodeQuery method has been changed from query to uri.

Posted by or...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 4e8d18d3d0f2b0594ec202637991abde09881731
Author: luke.me <lu...@kakaoenterprise.com>
AuthorDate: Sun Jun 18 12:46:51 2023 +0900

    The return value of the getDecodeQuery method has been changed from query to uri.
---
 .../camel/impl/engine/AbstractCamelContext.java     |  5 ++---
 .../camel/impl/engine/DefaultCamelContextTest.java  |  3 +--
 .../main/java/org/apache/camel/util/URISupport.java | 21 ++++++++++++++++++---
 .../java/org/apache/camel/util/URISupportTest.java  |  6 +++---
 4 files changed, 24 insertions(+), 11 deletions(-)

diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
index 82cede02b93..0439da8df0c 100644
--- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
+++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
@@ -710,9 +710,8 @@ public abstract class AbstractCamelContext extends BaseService
             answer.add(oldEndpoint);
             stopServices(oldEndpoint);
         } else {
-            String decodeQuery = URISupport.getDecodeQuery(uri);
-            if(decodeQuery != null) {
-                String decodeUri = StringHelper.before(uri, "?") + "?" + decodeQuery;
+            String decodeUri = URISupport.getDecodeQuery(uri);
+            if(decodeUri != null) {
                 oldEndpoint = endpoints.remove(getEndpointKey(decodeUri));
             }
             if(oldEndpoint != null){
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/engine/DefaultCamelContextTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/engine/DefaultCamelContextTest.java
index eda6076b268..336f681ccab 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/engine/DefaultCamelContextTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/engine/DefaultCamelContextTest.java
@@ -439,8 +439,7 @@ public class DefaultCamelContextTest extends TestSupport {
         for(Endpoint endpoint : routeEndpoints) {
             Endpoint oldEndpoint = endpoints.remove(ctx.getEndpointKey(endpoint.getEndpointUri()));
             if(oldEndpoint == null) {
-                String decodeQuery = URISupport.getDecodeQuery(endpoint.getEndpointUri());
-                String decodeUri = StringHelper.before(endpoint.getEndpointUri(), "?") + "?" + decodeQuery;
+                String decodeUri = URISupport.getDecodeQuery(endpoint.getEndpointUri());
                 oldEndpoint = endpoints.remove(ctx.getEndpointKey(decodeUri));
 
             } else {
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 4e7c1625e32..ed724de74e9 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
@@ -746,18 +746,33 @@ public final class URISupport {
         return rc;
     }
 
+    private static String makeUri(String uriWithoutQuery, String query){
+        int len = uriWithoutQuery.length();
+        if (query != null) {
+            len += 1 + query.length();
+            StringBuilder sb = new StringBuilder(len);
+            sb.append(uriWithoutQuery).append('?').append(query);
+            return sb.toString();
+        } else {
+            StringBuilder sb = new StringBuilder(len);
+            sb.append(uriWithoutQuery);
+            return sb.toString();
+        }
+    }
+
     public static String getDecodeQuery(final String uri) {
         try {
             URI u = new URI(uri);
             String query = URISupport.prepareQuery(u);
+            String uriWithoutQuery = URISupport.stripQuery(uri);
             if(query == null){
-                return null;
+                return uriWithoutQuery;
             }else {
                 Map<String, Object> parameters = URISupport.parseQuery(query, false, false);
                 if (parameters.size() == 1) {
                     // only 1 parameter need to create new query string
                     query = URISupport.createQueryString(parameters);
-                    return query;
+                    return makeUri(uriWithoutQuery, query);
                 } else {
                     // reorder parameters a..z
                     final Set<String> keySet = parameters.keySet();
@@ -766,7 +781,7 @@ public final class URISupport {
 
                     // build uri object with sorted parameters
                     query = URISupport.createQueryString(parametersArray, parameters, true);
-                    return query;
+                    return makeUri(uriWithoutQuery, query);
                 }
             }
         }catch(URISyntaxException ex){
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 974fe5d5bd5..832206e15d4 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
@@ -617,7 +617,7 @@ public class URISupportTest {
     public void testGetDecodeQuery() throws Exception{
         String out = URISupport.normalizeUri("smtp://localhost?username=davsclaus&password=secret");
         String enc = UnsafeUriCharactersEncoder.encode(out);
-        String dec = StringHelper.before(enc,"?") + "?" + URISupport.getDecodeQuery(enc);
+        String dec = URISupport.getDecodeQuery(enc);
         assertEquals(out, dec);
 
         out = URISupport.normalizeUri("smtp://localhost?password=secret&username=davsclaus");
@@ -627,14 +627,14 @@ public class URISupportTest {
         enc = UnsafeUriCharactersEncoder.encode(out);
         assertNotEquals(out, enc);
 
-        dec = StringHelper.before(enc,"?") + "?" + URISupport.getDecodeQuery(enc);
+        dec = URISupport.getDecodeQuery(enc);
         assertEquals(out, dec);
 
         out = URISupport.normalizeUri("bean://MyBean?method=RAW(addString(%22#@a%23, test))");
         enc = UnsafeUriCharactersEncoder.encode(out);
         assertNotEquals(out, enc);
 
-        dec = StringHelper.before(enc,"?") + "?" + URISupport.getDecodeQuery(enc);
+        dec = URISupport.getDecodeQuery(enc);
         assertEquals(out, dec);
 
     }