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.

  • public Object getPayloadXML() - getting the XML representation of SOAP Body payload
  • public void setPayloadXML(Object payload) - Set the SOAP body payload from XML
  • public void addHeader(boolean mustUnderstand, Object content) - Add a new SOAP header to the message.
  • public Object getEnvelopeXML() - Get the XML representation of the complete SOAP envelope
  • public void setTo(String reference)
  • public void setFaultTo(String reference)
  • public void setFrom(String reference)
  • public void setReplyTo(String reference)

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

  • Script mediator using a script off the registry

    <script key="string" language="string" [function="script-function-name"]/>
        

    The attribute key is the registry key to load the script. The language attribute specifies the scripting language of the script code (e.g. "js" for Javascript, "rb" for ruby, "groovy" for Groovy, "py" for Python..). The function is an optional attribute defining the name of the script function to invoke, if not specified it defaults to a function named 'mediate'. The function is passed a single parameter - which is the Synapse MessageContext. The function may return a boolean, if it does not, then true is assumed, and the script mediator returns this value.

  • Script mediator using a In-lined script

    <script language="string">...script source code...<script/>

UI Configuration

Script Type

  • Inline: Specify the script inline
  • Registry: Store the script in registry and refer it using the key

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/samples/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">
               <m:request>
                  <m:symbol>{symbol}</m:symbol>
               </m:request>
            </m:getQuote>);
    }