Search code examples
apache-synapsewso2-enterprise-integratorwso2-esb

How to Configure custom synapsis handler for a specific API in wso2 EI 6.1.1


I need to map my custom synapse handler to a specific API in WSO2 EI 6.1.1. I tried below two separate methods to configure the Custom synapse Handler to map with API

method 1: configure the handler in EI_HOME/conf/synapse-handlers.xml This way the handler applied to all APIs. ref:https://docs.wso2.com/display/EI600/Writing+a+Synapse+Handler

method 2: configure the handler in API xml

......</resource>
    <handlers>
       <handler class="org.test.TestHandler1"/>
    </handlers>
 </api>

this way it leads to an error when starting the EI server. looks like method 2 is possible on WSO2 ESB not WSO2 EI

looking for a way to do this. note: I am extending AbstractSynapseHandler to create custom synapse handler

thanks in advance


Solution

  • The link that you have provided contains a sample which extends org.apache.synapse.AbstractSynapseHandler. But if you want an API level handler, your handler should implement org.apache.synapse.rest.Handler instead of extending org.apache.synapse.AbstractSynapseHandler. Below is a sample Handler which should work in your case.

    import java.util.Map;
    import org.apache.synapse.MessageContext;
    import org.apache.synapse.rest.Handler;
    
    public class ReqResponseHandler implements Handler {
    
        @Override
        public void addProperty(String s, Object o) {
        }
    
        @Override
        public Map<?, ?> getProperties() {
            return null;
        }
    
        @Override
        public boolean handleRequest(MessageContext synapsisCtx) {
            System.out.println("**********Handling Request*********");
            return true;
        }
    
        @Override
        public boolean handleResponse(MessageContext synapsisCtx) {
            System.out.println("**********Handling Response*********");
            return true;
        }
    }
    

    The way that you configured the Handler in your API xml is correct. (However, note that you cannot add a handler to API through web interface.)

    For more details visit WSO2 documentation through below link. https://docs.wso2.com/display/EI600/Securing+APIs