Skip to content

Commit

Permalink
CAMEL-15114: Simple language - Date function should make it easy to o…
Browse files Browse the repository at this point in the history
…utput the exchange created timestamp
  • Loading branch information
davsclaus committed May 26, 2020
1 parent 234c168 commit 46f4c43
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 93 deletions.
Expand Up @@ -171,11 +171,12 @@ exception set on exchange. Will fallback and grab caught exceptions
exceptions (`Exchange.EXCEPTION_CAUGHT`) if the Exchange has any.

|date:_command_ |Date |evaluates to a Date object.
Supported commands are: *now* for current timestamp, *in.header.xxx* or
*header.xxx* to use the Date object header with the key xxx.
*exchangeProperty.xxx* to use the Date object in the exchange property with the key xxx.
Supported commands are: *now* for current timestamp,
*exchangeCreated* for the timestamp when the current exchange was created,
*header.xxx* to use the Long/Date object header with the key xxx.
*exchangeProperty.xxx* to use the Long/Date object in the exchange property with the key xxx.
*file* for the last modified timestamp of the file (available with a File consumer).
Command accepts offsets such as: *now-24h* or *in.header.xxx+1h* or even *now+1h30m-100*.
Command accepts offsets such as: *now-24h* or *header.xxx+1h* or even *now+1h30m-100*.

|date:_command:pattern_ |String |Date formatting using `java.text.SimpleDateFormat` patterns.

