Saturday, 14 January 2017

Custom Xpath function in Oracle Service Bus

OSB allows us to create custom Xpath function and these function case be used in XSLT, Xquery. Custom Xpath function is the way to extend the built in functionality of OSB.

In this blog, we will go through the steps that are involved in custom Xpath creation.During the startup of OSB server, OSB checks the custom Xpath function in $OSB_HOME/config/xpath-functions.

Let’s start step by step explanation.

Create a java class with a method named greeting that will accept input string and return the string with prefix Hi

package com.ankur.blogs;
public class CustomXPathFunctions {
public CustomXPathFunctions() {
super();
}
public static String greeting(String name){
return "Hi "+name;
}
}

Create a custom-osb-xpath.xml with below entries:

<?xml version="1.0" encoding="UTF-8"?>
<xpf:xpathFunctions xmlns:xpf="http://www.bea.com/wli/sb/xpath/config">
    <xpf:category id="Custom Functions">
        <xpf:function>
            <xpf:name>Gretting</xpf:name>
            <xpf:comment>Function used for (first or All) trim of string based on regex function</xpf:comment>
            <xpf:namespaceURI>http://www.oracle.com/osb/xpath-functions/Trim</xpf:namespaceURI>
            <xpf:className>com.ankur.blogs.CustomXPathFunctions</xpf:className>
            <xpf:method>java.lang.String greeting(java.lang.String)</xpf:method>
            <xpf:isDeterministic>false</xpf:isDeterministic>
            <xpf:scope>Pipeline</xpf:scope>
            <xpf:scope>SplitJoin</xpf:scope>
        </xpf:function>
    </xpf:category>
</xpf:xpathFunctions>

The above XML fragment shows the custom XPath function name(Gretting)Class name (CustomXPathFunctions) , Java Method(greeting).

Compile the java class. Create a folder META-INF and put the custom-osb-xpath.xml under META-INF folder.

Folder structure will look like:


Custom XPath


Now create a .jar file. Open command prompt and move to the directory where we kept the above file and run the below command:

jar -cf customxpath.jar .



Copy the custompath.jar and custom-osb-xpath.xml  to $OSB_HOME/config/xpath-functions directory, and copy the custompath.jar to $DOMAIN_HOME/lib directory.

Restart the Jdeveloper and Servers.

Let's create a simple XSLT and search the greeting functions from the component pallet:


You can drag the greeting function from component pallet to the middle pane of the XSLT:




This is the time to test the XSLT



You can see, Hi has been appended to the name, that's mean custom xpath function has been called successfully.

Done!

1 comment: