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/05/23 15:22:21 UTC
[camel] branch main updated: CAMEL-18070: Add propertiesExist 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 abbf92e60c8 CAMEL-18070: Add propertiesExist function to simple language.
abbf92e60c8 is described below
commit abbf92e60c8b690b5cfe607657c4361a459edf8d
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon May 23 17:18:38 2022 +0200
CAMEL-18070: Add propertiesExist function to simple language.
---
.../modules/languages/pages/simple-language.adoc | 3 +++
.../simple/ast/SimpleFunctionExpression.java | 15 ++++++++++++
.../apache/camel/language/simple/SimpleTest.java | 17 ++++++++++++++
.../camel/support/builder/ExpressionBuilder.java | 27 ++++++++++++++++++++++
4 files changed, 62 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 7ac6a90d07a..5626af3421b 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
@@ -172,6 +172,9 @@ to a bean class (such as calling a static method) then you can prefix with type,
not exists or has no value, then an optional default value can be
specified.
+|`propertiesExist:key` |String |Checks whether a property placeholder with the given key exists or not.
+The result can be negated by prefixing the key with `!`.
+
|routeId |String |Returns the id of the current route the
Exchange is being routed.
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 21993f5b577..a737c323d9a 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
@@ -244,6 +244,21 @@ public class SimpleFunctionExpression extends LiteralExpression {
return bean.createExpression(null, properties);
}
+ // properties-exist: prefix
+ remainder = ifStartsWithReturnRemainder("propertiesExist:", function);
+ if (remainder != null) {
+ String[] parts = remainder.split(":", 2);
+ if (parts.length > 2) {
+ throw new SimpleParserException("Valid syntax: ${propertiesExist:key was: " + function, token.getIndex());
+ }
+ String key = parts[0];
+ boolean negate = key != null && key.startsWith("!");
+ if (negate) {
+ key = key.substring(1);
+ }
+ return ExpressionBuilder.propertiesComponentExist(key, negate);
+ }
+
// properties: prefix
remainder = ifStartsWithReturnRemainder("properties:", function);
if (remainder != null) {
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 2fdc1574dd5..d0243353312 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
@@ -41,6 +41,7 @@ import org.apache.camel.language.bean.RuntimeBeanExpressionException;
import org.apache.camel.language.simple.myconverter.MyCustomDate;
import org.apache.camel.language.simple.types.SimpleIllegalSyntaxException;
import org.apache.camel.spi.Language;
+import org.apache.camel.spi.PropertiesComponent;
import org.apache.camel.spi.Registry;
import org.apache.camel.util.InetAddressUtil;
import org.junit.jupiter.api.Test;
@@ -1987,6 +1988,22 @@ public class SimpleTest extends LanguageTestSupport {
assertExpression("${body.replace(\"((\", \"--\").replace(\"((((\", \"----\")}", "Hello -- World ---- Again");
}
+ @Test
+ public void testPropertiesExist() throws Exception {
+ PropertiesComponent pc = context.getPropertiesComponent();
+
+ assertExpression("${propertiesExist:myKey}", "false");
+ assertExpression("${propertiesExist:!myKey}", "true");
+ assertPredicate("${propertiesExist:myKey}", false);
+ assertPredicate("${propertiesExist:!myKey}", true);
+
+ pc.addInitialProperty("myKey", "abc");
+ assertExpression("${propertiesExist:myKey}", "true");
+ assertExpression("${propertiesExist:!myKey}", "false");
+ assertPredicate("${propertiesExist:myKey}", true);
+ assertPredicate("${propertiesExist:!myKey}", false);
+ }
+
@Override
protected String getLanguageName() {
return "simple";
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 f30596795af..aecb1721a31 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
@@ -24,6 +24,7 @@ import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.regex.Pattern;
@@ -1803,6 +1804,32 @@ public class ExpressionBuilder {
};
}
+ public static Expression propertiesComponentExist(final String key, final boolean negate) {
+ return new ExpressionAdapter() {
+ private PropertiesComponent pc;
+
+ @Override
+ public Object evaluate(Exchange exchange) {
+ Optional<String> result = pc.resolveProperty(key);
+ boolean answer = result.isPresent();
+ if (negate) {
+ answer = !answer;
+ }
+ return answer;
+ }
+
+ @Override
+ public void init(CamelContext context) {
+ pc = context.getPropertiesComponent();
+ }
+
+ @Override
+ public String toString() {
+ return "propertiesExist(" + key + ")";
+ }
+ };
+ }
+
/**
* Returns an {@link TokenPairExpressionIterator} expression
*/