Class Mediator

The class mediator creates an instance of a custom specified class and sets it as a mediator. The class must implement the org.apache.synapse.api.Mediator interface. If any properties are specified, the corresponding setter methods are invoked on the class, once, during initialization.

Syntax

 <class name="class-name">
   <property name="string" value="literal"> 
    (either literal or XML child)
   </property>
 </class>

UI Configuration

Figure 1: Adding a Class mediator

Clicking on the 'Class' mediator option as shown in above figure, will open up the 'Class' mediator specific options. You can specify the specific implementaion class and load it by clicking 'LoadClass' button. Once you load the class, you'll see the configuration options as in figure2.

Class mediator configuration descriptions

  • Class Name: The class name of the class. You have to give the qualified name of the class and click "Load Class" button.
  • Properties defined for Class mediator: This list the properties with setter functions of the uploaded class. Following fields are listed for each property.
    • Property Name - Name of the property.
    • Property Value - The value to be set to the property.
    • Action - Link to delete the unused properties, from the list.

Example

<definitions xmlns="http://ws.apache.org/ns/synapse">

        <sequence name="fault">
            <makefault>
                <code value="tns:Receiver" xmlns:tns="http://www.w3.org/2003/05/soap-envelope"/>
                <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.SimpleClassMediator">
                    <property name="varible1" value="10"/>
                    <property name="varible2" value="5"/>
                </class>
                <send/>
            </out>
        </sequence>

    </definitions>

In this configuration, 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 "varible1" and "varible2" are passed to the instance mediator implementation class(SimpleClassMediator). Look at the following sample class mediator. Note how the SynapseMessageContext and to the full Synapse API in there.

A simple class mediator implementaion.

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 SimpleClassMediator implements Mediator {

        private static final Log log = LogFactory.getLog(DiscountQuoteMediator.class);

        private String varible1="10";

        private String varible2="10";

        private int varible3=0;

        public DiscountQuoteMediator(){}

        public boolean mediate(MessageContext mc) {
            // Do somthing useful..
            // Note the access to the Synapse Message context
            return true;
        }

        public String getType() {
            return null;
        }

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

        public int getTraceState() {
            return 0;
        }

        public void setvarible1(String newValue) {
            varible1=newValue;
        }

        public String getvarible1() {
            return varible1;
        }

        public void setvarible2(String newValue){
            varible2=newValue;
        }

        public String getvarible2(){
            return varible2;
        }
    }

A fully working sample is availble. See synapase configuration lanaguge guide for detailed(under section called class mediator) working sample.