简体   繁体   English

API请求输入字段的wso2 API Manager定制验证

[英]wso2 API Manager custom validations for API request input fields

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. 我正在WSO2 API管理器中发布Rest API,我想拦截每个API的我自己的API输入字段验证,例如输入参数格式和值等。如果验证成功,则API管理器应允许请求调用后端,否则拒绝带有错误消息的请求。

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 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? 如果要编写自定义处理程序,是否应该为每个API编写它,并在API Synapsis文件中进行配置更改? 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 . 我想要一个可以为所有API调用的处理程序,并且处理程序将执行适用于该特定API的相应方法。

You can have single custom handler which will be used by all APIs. 您可以有一个供所有API使用的自定义处理程序。 You can use the latest version of the API Manager which is 1.9.1 您可以使用API管理器的最新版本1.9.1。

You can extend the API Manager to support any custom authentication mechanism by writing your own authentication handler class. 您可以通过编写自己的身份验证处理程序类来扩展API管理器以支持任何自定义身份验证机制。 This custom handler must extend org.apache.synapse.rest.AbstractHandler class and implement the handleRequest() and handleResponse() methods. 此自定义处理程序必须扩展org.apache.synapse.rest.AbstractHandler类,并实现handleRequest()handleResponse()方法。

You can find a sample implementation here . 您可以在此处找到示例实现。 For more details, please have a look on Writing Custom Handlers 有关更多详细信息,请参见编写自定义处理程序

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. 然后按以下方式将RelayUtils导入到处理程序中。

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

Then build message before process message body as follows(add try catch blocks when needed). 然后按以下步骤在消息正文之前构建消息(必要时添加try catch块)。

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

Then you will be able to access message body as follows. 然后,您将可以按以下方式访问邮件正文。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM