Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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

...

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.

Return?Method NameDescription
YesgetPayloadXML()This gets an XML representation of SOAP Body payload.
NosetPayloadXML(payload)This sets the SOAP body payload from XML.
NoaddHeader(mustUnderstand, content)This adds a new SOAP header to the message.
YesgetEnvelopeXML()This gets the XML representation of the complete SOAP envelope.
NosetTo(reference)This is used to set the value which specifies the receiver of the message.
YessetFaultTo(reference)This is used to set the value which specifies the receiver of the faults relating to the message.
NosetFrom(reference)This is used to set the value which specifies the sender of the message.
NosetReplyTo(reference)This is used to set the value which specifies the receiver of the replies to the message.
YesgetPayloadJSON()This gets the JSON representation of a SOAP Body payload.
NosetPayloadJSON(payload)This sets the JSON representation of a payload obtained via the getPayloadJSON() method and sets it in the current message context.
YesgetProperty(name)This gets a property from the current message context.
NosetProperty(key, value)This is used to set a property in the current message context. The previously set property values are replaced by this method.

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.

...

Table of Contents
maxLevel3
minLevel3
locationtop
styleborder:1
locationtop
typeflat
separatorpipe

...

Syntax

...

Localtabgroup
Localtab
activetrue
titleUsing 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
XML
XML
<script language="stringjs"><![CDATA[...script source code...]]><script/>
Localtab
titleUsing 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.

Info

When you define a JavaScript script as a Registry Resource via the Management Console, give application/javascript as the content/media type of the Registry Resource when you create it as shown below.

specify the correct media typeImage Added

If you are creating the Registry Resource via Tooling, you need not specify the content/media type, because it gets automatically applied when you select the JavaScript File Template as shown below.

select the JavaScript File TemplateImage Added

Code Block
languagexml
<script key="string" language="stringjs" [function="script-function-name"]>
    <include key="string"/>
</script>

...

Localtabgroup
Localtab
activetrue
titleInline

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.
Localtab
titleUsing a script of the registry

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 Registryand then selecting the relevant script from the resource tree.

Image Modified

Info
titleNote

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

...


...

Examples

Table of Contents
maxLevel4
minLevel4

...

Code Block
languagexml
  <script language="js"
                 key="conf:/repository/esb/transform.js"
                 function="transform"/>
Return?Method NameExample
YesgetPayloadXML()

The script invoked can be as follows.

Code Block
languagejs
// 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.

NosetPayloadXML(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.
NoaddHeader(mustUnderstand, Object content)

The script invoked can be as follows.

Code Block
languagejs
<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.

NogetEnvelopeXML()

The script invoked can be as follows.

Code Block
languagejs
<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
YesgetPayloadJSON()

The script invoked can be as follows.

Code Block
languagejs
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.

 


NosetPayloadJSON(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

YesgetProperty (name)

The script invoked can be as follows.

Code Block
languagejava
<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.

NosetProperty(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.

Samples

The following samples demonstrate how to use the Script mediator.

...