The Class Mediator creates an instance of a custom-specified class and sets it as a mediator. Use the Class mediator for user-specific, custom developments only when there is no built-in mediator that already provides the required functionality.
Creating a Class Mediator
There are two ways you can create a class mediator:
We can use WSO2 Developer Studio to Create class mediator easily.
Create a New Mediator Project By selecting File-> New Project -> WSO2 -> Extensions -> Project Types -> Mediator Project
It will create the class mediator by extending the AbstractMediator. Then we need to implement the mediate method which is invoked when the mediator is executed at the mediation flow. The return statement of the mediate method decides whether the mediation flow should continue further or not.
MessageContext is the representation for a message within the ESB message flow. In the mediate method we get access to the message context. From the message context we can access message payload, message headers, properties, ESB configurations, etc. We can also set parameters to the mediator.
Sample Class mediator implementation is as follows.
After implementing the method export the project as .jar file and copy to/repository/components/lib folder and restart the ESB.
Creating the Proxy service
You can use the implemented class mediator within a proxy service as follows.
Then send a request to the proxy service and you will see following output in the consloe as we are printing to the console.
Creating a Class Mediator
There are two ways you can create a class mediator:
- Implement the mediator interface
- Extend AbstractMediator class
We can use WSO2 Developer Studio to Create class mediator easily.
Create a New Mediator Project By selecting File-> New Project -> WSO2 -> Extensions -> Project Types -> Mediator Project
It will create the class mediator by extending the AbstractMediator. Then we need to implement the mediate method which is invoked when the mediator is executed at the mediation flow. The return statement of the mediate method decides whether the mediation flow should continue further or not.
MessageContext is the representation for a message within the ESB message flow. In the mediate method we get access to the message context. From the message context we can access message payload, message headers, properties, ESB configurations, etc. We can also set parameters to the mediator.
Sample Class mediator implementation is as follows.
package org.wso2.test; import java.util.Enumeration; import java.util.Map; import java.util.Properties; import org.apache.synapse.MessageContext; import org.apache.synapse.core.axis2.Axis2MessageContext; import org.apache.synapse.mediators.AbstractMediator; public class SampleClassMediator extends AbstractMediator { public String classProperty; public boolean mediate(MessageContext context) { //Mediator Property System.out.println("------Mediator Property-------"); System.out.println("Using Property : " + classProperty); //Accessing Message Properties System.out.println("------Message Properties-------"); String prop = (String) context.getProperty("StringProp"); System.out.println("Using msg Prop : "+ prop); //Accessing Axis2 Properties System.out.println("------Axis2 Properties-------"); Map<String,Object> mapAxis2Properties = ((Axis2MessageContext)context).getAxis2MessageContext().getProperties(); for (Map.Entry entry : mapAxis2Properties.entrySet()) { System.out.println("AXIS:"+entry.getKey() + ", " + entry.getValue()); } //Accessing Transport Headers System.out.println("------Transport Headers-------"); Map<String,Object> mapTransportProperties = (Map<String, Object>) ((Axis2MessageContext)context).getAxis2MessageContext().getProperty("TRANSPORT_HEADERS"); for (Map.Entry entry : mapTransportProperties.entrySet()) { System.out.println("TRANS:" +entry.getKey() + ", " + entry.getValue()); } //Accessing Syanpse Configuration properties System.out.println("------Synapse Config Properties-------"); Properties p = ((Axis2MessageContext)context).getEnvironment().getSynapseConfiguration().getProperties(); Enumeration keys = p.keys(); while(keys.hasMoreElements()){ String key = (String)keys.nextElement(); String value = (String)p.get(key); System.out.println("PRP: "+ key + " | " + value); } //Accessing Soap envelop System.out.println("------Soap envelop-------"); System.out.println("MSG : " + context.getEnvelope()); return true; } public String getClassProperty() { return classProperty; } public void setClassProperty(String propValue) { this.classProperty = propValue; } }
After implementing the method export the project as .jar file and copy to
Creating the Proxy service
You can use the implemented class mediator within a proxy service as follows.
<?xml version="1.0" encoding="UTF-8"?> <proxy xmlns="http://ws.apache.org/ns/synapse" name="TestClassMedProxy" transports="http,https" statistics="disable" trace="disable" startOnLoad="true"> <target> <inSequence> <log level="custom"> <property name="BEGIN" value="Begin Sequence"/> </log> <property name="StringProp" value=""TestString"" scope="default" type="STRING"/> <class name="org.wso2.test.SampleClassMediator"> <property name="classProperty" value="5"/> </class> </inSequence> <outSequence> <send/> </outSequence> </target> <description/> </proxy>
Then send a request to the proxy service and you will see following output in the consloe as we are printing to the console.
[2016-10-18 07:03:27,414] INFO - LogMediator BEGIN = Begin Sequence ------Mediator Property------- Using Property : 5 ------Message Properties------- Using msg Prop : "TestString" ------Axis2 Properties------- AXIS:addressing.validateAction, false AXIS:local_WSO2_WSAS, org.wso2.carbon.core.init.CarbonServerManager@182ab504 AXIS:CARBON_TASK_JOB_METADATA_SERVICE, org.wso2.carbon.task.JobMetaDataProviderServiceHandler@4d72eb52 AXIS:file.resource.map, {} AXIS:CARBON_TASK_MANAGEMENT_SERVICE, org.wso2.carbon.task.TaskManagementServiceHandler@fdccc49 AXIS:mediation.event.broker, org.wso2.carbon.event.core.internal.CarbonEventBroker@51c8cdb1 AXIS:CARBON_TASK_MANAGER, org.wso2.carbon.task.TaskManager@1c87c019 AXIS:wso2tracer.msg.seq.buff, org.wso2.carbon.utils.logging.CircularBuffer@467590a0 AXIS:ContainerManaged, true AXIS:WORK_DIR, /home/anupama/Anupama/Workspace/WSO2Products/ESB/wso2esb-4.9.0/tmp/work AXIS:PASS_THROUGH_TRANSPORT_WORKER_POOL, org.apache.axis2.transport.base.threads.NativeWorkerPool@2fa2b2a3 AXIS:CARBON_TASK_REPOSITORY, org.apache.synapse.task.TaskDescriptionRepository@389047a3 AXIS:local_current.server.status, RUNNING AXIS:tenant.config.contexts, {} AXIS:CONFIGURATION_MANAGER, org.wso2.carbon.mediation.initializer.configurations.ConfigurationManager@1a35cfa0 AXIS:service.epr.map, {} AXIS:primaryBundleContext, org.eclipse.osgi.framework.internal.core.BundleContextImpl@2506f4 AXIS:GETRequestProcessorMap, {info=org.wso2.carbon.core.transports.util.InfoProcessor@70afacd6, wsdl=org.wso2.carbon.core.transports.util.Wsdl11Processor@4a223fca, wsdl2=org.wso2.carbon.core.transports.util.Wsdl20Processor@7035d472, xsd=org.wso2.carbon.core.transports.util.XsdProcessor@44c95d1b, tryit=org.wso2.carbon.tryit.TryitRequestProcessor@490d9048, wadltryit=org.wso2.carbon.tryit.WADLTryItRequestProcessor@120a3acc, stub=org.wso2.carbon.wsdl2form.StubRequestProcessor@4c42eabb, wsdl2form=org.wso2.carbon.wsdl2form.WSDL2FormRequestProcessor@42ed57ad, wadl2form=org.wso2.carbon.tryit.WADL2FormRequestProcessor@67c70068} ------Transport Headers------- TRANS:Content-Type, application/soap+xml; charset=UTF-8; action="urn:mediate" TRANS:Cookie, menuPanel=visible; menuPanelType=main; region1_manage_menu=visible; JSESSIONID=4B3870641F979576800AB05FAEB9360D; requestedURI=../../carbon/admin/index.jsp; region1_configure_menu=none; region3_registry_menu=none; region4_monitor_menu=none; region5_tools_menu=none; current-breadcrumb=manage_menu%2Cservices_menu%2Cservices_list_menu%23 TRANS:Host, anupama:8280 TRANS:Transfer-Encoding, chunked TRANS:User-Agent, Axis2 ------Synapse Config Properties------- PRP: synapse.xpath.func.extensions | org.wso2.carbon.mediation.security.vault.xpath.SecureVaultLookupXPathFunctionProvider PRP: statistics.clean.interval | 1000 PRP: synapse.commons.json.preserve.namespace | false PRP: resolve.root | ./. PRP: synapse.home | . PRP: __separateRegDef | true PRP: synapse.global_timeout_interval | 120000 PRP: synapse.temp_data.chunk.size | 3072 PRP: statistics.clean.enable | true PRP: synapse.observers | org.wso2.carbon.mediation.dependency.mgt.DependencyTracker PRP: synapse.sal.endpoints.sesssion.timeout.default | 600000 PRP: synapse.carbon.ext.tenant.info | org.wso2.carbon.mediation.initializer.handler.CarbonTenantInfoConfigurator ------Soap envelop------- MSG : <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"><soapenv:Body><test>Test Data</test></soapenv:Body></soapenv:Envelope>
No comments:
Post a Comment