Sample 352: Accessing the Synapse MessageContext API Through Scripts

<definitions xmlns="http://ws.apache.org/ns/synapse"> <sequence name="main"> <in> <!-- change the MessageContext into a response and set a response payload --> <script language="js"> mc.setTo(mc.getReplyTo()); mc.setProperty("RESPONSE", "true"); mc.setPayloadXML( <ns:getQuoteResponse xmlns:ns="http://services.samples/xsd"> <ns:return> <ns:last>99.9</ns:last> </ns:return> </ns:getQuoteResponse>); </script> </in> <send/> </sequence> </definitions>

Objective

Demonstrate how to access various methods on the Synapse MessageContext API using the script mediator

Pre-requisites

  • Start Synapse using the configuration numbered 352 (repository/conf/sample/synapse_sample_352.xml)
    Unix/Linux: sh synapse.sh -sample 352
    Windows: synapse.bat -sample 352

Executing the Client

This example shows how an inline JavaScript can access the Synapse message context API to set its 'To' EPR and to set a custom property to mark it as a response. Execute the stock quote client, and you will receive the response '99.9' as the last sale price as per the above script.

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

Note that the symbol 'mc' is bound to the Synapse MessageContext object by the script mediator. Then the user can invoke various methods on the message context using common JavaScript syntax such as mc.getProperty('name') and mc.setTo('epr').

Back to Catalog