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
The following commit(s) were added to refs/heads/master by this push:
new 4a1b990 CAMEL-15741: camel-joor - Add support for default values in
headerAs/exchangePropertyAs
4a1b990 is described below
commit 4a1b9900ddba674eba246742ebac54a675bdfa8b
Author: Claus Ibsen <[email protected]>
AuthorDate: Thu Mar 18 11:21:12 2021 +0100
CAMEL-15741: camel-joor - Add support for default values in
headerAs/exchangePropertyAs
---
.../apache/camel/catalog/docs/joor-language.adoc | 4 ++-
.../camel-joor/src/main/docs/joor-language.adoc | 4 ++-
.../apache/camel/language/joor/JoorCompiler.java | 34 +++++++++++++++++-----
.../org/apache/camel/language/joor/JoorHelper.java | 8 +++++
.../camel/language/joor/JoorLanguageTest.java | 24 +++++++++++++++
.../modules/languages/pages/joor-language.adoc | 4 ++-
6 files changed, 67 insertions(+), 11 deletions(-)
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/joor-language.adoc
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/joor-language.adoc
index 6679451..4b1e30e 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/joor-language.adoc
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/joor-language.adoc
@@ -57,7 +57,9 @@ The jOOR language allows the following functions to be used
in the script:
| Function | Description
| bodyAs(type) | To convert the body to the given type.
| headerAs(name, type) | To convert the header with the name to the given type.
+| headerAs(name, defaultValue, type) | To convert the header with the name to
the given type. If no header exists then use the given default value.
| exchangePropertyAs(name, type) | To convert the exchange property with the
name to the given type.
+| exchangePropertyAs(name, defaultValue, type) | To convert the exchange
property with the name to the given type. If no exchange property exists then
use the given default value.
| optionalBodyAs(type) | To convert the body to the given type returned
wrapped in `java.util.Optional`.
| optionalHeaderAs(name, type) | To convert the header with the name to the
given type returned wrapped in `java.util.Optional`.
| optionalExchangePropertyAs(name, type) | To convert the exchange property
with the name to the given type returned wrapped in `java.util.Optional`.
@@ -288,7 +290,7 @@ from("jms:incoming")
Here the jOOR script is externalized into the file
`src/main/resources/orders.joor` which allows you to edit this source
file while running the Camel application and try the changes with
hot-reloading.
-In XML DSL its a little bit easier as you can turn off pre-compilation in the
`<joor>` XML element:
+In XML DSL it's easier because you can turn off pre-compilation in the
`<joor>` XML element:
[source,xml]
----
diff --git a/components/camel-joor/src/main/docs/joor-language.adoc
b/components/camel-joor/src/main/docs/joor-language.adoc
index 6679451..4b1e30e 100644
--- a/components/camel-joor/src/main/docs/joor-language.adoc
+++ b/components/camel-joor/src/main/docs/joor-language.adoc
@@ -57,7 +57,9 @@ The jOOR language allows the following functions to be used
in the script:
| Function | Description
| bodyAs(type) | To convert the body to the given type.
| headerAs(name, type) | To convert the header with the name to the given type.
+| headerAs(name, defaultValue, type) | To convert the header with the name to
the given type. If no header exists then use the given default value.
| exchangePropertyAs(name, type) | To convert the exchange property with the
name to the given type.
+| exchangePropertyAs(name, defaultValue, type) | To convert the exchange
property with the name to the given type. If no exchange property exists then
use the given default value.
| optionalBodyAs(type) | To convert the body to the given type returned
wrapped in `java.util.Optional`.
| optionalHeaderAs(name, type) | To convert the header with the name to the
given type returned wrapped in `java.util.Optional`.
| optionalExchangePropertyAs(name, type) | To convert the exchange property
with the name to the given type returned wrapped in `java.util.Optional`.
@@ -288,7 +290,7 @@ from("jms:incoming")
Here the jOOR script is externalized into the file
`src/main/resources/orders.joor` which allows you to edit this source
file while running the Camel application and try the changes with
hot-reloading.
-In XML DSL its a little bit easier as you can turn off pre-compilation in the
`<joor>` XML element:
+In XML DSL it's easier because you can turn off pre-compilation in the
`<joor>` XML element:
[source,xml]
----
diff --git
a/components/camel-joor/src/main/java/org/apache/camel/language/joor/JoorCompiler.java
b/components/camel-joor/src/main/java/org/apache/camel/language/joor/JoorCompiler.java
index dea9f88..9d8418c 100644
---
a/components/camel-joor/src/main/java/org/apache/camel/language/joor/JoorCompiler.java
+++
b/components/camel-joor/src/main/java/org/apache/camel/language/joor/JoorCompiler.java
@@ -42,15 +42,25 @@ public class JoorCompiler extends ServiceSupport implements
StaticService {
private static final Pattern BODY_AS_PATTERN =
Pattern.compile("(optionalBodyAs|bodyAs)\\(([A-Za-z0-9.$]*)(.class)\\)");
private static final Pattern BODY_AS_PATTERN_NO_CLASS =
Pattern.compile("(optionalBodyAs|bodyAs)\\(([A-Za-z0-9.$]*)\\)");
private static final Pattern HEADER_AS_PATTERN
- =
Pattern.compile("(optionalHeaderAs|headerAs)\\((['|\"][A-Za-z0-9.$]*['|\"]\\s*,\\s*[A-Za-z0-9.$]*)(.class)\\)");
+ =
Pattern.compile("(optionalHeaderAs|headerAs)\\((['|\"][A-Za-z0-9.$]*['|\"]\\s*),\\s*([A-Za-z0-9.$]*.class)\\)");
private static final Pattern HEADER_AS_PATTERN_NO_CLASS
- =
Pattern.compile("(optionalHeaderAs|headerAs)\\((['|\"][A-Za-z0-9.$]*['|\"]\\s*,\\s*[A-Za-z0-9.$]*)\\)");
+ =
Pattern.compile("(optionalHeaderAs|headerAs)\\((['|\"][A-Za-z0-9.$]*['|\"])\\s*,\\s*([A-Za-z0-9.$]*)\\)");
+ private static final Pattern HEADER_AS_DEFAULT_VALUE_PATTERN
+ =
Pattern.compile("(headerAs)\\((['|\"][A-Za-z0-9.$]*['|\"])\\s*,(.+),\\s*([A-Za-z0-9.$]*.class)\\)");
+ private static final Pattern HEADER_AS_DEFAULT_VALUE_PATTERN_NO_CLASS
+ =
Pattern.compile("(headerAs)\\((['|\"][A-Za-z0-9.$]*['|\"])\\s*,(.+),\\s*([A-Za-z0-9.$]*)\\)");
private static final Pattern EXCHANGE_PROPERTY_AS_PATTERN
= Pattern.compile(
-
"(optionalExchangePropertyAs|exchangePropertyAs)\\((['|\"][A-Za-z0-9.$]*['|\"]\\s*,\\s*[A-Za-z0-9.$]*)(.class)\\)");
+
"(optionalExchangePropertyAs|exchangePropertyAs)\\((['|\"][A-Za-z0-9.$]*['|\"])\\s*,\\s*([A-Za-z0-9.$]*.class)\\)");
private static final Pattern EXCHANGE_PROPERTY_AS_PATTERN_NO_CLASS
= Pattern.compile(
-
"(optionalExchangePropertyAs|exchangePropertyAs)\\((['|\"][A-Za-z0-9.$]*['|\"]\\s*,\\s*[A-Za-z0-9.$]*)\\)");
+
"(optionalExchangePropertyAs|exchangePropertyAs)\\((['|\"][A-Za-z0-9.$]*['|\"])\\s*,\\s*([A-Za-z0-9.$]*)\\)");
+ private static final Pattern EXCHANGE_PROPERTY_AS_DEFAULT_VALUE_PATTERN
+ = Pattern.compile(
+
"(exchangePropertyAs)\\((['|\"][A-Za-z0-9.$]*['|\"])\\s*,(.+),\\s*([A-Za-z0-9.$]*.class)\\)");
+ private static final Pattern
EXCHANGE_PROPERTY_AS_DEFAULT_VALUE_PATTERN_NO_CLASS
+ = Pattern.compile(
+
"(exchangePropertyAs)\\((['|\"][A-Za-z0-9.$]*['|\"])\\s*,(.+),\\s*([A-Za-z0-9.$]*)\\)");
private static final Logger LOG =
LoggerFactory.getLogger(JoorCompiler.class);
private static final AtomicInteger UUID = new AtomicInteger();
@@ -212,15 +222,23 @@ public class JoorCompiler extends ServiceSupport
implements StaticService {
matcher = BODY_AS_PATTERN_NO_CLASS.matcher(script);
script = matcher.replaceAll("$1(message, $2.class)");
+ matcher = HEADER_AS_DEFAULT_VALUE_PATTERN.matcher(script);
+ script = matcher.replaceAll("$1(message, $2, $3, $4)");
+ matcher = HEADER_AS_DEFAULT_VALUE_PATTERN_NO_CLASS.matcher(script);
+ script = matcher.replaceAll("$1(message, $2, $3, $4.class)");
matcher = HEADER_AS_PATTERN.matcher(script);
- script = matcher.replaceAll("$1(message, $2$3)");
+ script = matcher.replaceAll("$1(message, $2, $3)");
matcher = HEADER_AS_PATTERN_NO_CLASS.matcher(script);
- script = matcher.replaceAll("$1(message, $2.class)");
+ script = matcher.replaceAll("$1(message, $2, $3.class)");
+ matcher = EXCHANGE_PROPERTY_AS_DEFAULT_VALUE_PATTERN.matcher(script);
+ script = matcher.replaceAll("$1(exchange, $2, $3, $4)");
+ matcher =
EXCHANGE_PROPERTY_AS_DEFAULT_VALUE_PATTERN_NO_CLASS.matcher(script);
+ script = matcher.replaceAll("$1(exchange, $2, $3, $4.class)");
matcher = EXCHANGE_PROPERTY_AS_PATTERN.matcher(script);
- script = matcher.replaceAll("$1(exchange, $2$3)");
+ script = matcher.replaceAll("$1(exchange, $2, $3)");
matcher = EXCHANGE_PROPERTY_AS_PATTERN_NO_CLASS.matcher(script);
- script = matcher.replaceAll("$1(exchange, $2.class)");
+ script = matcher.replaceAll("$1(exchange, $2, $3.class)");
return script;
}
diff --git
a/components/camel-joor/src/main/java/org/apache/camel/language/joor/JoorHelper.java
b/components/camel-joor/src/main/java/org/apache/camel/language/joor/JoorHelper.java
index 8359612..71a0800 100644
---
a/components/camel-joor/src/main/java/org/apache/camel/language/joor/JoorHelper.java
+++
b/components/camel-joor/src/main/java/org/apache/camel/language/joor/JoorHelper.java
@@ -41,6 +41,10 @@ public final class JoorHelper {
return message.getHeader(name, type);
}
+ public static <T> T headerAs(Message message, String name, Object
defaultValue, Class<T> type) {
+ return message.getHeader(name, defaultValue, type);
+ }
+
public static <T> Optional<T> optionalHeaderAs(Message message, String
name, Class<T> type) {
return Optional.ofNullable(message.getHeader(name, type));
}
@@ -49,6 +53,10 @@ public final class JoorHelper {
return exchange.getProperty(name, type);
}
+ public static <T> T exchangePropertyAs(Exchange exchange, String name,
Object defaultValue, Class<T> type) {
+ return exchange.getProperty(name, defaultValue, type);
+ }
+
public static <T> Optional<T> optionalExchangePropertyAs(Exchange
exchange, String name, Class<T> type) {
return Optional.ofNullable(exchange.getProperty(name, type));
}
diff --git
a/components/camel-joor/src/test/java/org/apache/camel/language/joor/JoorLanguageTest.java
b/components/camel-joor/src/test/java/org/apache/camel/language/joor/JoorLanguageTest.java
index 644e217..58b1a0e 100644
---
a/components/camel-joor/src/test/java/org/apache/camel/language/joor/JoorLanguageTest.java
+++
b/components/camel-joor/src/test/java/org/apache/camel/language/joor/JoorLanguageTest.java
@@ -147,6 +147,18 @@ public class JoorLanguageTest extends LanguageTestSupport {
}
@Test
+ public void testExchangeHeaderAsDefaultValue() throws Exception {
+ exchange.getIn().setHeader("foo", 22);
+
+ assertExpression("2 * headerAs('dog', 33, int.class)", "66");
+ assertExpression("3 * headerAs('dog', 33, Integer.class)", "99");
+ assertExpression("2 * headerAs('dog', 33, int)", "66");
+ assertExpression("3 * headerAs('dog', 33, Integer)", "99");
+
+ assertExpression("'Hello ' + headerAs('dog', 'World', String)", "Hello
World");
+ }
+
+ @Test
public void testExchangeOptionalHeaderAs() throws Exception {
exchange.getIn().setHeader("foo", 22);
@@ -189,6 +201,18 @@ public class JoorLanguageTest extends LanguageTestSupport {
}
@Test
+ public void testExchangePropertyDefaultValueAs() throws Exception {
+ exchange.setProperty("bar", 22);
+
+ assertExpression("2 * exchangePropertyAs('dog', 33, int.class)", "66");
+ assertExpression("3 * exchangePropertyAs('dog', 33, Integer.class)",
"99");
+ assertExpression("2 * exchangePropertyAs('dog', 33, int)", "66");
+ assertExpression("3 * exchangePropertyAs('dog', 33, Integer)", "99");
+
+ assertExpression("'Hello ' + exchangePropertyAs('dog', 'World',
String)", "Hello World");
+ }
+
+ @Test
public void testOptionalExchangePropertyAs() throws Exception {
exchange.setProperty("bar", 22);
diff --git a/docs/components/modules/languages/pages/joor-language.adoc
b/docs/components/modules/languages/pages/joor-language.adoc
index 68c5b08..66493e0 100644
--- a/docs/components/modules/languages/pages/joor-language.adoc
+++ b/docs/components/modules/languages/pages/joor-language.adoc
@@ -59,7 +59,9 @@ The jOOR language allows the following functions to be used
in the script:
| Function | Description
| bodyAs(type) | To convert the body to the given type.
| headerAs(name, type) | To convert the header with the name to the given type.
+| headerAs(name, defaultValue, type) | To convert the header with the name to
the given type. If no header exists then use the given default value.
| exchangePropertyAs(name, type) | To convert the exchange property with the
name to the given type.
+| exchangePropertyAs(name, defaultValue, type) | To convert the exchange
property with the name to the given type. If no exchange property exists then
use the given default value.
| optionalBodyAs(type) | To convert the body to the given type returned
wrapped in `java.util.Optional`.
| optionalHeaderAs(name, type) | To convert the header with the name to the
given type returned wrapped in `java.util.Optional`.
| optionalExchangePropertyAs(name, type) | To convert the exchange property
with the name to the given type returned wrapped in `java.util.Optional`.
@@ -290,7 +292,7 @@ from("jms:incoming")
Here the jOOR script is externalized into the file
`src/main/resources/orders.joor` which allows you to edit this source
file while running the Camel application and try the changes with
hot-reloading.
-In XML DSL its a little bit easier as you can turn off pre-compilation in the
`<joor>` XML element:
+In XML DSL it's easier because you can turn off pre-compilation in the
`<joor>` XML element:
[source,xml]
----