20

while 'playing around' with Camel using Spring DSL, I came across the following problem. Suppose the expected message flow looks like this:

  1. client sends HTTP POST message with XML body to CAMEL
  2. CAMEL proxies HTTP POST message towards server, with the URI slightly adapted using info from the received XML body (eg: use XPATH to filter out a certain parameter)
  3. after CAMEL has received a reply, CAMEL sends HTTP PUT message towards server, using parameters out of the XML body received in 1

So something like:

<route>
   <from uri="...">
   <to uri="...">
   <to uri="...">
 </route>

Question: how do I store the parameters in Spring DSL in step 1, so that I can use them later in step 3 ?

So, I would like to extract XML parameters out of the XML body of the message received in step 1 and put them into variables, which I then later on can use to compose the message to be sent in step 3.

For extracting the parameters, I was thinking of using XPATH. That looks ok, but I just don't see how to put the output of the XPATH into a variable and then use that variable later on ... (syntax ??)

Note: as you can see, my development knowledge is rather limited ... sorry for that. But it would still be great if someone could help with this :).

opstalj
  • 892
  • 4
  • 13
  • 22

2 Answers2

26

you can set store data in the Exchange properties or message headers like this...

.setHeader("ID", XPathBuilder.xpath("/order/@id", String.class))
.setProperty("ID", XPathBuilder.xpath("/order/@id", String.class))

and then retrieve them in a bean/processor from the Exchange like this...

String propId = (String) exchange.getProperty("ID");
String headerId = (String) exchange.getIn().getHeader("ID");                        }
Ben ODay
  • 20,784
  • 9
  • 45
  • 68
  • 2
    Thanks already for the reply. However, any chance to get the same code but for Spring DSL (XML notation) ? Cheers. – opstalj Feb 09 '12 at 09:24
  • 1
    In the meanwhile, I re-coded with Java DSL and it works nicely. Thanks once more. I do have 1 more question: what is the difference between using setHeader and setProperty ? – opstalj Feb 10 '12 at 07:25
  • 4
    Read this chapter to learn about the Camel concepts, and you will know whats the difference between properties and headers: http://manning.com/ibsen/chapter1sample.pdf – Claus Ibsen Feb 10 '12 at 09:07
  • Thanks. That chapter explains it well :). – opstalj Feb 15 '12 at 12:17
  • `setProperty` does not take three arguments. – swade Apr 26 '17 at 15:14
  • .log("access in route using ${exchangeProperty.ID}") – Gerry Oct 28 '21 at 22:25
10

I leave you some examples:

<setHeader headerName="token">
    <constant>someValue</constant>
</setHeader>

<setHeader headerName="userName">
    <simple>${properties:userName}</simple>  //from config
</setHeader>

<setProperty propertyName="bodyBkp">
    <simple>${in.body}</simple>
</setProperty>

<setProperty propertyName="orderNumber">
    <xpath resultType="String">//item[1]/orderNumber/text()</xpath>
</setProperty>

Getter

${exchangeProperty[orderNumber]}

${in.headers.token}

Documentation

Check the simple expression language: http://camel.apache.org/simple.html

Sometimes looking at the test cases of Camel can be helpful as well, in particular for Spring DSL:

Clauds
  • 950
  • 11
  • 24