Sunday, May 22, 2016

Create a Class mediator and Use it in the sequences of the sample proxy

Step 1: Creating Class mediator
First create maven project called org.example.mediator using eclipse. The add the following dependencies in the pom.xml.

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 

  <modelVersion>4.0.0</modelVersion> 

  <groupId>org.wso2.biruntha</groupId> 

  <artifactId>org.example.mediator</artifactId> 

  <version>0.0.1-SNAPSHOT</version> 

  <packaging>jar</packaging> 

  <name>org.example.mediator</name> 

  <url>http://maven.apache.org</url> 

  <properties> 

   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 

  </properties> 

  <repositories> 

     <repository> 

       <id>wso2-maven2-repository</id> 

       <url>http://dist.wso2.org/maven2</url> 

     </repository> 

     <repository> 

       <id>apache-Incubating-repo</id> 

       <name>Maven Incubating Repository</name> 

       <url>http://people.apache.org/repo/m2-incubating-repository</url> 

     </repository> 

     <repository> 

       <id>apache-maven2-repo</id> 

       <name>Apache Maven2 Repository</name> 

       <url>http://repo1.maven.org/maven2/</url> 

     </repository> 

   </repositories> 

   <build> 

     <plugins> 

       <plugin> 

         <groupId>org.apache.maven.plugins</groupId> 

         <artifactId>maven-compiler-plugin</artifactId> 

         <version>2.0</version> 

         <configuration> 

           <source>1.5</source> 

           <target>1.5</target> 

         </configuration> 

       </plugin> 

     </plugins> 

   </build> 

   <dependencies> 

        <dependency> 

         <groupId>junit</groupId> 

         <artifactId>junit</artifactId> 

         <version>3.8.1</version> 

         <scope>test</scope> 

        </dependency> 

     <dependency> 

       <groupId>org.apache.synapse</groupId> 

       <artifactId>synapse-core</artifactId> 

       <version>2.1.1-wso2v7</version> 

     </dependency> 

   </dependencies> 

 </project> 


Then to build the package use mvn clean install Step 2: Create class mediator create class mediator called MyMediator that extends AbstractMediator.

 package org.example.mediator; 

 import org.apache.synapse.MessageContext; 

 import org.apache.synapse.Mediator; 

 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.SOAPBody; 

 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 MyMediator extends AbstractMediator { 

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

   public MyMediator(){} 

   public boolean mediate(MessageContext mc) { 

     //get the SOAP body from message context 

    SOAPBody soapBody = mc.getEnvelope().getBody(); 

     //now you can modify the SOAP body as you wish 

     //... 

     return true; 

   } 

   public String getType() { 

     return null; 

   } 

   public void setTraceState(int traceState) { 

     this.traceState = traceState; 

   } 

   public int getTraceState() { 

     return 0; 

   } 

 } 


again run "mvn clean install" this will create org.example.mediator-0.0.1-SNAPSHOT.jar in the target folder.

copy that paste into wso2esb-4.9.0/repository/components/lin folder.

Then Run the ESB server.

cd wso2esb-4.9.0/samples/axis2Server/src/SimpleStockQuoteService
Run ant

cd wso2esb-4.9.0/samples/axis2Client ant stockquote -Dtrpurl=http://localhost:8280/services/StockQuoteProxy -Dmode=quote -Dsymbol=IBM

this will return stock price...

Step3 : Create proxy Service Follow [1] this to Create proxy service as below. And In the in sequence of the proxy add the class mediator we created already. After all of this the proxy will be like below.

 <?xml version="1.0" encoding="UTF-8"?> 

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

     name="StockQuoteProxy" 

     transports="http,https,local" 

     statistics="enable" 

     trace="disable" 

     startOnLoad="true"> 

   <target> 

    <inSequence> 

      <class name="org.example.mediator.MyMediator"/> 

    </inSequence> 

    <outSequence> 

      <send/> 

    </outSequence> 

    <endpoint> 

      <address uri="http://localhost:9000/services/SimpleStockQuoteService"/> 

    </endpoint> 

   </target> 

   <publishWSDL uri="file:./repository/samples/resources/proxy/sample_proxy_1.wsdl"/> 

   <description/> 

 </proxy> 



Run again ant stockquote -Dtrpurl=http://localhost:8280/services/StockQuoteProxy -Dmode=quote -Dsymbol=IBM

this will return stock price...

No comments:

Post a Comment