I have several camel routes and I set a header in the Exchange object with the name FILE_NAME in the one route while reading the data from a DB. As a next step this route goes further to my FTP route where the file should be downloaded. The problem is that the FTP route does not receive the headers of the previous route with the contentEnricher I am using. This is the official behaviour: http://camel.apache.org/content-enricher.html However, "fileName" parameter of the FTP endpoint could be constructed dynamically to download a particular file.
My FTP route looks like this, with schematic data now:
from("direct:myRoute")
.pollEnrich("ftp://foo@localhost/public/reports?password=secret&binary=true&fileName=data.txt")
.to("mock:result");
How could I download just the file provided in the header value of the previous route? Should I not use content enricher or should I store the fileName in a variable somewhere else? Thanks also in advance for your response.
EDIT1:
Thanks for the posts I got further but I need to come back to the same point as I can access the header values from Java DSL also from the simple expression in pollEnrich() but not in the to(). The process(Exchange exchange) prints the correct header values, the pollEnrich with the sftp consumer fetches the file from the sftp server but neither ${header.FILE_NAME_ONLY} nor ${in.header.FILE_NAME_ONLY} access it in the to(). So the file created will be named "value of obNumber"_ . Could you please have a look what is incorrect in the code snippet below?
from("direct:SFTP").routeId("SFTP")
.log("### SFTP")
.process(new Processor() {
public void process(Exchange exchange) throws Exception {
LOGGER.info("### Process SFTP " +
"FILE_NAME_ONLY = " + exchange.getIn().getHeader("FILE_NAME_ONLY" ) +
" FILE_PATH = " + exchange.getIn().getHeader("FILE_PATH") +
" AGG = " + exchange.getIn().getHeader("AGG"));
}
})
.choice()
.when(header("FILE_NAME_ONLY").isEqualTo(""))
.log("### SFTP FILE_NAME_ONLY is null!")
.endChoice()
.otherwise()
.log("### SFTP FILE_NAME_ONLY is NOT null!")
.pollEnrich().simple("sftp:" + ftpUid + "@" + ftpHost + "/" + ftpBasePath + "/${header.FILE_PATH}?password=" +
ftpPwd + "&binary=true&fileName=${header.FILE_NAME_ONLY}")
.to("file:extract?fileName=" + obNumber + "_${header.FILE_NAME_ONLY}")
.end();
Solution:
The final solution was the dynamic router as Jeremie B proposed on 25 Febr. The problem is that pollEnrich() swallows up the previous header variables. So they are available to construct the URI but cannot be accessed afterwards to name the file. I was using camel 2.16.1.
What I did:
Two examples that helped:
The manual (http://camel.apache.org/content-enricher.html) says:
From Camel 2.16 onwards both enrich and pollEnrich supports dynamic endpoints that uses an Expression to compute the uri, which allows to use data from the current Exchange.
You just have to use Camel 2.16 or newer.
EDIT1:
This should work correctly
.to("file:?fileName=extract/" + obNumber + "_${header.FILE_NAME_ONLY}")
or try this:
.recipientList(simple("file:?fileName=extract/" + obNumber + "_${header.FILE_NAME_ONLY}"))
or try this:
.setHeader("CamelFileName").simple("extract/"+obNumber+"_${header.FILE_NAME_ONLY}")
.to("file:")