Friday, September 5, 2014

Protocol Conversion from REST to SOAP using Messaging Bridge Enterprise Integration Pattern (EIP) and Content Type Negotiation using WSO2 ESB

Say you have a SOAP service. You need to expose it as a REST service. Furthermore, you may need other mediations such as XML to JSON conversion to happen.

This could be done by implementing the Messaging Bridge Enterprise Integration Pattern (EIP).
Ref: https://docs.wso2.com/display/IntegrationPatterns/Messaging+Bridge

Furthermore, with the WSO2 ESB Content type negotiations (conversions) such as XML to JSON is just a simple configuration that would take only a few seconds.

For the back-end service we'll use the ZooDataService we built in a previous example using WSO2 DSS to expose the data as SOAP services: Expose Data as a Service using WSO2 Data Services Server (DSS)

Now that we've got a service we'll see how to do the SOAP to REST conversion, and the XML to JSON conversion.

1. First lets take a look at the SOAP to REST Conversion:

For the SOAP to REST conversion we will adhere to the Messaging Bridge Integration pattern as mentioned above.

We will create an API in the WSO2 ESB for this purpose and expose the SOAP service as a REST service via the said API.

Below is the architecture for our system:




Now lets look at how to Create the ESB API:

Go to the ESB management console and add an API

Switch to Source View and enter the following configuration:

<api xmlns="http://ws.apache.org/ns/synapse" name="ZooData" context="/zoodata">
   <resource methods="GET" uri-template="/{animalID}">
      <inSequence>
         <payloadFactory media-type="xml">
            <format>
               <p:getAnimalByID xmlns:p="http://ws.wso2.org/dataservice">
                  <a:AnimalID xmlns:a="http://ws.wso2.org/dataservice">$1</a:AnimalID>
               </p:getAnimalByID>
            </format>
            <args>
               <arg evaluator="xml" expression="get-property('uri.var.animalID')"></arg>
            </args>
         </payloadFactory>
         <log level="full"></log>
         <call>
            <endpoint>
               <address uri="http://10.100.5.132:9769/services/ZooDataService/select_with_key_animal_operation" format="soap12"></address>
            </endpoint>
         </call>
         <property name="messageType" value="application/json" scope="axis2"></property>
         <respond></respond>
      </inSequence>
   </resource>
</api>
                        
                        

Now you can invoke the service Restully using the following GET request:

http://localhost:8281/zoodata/003

you will get the following XML output:

<animalCollection>
<animal>
<AnimalID>003</AnimalID>
<Species>Penguin</Species>
<Name>Rico</Name>
<Country>Madagaskar</Country>
<Speciality>Demolitions</Speciality>
</animal>
</animalCollection>


2. Next we'll look at how to convert the content types (Content Type Negotiation):

To convert the content type from XML to JSON  in the outgoing response simple change the value attribute of the 'messageType' property to 'application/json'. This works vice versa as well.

<property name="messageType" value="application/json" scope="axis2"></property>

now you'll get the output in JSON format:

{"animalCollection":{"animal":{"AnimalID":"003","Species":"Penguin","Name":"Rico","Country":"Madagaskar","Speciality":"Demolitions"}}}




Furthermore:
Now since you have achieved protocol & content type conversion you can use the WSO2 API Manager to expose this as a Managed API if you wish. You will be using the API Façade pattern in doing so.


For further information on API Façade pattern refer:
http://wso2.com/blogs/architecture/2013/05/a-pragmatic-approach-to-the-api-faade-pattern/
 


1 comment:

  1. I think there is a need to provide some more information about REST API,its utilities and other aspects.

    Powerbi Read Soap

    ReplyDelete