...
Code Block |
---|
|
public void testTransformMailBody() throws Exception {
// create a dummy input with some input data
InputReportIncident parameters = createInput();
// send the message (using the sendBody method that takes a parameters as the input body)
// to "direct:start" that kick-starts the route
// the response is returned as the out object, and its also the body of the response
Object out = context.createProducerTemplate().sendBody("direct:start", parameters);
// convert the response to a string using camel converters. However we could also have casted it to
// a string directly but using the type converters ensure that Camel can convert it if it wasn't a string
// in the first place. The type converters in Camel is really powerful and you will later learn to
// appreciate them and wonder why its not build in Java out-of-the-box
String body = context.getTypeConverter().convertTo(String.class, out);
// do some simple assertions of the mail body
assertTrue(body.startsWith("Incident 123 has been reported on the 2008-07-16 by Claus Ibsen."));
}
/**
* Creates a dummy request to be used for input
*/
protected InputReportIncident createInput() {
InputReportIncident input = new InputReportIncident();
input.setIncidentId("123");
input.setIncidentDate("2008-07-16");
input.setGivenName("Claus");
input.setFamilyName("Ibsen");
input.setSummary("bla bla");
input.setDetails("more bla bla");
input.setEmail("davsclaus@apache.org");
input.setPhone("+45 2962 7576");
return input;
}
|
Adding the File Backup
The next piece of puzzle that is missing is to store the mail body as a backup file. So we turn back to our route and the EIP patterns. We use the Pipes and Filters pattern here to chain the routing as:
Code Block |
---|
|
public void configure() throws Exception {
from("direct:start")
.to("velocity:MailBody.vm")
// using pipes-and-filters we send the output from the previous to the next
.to("file://target/subfolder");
}
|
Notice that we just add a 2nd .to on the newline. Camel will default use the Pipes and Filters pattern here when there are multi endpoints chained liked this. We could have used the pipeline verb to let out stand out that its the Pipes and Filters pattern such as:
Code Block |
---|
|
from("direct:start")
// using pipes-and-filters we send the output from the previous to the next
.pipeline("velocity:MailBody.vm", "file://target/subfolder");
|
But most people are using the multi .to style instead.
...