Kamil Roman
Kamil Roman

Reputation: 1071

conditional fileName expression in file endpoint

Is there any way to have a conditional expression in the fileName parameter of the file:// endpoint in Apache Camel?

I am using Java DSL (can't change this) to build the route, and the definition below does not seem to work

myroute.to("file://my-file-out?fileName=${header[myheader] eq 'EXPECTED_VALUE' ? 'EXPECTED' : 'UNEXPECTED'}")

Unfortunately I have to accomplish this in a single .to() method invocation as I am using a predefined (custom) application framework.

Upvotes: 2

Views: 454

Answers (2)

Donks
Donks

Reputation: 1

The filenname value can refer to a header or property that in turn could be a expression. I have not tried it before, but maybe the formula could be in any of the available expression languages eg Groovy. Could this be a solution within the bounds you have?

Upvotes: 0

pvpkiran
pvpkiran

Reputation: 27048

You can do it like this

myroute
   .choice()
   .when(header("myHeader").isEqualTo("EXPECTED_VALUE"))
     .to("file://my-file-out?fileName=EXPECTED_VALUE")
   .otherwise()
     .to("file://my-file-out?fileName=UNEXPECTED")
   .endChoice(); 

Another slightly different approach

myroute
   .choice()
   .when(header("myHeader").isEqualTo("EXPECTED_VALUE"))
     .setHeader("finalFileName", simple("EXPECTED_VALUE"))
   .otherwise()
     .setHeader("finalFileName", simple("UNEXPECTED"))
   .endChoice()
   .to("file:///tmp?fileName=${header.finalFileName}")
   .end()
 ;

Upvotes: 1

Related Questions