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 2023/10/11 10:20:43 UTC

[camel] 01/01: CAMEL-19977: camel-core - Java DSL - Add support for using Java 17 text blocks for long URIs which contains line breaks that need to change URI back into a single line for noramlization in Camel.

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

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

commit 4f254f6cce61ffed844ddaa08394a7322536b9a2
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Oct 11 12:20:22 2023 +0200

    CAMEL-19977: camel-core - Java DSL - Add support for using Java 17 text blocks for long URIs which contains line breaks that need to change URI back into a single line for noramlization in Camel.
---
 .../camel/impl/engine/AbstractCamelContext.java    |  4 +-
 .../camel/processor/UriAsJava17TextBlockTest.java  | 61 ++++++++++++++++++++++
 .../java/org/apache/camel/util/URISupport.java     | 18 +++++++
 3 files changed, 82 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 7a771fd0425..027bcf6d89c 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
@@ -775,8 +775,10 @@ public abstract class AbstractCamelContext extends BaseService
 
         LOG.trace("Getting endpoint with uri: {} and parameters: {}", uri, parameters);
 
-        // in case path has property placeholders then try to let property component resolve those
         if (!normalized) {
+            // java 17 text blocks to single line uri
+            uri = URISupport.textBlockToSingleLine(uri);
+            // in case path has property placeholders then try to let property component resolve those
             uri = EndpointHelper.resolveEndpointUriPropertyPlaceholders(this, uri);
         }
 
diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/UriAsJava17TextBlockTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/UriAsJava17TextBlockTest.java
new file mode 100644
index 00000000000..b92c20f752f
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/processor/UriAsJava17TextBlockTest.java
@@ -0,0 +1,61 @@
+/*
+ * 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.processor;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class UriAsJava17TextBlockTest extends ContextTestSupport {
+
+    @Test
+    public void testUriTestBlock() throws Exception {
+        assertEquals(1, context.getRoutesSize());
+
+        MockEndpoint mock = getMockEndpoint("mock:result?retainFirst=123&failFast=false&resultWaitTime=5000");
+        mock.expectedBodiesReceived("Hello World");
+
+        template.sendBody("direct:start", "Hello World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from(
+                        """
+                                   direct:start?
+                                   block=false&
+                                   timeout=1234
+                                """)
+                        .to("log:foo")
+                        .to("log:bar")
+                        .to("""
+                                mock:result
+                                ?retainFirst=123
+                                &failFast=false
+                                &resultWaitTime=5000""");
+            }
+        };
+    }
+}
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 4e2da8fff68..6945893da7f 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
@@ -43,6 +43,10 @@ public final class URISupport {
     public static final char[] RAW_TOKEN_START = { '(', '{' };
     public static final char[] RAW_TOKEN_END = { ')', '}' };
 
+    // Java 17 text blocks have new lines with optional white space
+    private static final String TEXT_BLOCK_MARKER = System.lineSeparator();
+    private static final Pattern TEXT_BLOCK_PATTERN = Pattern.compile("\n\\s*");
+
     // Match any key-value pair in the URI query string whose key contains
     // "passphrase" or "password" or secret key (case-insensitive).
     // First capture group is the key, second is the value.
@@ -86,6 +90,15 @@ public final class URISupport {
         return sanitized;
     }
 
+    public static String textBlockToSingleLine(String uri) {
+        // text blocks
+        if (uri != null && uri.contains(TEXT_BLOCK_MARKER)) {
+            uri = TEXT_BLOCK_PATTERN.matcher(uri).replaceAll("");
+            uri = uri.trim();
+        }
+        return uri;
+    }
+
     /**
      * Removes detected sensitive information (such as passwords) from the <em>path part</em> of an URI (that is, the
      * part without the query parameters or component prefix) and returns the result.
@@ -612,6 +625,8 @@ public final class URISupport {
      * @throws URISyntaxException is thrown if syntax error in the input uri
      */
     public static URI normalizeUriAsURI(String uri) throws URISyntaxException {
+        // java 17 text blocks to single line uri
+        uri = URISupport.textBlockToSingleLine(uri);
         return new URI(UnsafeUriCharactersEncoder.encode(uri, true));
     }
 
@@ -620,6 +635,9 @@ public final class URISupport {
      * values, or other unsafe URL characters, or have authority user/password, etc.
      */
     private static String doComplexNormalizeUri(String uri) throws URISyntaxException {
+        // java 17 text blocks to single line uri
+        uri = URISupport.textBlockToSingleLine(uri);
+
         URI u = new URI(UnsafeUriCharactersEncoder.encode(uri, true));
         String scheme = u.getScheme();
         String path = u.getSchemeSpecificPart();