This documentation is for WSO2 Enterprise Integrator version 6.0.0 . View documentation for the latest release in the 6.x.x family and the latest release in the 7.x.x family.

All docs This doc
Skip to end of metadata
Go to start of metadata

Note that WSO2 EI is shipped with the following changes to what is mentioned in this documentation:

  • <PRODUCT_HOME>/repository/samples/ directory that includes all Integration profile samples is changed to <EI_HOME>/samples/service-bus/.
  • <PRODUCT_HOME>/repository/samples/resources/ directory that includes all artifacts related to the Integration profile samples is changed to <EI_HOME>/samples/service-bus/resources/.


This sample demonstrate how you can write your own custom mediation in java by using the Class mediator to extend the mediation functionality.


Building the sample

The XML configuration for this sample is as follows: 

<definitions xmlns="">

    <sequence name="fault">
            <code value="tns:Receiver" xmlns:tns=""/>
            <reason value="Mediation failed."/>

    <sequence name="main" onError="fault">
                <endpoint name="stockquote">
                    <address uri="http://localhost:9000/services/SimpleStockQuoteService"/>
            <class name="samples.mediators.DiscountQuoteMediator">
                <property name="discountFactor" value="10"/>
                <property name="bonusFor" value="5"/>

This configuration file  synapse_sample_380.xml is available in the <ESB_HOME>/repository/samples directory.

To build the sample

  1. Start the ESB with the sample 380 configuration. For instructions on starting a sample ESB configuration, see Starting the ESB with a sample configuration.

    The operation log keeps running until the server starts, which usually takes several seconds. Wait until the server has fully booted up and displays a message similar to "WSO2 Carbon started in n seconds."

  2. Start the Axis2 server. For instructions on starting the Axis2 server, see Starting the Axis2 server.

  3. Deploy the back-end service SimpleStockQuoteService. For instructions on deploying sample back-end services, see Deploying sample back-end services.

Executing the sample

The sample client used here is the Stock Quote Client, which can operate in several modes. For further details on this sample client and its operation modes, see Stock Quote Client.

To execute the sample client

  • Run the following command from the <ESB_HOME>/samples/axis2Client directory.

    ant stockquote -Dsymbol=IBM -Dmode=quote -Daddurl=http://localhost:8280

Analyzing the output

When the client is run, you will see see the following discounted quote value on the client console.

[java] Standard :: Stock price = $138.77458254967408

When you have a look at the console running Synapse. You will see the original value and the discounted value for the requested quote as follows.

Quote value discounted.
Original price: 162.30945327447262
Discounted price: 138.77458254967408

According to  synapse_sample_380.xml, ESB hands over the request message to the specified endpoint, which sends it to the Axis2 server running on port 9000. But the response message is passed through the class mediator before sending it back to the client. Two parameters named "discountFactor" and "bonusFor" are passed to the instance mediator implementation class (i.e. samples.mediators.DiscountQuoteMediator) before each invocation. Code of the mediator implementation class is given below.

package samples.mediators;

import javax.xml.namespace.QName;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.synapse.MessageContext;
import org.apache.synapse.mediators.AbstractMediator;

public class DiscountQuoteMediator extends AbstractMediator {

	private static final Log log = LogFactory.getLog(DiscountQuoteMediator.class);
	private String discountFactor = "10";
	private String bonusFor = "10";
	private int bonusCount = 0;
	public DiscountQuoteMediator() {}
	public boolean mediate(MessageContext mc) {

		String price = mc.getEnvelope().getBody().getFirstElement().getFirstElement().
				getFirstChildWithName(new QName("http://services.samples/xsd", "last")).getText();

		//converting String properties into integers
		int discount = Integer.parseInt(discountFactor);
		int bonusNo = Integer.parseInt(bonusFor);
		double currentPrice = Double.parseDouble(price);

		//discounting factor is deducted from current price form every response
		Double lastPrice = new Double(currentPrice - currentPrice * discount / 100);

		//Special discount of 5% offers for the first responses as set in the bonusFor property
		if (bonusCount <= bonusNo) {
			lastPrice = new Double(lastPrice.doubleValue() - lastPrice.doubleValue() * 0.05);

		String discountedPrice = lastPrice.toString();

				(new QName("http://services.samples/xsd", "last")).setText(discountedPrice);

		System.out.println("Quote value discounted.");
		System.out.println("Original price: " + price);
		System.out.println("Discounted price: " + discountedPrice);

		return true;

	public String getType() {
		return null;

	public void setTraceState(int traceState) {
		traceState = 0;

	public int getTraceState() {
		return 0;

	public void setDiscountFactor(String discount) {
		discountFactor = discount;

	public String getDiscountFactor() {
		return discountFactor;

	public void setBonusFor(String bonus) {
		bonusFor = bonus;

	public String getBonusFor() {
		return bonusFor;


All classes developed for class mediation should implement the Mediator interface, which contains the mediate(...) method. mediate(...) method of the above class is invoked for each response message mediated through the main sequence, with the message context of the current message as the parameter. All the details of the message including the SOAP headers, SOAP body and properties of the context hierarchy can be accessed from the message context. In this sample, the body of the message is retrieved and the discount percentage is subtracted from the quote price. If the quote request number is less than the number specified in the "bonusFor" property in the configuration, a special discount is given.

  • No labels