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 2022/11/24 14:19:05 UTC

[camel] branch main updated: CAMEL-18745: Add originalBody as function to simple language.

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 e3df7ccb446 CAMEL-18745: Add originalBody as function to simple language.
e3df7ccb446 is described below

commit e3df7ccb446308f415d4bd04c51f1ec31fad4f26
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Nov 24 15:18:48 2022 +0100

    CAMEL-18745: Add originalBody as function to simple language.
---
 .../modules/languages/pages/simple-language.adoc   |  2 +
 .../simple/ast/SimpleFunctionExpression.java       |  2 +
 .../language/simple/BodyOriginalMessageTest.java   | 56 ++++++++++++++++++++++
 .../camel/support/builder/ExpressionBuilder.java   | 35 ++++++++++++++
 4 files changed, 95 insertions(+)

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 734f41c1c20..7023b95e8a5 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
@@ -95,6 +95,8 @@ converted body can be null.
 
 |bodyOneLine | String | Converts the body to a String and removes all line-breaks so the string is in one line.
 
+|originalBody | Object | The original incoming body (only available if allowUseOriginalMessage=true).
+
 |mandatoryBodyAs(_type_) |Type |Converts the body to the given type determined by its
 classname, and expects the body to be not null.
 
diff --git a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
index 0bff750ffaf..27388e08b55 100644
--- a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
+++ b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
@@ -429,6 +429,8 @@ public class SimpleFunctionExpression extends LiteralExpression {
             return ExpressionBuilder.bodyExpression();
         } else if (ObjectHelper.equal(expression, "bodyOneLine")) {
             return ExpressionBuilder.bodyOneLine();
+        } else if (ObjectHelper.equal(expression, "originalBody")) {
+            return ExpressionBuilder.originalBodyExpression();
         } else if (ObjectHelper.equal(expression, "id")) {
             return ExpressionBuilder.messageIdExpression();
         } else if (ObjectHelper.equal(expression, "messageTimestamp")) {
diff --git a/core/camel-core/src/test/java/org/apache/camel/language/simple/BodyOriginalMessageTest.java b/core/camel-core/src/test/java/org/apache/camel/language/simple/BodyOriginalMessageTest.java
new file mode 100644
index 00000000000..2e3a36fe4fc
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/language/simple/BodyOriginalMessageTest.java
@@ -0,0 +1,56 @@
+/*
+ * 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.language.simple;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.jupiter.api.Test;
+
+public class BodyOriginalMessageTest extends ContextTestSupport {
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext context = super.createCamelContext();
+        context.setAllowUseOriginalMessage(true);
+        return context;
+    }
+
+    @Test
+    public void testOriginalBody() throws Exception {
+        getMockEndpoint("mock:foo").expectedBodiesReceived("Bye World");
+        getMockEndpoint("mock:result").expectedBodiesReceived("World");
+
+        template.sendBody("direct:start", "World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                        .transform().simple("Bye ${body}")
+                        .to("mock:foo")
+                        .setBody(simple("${originalBody}"))
+                        .to("mock:result");
+            }
+        };
+    }
+}
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/builder/ExpressionBuilder.java b/core/camel-support/src/main/java/org/apache/camel/support/builder/ExpressionBuilder.java
index 8ba01fb5581..25f4ba10231 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/builder/ExpressionBuilder.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/builder/ExpressionBuilder.java
@@ -45,6 +45,7 @@ import org.apache.camel.spi.ClassResolver;
 import org.apache.camel.spi.Language;
 import org.apache.camel.spi.PropertiesComponent;
 import org.apache.camel.spi.Registry;
+import org.apache.camel.spi.UnitOfWork;
 import org.apache.camel.support.ConstantExpressionAdapter;
 import org.apache.camel.support.ExchangeHelper;
 import org.apache.camel.support.ExpressionAdapter;
@@ -826,6 +827,40 @@ public class ExpressionBuilder {
         };
     }
 
+    /**
+     * Returns the expression for the original incoming message body
+     */
+    public static Expression originalBodyExpression() {
+        return new ExpressionAdapter() {
+
+            private boolean enabled;
+            @Override
+            public Object evaluate(Exchange exchange) {
+                if (enabled) {
+                    UnitOfWork uow = exchange.adapt(Exchange.class).getUnitOfWork();
+                    if (uow != null) {
+                        Message msg = uow.getOriginalInMessage();
+                        if (msg != null) {
+                            return msg.getBody();
+                        }
+                    }
+                }
+                return null;
+            }
+
+            @Override
+            public void init(CamelContext context) {
+                super.init(context);
+                this.enabled = context.isAllowUseOriginalMessage();
+            }
+
+            @Override
+            public String toString() {
+                return "originalBody";
+            }
+        };
+    }
+
     /**
      * Returns a functional expression for the exchanges inbound message body
      */