Tuesday, May 2, 2017

How to Convert a JSON Message to SOAP and SOAP to JSON in a Generic Way WSO2 APIM.

Please note that we have tested this scenario on APIM 2.1.0. In this scenario we are using synapse IN and OUT extensions to transform json to xml and vice versa in a generic way.
  • Use below configuration as the IN extension. 
Please find more information on adding Mediation Extensions in [1]

admin-TestPhoneApi:v1.0.0-IN
<?xml version="1.0" encoding="UTF-8"?>
<sequence xmlns="http://ws.apache.org/ns/synapse" name="admin--TestPhoneApi:v1.0.0--IN">
    <log level="custom">
        <property name="TRACE" value="Global Mediation Extension"/>
    </log>
 <enrich>
            <source clone="true" xpath="$body/jsonObject/node()"/>
            <target type="body"/>
         </enrich>
         <enrich>
            <source type="body" clone="true"/>
            <target type="property" property="requestPayload"/>
         </enrich>       
         <payloadFactory media-type="xml">
             <format>
           <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://ws.cdyne.com/PhoneVerify/query">
 <soapenv:Body>
       $1
 </soapenv:Body>
</soapenv:Envelope>
</format>
           <args>
               <arg evaluator="xml" expression="$ctx:requestPayload"/>
            </args>
         </payloadFactory>
</sequence>

  • Use below configuration as the OUT extension.

admin-TestPhoneApi:v1.0.0-OUT

<?xml version="1.0" encoding="UTF-8"?>
<sequence xmlns="http://ws.apache.org/ns/synapse" name="admin--TestPhoneApi:v1.0.0--OUT">
    <log level="custom">
        <property name="TRACE" value="Global Mediation Extension"/>
    </log>
<property name="messageType" value="application/json" scope="axis2"/>
</sequence>

  • Create a simple API to send the message to back-end. In here we used "http://ws.cdyne.com/phoneverify/phoneverify.asmx" as the endpoint.

<?xml version="1.0" encoding="UTF-8"?>
<api xmlns="http://ws.apache.org/ns/synapse"
     name="admin--TestPhoneApi"
     context="/TestPhoneApi/1.0.0"
     version="1.0.0"
     version-type="context">
   <resource methods="POST" url-mapping="/*" faultSequence="fault">
      <inSequence>              
         <log level="full"/>
         <property name="api.ut.backendRequestTime"
                   expression="get-property('SYSTEM_TIME')"/>
         <filter source="$ctx:AM_KEY_TYPE" regex="PRODUCTION">
            <then>
               <send>
                  <endpoint name="admin--TestPhoneApi_APIproductionEndpoint_0">
                     <http format="soap12"
                           uri-template="http://ws.cdyne.com/phoneverify/phoneverify.asmx"/>
                     <property name="ENDPOINT_ADDRESS"
                               value="http://ws.cdyne.com/phoneverify/phoneverify.asmx"/>
                 </endpoint>
               </send>
            </then>
            <else>
               <sequence key="_sandbox_key_error_"/>
           </else>
         </filter>
      </inSequence>
      <outSequence>
         <class name="org.wso2.carbon.apimgt.gateway.handlers.analytics.APIMgtResponseHandler"/>
         <send/>
      </outSequence>
   </resource>
   <handlers>
      <handler class="org.wso2.carbon.apimgt.gateway.handlers.common.APIMgtLatencyStatsHandler"/>
      <handler class="org.wso2.carbon.apimgt.gateway.handlers.security.CORSRequestHandler">
         <property name="apiImplementationType" value="ENDPOINT"/>
      </handler>
      <handler class="org.wso2.carbon.apimgt.gateway.handlers.security.APIAuthenticationHandler"/>
      <handler class="org.wso2.carbon.apimgt.gateway.handlers.throttling.ThrottleHandler"/>
      <handler class="org.wso2.carbon.apimgt.gateway.handlers.analytics.APIMgtUsageHandler"/>
      <handler class="org.wso2.carbon.apimgt.gateway.handlers.analytics.APIMgtGoogleAnalyticsTrackingHandler">
         <property name="configKey" value="gov:/apimgt/statistics/ga-config.xml"/>
      </handler>
      <handler class="org.wso2.carbon.apimgt.gateway.handlers.ext.APIManagerExtensionHandler"/>
   </handlers>
</api>

  • Request 1 with headers :
{"CheckPhoneNumber":{"PhoneNumber":"18006785432","LicenseKey":"0"}}
  • Response for request 1:

{
  "CheckPhoneNumberResponse": {
    "CheckPhoneNumberResult": {
      "Company": "Toll Free",
      "Valid": true,
      "Use": "Assigned to a code holder for normal use.",
      "State": "TF",
      "RC": null,
      "OCN": null,
      "OriginalNumber": 18006785432,
      "CleanNumber": 8006785432,
      "SwitchName": null,
      "SwitchType": null,
      "Country": "United States",
      "CLLI": null,
      "PrefixType": "Landline",
      "LATA": null,
      "sms": "Landline",
      "Email": null,
      "AssignDate": null,
      "TelecomCity": null,
      "TelecomCounty": null,
      "TelecomState": "TF",
      "TelecomZip": null,
      "TimeZone": null,
      "Lat": null,
      "Long": null,
      "Wireless": false
    }
  }
}


  • Request 2 with headers:
{"CheckPhoneNumbers":{"PhoneNumbers":{"string":"1"},"LicenseKey":"0"}}
  • Response for request 2
{
  "CheckPhoneNumbersResponse": {
    "CheckPhoneNumbersResult": {
      "PhoneReturn": {
        "Valid": false,
        "OriginalNumber": 1,
        "CleanNumber": null,
        "Wireless": false
      }
    }
  }
}


No comments:

Post a Comment