Thursday, August 20, 2015

SOAP webservice as a REST API - WSO2 API Manager (1.9) -2

1. I'm going to use the weather service which is available in public. (http://www.webservicex.com/globalweather.asmx)

2. This SOAP service has a operation called "GetCitiesByCountry". You can get the following results when It access directly from the SOAPUI.

3. Now I'm going to create API in WSO2 API Manager to expose this service operation as a REST operation.

Eg: Expose the  GetCitiesByCountry as GET method as bellow.
https://localhost:8243/weather/v1.0.0/GetCitiesByCountry?country=Romania
4. We want to write a mediation extension to read the "country" from request and construct the SOAP payload which is  expect by back end service (I'm going to save this file as JSONtoSOAP.xml). This file shroud upload to /_system/governance/apimgt/customsequences/in in registry.
<sequence xmlns="http://ws.apache.org/ns/synapse" name="admin--weather:vv1.0.0--In">  
 <property name="country" expression="$url:country"></property>
   <payloadFactory media-type="xml">  
            <format>  
               <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"  
                                 xmlns:web="http://www.webserviceX.NET">  
                  <soapenv:Header/>  
                  <soapenv:Body>  
                     <web:GetCitiesByCountry>  
                     <web:CountryName>$1</web:CountryName>  
                     </web:GetCitiesByCountry>  
                  </soapenv:Body>  
               </soapenv:Envelope>  
            </format>  
            <args>  
               <arg xmlns:m0="http://services.samples/xsd" evaluator="xml" 
                                     expression="get-property('country')"></arg>  
            </args>  
         </payloadFactory>          
</sequence>


5. We want another mediation extension file to convert the SOAP response to JSON.(I'm going to save this file as SOAPtoJSON.xml). This file should upload to /_system/governance/apimgt/customsequences/out in registry.
<sequence xmlns="http://ws.apache.org/ns/synapse" name="admin--weather:vv1.0.0--Out">
   <property name="messageType" value="application/json" scope="axis2"/>
</sequence>

6.  Create an API as bellow. (Make sure to add new query parameter called country for GetCitiesByCountry resource)




7. Go to the implement wizard  and select the end point type as "Address Endpoint". Then define the endpoint as http://www.webservicex.com/globalweather.asmx


8.  Click on the "Advance Options" button and select the "Format" as SOAP 1.2.


9. Go to the manage wizard and select the custom mediation form the "Message Mediation Policies".


10. Click on "save & publish" button to publish this API to store.

11. Login to the store ,  subscribe that API  to an application and generate a token.

12. Use the following Curl command to invoke the  GetCitiesByCountry operation.

curl  -k -H "Authorization :Bearer 99215d98fa10ce6f2d83cd11ba9828" -H "Content-Type:application/json"   https://localhost:8243/weather/v1.0.0/GetCitiesByCountry?country=Romania

13. You can also use the SOAPUI to invoke that API. (Make sure to define the  Authorization header)


Related posts:

i) http://www.vitharana.org/2015/01/soap-web-service-as-rest-api-using-wso2.html

ii) http://www.vitharana.org/2015/08/advance-mediation-with-wso2-api-manager.html