Thursday, July 30, 2015

How to use Script Mediator in WSO2 ESB

WSO2 ESB provides the flexibility to use function of scripting language like JavaScript, Ruby or groovy.

This script mediator can be used in a separate file located in local registry or also can be written in embedded inline synapse configuration.

To support scripting language, synapse uses Bean Scripting Framework. To use scripting mediator we have to use a pre-defined mc variable. 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.

These are the list of Method names and its description to be used in script mediator.


Return Type
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.

Syntax:

To define the script mediator in synapse file you have put a single line in which you have to provide java script file and its key name. in below example we are calling a java script file which has the function to get employee details.

<localEntry key="EmpDetailsScript" src="file:repository/conf/emp/resources/scripts/EmpDetailsTransform.js"/>

Call Function from synapse:

To call the function we have write below code, in which we have to supply its key name and the function name which is to be called. In below code we are calling a function “getITEmployee” which will get get all IT employee details.

<script language="js" key="CallITEmployee" function=" getITEmployee"/>

Function in Java script:

We are using a payload method in which we are calling another request which will fetch the Employee details of a particular department.
  
function getITEmployee(mc) {
      var Department = mc.getProperty("Department");
mc.setPayloadXML(
      <emp:getEmployeeDetailRequest xmlns:emp="http://shriwithjava.blogspot.com/services/empdetails">
         <emp:Department>{Department}</emp:Department>
      </emp:getEmployeeDetailRequest>
        );
}


UI Configurations:

Below is the step to call script mediator from console UI. When you click on ‘script’ then below screen will appear.


In below screen you have to pass Script Type, Language Its function name and key of this java script.



Click on apply to use this mediator. Once you click on the save it will be added in service.



To see this is in coding format then click on the “Switch to design view”.
You can use normal other java script function as well to make your logic as required.

4 comments: