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 ec6c5c8706f9 CAMEL-22935: Add docs
ec6c5c8706f9 is described below

commit ec6c5c8706f905c6819773751c2168a2a8781adb
Author: Claus Ibsen <[email protected]>
AuthorDate: Mon Feb 2 11:38:18 2026 +0100

    CAMEL-22935: Add docs
---
 .../modules/languages/pages/simple-language.adoc   | 57 ++++++++++++++++++++++
 .../language/simple/SimpleCustomFunctionTest.java  | 38 +++++++++++++--
 2 files changed, 92 insertions(+), 3 deletions(-)

diff --git 
a/core/camel-core-languages/src/main/docs/modules/languages/pages/simple-language.adoc
 
b/core/camel-core-languages/src/main/docs/modules/languages/pages/simple-language.adoc
index 4a37b75a3a00..85550e46aaac 100644
--- 
a/core/camel-core-languages/src/main/docs/modules/languages/pages/simple-language.adoc
+++ 
b/core/camel-core-languages/src/main/docs/modules/languages/pages/simple-language.adoc
@@ -1823,6 +1823,63 @@ And in XML DSL you use the pretty attribute to true as 
show below:
 </route>
 ----
 
+== Adding custom functions to Simple language
+
+You can add custom functions to the Simple language by adding to the 
`org.apache.camel.spi.SimpleFunctionRegistry`.
+
+This can be done programmatically such as:
+
+[source,java]
+----
+SimpleFunctionRegistry reg = 
PluginHelper.getSimpleFunctionRegistry(getCamelContext());
+
+reg.addFunction("foo", new ExpressionAdapter() {
+    @Override
+    public Object evaluate(Exchange exchange) {
+        return "I was here " + exchange.getMessage().getBody();
+    }
+});
+----
+
+Here we add a custom function named `foo` which is implemented as a 
`org.apache.camel.Expression` which conveniently
+can be done by using the `org.apache.camel.support.ExpressionAdapter` class as 
above.
+
+The foo function can now be used in simple such as:
+
+[source,java]
+----
+from("direct:start2")
+        .setBody(simple("Hello ${foo}"))
+        .to("mock:result");
+----
+
+Notice how the foo function is used just like it was a built-in function using 
`$\{foo}` syntax.
+
+Since a custom function is just an `Expression` then it's possible to build 
custom functions from
+all the Camel languages such as Groovy, JSonPath and even Simple as well.
+
+[source,java]
+----
+var bar = context.resolveLanguage("simple").createExpression("${trim()} ~> 
${normalizeWhitespace()} ~> ${capitalize()} ~> ${quote()}");
+reg.addFunction("bar", bar);
+----
+
+The `bar` function is build using simple functions using the `~>` chain 
operators to perform a sequence of functions on the payload.
+
+Custom functions will by default use the message body as the payload. But they 
also support a single parameter as the input,
+which allows to provide the input as fixed value such as a string literal or 
to refer to an existing header or variable.
+
+For example the foo function uses variable with name `msg` as the input:
+
+[source,java]
+----
+from("direct:start")
+        .setVariable("msg", constant("Moon"))
+        .setBody(simple("Bye ${foo(${variable.msg})}"))
+        .to("mock:result");
+----
+
+
 == Dependencies
 
 The Simple language is part of *camel-core*.
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleCustomFunctionTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleCustomFunctionTest.java
index 25dfd64850e7..bbb1ba01cac6 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleCustomFunctionTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleCustomFunctionTest.java
@@ -31,20 +31,40 @@ public class SimpleCustomFunctionTest extends 
ContextTestSupport {
     @Test
     public void testCustomFunctionWithBody() throws Exception {
         SimpleFunctionRegistry reg = 
PluginHelper.getSimpleFunctionRegistry(context);
-        Assertions.assertEquals(1, reg.customSize());
+        Assertions.assertEquals(2, reg.customSize());
 
         getMockEndpoint("mock:result").expectedBodiesReceived("Hello I was 
here World");
         template.sendBody("direct:start", "World");
         assertMockEndpointsSatisfied();
     }
 
+    @Test
+    public void testCustomFunctionWithBodyFunction() throws Exception {
+        SimpleFunctionRegistry reg = 
PluginHelper.getSimpleFunctionRegistry(context);
+        Assertions.assertEquals(2, reg.customSize());
+
+        getMockEndpoint("mock:result").expectedBodiesReceived("Hello I was 
here Earth");
+        template.sendBody("direct:start2", "Earth");
+        assertMockEndpointsSatisfied();
+    }
+
     @Test
     public void testCustomFunctionWithExp() throws Exception {
         SimpleFunctionRegistry reg = 
PluginHelper.getSimpleFunctionRegistry(context);
-        Assertions.assertEquals(1, reg.customSize());
+        Assertions.assertEquals(2, reg.customSize());
 
         getMockEndpoint("mock:result").expectedBodiesReceived("Bye I was here 
Moon");
-        template.sendBody("direct:start2", "World");
+        template.sendBody("direct:start3", "World");
+        assertMockEndpointsSatisfied();
+    }
+
+    @Test
+    public void testCustomFunctionWithSimple() throws Exception {
+        SimpleFunctionRegistry reg = 
PluginHelper.getSimpleFunctionRegistry(context);
+        Assertions.assertEquals(2, reg.customSize());
+
+        getMockEndpoint("mock:result").expectedBodiesReceived("\"Hello World I 
Was Here\"");
+        template.sendBody("direct:start4", "  hello  world i was here   ");
         assertMockEndpointsSatisfied();
     }
 
@@ -61,14 +81,26 @@ public class SimpleCustomFunctionTest extends 
ContextTestSupport {
                     }
                 });
 
+                var bar = context.resolveLanguage("simple")
+                        .createExpression("${trim()} ~> 
${normalizeWhitespace()} ~> ${capitalize()} ~> ${quote()}");
+                reg.addFunction("bar", bar);
+
                 from("direct:start")
                         .setBody(simple("Hello ${function(foo)}"))
                         .to("mock:result");
 
                 from("direct:start2")
+                        .setBody(simple("Hello ${foo}"))
+                        .to("mock:result");
+
+                from("direct:start3")
                         .setVariable("msg", constant("Moon"))
                         .setBody(simple("Bye 
${function(foo,${variable.msg})}"))
                         .to("mock:result");
+
+                from("direct:start4")
+                        .setBody(simple("${bar}"))
+                        .to("mock:result");
             }
         };
     }

Reply via email to