Inlining Groovy scripts is thankfully easy. For reasons such as tidyness,
reuse, in-flight updates and sanity sake it is arguably be better to store
the
scripts as external entities in a repository.  For the benefit of others I
have collected the info I found elsewhere and some experimentation

in one place to show how it's done:

Below is a simple inliine script that builds a static XML document :

<sequence name="service_dispatch_out" trace="enable">
<script language="groovy">

                mc.setPayloadXML('''<hello
xmlns="http://grails-exchange.com
"><to>Everyone</to><from>Paul</from></hello>''')
</script>
<send/>
</sequence>

For an script in a repository follow this 'pattern':


Synapse XML:

<sequence name="service_dispatch_out" trace="enable">
        <script language="groovy"
key="samples/scripts/staticXMLresponse.groovy"/>
        <send/>
</sequence>


Repository XML:



<x><![CDATA[
static void mediate (mc) {

    mc.setPayloadXML('''<hello xmlns="http://grails-exchange.com
"><to>Everyone</to><from>Paul</from></hello>''')
}
]]></x>

Notice there needs to be a root tag, to support the (implied) XInclude I
guess, and that the script needs to adhere to an interface to be able to be
passed the MessageContext (mc) which gives programmatic access to the
current synapase message and sequence.

For interesting things you can do with the MessgeContext please have a look
at:
http://synapse.apache.org/apidocs/org/apache/synapse/mediators/bsf/ScriptMessageContext.html


All the best

Wayne

Reply via email to