微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 WSO2 APIM 中对自定义响应使用入序列和出序列?

如何解决如何在 WSO2 APIM 中对自定义响应使用入序列和出序列?

我使用的是 WSO2 APIM 2.1.0 和 IS 5.3.0

我目前正在尝试创建一个 API,通过调用管理服务 UserinformationRecoveryService 来注册某个用户,如果创建成功则给出自定义 JSON 响应,如果创建不成功则给出另一个响应,在这种情况下用户已经存在.

到目前为止,我已经按如下方式编写了 in 序列和 out 序列,但是我无法获得预期的输出。(即使用户已经存在,也总是会看到成功响应。也就是说,正在执行 else 块在输出序列中。)

按顺序

<sequence xmlns="http://ws.apache.org/ns/synapse" name="registerTestHrisIn">
   <log level="full" />
   <property name="userName" expression="//userName" scope="default" type="STRING" />
   <property name="password" expression="//password" scope="default" type="STRING" />
   <property name="givenname" expression="//givenname" scope="default" type="STRING" />
   <property name="lastname" expression="//lastname" scope="default" type="STRING" />
   <property name="emailaddress" expression="//emailaddress" scope="default" type="STRING" />
   <property name="organization" expression="//organization" scope="default" type="STRING" />
   <property name="telephone" expression="//telephone" scope="default" type="STRING" />
   <property name="authorization" expression="get-property('transport','Authorization')" scope="default" type="STRING" />
   <payloadFactory media-type="xml">
      <format>
         <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:ser="http://services.mgt.identity.carbon.wso2.org" xmlns:xsd="http://dto.mgt.identity.carbon.wso2.org/xsd">
            <soap:Header />
            <soap:Body>
               <ser:registerUser>
                  <ser:userName>$1</ser:userName>
                  <ser:password>$2</ser:password>
                  <ser:claims>
                     <xsd:claimUri>http://wso2.org/claims/givenname</xsd:claimUri>
                     <xsd:claimValue>$3</xsd:claimValue>
                  </ser:claims>
                  <ser:claims>
                     <xsd:claimUri>http://wso2.org/claims/lastname</xsd:claimUri>
                     <xsd:claimValue>$4</xsd:claimValue>
                  </ser:claims>
                  <ser:claims>
                     <xsd:claimUri>http://wso2.org/claims/emailaddress</xsd:claimUri>
                     <xsd:claimValue>$5</xsd:claimValue>
                  </ser:claims>
                  <ser:claims>
                     <xsd:claimUri>http://wso2.org/claims/organization</xsd:claimUri>
                     <xsd:claimValue>$6</xsd:claimValue>
                  </ser:claims>
                  <ser:claims>
                     <xsd:claimUri>http://wso2.org/claims/telephone</xsd:claimUri>
                     <xsd:claimValue>$7</xsd:claimValue>
                  </ser:claims>
                  <ser:profileName>default</ser:profileName>
                  <ser:tenantDomain>carbon.super</ser:tenantDomain>
               </ser:registerUser>
            </soap:Body>
         </soap:Envelope>
      </format>
      <args>
         <arg evaluator="xml" expression="$ctx:userName" />
         <arg evaluator="xml" expression="$ctx:password" />
         <arg evaluator="xml" expression="$ctx:givenname" />
         <arg evaluator="xml" expression="$ctx:lastname" />
         <arg evaluator="xml" expression="$ctx:emailaddress" />
         <arg evaluator="xml" expression="$ctx:organization" />
         <arg evaluator="xml" expression="$ctx:telephone" />
      </args>
   </payloadFactory>
</sequence>

输出序列

<sequence xmlns="http://ws.apache.org/ns/synapse" name="registerTestHrisOut">
   <property xmlns:ns="http://services.mgt.identity.carbon.wso2.org" name="registerUserResponse" expression="//ns:registerUserResponse/ns:return"/>
    <log level="full">
      <property name="registerUserResponse" expression="registerUserResponse"/>
   </log>
   <filter source="get-property('registerUserResponse')" regex=".*18003">
      <then>
         <payloadFactory media-type="json">
            <format>{"ServiceResponse": {"Code": "1","Status": "false","Message": " Failed"}}</format>
         </payloadFactory>
      </then>
      <else>
         <payloadFactory media-type="json">
            <format>{"ServiceResponse": {"Code": "1","Status": "true","Message": " Success"}}</format>
         </payloadFactory>
      </else>
   </filter>
</sequence>

需要捕获的错误响应

   <soapenv:Body>
      <ns:registerUserResponse xmlns:ns="http://services.mgt.identity.carbon.wso2.org">
         <ns:return xmlns:ax2354="http://mgt.identity.carbon.wso2.org/xsd" xmlns:ax2355="http://base.identity.carbon.wso2.org/xsd" xmlns:ax2358="http://beans.mgt.identity.carbon.wso2.org/xsd" xmlns:ax2359="http://dto.mgt.identity.carbon.wso2.org/xsd" xmlns:ax2360="http://mail.mgt.identity.carbon.wso2.org/xsd" xmlns:ax2364="http://beans.mgt.captcha.carbon.wso2.org/xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ax2358:VerificationBean">
            <ax2358:error>18003 Username 'TEST.COM/testUser' already exists in the system. Please pick another username.</ax2358:error>
            <ax2358:key xsi:nil="true" />
            <ax2358:notificationData xsi:nil="true" />
            <ax2358:redirectPath xsi:nil="true" />
            <ax2358:userId xsi:nil="true" />
            <ax2358:verified>false</ax2358:verified>
         </ns:return>
      </ns:registerUserResponse>
   </soapenv:Body>
</soapenv:Envelope>

非常感谢任何实现预期响应的解决方法。 提前致谢。

解决方法

让我们修改序列并尝试场景。

执行以下更改以从响应中提取正确的错误消息并在过滤器中进行验证

  • 更新 out-sequence 中的属性中介,指定到叶子节点的路径以提取错误信息

    <property 
       xmlns:ns="http://services.mgt.identity.carbon.wso2.org" 
       xmlns:xsd="http://beans.mgt.identity.carbon.wso2.org/xsd" 
       name="registerUserResponse" 
       expression="//ns:registerUserResponse/ns:return/xsd:error" 
    />
    
  • 更新Filter mediator中的regex模式如下(.*18003 to .*18003.*:这是定义在提到的key之后,会有一组字符串进行匹配)

    <filter source="get-property('registerUserResponse')" regex=".*18003.*">
        <then>
            ...
        </then>
        <else>
            ...
        </else>
    </filter>
    

请更新上述序列、部署并尝试该场景。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。