Tuesday, January 13, 2015

SOAP web service as REST API using WSO2 API Manager -1

Pre-requirement:

Download the latest version of   WSO2 API Manager ,WSO2 Application Server and WSO2 Developer Studio from wso2.com web site.

1. Deploy sample web service.
1.1 Open the carbon.xml file and changed the port off set value to 1, then start the server. (carbon.xml file under the wso2as-5.2.1/repository/conf directory)
 
 <Offset>1</Offset>

1.2. Log in to the administrator console(admin/admin) and deploy the SimpleStockQuoteService.aar file using aar file deploying wizard. (Please check the bellow image)

https://[host or IP]:9444/carbon/

1.3 Download this sample web service archive[i] (SimpleStockQuoteService.aar) and deploy on WSO2 Application Sever.

[i] : https://github.com/ajithnv/blog_resources/tree/master/soaptorest



 1.4 After few seconds refresh the service "List" page, now you should see the "SimpleStockQuoteService" service in the services list. (Please see bellow image)


1.5 Click on the "SimpleStockQuoteService" name, now  you should see the  WSDL locations(1) and endpoints(2) of that services along with some other features.


1.6 Create a SOAP UI project and invoke some operation. (as an example I'm going to invoke the getQuote operation)



1.7 Now I'm going to expose this operation(getQuote)  using WSO2 API Manager.

GET : http://<Host or IP>:<port>/stockquote/1.0.0/getquote?name=IBM

2.0 Create ESB configuration project

WSO2 Developer Studio(DevS)  provides rich graphical editor to create a message mediation flow without writing XML configuration by hand.

2.1 File --> New project ---> Other , then select the "ESB Config Project" and create new esb config project called 'ESBConfigProject'.

[You can find my project in following git repository location [i]. (Download and import that to DevS)]

[i] https://github.com/ajithnv/blog_resources/tree/master/soaptorest

This is to create the back-end payload at the in sequence which is expecting from the StockQuoteService.



When you switch to the "source" view , you should see the following configuration.

<?xml version="1.0" encoding="UTF-8"?>
<sequence xmlns="http://ws.apache.org/ns/synapse" name="admin--StockQuoteAPI:v1.0.0--In">
    <payloadFactory media-type="xml">
        <format>
            <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://services.samples/xsd" xmlns:ser="http://services.samples">
                <soapenv:Header/>
                <soapenv:Body>
                    <ser:getQuote>
                        <!--Optional:-->
                        <ser:request>
                            <!--Optional:-->
                            <xsd:symbol>$1</xsd:symbol>
                        </ser:request>
                    </ser:getQuote>
                </soapenv:Body>
            </soapenv:Envelope>
        </format>
        <args>
            <arg xmlns:m0="http://services.samples/xsd" evaluator="xml" expression="$url:name"/>
        </args>
    </payloadFactory>
</sequence>


Following sequence  is to convert  the message format to JSON at the out sequence.


When you switch to the "source" view , you should see the following configuration.

<?xml version="1.0" encoding="UTF-8"?>
<sequence xmlns="http://ws.apache.org/ns/synapse" name="admin--StockQuoteAPI:v1.0.0--Out">
    <property name="messageType" value="application/json" scope="axis2" type="STRING"/>
</sequence>

3. Create new composite application project  in developer studio to build a carbon archive. (Select the ESBConfigProject project at the creation flow )




4. Use the top right conner button to export as .car file (ESBCARApp_1.0.0.car).


5. Create new server role called EnterpriseServiceBus in API manager(This is to deploy the ESB car file in API Manager).



6. Create new deployment directory called "carbonapps" inside the  wso2am-1.9.0/repository/deployment/server/ directory.

7. Copy the ESBCARApp_1.0.0.car file to wso2am-1.9.0/repository/deployment/server/carbonapps directory. (This car file will deploy the above two sequences in API Manager)

8. 1) Create new  API in API manager.

Name    : StockQuoteAPI
Context : stockquote
Version : 1.0.0
Resource method : getquote
Query parameter : name



8.2) Select the  endpoint type  as "Address Endpoint" (http://localhost:9764/services/SimpleStockQuoteService/).



8.iii) Click on the "Advance Options" and select the message format as "SOAP 1.1".


8.iv) Go to the manage wizard , select the tier and "save & publish".




9. Go to the store , subscribe API to an application , generate a token.



10. Invoke API from SOAP UI. (Make sure to add the Authorization and SOAPAction as the headers)



11. The response message format should be JSON.
{"getQuoteResponse": {"return": {
   "@type": "ax23:GetQuoteResponse",
   "change": 3.8029902107822418,
   "earnings": -9.840572906863278,
   "high": -78.83716414296619,
   "last": 80.02780758052141,
   "lastTradeTimestamp": "Wed Aug 26 16:17:20 EDT 2015",
   "low": -78.09783867850369,
   "marketCap": 5555207.279685723,
   "name": "IBM Company",
   "open": 82.90256019282909,
   "peRatio": -18.258083449962875,
   "percentageChange": -4.95172669142163,
   "prevClose": -76.80129473564324,
   "symbol": "IBM",
   "volume": 5049
}}}

Related posts:

ii) http://www.vitharana.org/2015/08/soap-webservice-as-rest-api-wso2-api.html

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