Sample 351: Inline Scripts with the Script Mediator

<definitions xmlns="http://ws.apache.org/ns/synapse"> <sequence name="main"> <in> <!-- transform the custom quote request into a standard quote requst expected by the service --> <script language="js"> 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>); </script> <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"> var symbol = mc.getPayloadXML()..*::symbol.toString(); var price = mc.getPayloadXML()..*::last.toString(); mc.setPayloadXML( <m:CheckPriceResponse xmlns:m="http://services.samples/xsd"> <m:Code>{symbol}</m:Code> <m:Price>{price}</m:Price> </m:CheckPriceResponse>); </script> <send/> </out> </sequence> </definitions>

Objective

Sample 350 shows how to use scripts stored as external resources for mediation. This sample demonstrates how small scriplets can be specified inline with the Synapse configuration thus avoiding the requirement to have an external registry.

Pre-requisites

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

Executing the Client

The functionality and the behavior of this sample is identical to sample 350. Only difference is that, the 2 JS functions are embedded in the Synapse configuration. To try this out run the following command on the sample client.

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

Back to Catalog