WSO2 ESB Modify endpoint address on handler



The following instructions will guide you to build a custom handler which caters your requirement of modifying the IP address when the request goes to the back-end.
Please refer the following code of the TestHandler which implemented by extending the AbstractSynapseHandler.

Create a Handler class which extends the AbstractSynapseHandler
In the handleRequestOutFlow, we get the message context and from that, we retrieve the original address. Then we replace the original address with the new address which in this case is my testapi address (http://192.168.11.228:8280/testapi)


package com.sample;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.synapse.AbstractSynapseHandler;
import org.apache.synapse.MessageContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class TestHandler extends AbstractSynapseHandler {

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

    @Override
    public boolean handleRequestInFlow(MessageContext synCtx) {
        log.info("Request In Flow");
        return true;
    }

    public boolean handleRequestOutFlow(MessageContext synCtx) {

        /** Modify the below code for extract the dynamic IP address and do a string        manipulation to bind the new ip */

        EndpointReference mc= synCtx.getTo();
        Endpoint s_endpoint = synCtx.getEndpoint("sampleendpoint");
        EndpointDefinition endpointDefinition = new EndpointDefinition();

        /** Below code is to assign the new IP address*/

        EndpointReference endpointReference = new EndpointReference();
        endpointReference.setAddress("http://192.168.11.228:8280/testapi");
        ((Axis2MessageContext) synCtx).getAxis2MessageContext().setTo(endpointReference);
        log.info("Request Out Flow");

        return true;
    }
@Override public boolean handleResponseInFlow(MessageContext synCtx) { log.info("Response In Flow"); return true; } @Override public boolean handleResponseOutFlow(MessageContext synCtx) { log.info("Response Out Flow"); return true; } }

Then you need to create the jar using the above class, shut down the server and copy it to the lib folder in,

<ESB_HOME>/repository/components/lib

After that, to engage the deployed Synapse handler, you need to add the following configuration to the <ESB_HOME>/repository/conf/synapse-handlers.xml file.

<handlers>
    <handler name="TestHandler" class="com.sample.TestHandler"/>
</handlers>


Now you can start the server.

To test this scenario, you could create a sample proxy, endpoint and a API in the ESB as below.

Create an Address endpoint as below



Then modify the proxy service


<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="TestProxy"
       startOnLoad="true"
       statistics="disable"
       trace="disable"
       transports="http,https">
   <target>
      <inSequence>
         <header name="To" value="http://www.mocky.io/v2/5b34accf2f00008400376004"/>
         <call>
            <endpoint>
               <default/>
            </endpoint>
         </call>
      </inSequence>
   </target>
   <description/>
</proxy>
              

API

<api xmlns="http://ws.apache.org/ns/synapse" name="TestAPI" context="/testapi">
   <resource methods="POST GET">
      <inSequence>
         <log level="custom">
            <property name="API_Log" value="This is the API Log"/>
         </log>
      </inSequence>
      <outSequence/>
      <faultSequence/>
   </resource>
</api>



Once you invoke the proxy service, you should be able to see the log as below. Which implies that the testapi was called and the endpoint is being modified.






Comments

Popular posts from this blog

How to fix SoapUI freeze in Mac OS

Salesforce Auto generate renewal Opportunity with Line Items (i.e. Opportunity Products)

Salesforce Create multiple child records based on a number field in the parent using flow