I have a scenario where I have to read a file from the location on certain interval, extract the file name & file path, hit 2 rest services which is a Get & Post call using those inputs & place the file in appropriate location. I have managed a pseudo code as follows.
Wanted to know if there's a better way of achieving this using Camel. Appreciate your help!
The flow is -
- Extract the fileName
- Hit a Get endpoint ('getAPIDetails') using that fileName as an input to check if that fileName exists in that registry.
If the response is successful (status code 200)
- Call a Post endpoint ('registerFile') with fileName & filePath as RequestBody
- Move the file to C:/output folder (moving the file is still TODO in the code below).
If the file is not found (status code 404)
- Move the file to C:/error folder.
'FileDetails' below is a POJO consisting of fileName & filePath which will be used for passing as a RequestBody to post service call.
@Override
public void configure() throws Exception {
restConfiguration().component("servlet").port("8080")).host("localhost")
.bindingMode(RestBindingMode.json);
from("file:C://input?noop=true&scheduler=quartz2&scheduler.cron=0 0/1 * 1/1 * ? *")
.process(new Processor() {
public void process(Exchange msg) {
String fileName = msg.getIn().getHeader("CamelFileName").toString();
System.out.println("CamelFileName: " + fileName);
FileDetails fileDetails = FileDetails.builder().build();
fileDetails.setFileName(fileName);
fileDetails.setFilePath(exchange.getIn().getBody());
}
})
// Check if this file exists in the registry.
// Question: Will the 'fileName' in URL below be picked from process() method?
.to("rest:get:getAPIDetails/fileName")
.choice()
// If the API returns true, call the post endpoint with fileName & filePath as input params
.when(header(Exchange.HTTP_RESPONSE_CODE).isEqualTo(constant(200)))
// Question: Will 'fileDetails' in URL below be passed as a requestbody with desired values set in process() method?
// TODO: Move the file to C:/output location after Post call
.to("rest:post:registerFile?type=fileDetails")
.otherwise()
.to("file:C://error");
}