Question

In Camel route that converts WARC entries into a custom file format for each entry, I need to parse some dates that can be found the original HTTP headers.

I have in an input header the Date (and the Last-Modified date) as provided by an HTTP server (normally relying on the format described in RFC2616, i.e. something like Wed, 09 Apr 2014 11:59:14 GMT).

I need to parse this date in the input header and to store it as a Date in the output header.

I tried:

<setHeader headerName="weblab:dct:modified">
   <simple resultType="java.util.Date">${headers.ArchiveRecordPayloadHeader['Last-Modified']}</simple>
</setHeader>

This crashes saying that the date Wed, 09 Apr 2014 11:59:14 GMT is not in the right format.

I also tried:

<setHeader headerName="weblab:wlp:hasGatheringDate">
    <simple resultType="java.util.Date">${date:in.header.ArchiveRecordPayloadHeader['Date']:EEE, dd MMM yyyy HH:mm:ss zzz}</simple>
</setHeader>

But is crashes saying that it cannot find a Date in in.header.ArchiveRecordPayloadHeader['Date']. It seems that this is meant to format date not for parsing.

Any idea? Thank you in advance

Was it helpful?

Solution

Use Groovy to parse the date String such as

<setHeader headerName="myNewHeader">
    <groovy>new java.text.SimpleDateFormat('EEE, dd MMM yyyy HH:mm:ss zzz', java.util.Locale.US).parse(request.headers.myOldHeader)</groovy>
</setHeader>

In order to enable Groovy add the camel-groovy dependency to your classpath:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-groovy</artifactId>
    <version>${camel.version}</version>
</dependency>

OTHER TIPS

Might be easiest to call a bean which will convert the header to a date and store it back in another header.

A - Writing A Date Processor

Write a class that implements Processor interface so that you can get the Header.Date from Exchange object and set it to whatever format you want to;

public class DateProcessor implements Processor {

    private static final SimpleDateFormat SOURCE_DATE_FORMAT = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
    private static final SimpleDateFormat TARGET_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");

    @Override
    public void process(Exchange exchange) throws Exception {
        String dateString = exchange.getIn().getHeader("Date", String.class);

        Date date = SOURCE_DATE_FORMAT.parse(dateString);

        exchange.getIn().setHeader("Date", TARGET_DATE_FORMAT.format(date));
    }

}

B - Use Date Processor On Your Route

Use the Date Processor above within your route as follows;

private static void dateProcessorExample(CamelContext camelContext) throws Exception {
    camelContext.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("your source here")
                .process(new DateProcessor())
                .to("your target here");
        }
    });
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top