Sample 908: Introduction to the Script Mediator Using Nashorn Support
Note that WSO2 EI is shipped with the following changes to what is mentioned in this documentation:
<PRODUCT_HOME>/
repository/samples/
directory that includes all Integration profile samples is changed to<EI_HOME>/
samples/service-bus/
.<PRODUCT_HOME>/
repository/samples/resources/
directory that includes all artifacts related to the Integration profile samples is changed to<EI_HOME>/
samples/service-bus/resources/
.
Introduction
This sample demonstrates how to use scripts in mediation using the Nashorn support of the Script mediator. The Script Mediator is an extension of the ESB profile.
Prerequisites
Follow the steps below before starting these sample configurations.
- Start WSO2 EI with the sample 908 configuration. For instructions on starting a sample configuration, see Starting a sample configuration.
The operation log keeps running until the server starts, which usually takes several seconds. Wait until the server has fully booted up and displays a message similar to "WSO2 Carbon started in n seconds." - Start the Axis2 server. For instructions on starting the Axis2 server, see Starting the Axis2 server.
Deploy the back-end service SimpleStockQuoteService. For instructions on deploying sample back-end services, see Deploying sample back-end services.
Now you have a running WSO2 EI instance and a back-end service deployed.
Building the Sample
The XML configuration for this sample, which is available in the <EI_HOME>/samples/service-bus/synapse_sample_908.xml
file is as follows:
<!-- Introduction to the script mediator using nashornJs scripts --> <definitions xmlns="http://ws.apache.org/ns/synapse"> <localEntry key="stockquoteScript" src="file:samples/service-bus/resources/script/stockquoteTransformNashorn.js"/> <sequence name="main"> <in> <!-- transform the custom quote request into a standard quote request expected by the service --> <script language="nashornJs" key="stockquoteScript" function="transformRequest"/> <send> <endpoint> <address uri="http://localhost:9000/services/SimpleStockQuoteService"/> </endpoint> </send> </in> <out> <!-- transform the standard response back into the custom format the client expects --> <script language="nashornJs" key="stockquoteScript" function="transformResponse"/> <send/> </out> </sequence> </definitions>
The content of <EI_HOME>/samples/service-bus/resources/script/stockquoteTransformNashorn.js
, which is referred from the above synapse_sample_908.xml
file is as follows:
function transformRequest(mc) { var symbol = "IBM"; mc.setPayloadXML( "<m:getQuote xmlns:m=\"http://services.samples\">" + "<m:request>" + "<m:symbol>"+ symbol +"</m:symbol>" + "</m:request>" + "</m:getQuote>"); } function transformResponse(mc) { var payload = mc.getPayloadXML(); var expression = "//*[local-name()='symbol']"; var xpath = mc.getXpathResult(expression); var nodeList = xpath.selectNodes(payload); var symbol = nodeList.get(0).getText(); expression = "//*[local-name()='last']"; xpath = mc.getXpathResult(expression); nodeList = xpath.selectNodes(payload); var price = nodeList.get(0).getText(); mc.setPayloadXML( "<m:CheckPriceResponse xmlns:m=\"http://services.samples/xsd\">" + "<m:Code>"+ symbol +"</m:Code>" + "<m:Price>"+ price +"</m:Price>" + "</m:CheckPriceResponse>"); }
Navigate to the <EI_HOME>/samples/service-bus/synapse_sample_908.xml
file and execute the ant
command to build this sample.
Executing the Sample
Navigate to the <EI_HOME>/samples/axis2Client
directory and execute the following command to use the stock quote client to issue a custom quote as follows.
ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dmode=customquote
For further details on this sample client and its operation modes, see Stock Quote Client.
Analyzing the output
This sample is similar to Sample 350, but this uses Nashorn JavaScript and XPath Queries to do the transformation. The stockquoteTransformNashorn.js
script used this sample has two functions, transformRequest()
and transformResponse().
The Synapse configuration defined in the synapse_sample_908.xml
file uses the function attribute to specify which function should be invoked.
transformRequest()
JavaScript function to convert the custom request to a standard quote request. Subsequently, the received response is transformed and sent back to the client.