In this post i will be explaining how we can transform a SOAP based backend to receive requests in a restful manner through the WSO2 API Cloud.
Steps.
First log into the WSO2 Cloud and navigate to the API Cloud.
In the API cloud select the option to add a new API. We will be creating an API to demonstrate an invocation to the backend soap service ws.cdyne.com/phoneverify/phoneverify.asmx?wsdl
Give a name, context and version to the API and add a resource name with a GET Method. The resource name can be anything which you like since we will invoke the actual service usiing a custom sequence.
Mention the URI template as indicated in the below screenshot.
Next go to the implement tab. And select the endpoint type as HTTP/SOAP Endpoint and specify the endpoint as http://ws.cdyne.com/phoneverify/phoneverify.asmx. There is an important step we need to do here. We need to set the SOAP version for this request. In order to do that we need to select the advanced option for the endpoint. You can find the icon to configure advanced options below.
Click on the advanced option and set the format as the SOAP version we need to set. In this case we will set it as SOAP 1.1
<?xml version="1.0" encoding="UTF-8"?>
<sequence name="phoneInSequence" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
<header description="SOAPAction" name="SOAPAction" scope="transport" value="http://ws.cdyne.com/PhoneVerify/query/CheckPhoneNumber"/>
<property name="uri.var.phoneNumber" expression="$url:PhoneNumber"/>
<property name="REST_URL_POSTFIX" scope="axis2" action="remove"/>
<payloadFactory description="transform" media-type="xml">
<format>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:quer="http://ws.cdyne.com/PhoneVerify/query">
<soapenv:Header/>
<soapenv:Body>
<quer:CheckPhoneNumber>
<!--Optional:-->
<quer:PhoneNumber>$1</quer:PhoneNumber>
<!--Optional:-->
<quer:LicenseKey>0</quer:LicenseKey>
</quer:CheckPhoneNumber>
</soapenv:Body>
</soapenv:Envelope>
</format>
<args>
<arg expression="get-property('uri.var.phoneNumber')"/>
</args>
</payloadFactory>
<property description="messageProperty" name="messageType"
scope="axis2" type="STRING" value="application/soap+xml"/>
</sequence>
Next we need to create an out sequence which transforms the response as a json and sends it back to the API as the response. I created the out sequence as outSequence.xml. The content is as mentioned below.
<?xml version="1.0" encoding="UTF-8"?>
<sequence name="phoneOutSequence" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
<property description="message" name="messageType" scope="axis2"
type="STRING" value="application/json"/>
</sequence>
We need to then upload these two sequences to the in an out sequence of the API as below.
n the manage tab select a tier and publish the API.
Next navigate to the API in the store and subscribe to it with one of the tiers mentioned and generate the tokens for the subscription.
Next we can now invoke the API by just simply providing the phone number as the input. This phone number is captured into a variable and then using this the request payload will be construcred. Using the header mediator we can then set the SOAP action and the constructed request payload will be sent to the soap backend. The soap backend will accept this and return a xml payload. But due to the transformation in the out sequence it will return it as a JSON. This is what happens in the transformation.
Steps.
First log into the WSO2 Cloud and navigate to the API Cloud.
In the API cloud select the option to add a new API. We will be creating an API to demonstrate an invocation to the backend soap service ws.cdyne.com/phoneverify/phoneverify.asmx?wsdl
Give a name, context and version to the API and add a resource name with a GET Method. The resource name can be anything which you like since we will invoke the actual service usiing a custom sequence.
Mention the URI template as indicated in the below screenshot.
Next go to the implement tab. And select the endpoint type as HTTP/SOAP Endpoint and specify the endpoint as http://ws.cdyne.com/phoneverify/phoneverify.asmx. There is an important step we need to do here. We need to set the SOAP version for this request. In order to do that we need to select the advanced option for the endpoint. You can find the icon to configure advanced options below.
Click on the advanced option and set the format as the SOAP version we need to set. In this case we will set it as SOAP 1.1
In the next step we need to add a custom sequence which tranforms this rest call to a SOAP call and sends it to our SOAP backend. So we need to create a sequence simillar to the one below and save it as an xml file. I have saved my sequence as phoneInSequence.xml. I will explain what each step does.
<sequence name="phoneInSequence" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
<header description="SOAPAction" name="SOAPAction" scope="transport" value="http://ws.cdyne.com/PhoneVerify/query/CheckPhoneNumber"/>
<property name="uri.var.phoneNumber" expression="$url:PhoneNumber"/>
<property name="REST_URL_POSTFIX" scope="axis2" action="remove"/>
<payloadFactory description="transform" media-type="xml">
<format>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:quer="http://ws.cdyne.com/PhoneVerify/query">
<soapenv:Header/>
<soapenv:Body>
<quer:CheckPhoneNumber>
<!--Optional:-->
<quer:PhoneNumber>$1</quer:PhoneNumber>
<!--Optional:-->
<quer:LicenseKey>0</quer:LicenseKey>
</quer:CheckPhoneNumber>
</soapenv:Body>
</soapenv:Envelope>
</format>
<args>
<arg expression="get-property('uri.var.phoneNumber')"/>
</args>
</payloadFactory>
<property description="messageProperty" name="messageType"
scope="axis2" type="STRING" value="application/soap+xml"/>
</sequence>
Next we need to create an out sequence which transforms the response as a json and sends it back to the API as the response. I created the out sequence as outSequence.xml. The content is as mentioned below.
<?xml version="1.0" encoding="UTF-8"?>
<sequence name="phoneOutSequence" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
<property description="message" name="messageType" scope="axis2"
type="STRING" value="application/json"/>
</sequence>
We need to then upload these two sequences to the in an out sequence of the API as below.
n the manage tab select a tier and publish the API.
Next navigate to the API in the store and subscribe to it with one of the tiers mentioned and generate the tokens for the subscription.
Next we can now invoke the API by just simply providing the phone number as the input. This phone number is captured into a variable and then using this the request payload will be construcred. Using the header mediator we can then set the SOAP action and the constructed request payload will be sent to the soap backend. The soap backend will accept this and return a xml payload. But due to the transformation in the out sequence it will return it as a JSON. This is what happens in the transformation.
Thank you for sharing such valuable information and tips. This can give insights and inspirations for us; very helpful and informative! Would love to see more updates from you in the future.
ReplyDeletejava certification training in chennai
struts training in chennai
Thanks for this. I am looking for an inverse solution of exposing a soap service from a Rest API.
ReplyDelete