1. With the script program statements stored in a separate file, 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 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 these functions, it is possible to access the Synapse predefined in a script variable named "mc." The "mc" represents an implementation of the
ScriptMessageContext.java, which contains the following methods that can be accessed within the script as
- 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 has advantages over using the built-in Synapse Mediator types or implementing a custom Java class Mediator. The Script Mediators have the flexibility of a class Mediator with access to the Synapse
For both types of script mediator definitions, the
MessageContext passed into the script has additional methods over the standard Synapse
E4X XML objects and when using Ruby, REXML documents.
Click on the relevant tab to view the syntax for a script mediator using an Inline script, or a script mediator using a script of a registry
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 the value "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 refers it using the key.
If Inline Selected
The following options are available:
- Language - Chooses from a variety of scripting languages supported:
- Source - If inline is selected as the Script type, specify the source.
If Registry is Selected
Following options are available:
- Function - Function of the script language to execute.
- Key Type - Specifies whether to use Static or Dynamic Keys.
- Key - Registry location of the source. You can click on the "Configuration Registry" or "Governance Registry" links to choose the source from the "Registry Tree".
- Include Keys - Script 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
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. An example of test.js is shown bellow:
For additional examples of using the Script mediator, see Using Scripts in Mediation (Script Mediator).