Nirbhay Mishra
Nirbhay Mishra

Reputation: 1648

Using one header value in another in Apache Camel

In apache camel route, how can I use one header value in another?

I tried the following but it gives me error.

<setHeader headerName="dateFormat">
  <simple resultType="java.lang.String">ddMMyyyyHHms</simple>
</setHeader>

<setHeader headerName="startTime">
  <simple resultType="java.lang.String">${date:now:${header.dateFormat}}</simple>
</setHeader>

I am doing this because I need the dateFormat value to be in different places at the same time.

Upvotes: 3

Views: 2030

Answers (1)

SubOptimal
SubOptimal

Reputation: 22973

It might depend on your Camel version.

From the documentation of the simple expression language

From Camel 2.9 onwards you can nest functions, such as shown below:

<setHeader headerName="myHeader">
   <simple>${properties:${header.someKey}}</simple>
</setHeader>

Following snippet was tested with Camel 2.17.1

public class NestedTest extends CamelSpringTestSupport {

    @Override
    protected AbstractApplicationContext createApplicationContext() {
        return new ClassPathXmlApplicationContext("route.xml");
    }

    @Override
    public void setUp() throws Exception {
        deleteDirectory("target/inbound");
        super.setUp();
    }

    @Test
    public void nested() throws InterruptedException {
        getMockEndpoint("mock:end").expectedMessageCount(1);

        template.sendBodyAndHeader("file://target/inbound", 
                "hello camel", 
                Exchange.FILE_NAME, 
                "filename.in"
        );

        assertMockEndpointsSatisfied();
    }
}

route.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://camel.apache.org/schema/spring 
       http://camel.apache.org/schema/spring/camel-spring.xsd
    ">

    <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
    <route>
    <from uri="file://target/inbound"/>
    <setHeader headerName="dateFormat">
    <simple resultType="java.lang.String">ddMMyyyyHHmmss</simple>
    </setHeader>
    <setHeader headerName="startTime">
    <simple resultType="java.lang.String">${date:now:${header.dateFormat}}</simple>
    </setHeader>
    <log message="header timestamp: ${header.startTime}"/>
    <to uri="mock:end"/>
    </route>
    </camelContext>

</beans>

output

15:58:57 INFO  route1 - header timestamp: 07062016155857
15:58:56 INFO  MockEndpoint - Asserting: Endpoint[mock://end] is satisfied
15:58:57 INFO  NestedTest - *************************************************
15:58:57 INFO  NestedTest - Testing done: nested(camelinaction.NestedTest)
15:58:57 INFO  NestedTest - Took: 1.010 seconds (1010 millis)
15:58:57 INFO  NestedTest - *************************************************

Upvotes: 1

Related Questions