Friday, October 25, 2019

Convert XML to String in OSB : fn-bea:serialize

Sometime it is required to convert XML to string format. OSB allows us to convert XML to string using OSB inbuilt function.
You can use the fn-bea:serialize() function if you need to represent an XML document as a string instead of as an XML element.
For example, you may want to exchange an XML document through an EJB interface and the EJB method takes String as argument. The function has the following signature:
fn-bea:serialize($input as item())
Let’s see the example:
Suppose you have an variable called result in the OSB that contained XML, and you want to convert the same into string, then you can use the following expression in Assign activity.
Let’s run the example and see the output of Assign variable.

Using fn-bea:inlinedXML in OSB to parse XML Text

Using  fn-bea:inlinedXML in OSB to parse XML Text
fn-bea:inlinedXML in OSB
Consider a scenario where in you expect an XML String from the target application.
In case you find it challenging to use the Xpath to parse the XML string and retrieve value from a particular node the, Xquery function fn-bea:inlinedXML in OSB may be used to Parses textual XML and returns an instance of the XQuery 1.0 Data Model.In simple terms ,parse from xml string to xml Element that can be processed by the parser.
A working example is shown below, where I receive an XML String from end system $body,which is converted to an XML object,using the fn-bea:inlinedXML.Just make sure that your XML string is valid with proper tags and namespace declaration.
Once done the Xpath expression can be applied on $MyVar for Parsing the DOM Structure.
Ø $MyVar/RCRMInterface/Object/InterfaceActivity/ActivityID/text()

JSON to XML using NXSD in OSB 12C

NXSD is used to convert JSON to XML and vice versa. In this post will see the example to convert the JSON sample into XML sample.

Before NXSD, to convert from JSON structure to XML structure or vice versa, mostly java callout was being used. The SOA Suite has the capability to come up with an XML representation of data in all kinds of formats that are not XML. Native XML Schema Definitions i.e nXSD is used to convert the native format to XML structure and vice versa.

At any moment when in a Service Bus Pipeline we are dealing with data that is structured – but not in XML format, so it will not be accessible to our pipeline, for example, JSON, Fixed Position format or CSV. We can use the native mapping to convert from the native structure to XML. The reverse operation is also available with the NXSD mapping: from XML to the native format specified.
So, here will create an application which will convert JSON structure to XML structure.
Go to File -> New -> Application

From available options, select Service Bus Application.

Enter Application Name: OSBProjects and click Finish
To create, new Service Bus Project, Go to File -> New -> Project

From available options, select Service Bus Project.

Enter Project Name : NXSD_JsonToXML and click Finish.
To create the folder structure in the project right click on project name i.e. NXSD_JsonToXML Go to New -> From Gallery
Go to Folder option and click on OK

Enter folder name : Pipeline and click on OK
As mentioned in the previous step create two more folders: ProxyService and Schemas

To create the pipeline, Right click on Pipeline folder and Go to New -> Pipeline

Enter Service Name : PS_NXSD_JsonToXML_Pipeline and click on Next
Click on REST option and Expose as a Proxy Service option. Change the Proxy Location to ProxyService Folder and click on Finish.
Double click on the PS_NXSD_JsonToXML_Pipeline and Drag pipeline pair node from the component pallet.
To create the NXSD schema, Right click on Schemas folder and Go to New -> NXSD Schema
Native format builder will open, click on Next
Enter file Name for XSD Schema : JsonToXML.xsd
Specify the format type as : JSON Interchange Format and click on Next

Enter the JSONRequest in Sample and click on Next. Whatever JSON sample you want to convert in XML you can enter here or you can select the file which contains JSON sample by using browse option. Here you have the option to select your root element name also. By default it is Root-Element.
It will generate automatically XML schema i.e. nothing but the XSD for your JSON sample. Click on Next
It will show generated schema location. Click on Finish.
As shown below, XSD will be create to you native format i.e. JSON sample.
From component pallet select nXSD translate activity and drag it into Request pipeline. As we are converting JSON to XML so in Translate option will select Native to XML option.
In Input, we need to give the expression which has a JSON sample. So here in the body, we are passing a JSON sample. So will enter expression value as $body/text().
After that, we need to select the nXSD Schema. Here we have two options static and dynamic. As we already have generated the schema so will select the static option and browse the respective XSD i.e. JsonToXML.xsd file from schemas folder. Then we have an option to select the output variable. Here you can select any variable or body variable. As I want data to be saved in body variable only, so I choose body variable.
To convert JSON sample to XML, our code is ready. So will deploy the code.
Select option Deploy to Service Bus Server and click on Next.
Select the configured Application Server i.e. Integrated Weblogic Server.
On Service Bus Console, navigate to your project, pipeline folder and double click on the pipeline. Click on Green arrow to test the pipeline.
elect Media Type as application/json. Enter payload which you want to convert from JSON to XML. Make sure you use the same JSON format which you have used at the time of creation on nXSD. Click on Execute.
The output will be as shown below.

