Saturday, November 29, 2014

Synapse Handler

Synapse handler is similar to axis2 handler , that can be used in RESTAPIs to handle incoming request and the responses.

Sample handler ;


package test;

import org.apache.synapse.MessageContext;
import org.apache.synapse.rest.AbstractHandler;

public class TestSynapseHandler extends AbstractHandler {
@Override
public boolean handleRequest(MessageContext messageContext) {
String idKey = "idKey";
String idValue = "abc";
messageContext.setProperty(idKey, idValue);
return true;
}

@Override
public boolean handleResponse(MessageContext messageContext) {
// TODO Response handling
return true;
}

}

Handler has to be registered in the RESTAPI configuration.

Sample configuration;

<api xmlns="http://ws.apache.org/ns/synapse" name="calAPI" context="/cal">
   <resource methods="GET">
      <nSequence>
         <send>
            <endpoint>
               <address uri="http://localhost:9763/services/calservice"/>
            </endpoint>
         </send>
      </inSequence>
      <outSequence>
         <send>
            <endpoint>
               <default/>
            </endpoint>
         </send>
      </outSequence>
   </resource>
<handlers>
        < handler class="test.TestSynapseHandler"/>
   </handlers>
</api>












No comments:

Post a Comment