OSB: Java Callout with XML input and output parameter

With the Oracle Service Bus (OSB) it is possible to extend the functionality by using java callouts.

Java callouts are static operations implemented in Java and packaged in a jar file. The jar file and all dependencies have to be included into the OSB project. Then it is possible to select the operation in the java callout node.

Basic types like int and String are supported for the parameters and the return value of the callout (the full list and a good tutorial is available at Simple Java Callout Example). But much more interesting, because in OSB everything is about XML processing, is to use XML snippets for parameter and return value.

First we create a proxy service with a java callout.

BLOG_OSB_JavaColloutXML_01

Then we implement the java class for the java callout.

The static method called by the OSB:

1 public static XmlObject test(String name, final XmlObject inputXmlData) { 2 Element inputElement = (Element) inputXmlData.getDomNode().getFirstChild(); 3 processInput(inputElement); 4 final XmlObject result = createOutput(); 5 return result; 6 }

We need a method processing the input XmlObject and doing something with the containing elements:

1 private static void processInput(Element inputElement) { 2 // Iterate over input 3 final NodeList fieldsNodeList = inputElement.getChildNodes(); 4 for (int i = 0; i < fieldsNodeList.getLength(); i++) { 5 final Node fieldsNode = fieldsNodeList.item(i); 6 if (fieldsNode.getNodeType() == Node.ELEMENT_NODE) { 7 String nodeName = fieldsNode.getLocalName(); 8 if (nodeName == null) { 9 nodeName = fieldsNode.getNodeName(); // Depends on XML library 10 } 11 // Do something with it 12 } 13 } 14 }

For the return value we create a new XmlObject containing e single resultElement element. Inside this element the real return values are located.

1 private static XmlObject createOutput() { 2 // Create XmlObject and wrapping element 3 final XmlObject result = XmlObject.Factory.newInstance(); 4 final Node resultDomNode = result.getDomNode(); 5 final Document resultDocument = resultDomNode.getOwnerDocument(); 6 final Element resultElement = resultDocument.createElement("resultElement"); 7 resultDomNode.appendChild(resultElement); 8 // Create result nodes 9 for(int i=0; i<10; i++) { 10 String name = "test" + i; 11 String value = Integer.toString(i); 12 final Element element = resultDocument.createElement(name); 13 resultElement.appendChild(element); 14 element.appendChild(resultDocument.createTextNode(value));; 15 } 16 return result; 17 }

 

After compiling this class to a jar file and adding the jar file, xmlbeans and stax-api to the OSB project we can select our implemented method inside the java callout.

BLOG_OSB_JavaColloutXML_02

Finally we can deploy the whole thing to OSB and test it with the debug window.

BLOG_OSB_JavaColloutXML_03

 

Bernhard Mähr @ OPITZ-CONSULTING published at http://thecattlecrew.wordpress.com/

Leave a Reply