Wednesday, October 23, 2019

Error-Codes Example in WSO2 ESB

<?xml version="1.0" encoding="UTF-8"?>
<sequence name="fault_endpoint" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
    <property name="FORCE_ERROR_ON_SOAP_FAULT" scope="default" type="STRING" value="true"/>
    <switch source="$ctx:ErrorCode" xmlns:ns="http://org.apache.synapse/xsd">
        <case regex="101000">
            <payloadFactory media-type="xml">
                <format>
                    <Fault>
                        <ERROR_CODE>101000</ERROR_CODE>
                        <ERROR_MESSAGE>EndPoint Connection Failure</ERROR_MESSAGE>
                        <ERROR_DETAIL>Receiver Error</ERROR_DETAIL>
                    </Fault>
                </format>
                <args/>
            </payloadFactory>
        </case>
        <case regex="101001">
            <payloadFactory media-type="xml">
                <format>
                    <Fault>
                        <ERROR_CODE>101001</ERROR_CODE>
                        <ERROR_MESSAGE>EndPoint Connection Failure</ERROR_MESSAGE>
                        <ERROR_DETAIL>Receiver Error</ERROR_DETAIL>
                    </Fault>
                </format>
                <args/>
            </payloadFactory>
        </case>
        <case regex="101500">
            <payloadFactory media-type="xml">
                <format>
                    <Fault>
                        <ERROR_CODE>101500</ERROR_CODE>
                        <ERROR_MESSAGE>EndPoint Connection Failure</ERROR_MESSAGE>
                        <ERROR_DETAIL>Sender Error</ERROR_DETAIL>
                    </Fault>
                </format>
                <args/>
            </payloadFactory>
        </case>
        <case regex="101501">
            <payloadFactory media-type="xml">
                <format>
                    <Fault>
                        <ERROR_CODE>101501</ERROR_CODE>
                        <ERROR_MESSAGE>EndPoint Connection Failure</ERROR_MESSAGE>
                        <ERROR_DETAIL>Sender Error</ERROR_DETAIL>
                    </Fault>
                </format>
                <args/>
            </payloadFactory>
        </case>
        <case regex="101503">
            <payloadFactory media-type="xml">
                <format>
                    <Fault>
                        <ERROR_CODE>101503</ERROR_CODE>
                        <ERROR_MESSAGE>Network Connection Failure</ERROR_MESSAGE>
                        <ERROR_DETAIL>Connection Failure</ERROR_DETAIL>
                    </Fault>
                </format>
                <args/>
            </payloadFactory>
        </case>
        <case regex="101504">
            <payloadFactory media-type="xml">
                <format>
                    <Fault>
                        <ERROR_CODE>101504</ERROR_CODE>
                        <ERROR_MESSAGE>Network Connection Failure</ERROR_MESSAGE>
                        <ERROR_DETAIL>Connection Timeout</ERROR_DETAIL>
                    </Fault>
                </format>
                <args/>
            </payloadFactory>
        </case>
        <case regex="101505">
            <payloadFactory media-type="xml">
                <format>
                    <Fault>
                        <ERROR_CODE>101505</ERROR_CODE>
                        <ERROR_MESSAGE>Network Connection Failure</ERROR_MESSAGE>
                        <ERROR_DETAIL>Connection Closed</ERROR_DETAIL>
                    </Fault>
                </format>
                <args/>
            </payloadFactory>
        </case>
        <case regex="101506">
            <payloadFactory media-type="xml">
                <format>
                    <Fault>
                        <ERROR_CODE>101506</ERROR_CODE>
                        <ERROR_MESSAGE>Network Connection Failure</ERROR_MESSAGE>
                        <ERROR_DETAIL>Protocol Violation</ERROR_DETAIL>
                    </Fault>
                </format>
                <args/>
            </payloadFactory>
        </case>
        <case regex="101507">
            <payloadFactory media-type="xml">
                <format>
                    <Fault>
                        <ERROR_CODE>101507</ERROR_CODE>
                        <ERROR_MESSAGE>Network Connection Failure</ERROR_MESSAGE>
                        <ERROR_DETAIL>Connection Cancelled</ERROR_DETAIL>
                    </Fault>
                </format>
                <args/>
            </payloadFactory>
        </case>
        <case regex="101508">
            <payloadFactory media-type="xml">
                <format>
                    <Fault>
                        <ERROR_CODE>101508</ERROR_CODE>
                        <ERROR_MESSAGE>Network Connection Failure</ERROR_MESSAGE>
                        <ERROR_DETAIL>Connection Timeout</ERROR_DETAIL>
                    </Fault>
                </format>
                <args/>
            </payloadFactory>
        </case>
        <case regex="101509">
            <payloadFactory media-type="xml">
                <format>
                    <Fault>
                        <ERROR_CODE>101509</ERROR_CODE>
                        <ERROR_MESSAGE>Network Connection Failure</ERROR_MESSAGE>
                        <ERROR_DETAIL>Send Abort</ERROR_DETAIL>
                    </Fault>
                </format>
                <args/>
            </payloadFactory>
        </case>
        <case regex="101510">
            <payloadFactory media-type="xml">
                <format>
                    <Fault>
                        <ERROR_CODE>101510</ERROR_CODE>
                        <ERROR_MESSAGE>Network Connection Failure</ERROR_MESSAGE>
                        <ERROR_DETAIL>Response Processing Error</ERROR_DETAIL>
                    </Fault>
                </format>
                <args/>
            </payloadFactory>
        </case>
        <case regex="303001">
            <payloadFactory media-type="xml">
                <format>
                    <Fault>
                        <ERROR_CODE>303001</ERROR_CODE>
                        <ERROR_MESSAGE>EndPoint Connection Failure</ERROR_MESSAGE>
                        <ERROR_DETAIL>Address Endpoint is not ready to connect</ERROR_DETAIL>
                    </Fault>
                </format>
                <args/>
            </payloadFactory>
        </case>
        <case regex="303001">
            <payloadFactory media-type="xml">
                <format>
                    <Fault>
                        <ERROR_CODE>303000</ERROR_CODE>
                        <ERROR_MESSAGE>EndPoint Connection Failure</ERROR_MESSAGE>
                        <ERROR_DETAIL>Load Balance endpoint is not ready to connect</ERROR_DETAIL>
                    </Fault>
                </format>
                <args/>
            </payloadFactory>
        </case>
        <case regex="303001">
            <payloadFactory media-type="xml">
                <format>
                    <Fault>
                        <ERROR_CODE>303002</ERROR_CODE>
                        <ERROR_MESSAGE>EndPoint Connection Failure</ERROR_MESSAGE>
                        <ERROR_DETAIL>Load Balance endpoint is not ready to connect</ERROR_DETAIL>
                    </Fault>
                </format>
                <args/>
            </payloadFactory>
        </case>
        <default>
            <payloadFactory media-type="xml">
                <format>
                    <Fault>
                        <ERROR_CODE>$1</ERROR_CODE>
                        <ERROR_MESSAGE>$2</ERROR_MESSAGE>
                        <ERROR_DETAIL>$3</ERROR_DETAIL>
                    </Fault>
                </format>
                <args>
                    <arg evaluator="xml" expression="get-property('ERROR_CODE')"/>
                    <arg evaluator="xml" expression="get-property('ERROR_MESSAGE')"/>
                    <arg evaluator="xml" expression="get-property('ERROR_DETAIL')"/>
                </args>
            </payloadFactory>
        </default>
    </switch>
    <header action="remove" name="To" scope="default"/>
    <property name="RESPONSE" scope="default" type="STRING" value="true"/>
