This documentation is for WSO2 Enterprise Service Bus version 4.9.0 . View documentation for the latest release.

All docs This doc
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

The Script Mediator is used to invoke the functions of a variety of scripting languages such as JavaScript, Groovy, or Ruby.

A Script mediator can be created in one of the following methods.

  • With the script program statements stored in a separate file, referenced via the Local or Remote Registry entry.
  • With the script program statements embedded inline within the Synapse configuration.

Synapse uses the Apache Bean Scripting Framework for scripting language support. Any script language supported by BSF may be used to implement the Synapse Mediator. With the Script Mediator, you can invoke a function in the corresponding script. With these functions, it is possible to access the Synapse predefined in a script variable named mc. The mc variable represents an implementation of the MessageContext, named ScriptMessageContext.java, which contains the following methods that can be accessed within the script as mc.methodName.

  • public Object getPayloadXML() - Gets the XML representation of SOAP Body payload.
  • public void setPayloadXML(Object payload) - Sets the SOAP body payload from XML.
  • public void addHeader(boolean mustUnderstand, Object content) - Adds a new SOAP header to the message.
  • public Object getEnvelopeXML() - Gets 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 has advantages over using the built-in Synapse Mediator types or implementing a custom Java class Mediator. The Script Mediators have the flexibility of a class Mediator with access to the Synapse MessageContext and SynapseEnvironment APIs. Also, the ease of use and dynamic nature of scripting languages allow  the 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, which make them very usable in a Synapse mediation environment. e.g., JavaScript E4X or Ruby REXML.

For both types of script mediator definitions, the MessageContext passed into the script has additional methods over the standard Synapse MessageContext to enable working with XML natural to the scripting language. Example are when using JavaScript getPayloadXML and setPayloadXML, E4X XML objects and when using Ruby, REXML documents.



Syntax

Click on the relevant tab to view the syntax for a script mediator using an Inline script, or a script mediator using a script of a registry

The following syntax applies when you create a Script mediator with the script program statements embedded inline within the Synapse configuration.

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

The following syntax applies when you create a Script mediator with the script program statements stored in a separate file, referenced via the Local or Remote Registry entry.

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

UI Configuration

Click on the relevant tab to view the required UI configuration depending on the script type you have selected. The available script types are as follows:

  • Inline: If this script type is selected, the script is specified inline.
  • Registry: If this script type is selected, a script which is already saved in the registry will be referred using a key.

The parameters available to configure a Script mediator using an inline script are as follows.

Parameter NameDescription
Language

The scripting language for the Script mediator. You can select from the following available languages.

  • JavaScript - This is represented as js in the source view.
  • Groovy - This is represented as groovy in the source view.
  • Ruby - This is represented as rb in the source view.
 Source Enter the source in this parameter.

The parameters available to configure a Script mediator using a script saved in the registry are as follows.

Parameter NameDescription
Language

The scripting language for the Script mediator. You can select from the following available languages.

  • JavaScript - This is represented as js in the source view.
  • Groovy - This is represented as groovy in the source view.
  • Ruby - This is represented as rb in the source view.
FunctionThe function of the selected script language to be invoked. This is an optional parameter. If no value is specified, a default function named mediate will be applied. This function considers the Synapse MessageContext as a single parameter. The function may return a boolean. If it does not, then the value true is assumed and the Script mediator returns this value.
Key Type

You can select one of the following options.

  • Static Key: If this is selected, an existing key can be selected from the registry for the Key parameter.
  • Dynamic Key: If this is selected, the key can be entered dynamically in the Key parameter.
KeyThe Registry location of the source. You can click either Configuration Registry or the Governance Registry to select the source from the resource tree.
Include keys

This parameter allows you to include functions defined in two or more scripts your Script mediator configuration. After pointing to one script in the Key parameter, you can click Add Include Key to add the function in another script.

When you click Add Include  Key, the following parameters will be displayed. Enter the script to be included in the Key parameter by clicking either Configuration Registry or the Governance Registry and then selecting the relevant script from the resource tree.

Note

You can configure the mediator using XML. Click switch to source view in the Mediator window.

 


Examples

Example 1 - Using an inline script

The following 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.

<script language="js">mc.getPayloadXML()..symbol != "IBM";<script/>

Example 2 - Using a script saved in the registry

In the following example, script is loaded from the registry by using the key repository/conf/sample/resources/script/test.js

<script language="js"
    key="repository/conf/sample/resources/script/test.js"
    function="testFunction"/>

script language="js" indicates that the function invoked should be in the JavaScript language. The function named testFunction which is invoked should be saved as a resource in the Registry. The script can be as shown in the example below.

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>);
}

Example 3 - Adding an Include key

The following configuration has an include key.

<script language="js" key="stockquoteScript" function="transformRequest">
	<include key="sampleScript"/>
</script>

The script is written in JavaScript. The function to be executed is transformRequest. This function may be as follows in a script saved in the Registry.

// stockquoteTransform.js
function transformRequest(mc) {
transformRequestFunction(mc);
}

function transformResponse(mc) {
transformResponseFunction(mc);
}

In addition, the function in the script named sampleScript  which is included in the mediation configuration via the include key sub element is also executed in the mediation. Note that in order to do this,  sampleScript script should also be saved as a resource in the Registry. This script can be as follows.

// sample.js
function transformRequestFunction(mc) {
var symbol = mc.getPayloadXML()..*::Code.toString();
mc.setPayloadXML(
	<m:getquote m="http://services.samples">
		<m:request>
			<m:symbol>{symbol}</m:symbol>
		</m:request>
	</m:getquote>);
}
 
function transformResponse(mc) {
var symbol = mc.getPayloadXML()..*::symbol.toString();
var price = mc.getPayloadXML()..*::last.toString();
mc.setPayloadXML(
	<m:checkpriceresponse m="http://services.samples/xsd">
		<m:code>{symbol}</m:code>
		<m:price>{price}</m:price>
	</m:checkpriceresponse>);
}

Samples

The following samples demonstrate how to use the Script mediator.

See also Sample 441: Converting JSON to XML Using JavaScript

  • No labels