问题
For WSO2 api manager, what is the difference or relation between handlers and sequence.
I want to put some conditions for each api to follow. Based on condition it will check if api request has passed the condition or not. I am not sure for this if I need to use sequence or handlers.
回答1:
This answer assumes that by "sequence" the user means "mediation sequence," which are run via the APIManagerExtensionHandler handler (mediation can be global or per-API, but to the best of my knowledge it is executed in the same place).
Both mediation sequences and handlers are code extensions that run after the gateway receives a response or request. Handlers extend the org.apache.synapse.rest.AbstractHandler
class, requiring implementation of AbstractHandler.handleRequest
and AbstractHandler.handleResponse
; mediators extend org.apache.synapse.mediators.AbstractMediator
class and require implementing AbstractMediator.mediate
.
The primary difference between a custom handler and a mediation sequence is that using a custom handler requires you to write your own Java class, then package and deploy your handler. If your requirements can be satisfied with a combination of predefined mediators (provided by WSO2's ESB) then you can write an XML sequence to define the mediation tasks, with no new code needed.
In my experience, here are the primary differences between handlers and mediation sequences. Using one over the other should be determined by your specific requirements.
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.
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's ESB) do not satisfy your requirements, you won't have to write any custom code.
In short: if some combination of existing mediators will achieve your goal, using mediation sequences makes the most sense (even if it's a case of "close, but not quite," writing a custom mediator can be easier than creating an entirely new handler). If you require an increased level of customization or require mediation to occur before or after all other mediation is executed, you should consider writing a handler.
Edit: to actually answer the specific question: you can do logical checks of values in request headers using a mediation sequence fairly easily. These checks become a bit more difficult to do if you need to read body content... in which case a custom handler is essentially your only option.
来源:https://stackoverflow.com/questions/35592712/wso2-handlers-and-sequence