</sequence>

iterate Mediator Example in WSO2 ESB

Method-1
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="count_iterate"
       transports="https,http"
       statistics="disable"
       trace="disable"
       startOnLoad="true">
   <target>
      <inSequence>
         <property name="it_count" value="0" scope="operation"/>
         <iterate expression="//symbols/symbol" sequential="true">
            <target>
               <sequence>
                  <property name="synapse_it_count" expression="get-property('operation', 'it_count')"/>
                  <script language="js">var cnt_str = mc.getProperty('synapse_it_count');
     var cnt = parseInt(cnt_str);
     cnt++;
     mc.setProperty('synapse_it_count', cnt.toString());</script>
                  <property name="it_count" expression="get-property('synapse_it_count')" scope="operation"/>
                  <aggregate>
                     <completeCondition>
                        <messageCount min="-1" max="-1"/>
                     </completeCondition>
                     <onComplete expression="//symbol">
                        <log level="custom">
                           <property name="number of symbols" expression="get-property('operation','it_count')"/>
                        </log>
                        <respond/>
                     </onComplete>
                  </aggregate>
               </sequence>
            </target>
         </iterate>
      </inSequence>
   </target>
   <description/>
</proxy>

Note:  
1.<property name="counter" scope="default" type="STRING" expression="fn:count($body/ticket/IctAttachments/item)"/>

