...
Code Block |
---|
|
// first part from the webservice -> file backup
from(cxfEndpoint)
.to("velocity:MailBody.vm")
// then set the file name using the FilenameGenerator bean
.setHeader(FileComponent.HEADER_FILE_NAME, BeanLanguage.bean(FilenameGenerator.class, "generateFilename"))
// and store the file
.to("file://target/subfolder")
// return OK as response
.process(new Processor() {
public void process(Exchange exchange) throws Exception {
// the response we want to send
OutputReportIncident OK = new OutputReportIncident();
OK.setCode("0");
// set the response on the OUT message as we use InOut
exchange.getOut().setBody(OK);
}
});
|
The route using the inlined processor is a bit ugly as we have high level routing logic combined with low level java code. First of all I wanted to show how flexible Camel is, allowing use as a developer to always be in control and can use Java code for whatever you needs is. First of all we could move the code into a inner class and just refer to it:
Code Block |
---|
|
private static class OKResponseProcessor implements Processor {
public void process(Exchange exchange) throws Exception {
// the response we want to send
OutputReportIncident OK = new OutputReportIncident();
OK.setCode("0");
// set the response on the OUT message as we use InOut
exchange.getOut().setBody(OK);
}
}
|
And then out route is much nicer:
Code Block |
---|
|
// first part from the webservice -> file backup
from(cxfEndpoint)
.to("velocity:MailBody.vm")
// then set the file name using the FilenameGenerator bean
.setHeader(FileComponent.HEADER_FILE_NAME, BeanLanguage.bean(FilenameGenerator.class, "generateFilename"))
// and store the file
.to("file://target/subfolder")
// return OK as response
.process(new OKResponseProcessor());
|
Since our response is static and we don't need to any code logic to set it we can use the transform DSL in the route to set a constant OUT message. So we refactor the code a bit to loose the processor. First we define the OK response as:
Code Block |
---|
|
// webservice response for OK
OutputReportIncident OK = new OutputReportIncident();
OK.setCode("0");
|
And then we can refer to it in the route as a constant expression:
code:java}
// return OK as response
.transform(constant(OK));
Now we are nearly there, there is an important issue left with using CXF endpoints in Camel.