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:59 UTC

[camel] 03/06: Decode the parameters

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);
+
+    }
 }