Friday, June 13, 2014

WSO2 ESB - JSON to SOAP (XML) transformation using Script sample


  • Reqired SOAP request as generated using SoapUI
 <?xml version="1.0" encoding="utf-8"?>  
 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">  
 <soapenv:Body>  
 <m:newOrder xmlns:m="http://services.com">  
 <m:customerName>WSO2</m:customerName>  
 <m:customerEmail>customer@wso2.com</m:customerEmail>  
 <m:quantity>100</m:quantity>  
 <m:recipe>check</m:recipe>  
 <m:resetFlag>true</m:resetFlag>  
 </m:newOrder>  
 </soapenv:Body>  
 </soapenv:Envelope>  

  • The object that I used to test on Advanced REST Client[2]:

POST request
Content-Type   : application/json
Payload           : {"newOrder": { "request": {"customerName":"WSO2", "customerEmail":"customer@wso2.com", "quantity":"100", "recipe":"check", "resetFlag":"true"}}}


  • Proxy configuration

 <?xml version="1.0" encoding="UTF-8"?>  
 <proxy xmlns="http://ws.apache.org/ns/synapse"  
 name="IntelProxy"  
 transports="https,http"  
 statistics="disable"  
 trace="disable"  
 startOnLoad="true">  
 <target>   
 <inSequence>  
 <script language="js"><![CDATA[  
 var customerName = mc.getPayloadXML()..*::customerName.toString();  
 var customerEmail = mc.getPayloadXML()..*::customerEmail.toString();  
 var quantity = mc.getPayloadXML()..*::quantity.toString();  
 var recipe = mc.getPayloadXML()..*::recipe.toString();  
 var resetFlag = mc.getPayloadXML()..*::resetFlag.toString();  
 mc.setPayloadXML(  
 <m:newOrder xmlns:m="http://services.com">  
 <m:request>  
 <m:customerName>{customerName}</m:customerName>  
 <m:customerEmail>{customerEmail}</m:customerEmail>  
 <m:quantity>{quantity}</m:quantity>  
 <m:recipe>{recipe}</m:recipe>  
 <m:resetFlag>{resetFlag}</m:resetFlag>  
 </m:request>  
 </m:newOrder>);  
 ]]></script>  
 <header name="Action" value="urn:newOrder"/>  
 <log level="full"/>  
 </inSequence>  
 <outSequence>  
 <log level="full"/>  
 <property name="messageType" value="application/json" scope="axis2"/>  
 <send/>  
 </outSequence>  
 <endpoint>  
 <address uri="http://localhost/services/BusinessService/" format="soap11"/>  
 </endpoint>  
 </target>  
 <description/>  
 </proxy> 


Referance

[1] https://docs.wso2.org/display/ESB481/Sample+441%3A+Converting+JSON+to+XML+Using+JavaScript

[2] https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo

No comments:

Post a Comment