Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
java
java
        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"))
            // transform the message using velocity to generate the mail message
            .to("velocity:MailBody.vm")
            // and store the file    
            .to("file://target/subfolder")

...

Code Block
java
java
        // 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"))
            // transform the message using velocity to generate the mail message
            .to("velocity:MailBody.vm")
            // 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);
                }
            });

...

Code Block
java
java
        // 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"))
            // transform the message using velocity to generate the mail message
            .to("velocity:MailBody.vm")
            // and store the file    
            .to("file://target/subfolder")
            // return OK as response
            .process(new OKResponseProcessor());

...

Code Block
java
java
        // first part from the webservice -> file backup
        from(cxfEndpoint)
            // we need to convert the CXF payload to InputReportIncident that ourFilenameGenerator and velocity macro expects
            .convertBodyTo(InputReportIncident.class)
            // then weset the needfile toname convertusing the CXF payload to InputReportIncident that our velocity macro expectsFilenameGenerator bean
            .setHeader(FileComponent.HEADER_FILE_NAME, BeanLanguage.bean(FilenameGenerator.class, "generateFilename"))
            //.convertBodyTo(InputReportIncident.class) transform the message using velocity to generate the mail message
            .to("velocity:MailBody.vm")
            // then set the file name using the FilenameGenerator bean
            .setHeader(FileComponent.HEADER_FILE_NAME, BeanLanguage.bean(FilenameGenerator.class, "generateFilename"))
            // and and store the file    
            .to("file://target/subfolder")
            // return OK as response
            .transform(constant(OK));

Now the route is nice and simple.

Unit testing it

Now lets turn our attention to unit testing it. From part 4 we have an unit test that is capable of exposing a webservice and send a test request and assert a mail is received. We will refactor this unit test to start up Camel, as it's Camel that should expose the webservice.

As Camel is very flexible we can create a camel context, add the routes and start it in 3 lines of code so we do it:

Code Block
java
java

    protected void startCamel() throws Exception {
    	camel = new DefaultCamelContext();
        camel.addRoutes(new ReportIncidentRoutes());
        camel.start();

And the rest of the unit test is quite self documenting so we print it here in full:

Code Block
java
java

/**
 * Unit test of our routes
 */
public class ReportIncidentRoutesTest extends TestCase {

    private CamelContext camel;

    // should be the same address as we have in our route
    private static String ADDRESS = "http://localhost:8080/part-five/webservices/incident";

    protected void startCamel() throws Exception {
    	camel = new DefaultCamelContext();
        camel.addRoutes(new ReportIncidentRoutes());
        camel.start();
    }

    protected static ReportIncidentEndpoint createCXFClient() {
        // we use CXF to create a client for us as its easier than JAXWS and works
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setServiceClass(ReportIncidentEndpoint.class);
        factory.setAddress(ADDRESS);
        return (ReportIncidentEndpoint) factory.create();
    }

    public void testRendportIncident() throws Exception {
        // start camel
        startCamel();

        // assert mailbox is empty before starting
        Mailbox inbox = Mailbox.get("incident@mycompany.com");
        assertEquals("Should not have mails", 0, inbox.size());

        // create input parameter
        InputReportIncident input = new InputReportIncident();
        input.setIncidentId("123");
        input.setIncidentDate("2008-08-18");
        input.setGivenName("Claus");
        input.setFamilyName("Ibsen");
        input.setSummary("Bla");
        input.setDetails("Bla bla");
        input.setEmail("davsclaus@apache.org");
        input.setPhone("0045 2962 7576");

        // create the webservice client and send the request
        ReportIncidentEndpoint client = createCXFClient();
        OutputReportIncident out = client.reportIncident(input);

        // assert we got a OK back
        assertEquals("0", out.getCode());

        // let some time pass to allow Camel to pickup the file and send it as an email
        Thread.sleep(3000);

        // assert mail box
        assertEquals("Should have got 1 mail", 1, inbox.size());

        // stop camel
        camel.stop();
    }
}

Conclusion

We have now seen how we have created a more nicer solution leveraging Camel's powerful routing capabilities.