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 2019/06/23 06:40:20 UTC
[camel] 02/02: CAMEL-13674: Simple language - Add bodyOneLine
function
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git
commit 31da47edb3885fb83b9f0c32519ef830e28e79ff
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sat Jun 22 14:52:12 2019 +0200
CAMEL-13674: Simple language - Add bodyOneLine function
---
core/camel-base/src/main/docs/simple-language.adoc | 2 ++
.../simple/ast/SimpleFunctionExpression.java | 2 ++
.../apache/camel/language/simple/SimpleTest.java | 8 ++++++++
.../camel/support/builder/ExpressionBuilder.java | 22 +++++++++++++++++++++-
4 files changed, 33 insertions(+), 1 deletion(-)
diff --git a/core/camel-base/src/main/docs/simple-language.adoc b/core/camel-base/src/main/docs/simple-language.adoc
index fa1aff4..654b66c 100644
--- a/core/camel-base/src/main/docs/simple-language.adoc
+++ b/core/camel-base/src/main/docs/simple-language.adoc
@@ -78,6 +78,8 @@ classname. The converted body can be null.
classname and then invoke methods using a Camel OGNL expression. The
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.
+
|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-base/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java b/core/camel-base/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
index 641a8ca..ed4ce03 100644
--- a/core/camel-base/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
+++ b/core/camel-base/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
@@ -410,6 +410,8 @@ public class SimpleFunctionExpression extends LiteralExpression {
return ExpressionBuilder.bodyExpression();
} else if (ObjectHelper.equal(expression, "out.body")) {
return ExpressionBuilder.outBodyExpression();
+ } else if (ObjectHelper.equal(expression, "bodyOneLine")) {
+ return ExpressionBuilder.bodyOneLine();
} else if (ObjectHelper.equal(expression, "id")) {
return ExpressionBuilder.messageIdExpression();
} else if (ObjectHelper.equal(expression, "exchangeId")) {
diff --git a/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java b/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
index 261d18b..529f8e0 100644
--- a/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
@@ -1898,6 +1898,14 @@ public class SimpleTest extends LanguageTestSupport {
}
@Test
+ public void testBodyAsOneLine() throws Exception {
+ exchange.getIn().setBody("Hello" + System.lineSeparator() + "Great" + System.lineSeparator() + "World");
+ assertExpression("${bodyOneLine}", "HelloGreatWorld");
+ assertExpression("Hi ${bodyOneLine}", "Hi HelloGreatWorld");
+ assertExpression("Hi ${bodyOneLine} Again", "Hi HelloGreatWorld Again");
+ }
+
+ @Test
public void testNestedTypeFunction() throws Exception {
// when using type: function we need special logic to not lazy evaluate it so its evaluated only once
// and won't fool Camel to think its a nested OGNL method call expression instead (CAMEL-10664)
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 de302e6..f4f2744 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
@@ -1640,6 +1640,27 @@ public class ExpressionBuilder {
};
}
+ /**
+ * Returns the expression for the message body as a one-line string
+ */
+ public static Expression bodyOneLine() {
+ return new ExpressionAdapter() {
+ public Object evaluate(Exchange exchange) {
+ String body = exchange.getIn().getBody(String.class);
+ if (body == null) {
+ return null;
+ }
+ body = StringHelper.replaceAll(body, System.lineSeparator(), "");
+ return body;
+ }
+
+ @Override
+ public String toString() {
+ return "bodyOneLine()";
+ }
+ };
+ }
+
protected static void setProperty(CamelContext camelContext, Object bean, String name, Object value) {
try {
IntrospectionSupport.setProperty(camelContext, bean, name, value);
@@ -1647,5 +1668,4 @@ public class ExpressionBuilder {
throw new IllegalArgumentException("Failed to set property " + name + " on " + bean + ". Reason: " + e, e);
}
}
-
}