This section introduces handlers and uses an example to explain how to write a custom handler:
Table of Contents |
---|
...
CORSRequestHandler:
APIAuthenticationHandler:
Validates the OAuth2 bearer token used to invoke the API. It also determines whether the token is of typeProduction
orSandbox
and setsMessageContext
variables as appropriate.APIThrottleHandler:
Throttles requests based on the throttling policy specified by thepolicyKey
property. Throttling is applied both at the application level as well as subscription level.-
APIMgtUsageHandler:
Publishes events to WSO2 Data Analytics Server (WSO2 DAS) for collection and analysis of statistics. This handler only comes to effect if API usage tracking is enabled. See the Analytics section for more information. APIMgtGoogleAnalyticsTrackingHandler:
Publishes events to Google Analytics. This handler only comes into effect if Google analytics tracking is enabled. See Integrating with Google Analytics for more information.APIManagerExtensionHandler
: Triggers extension sequences. By default, the extension handler is listed at last in the handler chain, and therefore is executed last. You cannot change the order in which the handlers are executed, except the extension handler. To configure the API Gateway to execute extension handler first, uncomment the<ExtensionHandlerPosition>
section in the<APIM_HOME>/repository/conf/api-manager.xml
file and provide the valuetop
. This is useful when you want to execute your own extensions before our default handlers in situations like doing additional security checks such as signature verification on access tokens before executing the default security handler.
See Adding Mediation Extensions.
Writing a custom handler
Note |
---|
The outcome of using a Class Mediator vs. a Synapse Handler are very similar. However, when using a custom handler you need to maintain a customized velocity template file that needs to be manually merged when you upgrade your product to a newer version. Therefore, it is recommended to use custom Handlers when you wish to specify the exact order of execution of JARs as this can not be done with Mediators. |
Let's see how you can write a custom handler and apply it to the API Manager. In this example, we extend the authentication handler. Make sure your custom handler name is not the same as the name of an existing handler.
...
You can engage a custom handler to all APIs at once or only to selected APIs. To engage a custom handler to APIs, y ou need to add the custom handler with its logic in the <APIM_HOME>/repository/resources/api_templates/velocity_template.xml
file.
Note |
---|
It is not recommended to update the API source code via the source view UI or file system when engaging a custom handler to selected APIs, because the customizations get overridden by the publisher updates. |
For example, the following code segment adds the custom authentication handler that you wrote earlier to the velocity_template.xml
file while making sure that it skips the default APIAuthenticationHandler
implementation:
...