I have a WSO2 synapse file to handle my API requests at gateway level. I have configured my GET and POST/PUT for an endpoint as below. I have a custom handler which deals with allowing a specific host to call these APIs. I have a requirement to allow GET from any host and POST/PUT only from a specific host. However, I cannot configure it in my synapse file because handlers which we define in the file are getting applied to all HTTP methods. Is there way to configure handlers based on HTTP method for the same endpoint/context. How I can have my custom handler (com.abc.wso2.handler.HostRestrictionHandler) applicable to only POST?PUT calls? Below is my configuration.
<api xmlns="http://ws.apache.org/ns/synapse" context="/user/notification/v3" version="v2" version-type="context">
<resource methods="GET" uri-template="/*">
<inSequence>
<class name="org.wso2.carbon.apimgt.gateway.mediators.TokenPasser"/>
<filter regex="PRODUCTION" source="$ctx:AM_KEY_TYPE">
<then>
<class name="com.abc.mediators.DispatchMediator">
<property name="url" value="http://api.{HOST_NAME}.com/user/notification/v3"/>
</class>
<send>
<endpoint name="Notification_API">
<address>
<timeout>
<duration>30000</duration>
<responseAction>fault</responseAction>
</timeout>
<suspendOnFailure>
<errorCodes>-1</errorCodes>
<initialDuration>0</initialDuration>
<progressionFactor>1.0</progressionFactor>
<maximumDuration>0</maximumDuration>
</suspendOnFailure>
<markForSuspension>
<errorCodes>-1</errorCodes>
</markForSuspension>
</address>
</endpoint>
</send>
</then>
<else>
<sequence key="_sandbox_key_error_"/>
</else>
</filter>
</inSequence>
<outSequence>
<send/>
</outSequence>
</resource>
<resource methods="POST PUT" uri-template="/*">
<inSequence>
<property name="X-Forwarded-Host" scope="transport" expression="$trp:Host"/>
<class name="org.wso2.carbon.apimgt.gateway.mediators.TokenPasser"/>
<filter regex="PRODUCTION" source="$ctx:AM_KEY_TYPE">
<then>
<class name="com.abc.mediators.DispatchMediator">
<property name="url" value="http://api.{HOST_NAME}.com/user/notification/v3"/>
</class>
<send>
<endpoint name="Notification_API_2">
<address>
<timeout>
<duration>30000</duration>
<responseAction>fault</responseAction>
</timeout>
<suspendOnFailure>
<errorCodes>-1</errorCodes>
<initialDuration>0</initialDuration>
<progressionFactor>1.0</progressionFactor>
<maximumDuration>0</maximumDuration>
</suspendOnFailure>
<markForSuspension>
<errorCodes>-1</errorCodes>
</markForSuspension>
</address>
</endpoint>
</send>
</then>
<else>
<sequence key="_sandbox_key_error_"/>
</else>
</filter>
</inSequence>
<outSequence>
<send/>
</outSequence>
</resource>
<handlers>
<handler class="com.abc.wso2.handler.HostRestrictionHandler">
<property name="allowedHosts" value="my.customhost.com" />
</handler>
</handlers>
</api>