The Script Mediator is used to invoke the functions of a variety of scripting languages such as JavaScript, Groovy, or Ruby.
...
Return? | Method Name | Description |
---|
Yes | getPayloadXML() | This gets an XML representation of SOAP Body payload. |
No | setPayloadXML(payload) | This sets the SOAP body payload from XML. |
No | addHeader(mustUnderstand, content) | This adds a new SOAP header to the message. |
Yes | getEnvelopeXML() | This gets the XML representation of the complete SOAP envelope. |
No | setTo(reference) | This is used to set the value which specifies the receiver of the message. |
Yes | setFaultTo(reference) | This is used to set the value which specifies the receiver of the faults relating to the message. |
No | setFrom(reference) | This is used to set the value which specifies the sender of the message. |
No | setReplyTo(reference) | This is used to set the value which specifies the receiver of the replies to the message. |
Yes | getPayloadJSON() | This gets the JSON representation of a SOAP Body payload. |
No | setPayloadJSON(payload) | This sets the JSON representation of a payload obtained via the getPayloadJSON() method and sets it in the current message context. |
Yes | getProperty(name) | This gets a property from the current message context. |
No | setProperty(key, value) | This is used to set a property in the current message context. The previously set property values are replaced by this method. |
...
Localtabgroup |
---|
Localtab |
---|
active | true |
---|
title | Using an Inline script |
---|
| The following syntax applies when you create a Script mediator with the script program statements embedded inline within the Synapse configuration. Code Block |
---|
| <script language="string">><![CDATA[...script source code...<script]]><script/> |
|
Localtab |
---|
title | Using a script of the registry |
---|
| 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. Code Block |
---|
| <script key="string" language="string" [function="script-function-name"]>
<include key="string"/>
</script> |
|
|
...
Code Block |
---|
|
<script language="js">mc><![CDATA[mc.getPayloadXML()..symbol != "IBM";<script]]><script/> |
Example 2 - Using a script saved in the registry
...
Return? | Method Name | Example |
---|
Yes | getPayloadXML() | The script invoked can be as follows. Code Block |
---|
| // sample.js02.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>);
} |
mc.getPayloadXML() returns the response received in XML form.
|
No | setPayloadXML(payload) | See the example above for the getPayloadXML() method. mc.setPayloadXML( <m:getquote m="http://services.samples"> <m:request> <m:symbol>{symbol}</m:symbol</m:request></m:getquote> ) is used in that script to set the XML representation of the SOAP body (obtained using the getPayloadXML() method) to the current message context. |
No | addHeader(mustUnderstand, Object content) | The script invoked can be as follows. Code Block |
---|
| <script language="js">
var wsse = new Namespace('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd');
var envelope = mc.getEnvelopeXML();
var username = envelope..wsse::Username.toString();
var password = envelope..wsse::Password.toString();
mc.addHeader(false, <urn:AuthenticationInfo><urn:userName>{username}</urn:userName><urn:password>{password}</urn:password></urn:AuthenticationInfo>);
</script> |
The addHeader method configured as mc.addHeader(false, <urn:AuthenticationInfo><urn:userName>{username}</urn:userName><urn:password>{password}</urn:password></urn:AuthenticationInfo>) in the above script is used to extract user name and password values included in the request and add them to the header structure required for the backend service.
|
No | getEnvelopeXML() | The script invoked can be as follows. Code Block |
---|
| <script language="js">
var wsse = new Namespace('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd');
var envelope = mc.getEnvelopeXML();
var username = envelope..wsse::Username.toString();
var password = envelope..wsse::Password.toString();
mc.addHeader(false, <urn:AuthenticationInfo><urn:userName>{username}</urn:userName><urn:password>{password}</urn:password></urn:AuthenticationInfo>); </script> - See more at: http://sajithblogs.blogspot.com/2013/08/wso2-esb-adding-complex-soap-headers-to.html#sthash.jqpiEmf0.dpuf |
|
Yes | getPayloadJSON() | The script invoked can be as follows. Code Block |
---|
| function transform(mc) {
payload = mc.getPayloadJSON();
results = payload.results;
var response = new Array();
for (i = 0; i < results.length; ++i) {
location_object = results[i];
l = new Object();
l.name = location_object.name;
l.tags = location_object.types;
l.id = "ID:" + (location_object.id);
response[i] = l;
}
mc.setPayloadJSON(response);
} |
mc.getPayloadJSON() returns the JSON payload (received as the response) as a JavaScript object. This object can be manipulated as a normal JavaScript variable within a script as shown in the above JavaScript code. See JSON Support for further information about how this script is used.
|
No | setPayloadJSON(payload) | See the example script for the getPayloadJSON() method. The mc.setPayloadJSON() method can be used to replace the existing payload with a new payload. In the above script, we build a new array object by using the fields of the incoming JSON payload and set that array object as the new payload. See JSON Support for further information about how this script is used |
Yes | getProperty (name) | The script invoked can be as follows. Code Block |
---|
| <script language="js">
var time1 = mc.getProperty("TIME_1");
var time2 = mc.getProperty("TIME_2");
var timeTaken = time2 - time1;
print("Time Duration : " + timeTaken + " ms ");
mc.setProperty("RESPONSE_TIME", timeTaken);
</script> |
In this example, the getProperty method is used to get two time durations. The difference between the two time durations is calculated and the setProperty method is used to set this difference in the message context. |
No | setProperty(property) | See the example for the getProperty method. The setProperty method is used to set the response time calculated from the time durations obtained (using the getProperty method) in the message context. |
...