2
votes

is there any sample code who can help for my below requirement. i am publishing Rest APIs in WSO2 API Manager , i would like to intercept my own API input field validations for each API like input parameter format and value etc .If validation success then API Manager should allow the request to invoke back-end , else reject the request with error message.

I have gone though some documents and i understand we can achieve this by adding mediation extensions and custom handler, however I couldn't find any sample code for this.

https://docs.wso2.com/display/AM140/Adding+a+Mediation+Extension

If we are writing custom handler , should we write it for each API and do config changes in API Synapsis file? I would like to have a single handler which will invoke for All APIs, and handler will execute the corresponding method which is applicable for that specific API .

2

2 Answers

0
votes

You can have single custom handler which will be used by all APIs. You can use the latest version of the API Manager which is 1.9.1

You can extend the API Manager to support any custom authentication mechanism by writing your own authentication handler class. This custom handler must extend org.apache.synapse.rest.AbstractHandler class and implement the handleRequest() and handleResponse() methods.

You can find a sample implementation here. For more details, please have a look on Writing Custom Handlers

0
votes

If you need to access message body within handler then you may use following code block and access message body.

SOAPEnvelope env = messageContext.getEnvelope();
 if (env != null) {
        SOAPBody soapbody = env.getBody();
 }

Also if you need to build message then you can do that as well.

Add following dependency to your handler implementation project

   <dependency>
       <groupId>org.apache.synapse</groupId>
       <artifactId>synapse-nhttp-transport</artifactId>
   </dependency>

Then import RelayUtils to handler as follows.

import org.apache.synapse.transport.passthru.util.RelayUtils;

Then build message before process message body as follows(add try catch blocks when needed).

RelayUtils.buildMessage(((Axis2MessageContext)messageContext).getAxis2MessageContext());

Then you will be able to access message body as follows.

<soapenv:Body><test>sanjeewa</test></soapenv:Body>