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>
// stockquoteTransform.js
function transformRequest(mc) {
transformRequestFunction(mc);
}
function transformResponse(mc) {
transformResponseFunction(mc);
}
// 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>);
}
<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>
ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dmode=customquote
[java] Custom :: Stock price = $161.76045110619708
Apache Synapse - http://synapse.apache.org/
No comments:
Post a Comment