简体   繁体   中英

How to use the in sequence and out sequence to a custom response in WSO2 APIM?

I am using WSO2 APIM 2.1.0 and IS 5.3.0

I'm currently trying to create an API that registers a certain user by calling the admin service UserInformationRecoveryService which gives out a custom JSON response if the creation is successful and another response if it is unsuccessful, in which case the user already exists.

So far I have written the in sequence and the out sequence as follows but I am having trouble getting the expected output.(The success response is always seen even when the user already exists. That is, the else block is getting executed in the out sequence.)

In Sequence

<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>

Out 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>

Error response which needs to be caught

   <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>

Any workaround to achieve the expected response is highly appreciated. Thanks in advance.

Let's revamp the sequences and try the scenarios.

Perform the following changes to extract the correct error message from the response and to validate in the Filter

  • Update the property mediator in the out-sequence as following to specify the path up to the leaf node to extract the error message

    <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" />
  • Update the regex pattern in the Filter mediator as following ( .*18003 to .*18003.* : this is to define that after the mentioned key, there will be a set of strings to be matched)

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

Please update the sequences as mentioned, deploy, and try the scenario.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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