Tuesday, February 4, 2014

Rest Service with ODE process

Overview

Web services can be two types as SOAP or REST. BPEL processes communicate using soap messages.This post describes how we can call Rest service within our BPEL process.

REST vs SOAP

SOAP is a communication protocol defines how we can communicate using XML based messages. SOAP Messages contains the actual data or payload. WSDL file provides how those data should be defined,operations on data and port types . SOAP is mainly used in long running processes  where we need to store the state(Session). 

Rest is a architectural style which communicate with URI(Unified resource Identifier) where data transmits like HTTP requests in real world scenarios. Rest services differ from traditional HTTP requests since rest supports unique operations GET,POST ,PUT and DELETE . Rest Messages are stateless.

Before Start

I have developed rest service based BPEL process using WSO2 Developer studio and run it using WSO2 BPS Server.If you need to get basic idea about BPEL read my previous posts on BPEL and developing simple BPEL process.

Why we need to access Rest Service via WSDL

Since Rest is light weight most of social networks and public services are deployed as restful services. There are some requirements where we need to provide WSDL file for client but through that we need to access rest service specially writing BPEL processes.

Apache ODE – WSDL 1.1 Extensions for REST

Traditional HTTP binding on WSDL support only GET and POST operations. But Rest requires additional operations of PUT and DELETE. This can be satisfied using WSDL 1.1 Extensions for REST with Apache ODE.

User Service Rest Process

I wrote sample Rest process called User rest process. This is the BPEL model of the process.

Here User Process takes input as user ID and user name and create user  in the service store with PUT operation. POST will add username appending with '@wso2.com' and GET operation provides the user object to response which will be the output. Before sending the output DELETE operation will delete the user. Process doesn't do meaningful task but it was modeled to cover all four operations of rest service. Lets look at sample Rest request made during the the process.

Get
Get operation will give me user name based on given user_id.
http://localhost:9764/UserService_1.0.0/services/user_service/userservice/users/name/1
Put
Put will add new user
http://localhost:9764/UserService_1.0.0/services/user_service/userservice/users/name/1/waruna
Post
Post will update the username for given id
location="http://localhost:9764/UserService_1.0.0/services/user_service/userservice/users/name/1/waruna@wso2.com
Delete
delete will remove the user for given ID
location="http://localhost:9764/UserService_1.0.0/services/user_service/userservice/users/name/1

All these rest operations are to be specified to get user input than hard coded values. We know BPEL access external service using partner links via  WSDL files.When developing BPEL process to  call Rest service we need to create separate WSDL file for that. That will use WSDL 1.1 extensions to add rest service calls.
For example I will describe How step by step we can get user input and pass into PUT Rest operation via WSDL. For other Rest operations we can use the same approach.

1) Here is the user schema for consist of username and user ID.

<wsdl:types>
<xsd:schema attributeFormDefault="qualified"
elementFormDefault="unqualified" targetNamespace="http://wso2.org/bps/ignorens">
<xsd:element name="user">
<xsd:complexType>
<xsd:sequence>
<xsd:element minOccurs="0" name="uid" nillable="true"
type="xsd:string" />
<xsd:element minOccurs="0" name="uname" nillable="true"
type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="userID">
<xsd:complexType>
<xsd:sequence>
<xsd:element minOccurs="0" name="uid" nillable="true"
type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</wsdl:types>

2) Put Message can be defined as follows.
<wsdl:message name="putUserNameRequest">
<wsdl:part name="user" element="ignore:user" />
</wsdl:message>
<wsdl:message name="putUserNameResponse">
<wsdl:part name="part" type="xsd:string" />
</wsdl:message>


3) Port type for PUT operation can be specified as follows.
<wsdl:portType name="UserServicePutPT">
<wsdl:operation name="putUserName">
<wsdl:input message="tns:putUserNameRequest" />
<wsdl:output message="tns:putUserNameResponse" />
</wsdl:operation>
</wsdl:portType>


4) WSDL Binding for PUT operation can be specified as follows. Here I am using {} to get message request data to pass it via Rest Put URL.

<wsdl:binding name="UserServicePutHTTP" type="tns:UserServicePutPT">
<http:binding verb="PUT" />
<wsdl:operation name="putUserName">
<http:operation
location="http://10.100.5.24:9764/UserService_1.0.0/services/user_service/userservice/users/name/{uid}/{uname}" />
<wsdl:input>
<http:urlReplacement />
</wsdl:input>
<wsdl:output>
<mime:content part="part" type="text/xml" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
5) Service for Put operation can be written as follows.
<wsdl:service name="UserServicePut">
<wsdl:port binding="tns:UserServicePutHTTP" name="UserServicePutHTTP">
<http:address
location="http://10.100.5.24:9764/UserService_1.0.0/services/user_service/userservice/users/name/1" />
</wsdl:port>
</wsdl:service>


You can checkout Rest user process from following link.

https://svn.wso2.org/repos/wso2/carbon/platform/branches/turing/products/bps/3.2.0/modules/samples/product/src/main/resources/bpel/2.0/UserRestProcess/

Please find sample rest process from here.
https://github.com/wso2/product-bps/tree/master/modules/services/UserService

2 comments :

  1. how can I send json content RequestBody param to Rest service POST method from bpel process?

    ReplyDelete
  2. Do you have the tutorial or video about how can I create this Project? I want to know this because in your post, I can't create my project. I don't know what I have to do first, then and last. Thanks!!!

    ReplyDelete