javaapache-camelspring-camelcamel-spring-dsl

How to escape regex in Java methods call in simple language in Apache's Camel Spring DSL (XML) route?


I have a setHeader tag in my route:

<setHeader headerName="timestampPart3"><simple>${header.timestampPart2.split("\\.")[0]}</simple></setHeader>
<log message="After: ${body} ${headers}"/>

I expect, it will split the String inside the timestampPart2 header and take the first element.

In fact, it just ignores the splitting:

timestampPart2=114128.0, timestampPart3=114128.0

So how I should implement regex escaping in Spring DSL? If for some reason it is impossible, how to work that around?


Solution

  • To set an Exchange message header you would need to use the name field and not the headerName field:

    <setHeader name="timestampPart3">
        <simple>some-simple-expression</simple>
    </setHeader>
    

    Additionally you need to provide a raw regex expression for string #split method that will get sanitized (if needed) under the hood automatically by the OGNL - Camel parsing layer for you:

    <setHeader name="timestampPart3">
        <simple>${header.timestampPart2.split("\.")[0]}</simple>
    </setHeader>
    

    Note the single back-slack character escaping the . special character. In short, you should provide a regular expression without escaping control sequences as you would do when inputing the same regular expression through a compile String literal in Java (it is raw XML (input stream) after all)