WSO2 ESB - Script Mediator

Script Mediator

Synapse supports Mediators implemented in a variety of scripting languages such as JavaScript, Python or Ruby. There are two ways of defining script mediators, either with the script program statements stored in a separate file which is referenced via the local or remote registry entry, or with the script program statements embedded in-line within the Synapse configuration. Synapse uses the Apache Bean Scripting Framework for the scripting language support, any script language supported by BSF may be used to implement a Synapse Mediator. With the script mediator, you can invoke a function in the corresponding script. With in these functions, it is possible to access the Synapse predefined in a script variable named 'mc’. This ‘mc’ represents an implementation of the MessageContext, named ScriptMessageContext.java. That contains following additional methods that can be accessed within the script by mc.methodName.

Implementing a Mediator with a script language can have advantages over using the built in Synapse Mediator types or implementing a custom Java class Mediator. Script Mediators have all the flexibility of a class Mediator with access to the Synapse MessageContext and SynapseEnvironment APIs, and the ease of use and dynamic nature of scripting languages allows rapid development and prototyping of custom mediators. An additional benefit of some scripting languages is that they have very simple and elegant XML manipulation capabilities, for example JavaScript E4X or Ruby REXML, so this makes them well suited for use in the Synapse mediation environment. For both types of script mediator definition the MessageContext passed into the script has additional methods over the standard Synapse MessageContext to enable working with the XML in a way natural to the scripting language. For example when using JavaScript getPayloadXML and setPayloadXML, E4X XML objects, and when using Ruby, REXML documents.

Syntax

UI Configuration

Script Type

If Inline Selected

Script Mediator with Inlinded Source

Language

Choose from variety of scripting languages supported

Source

If inline selected as the Script type, specify the source

If Registry Selected

Script Mediator with Registry Source

Function

Function of the script language to execute

Key

Registry location of the source

Include Keys

Scripts sources to be included

Example

  1. <script language="js">mc.getPayloadXML()..symbol != "IBM";<script/>
    The above configuration is an example of an inline mediator using JavaScript/E4X which returns false if the SOAP message body contains an element named 'symbol' which has a value of 'IBM' would be:
  2. <script language="js"
        key="repository/conf/sample/resources/script/test.js"
        function="testFunction"/>
        
    In the above example, script is loaded from the registry by using the key repository/conf/sample/resources/script/test.js. The script is written in java script. The function to be invoked is testFunction . As example for test.js shown in the bellow.
    function testFunction(mc) {
         var symbol = mc.getPayloadXML()..*::Code.toString();
         mc.setPayloadXML(
            <m:getQuote xmlns:m="http://services.samples/xsd">
               <m:request>
                  <m:symbol>{symbol}</m:symbol>
               </m:request>
            </m:getQuote>);
    }