Saturday, December 10, 2016

WSO2 API Manager, What is the Difference, relation between handlers and mediators.


I’ve already discussed how to write a custom handler in my previous post, but say, you are having a use case which will mix up you to get a decision such as “should i achieve this by writing custom handler? Or should i write a custom mediator instead of handler?”  for getting a decision in a such situations hope this post will helps you...


One main difference between a class mediator and a handler is that handlers are executed in both request flow and response flow, and you can write 2 logics for those 2 cases in the same handler. But a class mediator has only a single section (i.e. mediate() method). When you engage a class mediator in a sequence, you can decide if you want to put it in request flow or response flow, or in both.
If your logic is not complex, you can use existing mediators instead of writing a class mediator. In that case, you don't need to write any java code.
Existing handlers are executed first in the request flow. Mediation sequences are executed after that. But if you write a custom handler, you can put it after mediation sequences as well, because mediation sequences are also executed by a handler (APIManagerExtensionHandler). So if you place your handler after APIManagerExtensionHandler(which you can find in <APIM HOME>/repository/deployment/server/synapse-configs/default/api/apiconfiguration file), it will execute after mediation sequences.(figure 1.0)

Figure 1.0

The most important point is both mediation sequences and handlers are code extensions that run after the gateway receives a response or request.

According to my findings, here are the primary differences between handlers and mediation sequences. Using one over the other should be determined by your specific requirements.

Custom Handlers


  • Are strictly "per-API," though they can be added to each API via inclusion in the velocity-template.xml file.
  • Can be executed in any order with respect to other handlers.
  • Do not require sequences in addition to the inclusion of the handler within the API definition sequence. All of the tasks must be contained in Java code.
  • Handlers extend the org.apache.synapse.rest.AbstractHandler class,requiring implementation of AbstractHandler.handleRequest and AbstractHandler.handleResponse

Mediation Sequences


  • Can be configured to be global or API specific.
  • Can be executed in any order with respect to other mediation sequences, but cannot mediate before other handlers unless all other mediators also do so (unless you write a custom mediation handler).
  • Allow you to invoke other predefined mediators within an XML tree to describe the mediation tasks. Unless the predefined mediators (provided by WSO2) do not satisfy your requirements, you won't have to write any custom code.
  • Mediators extend org.apache.synapse.mediators.AbstractMediator class and require implementing AbstractMediator.mediate

Further reading links

what i have written above just scratches the surface. I am by no means an expert on this and am keen to learn from your experience