This documentation is for WSO2 Enterprise Service Bus version 4.8.1 . View documentation for the latest release.
Skip to end of metadata
Go to start of metadata

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 once on the class during initialization.

Use the Class mediator for user-specific, custom developments only when there isn't a built-in mediator that already provides the required functionality. Because there is high overhead in maintaining custom classes, avoid using them unless the scenario is frequently re-used and heavily user-specific.

For best results, use WSO2 Developer Studio for debugging Class mediators.



Syntax

<class name="class-name">
   <property name="string" value="literal">
   </property>
 </class>

UI Configuration

Class Name - The name of the class. You have to give the qualified name of the class and click on the "Load Class" button.

Note

You can configure the Mediator using XML. Click on "switch to source view" in the "Mediator" window.


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="variable1" value="10"/>
                    <property name="variable2" value="5"/>
                </class>
                <send/>
            </out>
        </sequence>

    </definitions>

In this configuration, the 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 variable1 and variable2 are passed to the instance mediator implementation class (SimpleClassMediator). Look at the following sample Class Mediator and note the SynapseMessageContext and the full Synapse API in there.

A simple Class Mediator Implementation
package samples.mediators;

    import org.apache.synapse.MessageContext;
    import org.apache.synapse.mediators.AbstractMediator;
    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 extends AbstractMediator {

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

        private String variable1="10";

        private String variable2="10";

        private int variable3=0;

        public SimpleClassMediator(){}

        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 setVariable1(String newValue) {
            variable1=newValue;
        }

        public String getVariable1() {
            return variable1;
        }

        public void setVariable2(String newValue){
            variable2=newValue;
        }

        public String getVariable2(){
            return variable2;
        }
    }

For more examples, see Extending the Mediation in Java (Class Mediator).

  • No labels