4
votes

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

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

2

2 Answers

3
votes

I assume you do not want to edit API XML manually for all the APIs to engage a custom handler. Instead, you want to be able to engage a handler to all the APIs automatically when you publish the APIs. You can do it by editing the $APIM_HOME/repository/resources/api_templates/velocity_template.xml. This is the template file which decides which handlers to be engaged with APIs by default. In this file, at the end, you will find a 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 have added my handler org.wso2.carbon.samples.handlers.MyCustomHandler. That's it you should do. If you create and publish an API now, MyCustomHandler will be engaged with your API automatically. However, already published APIs won't have an effect even if you update velocity_templates.xml. You need to republish them in order to get the effect.

1
votes

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

I solved this by creating a standalone HTTP API in a WAR file that I deployed to API Manager's Carbon instance. The same service that calls the WSO2 Publisher API calls my HTTP API afterward. The API takes in handler class names as parameters, and injects the appropriate elements into the API definition XML files on the local filesystem (I used JDOM). API Manager automatically detects and reloads the modified XML and it's good to go.