Expand Down
Expand Up @@ -422,23 +422,28 @@ public Object evaluate(Exchange exchange) {
Date date;
if ("now".equals(command)) {
date = new Date();
} else if (command.startsWith("header.") || command.startsWith("in.header.")) {
} else if ("exchangeCreated".equals(command)) {
long num = exchange.getCreated();
date = new Date(num);
} else if (command.startsWith("header.")) {
String key = command.substring(command.lastIndexOf('.') + 1);
date = exchange.getIn().getHeader(key, Date.class);
if (date == null) {
throw new IllegalArgumentException("Cannot find java.util.Date object at command: " + command);
}
} else if (command.startsWith("out.header.")) {
String key = command.substring(command.lastIndexOf('.') + 1);
date = exchange.getMessage().getHeader(key, Date.class);
if (date == null) {
throw new IllegalArgumentException("Cannot find java.util.Date object at command: " + command);
Object obj = exchange.getMessage().getHeader(key);
if (obj instanceof Date) {
date = (Date) obj;
} else if (obj instanceof Long) {
date = new Date((Long) obj);
} else {
throw new IllegalArgumentException("Cannot find Date/long object at command: " + command);
}
} else if (command.startsWith("exchangeProperty.")) {
String key = command.substring(command.lastIndexOf('.') + 1);
date = exchange.getProperty(key, Date.class);
if (date == null) {
throw new IllegalArgumentException("Cannot find java.util.Date object at command: " + command);
Object obj = exchange.getProperty(key);
if (obj instanceof Date) {
date = (Date) obj;
} else if (obj instanceof Long) {
date = new Date((Long) obj);
} else {
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);
Expand Down
Expand Up @@ -33,69 +33,7 @@
import org.slf4j.LoggerFactory;

/**
* A <a href="http://camel.apache.org/simple.html">simple language</a>
* which maps simple property style notations to access headers and bodies.
* Examples of supported expressions are:
* <ul>
* <li>exchangeId to access the exchange id</li>
* <li>id to access the inbound message id</li>
* <li>in.body or body to access the inbound body</li>
* <li>in.body.OGNL or body.OGNL to access the inbound body using an OGNL expression</li>
* <li>mandatoryBodyAs(&lt;classname&gt;) to convert the in body to the given type, will throw exception if not possible to convert</li>
* <li>bodyAs(&lt;classname&gt;) to convert the in body to the given type, will return null if not possible to convert</li>
* <li>headerAs(&lt;key&gt;, &lt;classname&gt;) to convert the in header to the given type, will return null if not possible to convert</li>
* <li>out.body to access the inbound body</li>
* <li>in.header.foo or header.foo to access an inbound header called 'foo'</li>
* <li>in.header.foo[bar] or header.foo[bar] to access an inbound header called 'foo' as a Map and lookup the map with 'bar' as key</li>
* <li>in.header.foo.OGNL or header.OGNL to access an inbound header called 'foo' using an OGNL expression</li>
* <li>out.header.foo to access an outbound header called 'foo'</li>
* <li>property.foo to access the exchange property called 'foo'</li>
* <li>property.foo.OGNL to access the exchange property called 'foo' using an OGNL expression</li>
* <li>sys.foo to access the system property called 'foo'</li>
* <li>sysenv.foo to access the system environment called 'foo'</li>
* <li>exception.messsage to access the exception message</li>
* <li>threadName to access the current thread name</li>
* <li>date:&lt;command&gt; evaluates to a Date object
* Supported commands are: <tt>now</tt> for current timestamp,
* <tt>in.header.xxx</tt> or <tt>header.xxx</tt> to use the Date object in the in header.
* <tt>out.header.xxx</tt> to use the Date object in the out header.
* <tt>property.xxx</tt> to use the Date object in the exchange property.
* <tt>file</tt> for the last modified timestamp of the file (available with a File consumer).
* Command accepts offsets such as: <tt>now-24h</tt> or <tt>in.header.xxx+1h</tt> or even <tt>now+1h30m-100</tt>.
* </li>
* <li>date:&lt;command&gt;:&lt;pattern&gt; for date formatting using {@link java.text.SimpleDateFormat} patterns</li>
* <li>date-with-timezone:&lt;command&gt;:&lt;timezone&gt;:&lt;pattern&gt; for date formatting using {@link java.text.SimpleDateFormat} timezones and patterns</li>
* <li>bean:&lt;bean expression&gt; to invoke a bean using the
* {@link org.apache.camel.language.bean.BeanLanguage BeanLanguage}</li>
* <li>properties:&lt;[locations]&gt;:&lt;key&gt; for using property placeholders using the
* {@link org.apache.camel.component.properties.PropertiesComponent}.
* The locations parameter is optional and you can enter multiple locations separated with comma.
* </li>
* </ul>
* <p/>
* The simple language supports OGNL notation when accessing either body or header.
* <p/>
* The simple language now also includes file language out of the box which means the following expression is also
* supported:
* <ul>
* <li><tt>file:name</tt> to access the file name (is relative, see note below))</li>
* <li><tt>file:name.noext</tt> to access the file name with no extension</li>
* <li><tt>file:name.ext</tt> to access the file extension</li>
* <li><tt>file:ext</tt> to access the file extension</li>
* <li><tt>file:onlyname</tt> to access the file name (no paths)</li>
* <li><tt>file:onlyname.noext</tt> to access the file name (no paths) with no extension </li>
* <li><tt>file:parent</tt> to access the parent file name</li>
* <li><tt>file:path</tt> to access the file path name</li>
* <li><tt>file:absolute</tt> is the file regarded as absolute or relative</li>
* <li><tt>file:absolute.path</tt> to access the absolute file path name</li>
* <li><tt>file:length</tt> to access the file length as a Long type</li>
* <li><tt>file:size</tt> to access the file length as a Long type</li>
* <li><tt>file:modified</tt> to access the file last modified as a Date type</li>
* </ul>
* The <b>relative</b> file is the filename with the starting directory clipped, as opposed to <b>path</b> that will
* return the full path including the starting directory.
* <br/>
* The <b>only</b> file is the filename only with all paths clipped.
* The Camel simple language.
*/
@Language("simple")
public class SimpleLanguage extends LanguageSupport implements StaticService {
Expand Down
Expand Up @@ -100,7 +100,7 @@ public void testProducerWithDateHeader() throws Exception {
cal.set(1974, Calendar.APRIL, 20);
Date date = cal.getTime();

template.sendBodyAndHeader("file://target/data/filelanguage?fileName=mybirthday-${date:in.header.birthday:yyyyMMdd}.txt", "Hello World", "birthday", date);
template.sendBodyAndHeader("file://target/data/filelanguage?fileName=mybirthday-${date:header.birthday:yyyyMMdd}.txt", "Hello World", "birthday", date);

assertFileExists("target/data/filelanguage/mybirthday-19740420.txt");
}
Expand Down
Expand Up @@ -125,7 +125,7 @@ public void testDate() throws Exception {
assertExpression("backup-${date:file:yyyyMMdd}", "backup-" + expected);

assertExpression("backup-${date:header.birthday:yyyyMMdd}", "backup-19740420");
assertExpression("hello-${date:out.header.special:yyyyMMdd}", "hello-20080808");
assertExpression("hello-${date:header.special:yyyyMMdd}", "hello-20080808");

try {
this.assertExpression("nodate-${date:header.xxx:yyyyMMdd}", null);
Expand All @@ -144,7 +144,7 @@ public void testDateUsingAlternativeStartToken() throws Exception {
assertExpression("backup-$simple{date:file:yyyyMMdd}", "backup-" + expected);

assertExpression("backup-$simple{date:header.birthday:yyyyMMdd}", "backup-19740420");
assertExpression("hello-$simple{date:out.header.special:yyyyMMdd}", "hello-20080808");
assertExpression("hello-$simple{date:header.special:yyyyMMdd}", "hello-20080808");

try {
this.assertExpression("nodate-$simple{date:header.xxx:yyyyMMdd}", null);
Expand Down Expand Up @@ -212,10 +212,10 @@ public Exchange createExchange() {

Calendar cal = Calendar.getInstance();
cal.set(1974, Calendar.APRIL, 20);
answer.getIn().setHeader("birthday", cal.getTime());
answer.getMessage().setHeader("birthday", cal.getTime());

cal.set(2008, Calendar.AUGUST, 8);
answer.getOut().setHeader("special", cal.getTime());
answer.getMessage().setHeader("special", cal.getTime());
return answer;
}

Expand Down
Expand Up @@ -560,10 +560,6 @@ public void testDateExpressions() throws Exception {
inHeaderCalendar.set(1974, Calendar.APRIL, 20);
exchange.getIn().setHeader("birthday", inHeaderCalendar.getTime());

Calendar outHeaderCalendar = Calendar.getInstance();
outHeaderCalendar.set(1975, Calendar.MAY, 21);
exchange.getOut().setHeader("birthday", outHeaderCalendar.getTime());

Calendar propertyCalendar = Calendar.getInstance();
propertyCalendar.set(1976, Calendar.JUNE, 22);
exchange.setProperty("birthday", propertyCalendar.getTime());
Expand All @@ -572,10 +568,9 @@ public void testDateExpressions() throws Exception {
assertExpression("${date:header.birthday:yyyyMMdd}", "19740420");
assertExpression("${date:header.birthday+24h:yyyyMMdd}", "19740421");

assertExpression("${date:in.header.birthday}", inHeaderCalendar.getTime());
assertExpression("${date:in.header.birthday:yyyyMMdd}", "19740420");
assertExpression("${date:in.header.birthday+24h:yyyyMMdd}", "19740421");

// long
assertExpression("${date:exchangeProperty.birthday}", propertyCalendar.getTime().getTime());
// date
assertExpression("${date:exchangeProperty.birthday}", propertyCalendar.getTime());
assertExpression("${date:exchangeProperty.birthday:yyyyMMdd}", "19760622");
assertExpression("${date:exchangeProperty.birthday+24h:yyyyMMdd}", "19760623");
Expand Down Expand Up @@ -617,6 +612,12 @@ public void testDateNow() throws Exception {
assertNotNull(out);
}

@Test
public void testDateExchangeCreated() throws Exception {
Object out = evaluateExpression("${date:exchangeCreated:hh:mm:ss a}", "" + exchange.getCreated());
assertNotNull(out);
}

@Test
public void testDatePredicates() throws Exception {
assertPredicate("${date:now} < ${date:now+60s}");
Expand Down

0 comments on commit 46f4c43

Please sign in to comment.