2.<log>
  <property expression="$ctx:counter" name="counter"/>
</log>

Method-2
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="count_iterate"
       startOnLoad="true"
       statistics="disable"
       trace="disable"
       transports="https,http">
   <target>
      <inSequence>
         <property name="it_count" scope="operation" value="0"/>
         <iterate expression="//symbols/symbol" sequential="true">
            <target>
               <sequence>
                  <property expression="number(get-property('operation','it_count') +1)"
                            name="it_count"
                            scope="operation"/>
                  <aggregate>
                     <completeCondition>
                        <messageCount max="-1" min="-1"/>
                     </completeCondition>
                     <onComplete expression="//symbol">
                        <log level="custom">
                           <property expression="get-property('operation','it_count')"
                                     name="number of symbols"/>
                        </log>
                        <respond/>
                     </onComplete>
                  </aggregate>
               </sequence>
            </target>
         </iterate>
      </inSequence>
   </target>
   <description/>

</proxy>             


Input:
<symbols>
   <symbol>WSO2</symbol>
   <symbol>MSFT</symbol>
   <symbol>IBM</symbol>
   <symbol>ADBE</symbol>
   <symbol>AAPL</symbol>
   <symbol>ORCL</symbol>
   <symbol>RHT</symbol>
   <symbol>FB</symbol>
   <symbol>TWTR</symbol>
   <symbol>LNKD</symbol>
</symbols>

Friday, October 18, 2019

How to send error email in wso2_ESB-part1

<?xml version="1.0" encoding="UTF-8"?>
<template name="Error_Email_Notification" xmlns="http://ws.apache.org/ns/synapse">
    <sequence>
        <clone continueParent="true">
            <target>
                <sequence>
                    <property expression="get-property($url:key)" name="To" scope="transport" type="STRING" xmlns:ns="http://org.apache.synapse/xsd"/>
                    <property expression="get-property('SERVER_HOST')" name="ServerHost" scope="default" type="STRING" xmlns:ns="http://org.apache.synapse/xsd"/>
                    <property expression="fn:concat('[', $ctx:ServerHost ,']')" name="environment" scope="default" type="STRING" xmlns:ns="http://org.apache.synapse/xsd"/>
                    <property expression="fn:concat($ctx:environment, ' An Error Occurred Service : ',$ctx:proxy.name)" name="Subject" scope="transport" type="STRING" xmlns:ns="http://org.apache.synapse/xsd"/>
                    <property name="OUT_ONLY" scope="default" type="STRING" value="true"/>
                    <property name="transport.mail.Format" scope="axis2" type="STRING" value="Attachment"/>
                    <property name="AttachmentFile" scope="axis2" type="STRING" value="ErrorMessage.xml"/>
                    <property expression="$ctx:messageBody" name="transport.mail.bodyWhenAttached" scope="axis2" type="STRING" xmlns:ns="http://org.apache.synapse/xsd"/>
                    <header action="remove" name="To" scope="default"/>
                    <property action="remove" name="NO_ENTITY_BODY" scope="axis2"/>
                    <property name="RESPONSE" scope="default" type="STRING" value="true"/>
                    <property name="ContentType" scope="axis2" type="STRING" value="text/html"/>
                    <property name="messageType" scope="axis2" type="STRING" value="text/html"/>
                    <property name="FORCE_ERROR_ON_SOAP_FAULT" scope="default" type="STRING" value="true"/>
                    <payloadFactory media-type="xml">
                        <format>
                            <Fault>
                                <ERROR_CODE>$1</ERROR_CODE>
                                <ERROR_MESSAGE>$2</ERROR_MESSAGE>
                                <ERROR_DETAIL>$3</ERROR_DETAIL>
                            </Fault>
                        </format>
                        <args>
                            <arg evaluator="xml" expression="get-property('ERROR_CODE')" xmlns:ns="http://org.apache.synapse/xsd"/>
                            <arg evaluator="xml" expression="get-property('ERROR_MESSAGE')" xmlns:ns="http://org.apache.synapse/xsd"/>
                            <arg evaluator="xml" expression="get-property('ERROR_DETAIL')" xmlns:ns="http://org.apache.synapse/xsd"/>
                        </args>
                    </payloadFactory>
                    <send>
                        <endpoint>
                            <address uri="mailto:"/>
                        </endpoint>
                    </send>
                </sequence>
            </target>
        </clone>
    </sequence>
