Tuesday, April 27, 2010

Running Synapse ESB's Script Mediator with include option

Apache Synapse ESB's Script Mediator can be used for message mediation. Sample 350 of Synapse samples is demonstrating how this Script Mediator can be used in a real world scenario.

Suppose the script you specified is calling a function of another script. Then the latter script should also be included in the script mediator configuration. It's done using the <include> element. The key attribute of the <include> element can be specified with the script program statements stored in a separate file, which is referenced via the local or remote registry entry. Following example shows how it can be done.
<script key="string" language="string" [function="script-function-name"]>
<include key="string"/>
</script>
I am using the same example used in Sample 350 to demonstrate this scenario.

1) Replace the stockquoteTransform.js with the following javascript.

// stockquoteTransform.js
function transformRequest(mc) {
transformRequestFunction(mc);
}

function transformResponse(mc) {
transformResponseFunction(mc);
}

2) Add the following sample.js file to SYNAPSE_HOME/repository/samples/resources/script

// sample.js
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>);
}

function transformResponse(mc) {
var symbol = mc.getPayloadXML()..*::symbol.toString();
var price = mc.getPayloadXML()..*::last.toString();
mc.setPayloadXML(
<m:checkpriceresponse m="http://services.samples/xsd">
<m:code>{symbol}</m:code>
<m:price>{price}</m:price>
</m:checkpriceresponse>);
}

3) Update the synapse configuration with the following configuration.

<definitions xmlns="http://ws.apache.org/ns/synapse">
<localEntry key="stockquoteScript" src="file:repository/conf/sample/resources/script/stockquoteTransform.js"/>
<localEntry key="sampleScript" src="file:repository/samples/resources/script/sample.js"/>

<in>
<!-- transform the custom quote request into a standard quote request expected by the service -->
<script language="js" key="stockquoteScript" function="transformRequest">
<include key="sampleScript"/>
<script>
<send>
<endpoint>
<address uri="http://localhost:9000/soap/SimpleStockQuoteService"/>
</endpoint>
</send>
</in>
<out>
<!-- transform the standard response back into the custom format the client expects -->
<script language="js" key="stockquoteScript" function="transformResponse"/>
<include key="sampleScript"/>
<script>
<send/>
</out>
</definitions>

4) Start the Synapse ESB with the above configuration. Deploy the SimpleStockQuote service (which is shipped with Synapse) on SimpleAxisServer. Then invoke the client giving the following command.

ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dmode=customquote

5) It will yield the following result.
   [java] Custom :: Stock price = $161.76045110619708 

6) For more information.
WSO2 ESB articles - http://wso2.org/library/esb
Apache Synapse - http://synapse.apache.org/

No comments: