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 af3b628f7f4 CAMEL-22194: camel-freemarker - Add support for loading 
templates via ref|bean via ResourceLoader
af3b628f7f4 is described below

commit af3b628f7f4fa5df3cb13d8312675f5a0fe9d5ec
Author: Claus Ibsen <[email protected]>
AuthorDate: Mon Jun 23 20:56:32 2025 +0200

    CAMEL-22194: camel-freemarker - Add support for loading templates via 
ref|bean via ResourceLoader
---
 .../component/freemarker/FreemarkerEndpoint.java   |  9 +++-
 .../component/freemarker/FreemarkerRefTest.java    | 57 ++++++++++++++++++++++
 2 files changed, 65 insertions(+), 1 deletion(-)

diff --git 
a/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerEndpoint.java
 
b/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerEndpoint.java
index 509622be157..96896c15030 100644
--- 
a/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerEndpoint.java
+++ 
b/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerEndpoint.java
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.component.freemarker;
 
+import java.io.InputStreamReader;
 import java.io.Reader;
 import java.io.StringReader;
 import java.io.StringWriter;
@@ -30,6 +31,7 @@ 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.ObjectHelper;
 
 /**
@@ -166,13 +168,18 @@ public class FreemarkerEndpoint extends ResourceEndpoint {
         if (dataModel == null) {
             dataModel = ExchangeHelper.createVariableMap(exchange, 
isAllowContextMapAll());
         }
+
         // let freemarker parse and generate the result in buffer
         Template template;
+        if (reader == null && ResourceHelper.hasScheme(path)) {
+            // favour to use Camel to load via resource loader
+            reader = new InputStreamReader(getResourceAsInputStream());
+        }
 
         if (reader != null) {
             log.debug("Freemarker is evaluating template read from header {} 
using context: {}",
                     FreemarkerConstants.FREEMARKER_TEMPLATE, dataModel);
-            template = new Template("temp", reader, new 
Configuration(Configuration.VERSION_2_3_32));
+            template = new Template("temp", reader, new 
Configuration(Configuration.VERSION_2_3_34));
         } else {
             log.debug("Freemarker is evaluating {} using context: {}", path, 
dataModel);
             if (getEncoding() != null) {
diff --git 
a/components/camel-freemarker/src/test/java/org/apache/camel/component/freemarker/FreemarkerRefTest.java
 
b/components/camel-freemarker/src/test/java/org/apache/camel/component/freemarker/FreemarkerRefTest.java
new file mode 100644
index 00000000000..a049fcddde9
--- /dev/null
+++ 
b/components/camel-freemarker/src/test/java/org/apache/camel/component/freemarker/FreemarkerRefTest.java
@@ -0,0 +1,57 @@
+/*
+ * 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.freemarker;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class FreemarkerRefTest extends CamelTestSupport {
+
+    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.", 
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(
+                        "freemarker:ref:mytemp?allowContextMapAll=true");
+            }
+        };
+    }
+}

Reply via email to