com.atlassian.confluence.content.render.xhtml.migration.exceptions.UnknownMacroMigrationException: The macro 'next_previous_link3' is unknown.

Sample 441: Converting JSON to XML Using JavaScript

Objective: Demonstrate the ability to expose a SOAP service over JSON by switching between JSON and XML/SOAP message formats using JavaScript and the Script mediator.

Sample 441 configuration
<definitions xmlns="http://ws.apache.org/ns/synapse">
    <proxy name="JSONProxy" transports="http https">
        <target>
            <endpoint>
                <address uri="http://localhost:9000/services/SimpleStockQuoteService" format="soap11"/>
            </endpoint>
            <inSequence>
                <!-- transform the custom quote request into a standard quote requst expected by the service -->
                <script language="js"><![CDATA[
                    var symbol = mc.getPayloadJSON().symbol.toString();
                    mc.setPayloadXML(
                        <m:getQuote xmlns:m="http://services.samples">
                            <m:request>
                                <m:symbol>{symbol}</m:symbol>
                            </m:request>
                        </m:getQuote>);
                ]]></script>
                <header name="Action" value="urn:getQuote"/>
            </inSequence>
            <outSequence>
                <script language="js"><![CDATA[
                    var symbol = mc.getPayloadXML()..*::symbol.toString();
                    var price = parseFloat(mc.getPayloadXML()..*::last);
                    mc.setPayloadJSON(
	                    {
                            "Quote" : {
                                "Code" : symbol,
                                "Price" : price,
                                "Current" : {
                                    "High" : parseFloat(mc.getPayloadXML()..*::high),
                                    "Last" : parseFloat(mc.getPayloadXML()..*::last)
                                } 
                            },
                            "Status" : (price >= 100 ? "OK" : "NOT-OK")
                 });]]></script>
                <property name="messageType" scope="axis2" value="application/json"/>
                <send/>
            </outSequence>
        </target>
    </proxy>
</definitions>

The JavaScript specified by the Script mediator found in the inSequence performs the JSON to SOAP (XML) transformation as the SimpleStockQuoteService endpoint accepts SOAP. Similarly, the Script mediator in the outSequece performs the SOAP to JSON transformation.

1. Deploy the SimpleStockQuoteService in sample Axis2 server and start it on port 9000.

2. Start WSO2 ESB with the sample configuration 441 (i.e., wso2esb-samples -sn 441).

Invoke the above proxy service with following request:

curl -v -X POST \
    -H "Content-Type:application/json" \
    -d '{"symbol":"WSO2", "ID":"StockQuote"}' \
    "http://localhost:8280/services/JSONProxy"

If the invocation is successful, you will see a response similar to the following:

{
   "Quote":{
      "Current":{
         "High":-61.53602401623976,
         "Last":62.32682938796667
      },
      "Code":"WSO2",
      "Price":62.32682938796667
   },
   "Status":"NOT-OK"
}

For more information about using JSON with the ESB, see JSON Support. To see an example of converting JSON to XML using XSLT instead of JavaScript, see Sample 440: Converting JSON to XML Using XSLT. For additional examples of the Script mediator, see Using Scripts in Mediation (Script Mediator).

 

com.atlassian.confluence.content.render.xhtml.migration.exceptions.UnknownMacroMigrationException: The macro 'next_previous_links2' is unknown.