This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.
Sample 350: Introduction to the Script Mediator Using JavaScript
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/
.
Objective: Demonstrate how to use scripts in mediation using the Script mediator
The ESB Script Mediator is a ESB extension, and thus all prerequisites are not bundled by default with the ESB distribution. Before you use some scripts, you may need to manually add the required JAR files to the ESB lib
directory, and optionally perform other installation tasks as may be required by the individual scripting language. This is explained in the Samples Setup guide.
Prerequisites:
- Start the Synapse configuration numbered 350: i.e.
wso2esb-samples.sh -sn 350
- Start the Axis2 server and deploy the
SimpleStockQuoteService
if not already done.
<definitions xmlns="http://ws.apache.org/ns/synapse"> <localEntry key="stockquoteScript" src="file:repository/samples/resources/script/stockquoteTransform.js"/> <sequence name="main"> <in> <!-- transform the custom quote request into a standard quote request expected by the service --> <script language="js" 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="js" key="stockquoteScript" function="transformResponse"/> <send/> </out> </sequence> </definitions>
stockquoteTransform.js
is as follows:
function transformRequest(mc) { var symbol = mc.getPayloadXML()..*::Code.toString(); mc.setPayloadXML( <m:getQuote xmlns:m="http://services.samples"> <m:request> <m:symbol>{symbol}</m:symbol> </m:request> </m:getQuote>); } function transformResponse(mc) { var symbol = mc.getPayloadXML()..*::symbol.toString(); var price = mc.getPayloadXML()..*::last.toString(); mc.setPayloadXML( <m:CheckPriceResponse xmlns:m="http://services.samples/xsd"> <m:Code>{symbol}</m:Code> <m:Price>{price}</m:Price> </m:CheckPriceResponse>); }
This sample is similar to sample 8, but instead of using XSLT, the transformation is done with JavaScript and E4X. The script used in this example has two functions, 'transformRequest' and 'transformResponse', and the Synapse configuration uses the function attribute to specify which function should be invoked. Use the stock quote client to issue a custom quote client as follows.
ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dmode=customquote
For additional examples, see Using Scripts in Mediation (Script Mediator).