Friday, October 28, 2016

Smooks Data Mapping with WSO2 ESB

This post will contain detailed instructions on how to do a XML to XML transformation using WSO2 ESB 4.9.0. WSO2 Developer Studio 3.8.0 is used to create the project.  

Create Maven Multi Module Project

Create Maven Multi Module Project with following details. This is the parent project for all the other projects.
  • Group Id - com.wso2.training.SmooksDataMapper 
  • ArtifactID - SmooksDataMapperLab 



Create Registry Resource Project

  • To create a Registry Resources Project, go to File -> New -> Projects -> WSO2 -> Repository and select Registry Resources Project.
    • Name : SmooksRegistryProject
    • Group ID :com.wso2.training.SmooksRegistryProject
  • Create a new Registry Resource for Smooks Configuration. Select From Existing Template option and select the template as Smooks Configuration.
    • ResourceName : SmooksConfigXMLToXML
    • Template : Smooks Configuration
    • Registry : Conf
    • Registry Path :/system/config/smooks 

  • Now you will see smooks-configuration file has been created and it will be open in the embedded JBoss Smooks editor.

Create a Composite Application Project
  • Create CAR project
    • Name : SmooksDataMapperCAR
    • Group ID : com.wso2.training.SmooksDataMapperCAR
    • Tick specify Parent from Wokspace
  • Add following two files there under a folder.
model-input.xml

<order id='444'>
    <header>
           <customer number="555">Amila</customer>
    </header>
    <order-items>
       <order-item id='1'>
           <product>1</product>
           <quantity>2</quantity>
           <price>400</price>
           </order-item>
    </order-items>
</order>


model-output.xml

<salesorder>
 <details>
       <orderid></orderid>
       <customer>
           <id></id>
           <name></name>
       </customer>
    </details>
    <itemList>
       <item>
           <id></id>
           <productId></productId>
           <quantity></quantity>
           <price></price>
       </item>
    </itemList>
</salesorder>

 Define Smooks Configuration
  • Go to Smooks Configuration
    • Set Input type as xml
    • Add the input model with data(model-input.xml) by browsing the workspace
         That will automatically create a input model in input model view

  • Then click on the add button on Input Task and then click on apply template

  • set Message Type as XML and click on Next
  • Set “Create Template From:” as Sample XML & point the mapping model(model-output.xml) from workspace and  then click on finish.
 
  • This will dynamically generate the graphical model as follows.

  • Configure Mapping

You can do the mapping by just connecting relevant elements or attributes by clicking on one element on the input model and connecting it to the corresponding element in the output model as shown below. Ensure to do the mapping from top to bottom or else the configuration will fail at run time. So do the mapping exactly as shown below.


 Run Smooks Configuration
  • Add libraries from smooks framework. To add these libraries select the Registry Resources project and go to Project -> Properties.
  • Select Java Build Path and go to Libraries tab.
  • Click on Add Library button and you will see Add Library page which has WSO2 Classpath Libraries entry at the bottom.
  • Select WSO2 Classpath Libraries and click Next.
  • Now you will see a multi-tab page which includes most of the third party libraries that will be needed for your SOA development. To add smooks related libraries, go to Smooks tab.

  • Select all the libraries and click Finish.
  • Now you will see, all the smooks related libraries are added to project classpath.
  • Now you can run smooks configuration file by right click on the file (SmooksConfigXMLToXML.xml) and select Run As -> Smooks Run Configuration 
  • If your mapping was successful it will create the output as below.
 
 Create ESB Proxy Service
  •  Create a new ESB config Project
    • Name : SmooksTransESBProject
    • Group Id : com.wso2.training.SmooksTransESBProject
    • Select Specify Parent from Workspace
  • Create  new proxy service within that ESB config project
    • Proxy Service Name : SmooksTransProxy
    • Proxy Service Type : Custom Proxy 
  • Add mediators to the inSequence of the proxy as follows.
    • Log Meidator - Log Level : Full
    • Smooks Mediator -  Configuration Key : SmooksConfigXMLToXML.xml (Select by browsing the workspace)
    • Log Mediator - Log Level : Full    
    • Add respond Mediator
       
Deploying the CAR file
  • Go to the pom.xml of the Composite Application Project (SmooksDataMapperCAR)
  • Select SmooksRegistryProject and SmooksTransESBProject as the dependencies.
  • Right clieck on CAR project -> Export Composite Application and give a Export Destination
  • Then go to ESB management console and upload the CAR file.
Test the Service

Test using try it tool and give the input as follows.

<order id='764'>
    <header>
           <customer number="444">James</customer>
    </header>
    <order-items>
       <order-item id='7'>
           <product>6</product>
           <quantity>8</quantity>
           <price>543</price>
       </order-item>
    </order-items>
</order>


References : http://wso2.com/library/tutorials/2011/06/perform-data-mapping-smooks-editor-wso2-carbon-studio/

2 comments:

  1. Hi, I'm also followed the same step but JBoss Smooks editor is not opening for me. Can you please suggest me any plugin need to install in WSO2 Developer Studio?

    ReplyDelete