</template>

Note:TO_email-ids taking in local_entries. 

Database Adapter configuration in WebLogic Server

Oracle database adapter is a JCA connector, which is a DBAdapter.jar file deployed to the WebLogic server during the installation of FMW. The DB adapter enables the Oracle SOA Suite application (ex: a BPEL process) communicate to the Oracle database via a JNDI data source. The DB adapter relies on the JDBC driver to establish communication. The interaction between the SOA application to the Oracle database via the DB adapter is XML message. The DB adapter received XML message from SOA apps and response back XML message to SOA apps, so it is loosely modeled and it easily plugs into SOA application.

DB adapter can consist of multiple instances, Each instance (example: eis/DB/HR ) points to a single database and the instance must map with a JNDI data source to connect to Database.


In this section, we talk about how to create a JDBC data source and a DB adapter in Weblogic 12C. I have used  HR Schema comes with Oracle database 
 Pre-Requisite
Oracle WebLogic 12C
HR Schema (Bundles with Oracle database)

Create JDBC Datasource

 Login into  the WebLogic Server

Expand Services >> Click New >> Select Generic Data Source

Input a Name >>input a JDBC Name (format is jdbc/<SCHEMANAME>) >> Database type as Oracle >> Click Next
Note: Copy this JNDI name in a notepad file, we need this name to configure DB adapter

Click driver as Oracle >> Click Next
Click Next
Input the Database connection configuration >> Click Next

Click the button Test Configuration >> Verify the connection result

Enable the server >> Click Finish
Note: Please point the server where you want to have the data source available. You may enable datasource to AdminServer, Cluster, Managed servers based upon how WebLogic configured.
The JDBC Data Sources wizard will list the data source that we created above

Configure datasource in DBAdapter

 Click on the Deployments >> Click Next >> Click DbAdapter
Click Configuration >> Click Outbound Connection Tools >> Click New

Click Next

Input your instance name (eis/DB/HR), You will need this name in your SOA application to create DB Adapter

Click Ok
It will display the successful message >> Click Ok
Now click the Configuration >> Outbound Connection Pools >> Click the instance (eis/DB/HR) we created above
Click on the propertyValue against the XADataSourceName >> Input the JNDI name (jdbc/HR) >> Hit Enter button on your keyboard >> Click Save
Note: we highlighted the JNDi name in green colour above.

You will notice the successful message as shown below >> Click Save

 Click Deployments >> Click Next >> Enable the checkbox for DBADapter >> Click Update

Click the box ‘Update this application in place with new deployment plan changes. (A deployment plan must be specified for this option)’ >> Click Update
Note: If you are configuring the DB adapter in a clustered environment then copy the plan.xml file to the other machine before the update. The plan.xml file location is mention in the Deployment Plan path as shown in the picture below
Success message appears on the screen, Restart the WebLogic server for the changes take effect

That’s it. We have configured a database connection in DBAdapter. Now use this DB adapter information in your SOA application