This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push: new 970b1879974 CAMEL-22194: camel-thymeleaf - Add support for loading templates via ref|bean via ResourceLoader 970b1879974 is described below commit 970b187997465f58c52ffdc5ff4c7bcf9388e776 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Mon Jun 23 21:16:31 2025 +0200 CAMEL-22194: camel-thymeleaf - Add support for loading templates via ref|bean via ResourceLoader --- .../component/thymeleaf/ThymeleafEndpoint.java | 7 +++ .../component/thymeleaf/ThymeleafRefTest.java | 59 ++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/components/camel-thymeleaf/src/main/java/org/apache/camel/component/thymeleaf/ThymeleafEndpoint.java b/components/camel-thymeleaf/src/main/java/org/apache/camel/component/thymeleaf/ThymeleafEndpoint.java index 7e5a2c7074e..0c7f49ef59c 100644 --- a/components/camel-thymeleaf/src/main/java/org/apache/camel/component/thymeleaf/ThymeleafEndpoint.java +++ b/components/camel-thymeleaf/src/main/java/org/apache/camel/component/thymeleaf/ThymeleafEndpoint.java @@ -27,6 +27,8 @@ import org.apache.camel.component.ResourceEndpoint; import org.apache.camel.spi.UriEndpoint; import org.apache.camel.spi.UriParam; import org.apache.camel.support.ExchangeHelper; +import org.apache.camel.support.ResourceHelper; +import org.apache.camel.util.IOHelper; import org.apache.camel.util.ObjectHelper; import org.thymeleaf.TemplateEngine; import org.thymeleaf.context.Context; @@ -348,6 +350,11 @@ public class ThymeleafEndpoint extends ResourceEndpoint { exchange.getIn().removeHeader(ThymeleafConstants.THYMELEAF_VARIABLE_MAP); } + if (!resolver.equals(ThymeleafResolverType.URL) && ResourceHelper.hasScheme(this.template)) { + // favour to use Camel to load via resource loader + this.template = IOHelper.loadText(getResourceAsInputStream()); + } + // let thymeleaf parse and generate the result TemplateEngine templateEngine = getTemplateEngine(); Context context = new Context(); diff --git a/components/camel-thymeleaf/src/test/java/org/apache/camel/component/thymeleaf/ThymeleafRefTest.java b/components/camel-thymeleaf/src/test/java/org/apache/camel/component/thymeleaf/ThymeleafRefTest.java new file mode 100644 index 00000000000..472ef6dc127 --- /dev/null +++ b/components/camel-thymeleaf/src/test/java/org/apache/camel/component/thymeleaf/ThymeleafRefTest.java @@ -0,0 +1,59 @@ +/* + * 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.thymeleaf; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ThymeleafRefTest extends ThymeleafAbstractBaseTest { + + private static final String TEMP + = "Hello [(${headers.name})]. You ordered item [(${exchange.properties.item})] on [(${body})]."; + + @Test + public void testRef() { + Exchange exchange = template.request("direct:a", new Processor() { + @Override + public void process(Exchange exchange) { + exchange.getIn().setBody("Tuesday"); + exchange.getIn().setHeader("name", "Christian"); + exchange.setProperty("item", "8"); + } + }); + + assertEquals("Hello Christian. You ordered item 8 on Tuesday.\n", exchange.getMessage().getBody()); + assertEquals("Christian", exchange.getMessage().getHeader("name")); + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + + public void configure() { + context.getRegistry().bind("mytemp", TEMP); + + from("direct:a").to( + "thymeleaf:ref:mytemp?templateMode=TEXT&resolver=STRING&allowContextMapAll=true"); + } + }; + } + +}