Add custom handler programmatically to WSO2 API Manager

I create and subscribe to new APIs through an automated process that uses the WSO2 Publisher API Manager and the Store HTTP API respectively. I have custom handlers that I then map to my APIs by editing XML files in <APIM_HOME>/repository/deployment/server/synapse-configs/default/api

.

Is there a way to programmatically map handlers for newly created APIs so that I don't have to manually edit the XML? In other words, an API or some other method to view the current handlers for the API and add / remove?

+3


source to share


2 answers


In my case, I have many handlers and many APIs. Not all handlers apply to all APIs, and no handler applies to all APIs.



I solved this by creating a standalone HTTP API in a WAR file that I deployed to an instance of the API Manager API. The same service that calls the WSO2 publisher API calls the HTTP API after that. The API takes handler class names as parameters and injects the appropriate elements into the XML API definition files on the local file system (I used JDOM). API Manager automatically detects and reloads changed XML, which is a good thing.

0


source


I am assuming that you do not want to manually edit the XML API for all APIs in order to use a custom handler. Instead, you want to be able to invoke a handler for all APIs automatically when the API is published. You can do this by editing $ APIM_HOME / repository / resources / api_templates / velocity_template.xml. This is a template file that decides which handlers will interact with the default API. In this file, at the end, you'll find the handlers section. You need to edit this file and add your handler there, as shown below.

## print the handlers
#if($handlers.size() > 0)
<handlers xmlns="http://ws.apache.org/ns/synapse">
    <handler class="org.wso2.carbon.samples.handlers.MyCustomHandler"/>
    #foreach($handler in $handlers)
    <handler xmlns="http://ws.apache.org/ns/synapse" class="$handler.className">
        #if($handler.hasProperties())
            #set ($map = $handler.getProperties() )
            #foreach($property in $map.entrySet())
                <property name="$!property.key" value="$!property.value"/>
            #end
        #end
    </handler>
    #end
</handlers>
#end

      



As you can see, I've added a handler org.wso2.carbon.samples.handlers.MyCustomHandler

. This is what you should do. If you create and publish the API now, MyCustomHandler will automatically interact with your API. However, the APIs already published will have no effect even if you update the velocity_templates.xml file. You have to reprint them to get the effect.

+2


source







All Articles