Apache Synapse

##### Main Menu

- Home
- Download
- History
- License
- Thanks
- Sponsorship
- Security

##### Documentation

- Installation Guide
- Quick Start Guide
- Samples Setup Guide
- Samples Catalog
- Configuration Language
- Mediators Catalog
- Transports Catalog
- Extending Synapse
- Upgrading
- Deployment
- Javadocs
- FAQ

##### Developer Resources

- Developer Guide
- Development Best Practices
- Release Process

##### Project Details

- Overview
- Mailing Lists
- Source Repository
- Issue Tracking
- Dependencies
- Project Team
- Project Summary

##### Previous Releases

- Version 1.2
- Version 1.1.1
- Version 1.1
- Version 1.0

## 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