Synapse supports Mediators implemented in a variety of scripting languages such as JavaScript, Python or Ruby. There are two ways of defining the Script Mediators:
Note |
---|
WSO2 ESB uses Rhino engine to execute JavaScripts. Rhino engine converts the script to a method inside a Java class. Thereby, when processing large JSON data volumes the code length must be less than 65536 characters since the Script mediator converts the payload into a Java object. However, you can use the following alternative options to process large JSON data volumes.
|
1. With the script program statements stored in a separate file, referenced via the Local or Remote Registry entry.
...
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.
...
Table of Contents | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
...
Syntax
Script Mediator Using a Script of the Registry
Code Block | ||||
---|---|---|---|---|
| ||||
<script key="string" language="string" [function="script-function-name"]/>
|
...
Script mediator using a In-lined script
Code Block | ||||
---|---|---|---|---|
| ||||
<script language="string">...script source code...<script/>
|
...
Example
1.
Code Block | ||||
---|---|---|---|---|
| ||||
<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
.
2.
Code Block | ||||
---|---|---|---|---|
| ||||
<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
. An example of test.js is shown bellow:
3.
Code Block |
---|
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>);
}
|
Excerpt | ||
---|---|---|
| ||
Description of the Script Mediator in WSO2 ESB. |