1. With the script program statements stored in a separate file, which is referenced via the Local or Remote Registry entry.
2. With the script program statements embedded inline within the Synapse configuration.
Synapse uses the Apache Bean Scripting Framework for the scripting language support, any script language supported by BSF may be used to implement the Synapse Mediator. With the Script Mediator you can invoke a function in the corresponding script. With in these functions, it is possible to access the Synapse predefined in a script variable named "mc." This "mc" represents an implementation of the
ScriptMessageContext.java. That contains following additional methods that can be accessed within the script by
- public Object getPayloadXML() - Gets the XML representation of SOAP Body payload.
- public void setPayloadXML(Object payload) - Sets the SOAP body payload from XML.
- public void addHeader(boolean mustUnderstand, Object content) - Adds a new SOAP header to the message.
- public Object getEnvelopeXML() - Gets the XML representation of the complete SOAP envelope
- public void setTo(String reference)
- public void setFaultTo(String reference)
- public void setFrom(String reference)
- public void setReplyTo(String reference)
Implementing a Mediator with a script language can have advantages over using the built in Synapse Mediator types or implementing a custom Java class Mediator. The Script Mediators have all the flexibility of a class Mediator with access to the Synapse
MessageContext passed into the script has additional methods over the standard Synapse
E4X XML objects and when using Ruby, REXML documents.
Script Mediator Using a Script of the Registry
The attributes of of the <
- "key" - The registry key to load the script.
- "function" - Optional attribute defining the name of the script function to invoke, if not specified it defaults to a function named "mediate." The function is passed a single parameter, which is the Synapse MessageContext. The function may return a boolean, if it does not, then true is assumed and the script mediator returns this value.
Script mediator using a In-lined script
- #Inline - Specifies the script inline.
- #Registry - Stores the script in registry and refer it using the key.
If Inline Selected
The following options are available:
- Language - Choose from variety of scripting languages supported:
- Source - If inline selected as the Script type, specify the source.
If Registry Selected
Following options are available:
- Function - Function of the script language to execute.
- Key - Registry location of the source. You can click on the "Configuration Registry" or "Governance Registry" link to choose the source from the "Registry Tree". See more information in Registry Browser and Namespace.
- Include Keys - Scripts sources to be included.
You can configure the Mediator using XML. Click on "switch to source view" in the "Mediator" window.
The above configuration is an example of an inline mediator using
symbol which has a value of
IBM would be:
In the above example, script is loaded from the registry by using the key
repository/conf/sample/resources/script/test.js. The script is written in Java script. The function to be invoked is
testFunction. As example for test.js shown in the bellow.