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 <[email protected]> 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
