Saturday, January 2, 2016

Adding a Tenant Specific Custom Handler WSO2 API Manager

Writing a Handler for WSO2 API Manager is pretty straight forward and you can find it in [1]. When it comes to multi tenancy there might be instances that this particular handler should be applied only to a specific tenant or a set of tenants. In doing so we have to edit the /repository/resources/api_templates/velocity_template.xml to include the handler to only a specific tenant. This is the template file used in WSO2 API Manager to create synapse file required to create the api. Here we have to write a simple velocity logic to add the handler only for the specific tenants. If this logic is not added the handler will be applied to all the tenant hence resulting in a performance tradeoff. 

You can add the below logic right after the handlers tag in the velocity_template.xml

        #if ( $apiContext.contains("/abc.com/") )
           <handler class="package.path.to.class" />
        #end

This will add the handler to the abc.com tenant only. You can also extend this to multiple tenant with having a logical OR "||" operator for the contains check.

Just write and save the file and will be added to the template.

[1] https://docs.wso2.com/display/AM191/Writing+Custom+Handlers

No comments:

Post a Comment