Sample 350: Introduction to the Script Mediator using JavaScript

<definitions xmlns="http://ws.apache.org/ns/synapse"> <registry provider="org.apache.synapse.registry.url.SimpleURLRegistry"> <!-- the root property of the simple URL registry helps resolve a resource URL as root + key --> <parameter name="root">file:repository/conf/sample/resources/</parameter> <!-- all resources loaded from the URL registry would be cached for this number of milli seconds --> <parameter name="cachableDuration">15000</parameter> </registry> <localEntry key="stockquoteScript" src="file:repository/conf/sample/resources/script/stockquoteTransformRequest.js"/> <sequence name="main"> <in> <!-- transform the custom quote request into a standard quote request expected by the service --> <script language="js" key="stockquoteScript" function="transformRequest"/> <send> <endpoint> <address uri="http://localhost:9000/services/SimpleStockQuoteService"/> </endpoint> </send> </in> <out> <!-- transform the standard response back into the custom format the client expects --> <script language="js" key="script/stockquoteTransformResponse.js" function="transformResponse"/> <send/> </out> </sequence> </definitions>

The JavaScript resource file referenced by the configuration looks like this.

<x><![CDATA[ function transformRequest(mc) { var symbol = mc.getPayloadXML()..*::Code.toString(); mc.setPayloadXML( <m:getQuote xmlns:m="http://services.samples"> <m:request> <m:symbol>{symbol}</m:symbol> </m:request> </m:getQuote>); } function transformResponse(mc) { var symbol = mc.getPayloadXML()..*::symbol.toString(); var price = mc.getPayloadXML()..*::last.toString(); mc.setPayloadXML( <m:CheckPriceResponse xmlns:m="http://www.apache-synapse.org/test"> <m:Code>{symbol}</m:Code> <m:Price>{price}</m:Price> </m:CheckPriceResponse>); } ]]></x>

Objective

Showcase the ability to configure the Synapse runtime using common scripting languages such as JavaScript

Pre-requisites

  • Deploy the SimpleStockQuoteService in the sample Axis2 server and start Axis2
  • Start Synapse using the configuration numbered 350 (repository/conf/sample/synapse_sample_350.xml)
    Unix/Linux: sh synapse.sh -sample 350
    Windows: synapse.bat -sample 350

Executing the Client

This sample is similar to sample 8 but instead of using XSLT, the transformation is done using JavaScript and E4X. Note that the script source is loaded from a resource in the file system which must be wrapped in CDATA tags within an XML element. The script used in this example has two functions, 'transformRequest' and 'transformResponse'. The Synapse configuration uses the 'function' attribute to specify which function should be invoked. Use the stock quote client to send a custom quote request as follows.

ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dmode=customquote

Synapse uses the script mediator and the specified JavaScript function to convert the custom request to a standard quote request. Subsequently the response received is transformed and sent back to the client.

Back to Catalog