Friday, October 25, 2019

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




Monday, October 7, 2019

SOA Interview Questions and Answers(Fault handling)

1.Standard faults in BPEL?

Ans: Binding faults
        Selection failure
        Remote faults
        Invalid reply
        join failure

2.How to get fault trace in catch all block?

Ans: By using get fault As String () function.

3.Can we have custom faults in Bpel ?

Ans: yes, we can create custom faults in Bpel,those faults are usually called Business faults and we usually handle it by using specific catch block.


4. Can we re-throw fault from catch block?

Ans: Yes, we can re-throw fault in catch block using re-throw activity.

5. where we keep fault-policies.xml or fault.binding.xml files ?

Ans:We can Keep these files either local to project or in MDS  and in both the cases we have to specify the path in "composite.xml file "

Fault management form-work: Two xml files.

a) fault -policies.xml
b)fault -binding.xml

-Scope level we are applying catch and catching block.
catch all-Can handle any kind of exception.
Catch-Supports particular exception.



Sunday, September 29, 2019

SOA Interview Questions and Answers part-1

1.What is SOA?

Ans: SOA is an architecture for building applications using reusable, interoperable services which have well defined business functionalities and can be orchestrated to achieve a specific functionality by utilizing them together.


2.What is Web Service ?


Ans:Web service is a piece of code which is available on web (internet). That code of piece can be developed in any language (java, .net etc). A client invokes the web service by sending xml message and it wait for xml response (synchronously or asynchronously)



3.What is WSDL ?


Ans : WSDL stands for Web Services Description Language
WSDL is a document written in XML. The document describes a Web service. It specifies the location of the service and the operations (or methods) the service exposes.


4.Explain elements/tags of WSDL?

Ans:Types, messages, port type, Binding, Services.
  a) Types:      Pointing to XSD
  b)messages: Based on operation, we are creating multiple message,each                            message pointing to one element in XSD. Syn-operation two                              message,a syn-operation we have to create two-message,one                            way operation create one message
  c)Port type: it means define the list of operations
  d)Binding  :Its contains style of communication(RPC,Document)
  e)Services :Its  contains communication address in the URL 

5. Difference between Abstract and Concrete WSDL ?

Ans : Abstract WSDL contains only messages and operations. Abstract WSDL is 
used by SOAP Server. 
Where as concrete WSDL contains messages, operations and transport 
specific information (JMS or Http). This is used by SOAP client. 

6.Difference between Include and Import in context to XML schema ?

Ans: 

Include:If two files(xsd) Target namespace same is called 'include'
Import:if two files (xsd) target namespace is different, have to use import.

7.What is the difference between 

Ans:Sequence- flowing the order all elements in XSD
       Choice-only one element in XSD
       All:all elements in XSD,don't ' flowing order
8.What is the difference between Complex type and simple type ?

Complex: 1. To declare child elements
               2. To do reuse-ability  

In reuse-ability scenario, We are providing has to complex type.

Simple type: We are using to apply "restriction on element "
                 - length,list of values,Range of values,Pattern.
9.In WSDL can we have multiple port types?
Ans: Yes

10.In WSDL Can we have multiple bindings?

Ans: Yes

11.By Seeing WSDL, can we justify syn or asyn  or one way operation ?

Ans: Yes, we can justify, syn- operation contain two messages, one input message and one out message.

One way operation contains only one operation.

The structure asyn-operation different, it contains two port types, each port type one contains only one operation,each operation contains only one input message.

In Bpel my syn-flow,start with "receive and end with reply".  
one way -flow start with" receive and there is no reply".
asyn-flow start with "receive and end with invoke".(Call back handler)

12.What is the difference between receive and pick activity?

Ans:If the expose service contains single operation, we can go with receive.
        if the expose service contains multiple operation, we have to use pick activity.

13.What is the difference between  expose service and external reference ?

Ans:Expose service, we are creating based on "XSD"
       External reference we are creating based  on service URL.

14.What is the difference  between partner role and my role in partner link?

Ans:my role we are using while exposing the service
       partner role we are selective for external reference.

15. What is the difference between Assign and Transformation ?

Ans:Assign support one to one mapping, its internally uses X path to transform data .

Transform  support one to one mapping and one to many mapping, many to many mapping , many to one mapping ,it internal uses XSLT to transform data.


X-path:X-path is the Query language to process my XML data.

16.How can we declare collection in Schema?

Ans:We have to use minoccures & Max-occurs to declare collections in Schema.If dont  provide any values to minoccures & Max-occurs by default,it taking "1 and 1".

17.We want do parallel processing of data, what is the activity, we have to use?

Ans:We have to use flow activity.

Single Thread Model:

It set this property in composite.xml.it set true.

<property name : "single thread model" value ="true"/> 

Advantage  of this, it allows one thread to process.


18.What is the difference between compensate and compensate scope?

Ans:If we use compensate we will revert all scopes executed till that point.
       if we use compensate scope we can revert particular scope   



19.What is the difference between  flow and flowN?

Ans:flow activity used to perform one or more activities parallel.we have to specify the parallel flows in design time.
flow N activity used to generate and execute number of parallel flows dynamically at run time,based on the N values. N values logically decides based on the input data.

20.Types of rejection message handlers ?

Ans:a.Web service handler
       b.Custom java handler
       c.JMS Queue
       d.file