Labnn - REST enabling CRUD Scenario:
I have the following DB table Patient. I need to REST enable Create, Retrieve, Update and Delete functionality. For this I will use the SOA DB adapter to service enable the DB operations. I will then create a REST interface in OSB for these services.
DB Setup:
CREATE TABLE PATIENT ( PAT_ID NUMBER NOT NULL , PAT_NAME VARCHAR2(20) NOT NULL , PAT_SURNAME VARCHAR2(20) NOT NULL , PAT_AGE NUMBER(3) NOT NULL , PAT_SEX VARCHAR2(1) NOT NULL , PAT_NATIONALITY VARCHAR2(20) , PAT_COMMENTS VARCHAR2(255) , CONSTRAINT PATIENT_PK PRIMARY KEY ( PAT_ID ) ENABLE );
Create a couple of patients
Insert into patient values (1, 'Name', 'Surname', 21, 'M', 'IE', ''); Insert into patient values (2, 'Name', 'Surname', 22, 'F', 'UK', '');
Service Enable the DB operations - INSERT
Create a new SOA application in JDeveloper
Now create a DB adapter service for INSERT Drag and drop the Database Adapter icon to the External References area.
Create a new DB connection nd point to the schema where you have created the PATIENT table
Amend the JNDI name as shown
Select Insert Only
Import the table
Note, all columns/attributes have been selected
Accept defaults
The service has been created, a couple of files have been added to the project.
We will import these later into OSB for REST enablement.
Service Enable the DB operations SELECT
Drag and drop the Database Adapter icon to the External References area.
Amend the JNDI name as shown
Service Enable the DB operations UPDATE
Drag and drop the Database Adapter icon to the External References area.
Amend the JNDI name as shown
Service Enable the DB operations DELETE
Drag and drop the Database Adapter icon to the External References area.
Amend the JNDI name as shown
Create required data sources on target WLS
The Database adapter will leverage the resource eis/DBscottDS. This needs to be defined on ghe target system, in our case, the WLS hosting OSB.
Define the data source - scottDS
Target to the appropriate WLS server, in my case AdminServer
Create the eis for the database adapter Click on Deployments, the DBAdapter is already deployed. Click on Configuration Outbound connection Pools
Add a new resource eis/DB/scottDS
Configure the XADataSourceName property
Update the DBAdapter
Select and click Update
Create the OSB project
Add the following folders to the project
Import the JCA artifacts
These are available in the JDeveloper project
Click on the JCA folder
Click Import
Click load another and load the following, one after the other o o o InsertPatientService_db.jca UpdatePatientService_db.jca DeletePatientService_db.jca
Move WSDL resources to the WSDL folder
Create OSB Business Services
Back in the JCA folder, select the JCA binding
Target to the Business Services Folder
Move WSDLs to the WSDL folder
Create OSB Proxy Service
The proxy service will accept HTTP requests and will branch on the VERB(method GET, POST, PUT, DELETE). We will use the OSB Conditional Branch for this. Select the Proxy Services folder and create a new proxy service
Click Next>>
Click Next>>
Click Last>>
Click Save
Click on the Process icon
Now we want to branch on the HTTP method. This can be accessed as follows ./ctx:transport/ctx:request/http:http-method/text()
Implementing GET / Select
We will implement a pipeline pair in the GET Branch. In the request pipeline we will process such HTTP requests as http://localhost:7001/PatientREST/Proxy_Services/PatientCRUDProxy/patientID=1 We need to extract the patientID value and pass it on to the SELECT Business service. Add a pipeline pair
Edit the Stage o The first step is to assign the incoming parameters to OSB variables Note, there are 2 parameters patientID=1
Accessed as follows fn:tokenize(data($inbound/ctx:transport/ctx:request/http:relativeURI),'=')[1]
fn:tokenize(data($inbound/ctx:transport/ctx:request/http:relativeURI),'=')[2] o Assign parameter name (i.e. patientID) to a variable
Click on Expression
Add another Assign for the second parameter
We can log these to make sure the assign works as expected
Set severity level to Warning
Save and test
Check OSB log window
So both have been extracted successfully.
The patientID will be used as input to the SELECT service. The service expects the following structure
<soap:Body xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <sel:PatientPrimaryKey xmlns:sel="http://xmlns.oracle.com/pcbpel/adapter/db/top/SelectPatientService"> <sel:patId>1000.00</sel:patId> </sel:PatientPrimaryKey></soap:Body>
We will assign this structure to the OSB $body variable passing thru the incoming patientID. Add an Assign
I have amended the data to use the parameter passed in via the HTTP GET
<soap:Body xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <sel:SelectPatientServiceSelect_inPatIDInputParameters xmlns:sel="http://xmlns.oracle.com/pcbpel/adapter/db/top/SelectPatientService"> <sel:inPatID>{$firstAttrValue}</sel:inPatID> </sel:SelectPatientServiceSelect_inPatIDInputParameters>
</soap:Body>
Now add a Namespace definition for sel
Log $body
Save and test
Now route to the SELECT business service
Save and Test
Add Error Handler
OSB has comprehensive error handling facilities. It is a good idea to add an error handler to the pipeline. This can then Report to the OSB console.
Add an Error Handler to the Request Pipeline
Implementing POST / Insert
Here we simply route to the INSERT service. Add a pipeline pair Add a Route node
Save and Test Click on The Business Service Tester to see what the required input format
<ins:PatientCollection xmlns:ins="http://xmlns.oracle.com/pcbpel/adapter/db/top/InsertPatientService"> <!--Zero or more repetitions:--> <ins:Patient> <ins:patId>1000.00</ins:patId> <!--Optional:--> <ins:patName>string</ins:patName> <!--Optional:--> <ins:patSurname>string</ins:patSurname> <!--Optional:--> <ins:patAge>3</ins:patAge> <!--Optional:--> <ins:patSex>s</ins:patSex> <!--Optional:--> <ins:patNationality>string</ins:patNationality> <!--Optional:--> <ins:patComments>string</ins:patComments> </ins:Patient> </ins:PatientCollection>
Start the proxy test console
o o
Add the payload Click on Transport
Set HTTP Method to POST
Execute Check DB via SQLPLUS
Patient nr 5 has been created
Implementing PUT / Update
Here we simply route to the UPDATE service. Add a pipeline pair Add a Route node
Get the update payload via the business service tester
Test via the Proxy service with the following payload
<upd:PatientCollection xmlns:upd="http://xmlns.oracle.com/pcbpel/adapter/db/top/UpdatePatientService"> <!--Zero or more repetitions:--> <upd:Patient> <upd:patId>5</upd:patId> <!--Optional:--> <upd:patName>string</upd:patName> <!--Optional:--> <upd:patSurname>string</upd:patSurname> <!--Optional:--> <upd:patAge>3</upd:patAge> <!--Optional:--> <upd:patSex>s</upd:patSex> <!--Optional:--> <upd:patNationality>string</upd:patNationality> <!--Optional:--> <upd:patComments>VERY HEALTHY - can be sent home</upd:patComments> </upd:Patient> </upd:PatientCollection>
Set HTTP Method to PUT
Implementing DELETE/ Delete
Here we simply route to the DELETE service. Add a pipeline pair Add a Route node
Test as per usual.
Patient 5 has been deleted.