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/17 19:37:45 UTC

[camel] branch camel-3.14.x updated (a7120838d81 -> 518ab5bc8ed)

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a change to branch camel-3.14.x
in repository https://gitbox.apache.org/repos/asf/camel.git


    from a7120838d81 CAMEL-18110: DeliverSm message payload opt param fix (#7618)
     new f42177cad95 CAMEL-18119 - Allow Simple expressions to render any Object that can be converted to Date (#7627)
     new 518ab5bc8ed CAMEL-18119 - Allow Simple expressions to render any Object that can be converted to Date (#7627)

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../language/simple/SimpleExpressionBuilder.java   | 10 ++++--
 .../apache/camel/language/simple/SimpleTest.java   | 18 +++++++++++
 .../language/simple/myconverter/MyCustomDate.java  | 37 +++++++++++-----------
 .../simple/myconverter/MyCustomDateConverter.java} | 17 ++++++----
 .../services/org/apache/camel/TypeConverter        |  1 +
 5 files changed, 57 insertions(+), 26 deletions(-)
 copy components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/simple/onetomany/Book.java => core/camel-core/src/test/java/org/apache/camel/language/simple/myconverter/MyCustomDate.java (61%)
 copy core/camel-core/src/test/java/org/apache/camel/{converter/myconverter/InstanceMethodWithExchangeTestConverter.java => language/simple/myconverter/MyCustomDateConverter.java} (69%)


[camel] 01/02: CAMEL-18119 - Allow Simple expressions to render any Object that can be converted to Date (#7627)

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch camel-3.14.x
in repository https://gitbox.apache.org/repos/asf/camel.git

commit f42177cad9595abef7832b114f1b09ac77a77d0a
Author: adessaigne <an...@gmail.com>
AuthorDate: Tue May 17 20:23:21 2022 +0200

    CAMEL-18119 - Allow Simple expressions to render any Object that can be converted to Date (#7627)
---
 .../language/simple/SimpleExpressionBuilder.java   | 10 ++++-
 .../apache/camel/language/simple/SimpleTest.java   | 18 +++++++++
 .../language/simple/myconverter/MyCustomDate.java  | 46 ++++++++++++++++++++++
 .../simple/myconverter/MyCustomDateConverter.java  | 35 ++++++++++++++++
 .../services/org/apache/camel/TypeConverter        |  1 +
 5 files changed, 108 insertions(+), 2 deletions(-)

diff --git a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/SimpleExpressionBuilder.java b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/SimpleExpressionBuilder.java
index 9fb0a809f39..ad9a2923eb0 100644
--- a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/SimpleExpressionBuilder.java
+++ b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/SimpleExpressionBuilder.java
@@ -464,7 +464,10 @@ public final class SimpleExpressionBuilder {
                     } else if (obj instanceof Long) {
                         date = new Date((Long) obj);
                     } else {
-                        throw new IllegalArgumentException("Cannot find Date/long object at command: " + command);
+                        date = exchange.getContext().getTypeConverter().tryConvertTo(Date.class, exchange, obj);
+                        if (date == null) {
+                            throw new IllegalArgumentException("Cannot find Date/long object at command: " + command);
+                        }
                     }
                 } else if (command.startsWith("exchangeProperty.")) {
                     String key = command.substring(command.lastIndexOf('.') + 1);
@@ -474,7 +477,10 @@ public final class SimpleExpressionBuilder {
                     } else if (obj instanceof Long) {
                         date = new Date((Long) obj);
                     } else {
-                        throw new IllegalArgumentException("Cannot find Date/long object at command: " + command);
+                        date = exchange.getContext().getTypeConverter().tryConvertTo(Date.class, exchange, obj);
+                        if (date == null) {
+                            throw new IllegalArgumentException("Cannot find Date/long object at command: " + command);
+                        }
                     }
                 } else if ("file".equals(command)) {
                     Long num = exchange.getIn().getHeader(Exchange.FILE_LAST_MODIFIED, Long.class);
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 f3dcbdd8ca2..fba03bc0e98 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
@@ -38,6 +38,7 @@ import org.apache.camel.LanguageTestSupport;
 import org.apache.camel.Predicate;
 import org.apache.camel.component.bean.MethodNotFoundException;
 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.Registry;
@@ -604,6 +605,23 @@ public class SimpleTest extends LanguageTestSupport {
         assertExpression("${date:header.birthday:yyyy-MM-dd'T'HH:mm:ss:SSS}", "1974-04-20T08:55:47:123");
     }
 
+    @Test
+    public void testDateWithConverterExpressions() throws Exception {
+        exchange.getIn().setHeader("birthday", new MyCustomDate(1974, Calendar.APRIL, 20));
+        exchange.setProperty("birthday", new MyCustomDate(1974, Calendar.APRIL, 20));
+        exchange.getIn().setHeader("other", new ArrayList<>());
+
+        assertExpression("${date:header.birthday:yyyyMMdd}", "19740420");
+        assertExpression("${date:exchangeProperty.birthday:yyyyMMdd}", "19740420");
+
+        try {
+            assertExpression("${date:header.other:yyyyMMdd}", "19740420");
+            fail("Should thrown an exception");
+        } catch (IllegalArgumentException e) {
+            assertEquals("Cannot find Date/long object at command: header.other", e.getMessage());
+        }
+    }
+
     @Test
     public void testDateWithTimezone() throws Exception {
         Calendar cal = Calendar.getInstance();
diff --git a/core/camel-core/src/test/java/org/apache/camel/language/simple/myconverter/MyCustomDate.java b/core/camel-core/src/test/java/org/apache/camel/language/simple/myconverter/MyCustomDate.java
new file mode 100644
index 00000000000..614bf762153
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/language/simple/myconverter/MyCustomDate.java
@@ -0,0 +1,46 @@
+/*
+ * 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.myconverter;
+
+/**
+ * This custom date object allows to check that Simple expressions can call
+ * custom type converters when needing real java.util.Date instances
+ */
+public final class MyCustomDate {
+
+    private final int year;
+    private final int month;
+    private final int date;
+
+    public MyCustomDate(int year, int month, int date) {
+        this.year = year;
+        this.month = month;
+        this.date = date;
+    }
+
+    public int getYear() {
+        return year;
+    }
+
+    public int getMonth() {
+        return month;
+    }
+
+    public int getDate() {
+        return date;
+    }
+}
diff --git a/core/camel-core/src/test/java/org/apache/camel/language/simple/myconverter/MyCustomDateConverter.java b/core/camel-core/src/test/java/org/apache/camel/language/simple/myconverter/MyCustomDateConverter.java
new file mode 100644
index 00000000000..c38cdc416ab
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/language/simple/myconverter/MyCustomDateConverter.java
@@ -0,0 +1,35 @@
+/*
+ * 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.myconverter;
+
+import java.util.*;
+
+import org.apache.camel.Converter;
+
+@Converter
+public final class MyCustomDateConverter {
+    private MyCustomDateConverter() {
+        // Prevent instantiation
+    }
+
+    @Converter
+    public static Date toDate(MyCustomDate value) {
+        Calendar cal = Calendar.getInstance();
+        cal.set(value.getYear(), value.getMonth(), value.getDate());
+        return cal.getTime();
+    }
+}
diff --git a/core/camel-core/src/test/resources/META-INF/services/org/apache/camel/TypeConverter b/core/camel-core/src/test/resources/META-INF/services/org/apache/camel/TypeConverter
index 20f38720e99..68f1a31a3cb 100644
--- a/core/camel-core/src/test/resources/META-INF/services/org/apache/camel/TypeConverter
+++ b/core/camel-core/src/test/resources/META-INF/services/org/apache/camel/TypeConverter
@@ -16,3 +16,4 @@
 #
 
 org.apache.camel.converter.myconverter
+org.apache.camel.language.simple.myconverter


[camel] 02/02: CAMEL-18119 - Allow Simple expressions to render any Object that can be converted to Date (#7627)

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch camel-3.14.x
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 518ab5bc8ed884c9fb7afca5d968d0a078bc3e62
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue May 17 20:25:26 2022 +0200

    CAMEL-18119 - Allow Simple expressions to render any Object that can be converted to Date (#7627)
---
 .../org/apache/camel/language/simple/myconverter/MyCustomDate.java    | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/core/camel-core/src/test/java/org/apache/camel/language/simple/myconverter/MyCustomDate.java b/core/camel-core/src/test/java/org/apache/camel/language/simple/myconverter/MyCustomDate.java
index 614bf762153..ce2b205d54a 100644
--- a/core/camel-core/src/test/java/org/apache/camel/language/simple/myconverter/MyCustomDate.java
+++ b/core/camel-core/src/test/java/org/apache/camel/language/simple/myconverter/MyCustomDate.java
@@ -17,8 +17,8 @@
 package org.apache.camel.language.simple.myconverter;
 
 /**
- * This custom date object allows to check that Simple expressions can call
- * custom type converters when needing real java.util.Date instances
+ * This custom date object allows to check that Simple expressions can call custom type converters when needing real
+ * java.util.Date instances
  */
 public final class MyCustomDate {