Versions Compared

Key

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

...

Now we are nearly there, there is an important issue left with using CXF endpoints in Camel.
TODO: convertBodyTo, ListHolder, Willems featureIn part 4 we started the route by sending the InputReportIncident object containing the webservice input. Now we are using CXF endpoints directly in our routing so its a CxfExchange that is created and passed in the routing. CxfExchange stores the payload in a CXF holder class org.apache.cxf.message.MessageContentsList. So to be able to get our InputReportIncident class we need to get this object from the holder class. For this we show how it's done in Java using a processor, then later we show a nicer solution.

Code Block
java
java

public void process(final Exchange exchange) {
    // Get the parameter list
    List parameter = exchange.getIn().getBody(List.class);
    // Get the first object in the list that is our InputReportIncident
    Object input = parameter.get(0);
    // replace with our input
    exchange.getOut().setBody(input);
}

Well this isn't the nicest code, but again we want to show how it's done using plain Java, that is actually how Camel also can assist you in this nicer solution - we simply convert the body to the expected type using convertBodyTo. This is an important feature in Camel and you can use it for other situations as well.

Code Block
java
java

        // first part from the webservice -> file backup
        from(cxfEndpoint)
            // we need to convert the CXF payload to InputReportIncident that our velocity macro expects
            .convertBodyTo(InputReportIncident.class)
            // we need to convert the CXF payload to InputReportIncident that our velocity macro expects
            //.convertBodyTo(InputReportIncident.class)
            .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
            .transform(constant(OK));

Now the route is nice and simple.