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 2025/09/25 16:49:00 UTC
(camel) 01/01: CAMEL-22453: camel-sql: Fix support for using Java String text blocks for multi line queries in Camel endpoints.
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch sql
in repository https://gitbox.apache.org/repos/asf/camel.git
commit 4dfa6e3d34a475a6a0b07f8be6b59cb5b715e3e7
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Sep 25 18:48:40 2025 +0200
CAMEL-22453: camel-sql: Fix support for using Java String text blocks for multi line queries in Camel endpoints.
---
.../component/sql/SqlProducerInTextBlockTest.java | 45 ++++++++++++++++++++++
.../camel/processor/UriAsJava17TextBlockTest.java | 5 ++-
.../java/org/apache/camel/util/URISupport.java | 24 +++++++++++-
3 files changed, 70 insertions(+), 4 deletions(-)
diff --git a/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlProducerInTextBlockTest.java b/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlProducerInTextBlockTest.java
new file mode 100644
index 00000000000..67afef7eb0f
--- /dev/null
+++ b/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlProducerInTextBlockTest.java
@@ -0,0 +1,45 @@
+/*
+ * 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.component.sql;
+
+import org.apache.camel.builder.RouteBuilder;
+
+public class SqlProducerInTextBlockTest extends SqlProducerInTest {
+
+ private static final String SQL = """
+ select *
+ from projects
+ where project in (:#in:names)
+ order by id
+ """;
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ // required for the sql component
+ getContext().getComponent("sql", SqlComponent.class).setDataSource(db);
+
+ from("direct:query")
+ .to("sql:" + SQL)
+ .to("log:query")
+ .to("mock:query");
+ }
+ };
+ }
+}
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
index 1bf47ebd4ae..828a421c0c8 100644
--- 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
@@ -51,10 +51,11 @@ public class UriAsJava17TextBlockTest extends ContextTestSupport {
.to("log:foo")
.to("log:bar")
.to("""
- mock:result
- ?retainFirst=123
+ mock:result?
+ retainFirst=123
&failFast=false
&resultWaitTime=5000""");
+ // the ? must always be at end of line, but & can be in beginning also
}
};
}
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 b2a3db90658..beaec95280f 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
@@ -32,9 +32,9 @@ import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.StringJoiner;
+import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Function;
import java.util.regex.Pattern;
-import java.util.stream.Collectors;
import static org.apache.camel.util.CamelURIParser.URI_ALREADY_NORMALIZED;
@@ -124,7 +124,27 @@ public final class URISupport {
public static String textBlockToSingleLine(String uri) {
// Java 17 text blocks have new lines with optional white space
if (uri != null) {
- uri = uri.lines().map(String::trim).collect(Collectors.joining());
+ // we want text blocks to be as-is before query parameters
+ // as this allows some Camel components to provide code or SQL
+ // to be provided in the context-path.
+ // query parameters are key=value pairs in Camel endpoints and therefore
+ // the lines should be trimmed (after we detect the first ? sign).
+ final AtomicBoolean query = new AtomicBoolean();
+ StringJoiner sj = new StringJoiner("");
+ // use lines() to support splitting in any OS platform (also Windows)
+ uri.lines().forEach(l -> {
+ l = l.trim();
+ if (!query.get()) {
+ l = l + " ";
+ if (l.indexOf('?') != -1) {
+ query.set(true);
+ l = l.trim();
+ }
+ }
+ sj.add(l);
+ });
+ uri = sj.toString();
+ uri = uri.trim();
}
return uri;
}