Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejava
package samples.mediators;

import orgjavax.apachexml.synapsenamespace.MessageContextQName;
import
org.apache.synapse.Mediator;
import org.apache.axiomcommons.omlogging.OMElementLog;
import org.apache.axiomcommons.omlogging.OMAbstractFactoryLogFactory;
import org.apache.axiomsynapse.om.OMFactoryMessageContext;
import org.apache.axiomsynapse.soap.SOAPFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.xml.namespace.QName;

mediators.AbstractMediator;

public class DiscountQuoteMediator implementsextends MediatorAbstractMediator {

    	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);
            			bonusCount++;
        		}

        		String discountedPrice = lastPrice.toString();

        		mc.getEnvelope().getBody().getFirstElement().getFirstElement().getFirstChildWithName
                				(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=		discountFactor = discount;
    	}

    	public String getDiscountFactor() {
        		return discountFactor;
    	}

    	public void setBonusFor(String bonus) {
        bonusFor=		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.

...