Sunday, March 31, 2013

OSB 11g - XMLObject Java Callout Example Part - II

In Previous post, I explained how to deal with XmlObject in Java Callout. We can't use the response XmlObject in OSB message flow but we can easily pass that XmlObject to another Java Callout.

In This post, I will explain how we can pass XmlObject response from one Java Callout to another Java Callout action.

1.)  Create one Java class and create one static method that takes XmlObject as input parameter and return String as return type.

Java Class
Java Class


package CustomerAccountInfo;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import org.apache.xmlbeans.XmlObject;

public class CustomerAccount {
    public CustomerAccount() {
        super();
    }
    public static String getAccountInfo(XmlObject customer) {
        String CustomerAccountId = "";
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {
            InputStream inputStream = customer.newInputStream();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(inputStream);
            doc.getDocumentElement().normalize();
            NodeList nList = doc.getElementsByTagName("Customer");
            for (int temp = 0; temp < nList.getLength(); temp++) {
                Node nNode = nList.item(temp);
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element)nNode;
                    String Name =eElement.getElementsByTagName("Name").item(0).getTextContent();
                    String City =eElement.getElementsByTagName("City").item(0).getTextContent();
                    String Country =eElement.getElementsByTagName("Country").item(0).getTextContent();
                    CustomerAccountId =CustomerAccountId + Name + City + Country;
                }
            }
            return CustomerAccountId;
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }
}


2.) Package above Java class to jar file and import it to OSB project.

Imported Resources
Imported Resources

3.) Add another Java Callout action in TargetServicePS message flow and assign result variable of above Java Callout to input variable of this Java Callout.

Java Callout
Java Callout

4.) Now assign response from second Java Callout action to input variable of target Service.

Target Service
Target Service


Saturday, March 30, 2013

OSB 11g - XMLObject Java Callout Example Part- I

In my previous post, I explained how to invoke simple Java code from Oracle Service Bus using Java Callout action. But in another scenario's we sometime needs to deal with XmlObject using Java Callout, so in this post I will show you how we can leverage OSB Java Callout functionality to handle XmlObjects.

Follow below steps to invoke Java code which in turn return XmlObject.

1.) Create a Java Class and inside it, create one static method which returns XmlObject as return type. Make sure that the Java method is static one.

Java Class
Java Class


package CustomerInfo;
import org.apache.xmlbeans.XmlObject;

public class CustomerAddressInfo {
    public CustomerAddressInfo() {
        super();
    }
    static XmlObject newObject;

    public static XmlObject CustomerAddress(String CustId) {
        String xmlString =
            String.format("<Customer xmlns=\"http://www.xmlobject.test/xml\">\n" +
                "<Name>" + "Vivek " + "</Name>\n" +
                "<City>" + "Mumbai " + "</City>\n" +
                "<Country>" + "India " + "</Country>\n" +
                "</Customer>\n");
        try {
            XmlObject object = XmlObject.Factory.parse(xmlString);

            return object;
        } catch (Exception e) {
            e.printStackTrace();
            return newObject;
        }
    }

}


To compile above Java code you need "xbean-1.0.3" jar file, which you can download from here.

2.) Package above Java class to jar file and import it to OSB project.

3.) Create one target service, same as we created in previous post and import wsdl and xsd of that target service to OSB project.

Imported Resources
Imported Resources

4.) Create Business and Proxy service from above imported WSDL file.

Business and Proxy Services
Business and Proxy Services


5.) Now go to "Message Flow" of OSB proxy service and add one "Java Callout" Activity.

6.)  Click on "Method" link and It will pop-up a window. Then Select the above imported jar file and choose static method.

7.) Now specify Input and Output parameters.

Java callout
Java Callout


8.) To check the Result variable simple use one Log activity after Java Callout action and you will get below result.

< Customer  xmlns ="http://www.xmlobject.test/xml">
 < Name>Vivek</ Name>
 < City>Mumbai</ City>
 < Country>India</ Country>
 </ Customer>


There are some limitations when we deal with XmlObject in OSB Java Callout. We can't use the Result variable inside Message Flow, we can only pass this Result variable to another Java Callout.

In My next Post, I will explain how to pass above Result variable to another Java Callout action.





Sunday, March 10, 2013

OSB 11g - Simple Java Callout Example

Sometime Oracle Service Bus is not sufficient to perform some of the tasks, to do that tasks we need to use Java code. Oracle Service Bus has provided “Java Callout” feature, by using it we can invoke Java code from OSB.

In this Post, I will explain a very basic Java Callout example in Oracle Service Bus.

Java Callout
Java Callout
Follow below steps to use Java Callout activity in Oracle Service Bus.

<!--[if !supportLists]-->     1.)    <!--[endif]-->Create one static method inside class and package it to a jar file. Use the following guidelines for the Java Callout method:


  • The method must be static.

      Only the following Java types are supported for input parameters:





  • boolean, byte, char, double, float, int, long, short and arrays of these types
  • java.lang.[Boolean | Byte | Character | Double | Float | Integer | Long | Short | String] and arrays of these types
  •  java.math.[BigInteger | BigDecimal] and arrays of these types
  •  org.apache.xmlbeans.XmlObject and arrays of this type

 Only the following Java types are supported for method return:
  • All types supported for input parameters except their array equivalent
  •  void
Java Class
Java Class

2.)  Create a new OSB project and import WSDL and XSD of End service that we want to invoke from it.

3.)  Create Business Service from above imported WSDL file and similarly create Proxy Service either from that WSDL file or from Business Service.

4.)  Now Import the Jar file into new OSB project. To import jar file go to Create Resources --> Select Resource Type --> Select “Jar” under Utility section.

5.)  Now Go to Proxy Service message flow and add “Java Callout” activity.


Add Java Callout Activity
Message Flow- Java Callout Activity

6.)  Click on “Method” link and it will open a Pop-Up window.
Select Java Method
Java Method
7.) Select imported jar file.

Select Imported Jar file
Jar File
8.)  Expand Java class and choose the required method.

Choose Java Method
Choose Java Method

For small business website solutions, we recommend website solutions from AJIBOYE.

9.)  Now we need to specify Input and output parameters.



Specify Input and Output parameters
Specify Input/Output


Expression: link to the XQuery/XSLT Expression Editor, where you can create an expression to retrieve a value for the argument

Return Parameter as Reference: This option makes the return value of a Java Callout invocation a <java-content ref="jcid"> reference element regardless of its actual type, where jcid is the key to the object in the pipeline object repository. In the Result value field, enter the name of the variable to contain the java-content reference. This option lets you work with a referenced object in the message flow in addition to the pipeline XML for providing pass through, performing message enrichment with Java Callout and inline actions, or performing message transformation between Java and non-Java transports.

Result Type: The variable to which the result is assigned. The label for the field indicates the data type of the result. If the result is a byte array (the only possible array returned), the binary-content XML element is returned.

Service Account: An optional Service Account, which can be specified if there is a security context for this Java method. Click Browse to select a service account. In the case of fixed and mapped service accounts, the userid/password from the service account is authenticated in the local system and the security context propagated to the Java Callout. In the case of passthru, the security context is propagated to the Java Callout. This context is the message level context if defined (with WS-Security). Otherwise, it is the transport level context.






10.)  In Expression  provide Input parameter value.


Input Parameter


11.)  Provide Result variable name.


Result Variable
Result Variable

12.) Assign/Replace response from Java Callout to End service Input.

13.) The Response from Java Callout is passed to End Service.


Java Response to End Service Request
Java Response to End Service Request
14.) Test the proxy service.

Test Console
Test Console



Test Result
Test Result