Sample 380: Writing Custom Mediation Logic in Java

<definitions xmlns="http://ws.apache.org/ns/synapse"> <sequence name="fault"> <makefault> <code xmlns:tns="http://www.w3.org/2003/05/soap-envelope" value="tns:Receiver"/> <reason value="Mediation failed."/> </makefault> <send/> </sequence> <sequence name="main" onError="fault"> <in> <send> <endpoint name="stockquote"> <address uri="http://localhost:9000/services/SimpleStockQuoteService"/> </endpoint> </send> </in> <out> <class name="samples.mediators.DiscountQuoteMediator"> <property name="discountFactor" value="10"/> <property name="bonusFor" value="5"/> </class> <send/> </out> </sequence> </definitions>

Objective

Demonstrate the use of class mediator to extend the mediation functionality of Synapse

Pre-requisites

  • Deploy the SimpleStockQuoteService in the sample Axis2 server and start Axis2
  • Start Synapse using the configuration numbered 380 (repository/conf/sample/synapse_sample_380.xml)
    Unix/Linux: sh synapse.sh -sample 380
    Windows: synapse.bat -sample 380

Executing the Client

In this configuration, Synapse 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. Class mediator in turns hands over the message to the specified Java class for further processing. In that regard, the class mediator acts as a delegating mediator which delegates the message to a custom Java class for processing. Two parameters named 'discountFactor' and 'bonusFor' are passed to the mediator implementation class (i.e. samples.mediators.DiscountQuoteMediator) before each invocation. Source of the mediator implementation class is shown below.

package samples.mediators; import org.apache.synapse.MessageContext; import org.apache.synapse.Mediator; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.OMAbstractFactory; import org.apache.axiom.om.OMFactory; import org.apache.axiom.soap.SOAPFactory; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import javax.xml.namespace.QName; public class DiscountQuoteMediator implements Mediator { 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","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","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 the class mediator should implement the 'Mediator' interface, which contains the mediate(...) method. The 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 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.

To test the custom code and the class mediator, invoke the test client as follows.

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

You will see the below output in the client console with the discounted quote value.

Standard :: Stock price = $95.26454380258552

If you check the Synapse console, you will notice the messages printed by the custom mediator during mediation.

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

Back to Catalog