Introduction

This document describes how to get started with developing RESTful Services using Oracle REST Data Services.

Prerequisities

About Examples in this document

This document assumes your environment has been configured as follows:

Lesson 1 - REST enable a database table

Create a database schema

Let's create a database schema, that we can use to try out RESTful Services. In SQL Developer connect to the database as sys and enter the following in the SQL Worksheet:

CREATE USER ordstest IDENTIFIED BY password;
GRANT "CONNECT" TO ordstest;
GRANT "RESOURCE" TO ordstest;
GRANT UNLIMITED TABLESPACE TO ordstest

Connect to ordstest Schema

In SQL Developer create a connection to the ordstest schema, connect to it and open an SQL worksheet.

Create a database table

Enter the following in the SQL Worksheet to create an example table:


CREATE TABLE EMP ( EMPNO NUMBER(4,0), ENAME VARCHAR2(10 BYTE), JOB VARCHAR2(9 BYTE), MGR NUMBER(4,0), HIREDATE DATE, SAL NUMBER(7,2), COMM NUMBER(7,2), DEPTNO NUMBER(2,0), CONSTRAINT PK_EMP PRIMARY KEY (EMPNO) );

Insert some sample data

Execute the following insert statements to add some sample data to the EMP table:

Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7369,'SMITH','CLERK',7902,to_date('17-DEC-80','DD-MON-RR'),800,null,20);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7499,'ALLEN','SALESMAN',7698,to_date('20-FEB-81','DD-MON-RR'),1600,300,30);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7521,'WARD','SALESMAN',7698,to_date('22-FEB-81','DD-MON-RR'),1250,500,30);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7566,'JONES','MANAGER',7839,to_date('02-APR-81','DD-MON-RR'),2975,null,20);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7654,'MARTIN','SALESMAN',7698,to_date('28-SEP-81','DD-MON-RR'),1250,1400,30);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7698,'BLAKE','MANAGER',7839,to_date('01-MAY-81','DD-MON-RR'),2850,null,30);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7782,'CLARK','MANAGER',7839,to_date('09-JUN-81','DD-MON-RR'),2450,null,10);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7788,'SCOTT','ANALYST',7566,to_date('19-APR-87','DD-MON-RR'),3000,null,20);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7839,'KING','PRESIDENT',null,to_date('17-NOV-81','DD-MON-RR'),5000,null,10);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7844,'TURNER','SALESMAN',7698,to_date('08-SEP-81','DD-MON-RR'),1500,0,30);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7876,'ADAMS','CLERK',7788,to_date('23-MAY-87','DD-MON-RR'),1100,null,20);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7900,'JAMES','CLERK',7698,to_date('03-DEC-81','DD-MON-RR'),950,null,30);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7902,'FORD','ANALYST',7566,to_date('03-DEC-81','DD-MON-RR'),3000,null,20);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7934,'MILLER','CLERK',7782,to_date('23-JAN-82','DD-MON-RR'),1300,null,10);
commit;

Enable the EMP table for REST

In SQL Developer right click on the ordstest connection, and choose 'REST Services| Enable RESTful Services...', the following dialog appears:

Enable RESTful Services Wizard Step 1 of 3

Enable RESTful Services Wizard Step 2 of 2

Next right click on the EMP table, and choose 'Enable RESTful Services...', the following dialog appears:

Enable RESTful Services Wizard Step 2 of 2

Enable RESTful Services Wizard Step 2 of 2

The EMP table is now exposed as a REST HTTP endpoint.

Test the REST Endpoint

In a web browser enter the following URL:

http://localhost:8080/ords/ordstest/emp/

A JSON document similar to the following will be displayed:

EMP Table JSON

Lesson 2 - Create a RESTful Service from an SQL Query

Configure RESTful Services User

Oracle REST Data Services provides a REST API (called the Resource Modules API) that enables Oracle SQL Developer to create and edit RESTful Service definitions. Access to the Resource Modules API is protected, a user with the correct role must be provisioned and the created user's credentials must be used when accessing the API from SQL Developer.

In the folder where Oracle REST Data Services was installed, enter the following command in a command prompt:

java -jar ords.war user test_developer "SQL Developer"

Tip: It is not recommended to store user credentials in the credentials file in production deployments, instead users should be provisioned in the host application server.

Create RESTful Service

Tip: To complete this step Oracle REST Data Services must be running Tip: To complete this step the ordstest schema must have been created, and the schema enabled for use with ORDS, see Lesson 1 in this tutorial.

Create RESTful Connection

Create REST Connection 1

Create REST Connection 1

The existing Modules and Privileges will be loaded, if this is your first time using this tutorial, then there will be no Modules or Privileges yet.

Create Module

Create Resource Module 1

Create Resource Module 2

Create Resource Module 3

Create Resource Module 4

The resource module is created, the next step is to define the query for the GET resource handler.

Define Resource Handler Query

Test RESTful Service

In a web browser enter the following URL:

http://localhost:8080/ords/ordstest/test/emp/

A JSON document similar to the following will be displayed:

EMP REST JSON

Lesson 3 - Protecting Resources

About Protected Resources

Up to this point we have deliberately disabled security on the RESTful endpoints we have created, because it is easier to test them without security. In this lesson we will protect the /test/emp/ service, requiring users to authenticate before accessing the service.

Controlling access to protected resources is done by defining 'Privileges'. Privileges restrict access to only users having at least one of a set of specified roles. A privilege is then associated with one or more Resource Modules, before those Resource Modules can be accessed the user must be authenticated, and then authorised to ensure they have one of the required roles.

Create a Privilege

Create Privilege

We have created a Privilege that protects the test module. We have not restricted it to any particular role, this will just require that the user be authenticated before accessing the test module.

Test RESTful Service

In a web browser enter the following URL:

http://localhost:8080/ords/ordstest/test/emp/

You will now be prompted to sign in. Click the link to sign-in and enter the test_developer credentials. The contents of the JSON document will now be displayed.

Lesson 4 - Register an OAuth Client Application

About OAuth 2.0

OAuth 2.0 is a standard Internet protocol that provides a means for HTTP servers providing REST APIs to give limited access to third party applications on behalf of an end user.

So before a third party application can access a REST API it must be registered and the user must approve access.

This lesson outlines how to complete these steps. It is not a full featured demonstration of how to create and integrate a third party application, it just outlines the concepts involved.

About Third Party Users

Before a third party can register an application they must be assigned a user identity that enables them to register applications. Users possessing the SQL Developer (such as the test_developer user created in Lesson 1) are permitted to register OAuth clients.

Tip In a real application you may wish to provision particular users that can register OAuth clients, these users should be granted the OAuth Client Developer role.

Register a Client Application

In a web browser enter the following URL:

http://localhost:8080/ords/ordstest/oauth/admin/clients/

The client registration is created, and the 'Authorization URI' for the client is displayed. We have created a client that will use the 'Implicit Grant' authorisation flow.

Tip Make a note of the 'Client Identififer' assigned to the client and the 'Authorization URI' value. These values are used to start the authorization flow.

Approving a Client Application

In a real third party client application, the client will initiate the approval flow by directing a web browser to the 'Authorization URI'. The end user will be prompted to sign in and approve access to the client application. The browser will be redirected back to the client's registered 'Redirect URI' with a URI fragment containing the access_token for the approval.

To simulate this process, enter the 'Authorization URI' that was noted in the previous step in a web browser, the URL should look like the following:

http://localhost:8080/ords/ordstest/oauth/auth?response_type=token&client_id=5B77A34A266EFB0056BE3497ED7099.&state=d5b7944-d27d-8e2c-4d5c-fb80e1114490&_auth_=force
http://example.org/redirect#token_type=bearer&access_token=-i_Ows8j7JYu0p07jOFMEA..&expires_in=3600

Issuing an Authorized Request

Once an access token has been acquired, the client application needs to remember the access token and include it with every request to the protected resource. The access token must be included in the HTTP Authorization request header as shown below:

Host: localhost:8080
GET /ords/ordstest/test/emp/
Authorization: Bearer -i_Ows8j7JYu0p07jOFMEA..

Create HTTP Request

To demonstrate creating a valid HTTP request we will use the cURL command line tool (Install cURL using the preceding link if necessary), in a real application this request would be performed by the client making a HTTP request, for example performing an XMLHttpRequest.

curl -i -H'Authorization: Bearer -i_Ows8j7JYu0p07jOFMEA..' http://localhost:8080/ords/ordstest/test/emp/
HTTP/1.1 200 OK
Content-Type: application/json
Transfer-Encoding: chunked

{
 "items":[
  {"empno":7369,"ename":"SMITH","job":"CLERK","mgr":7902,"hiredate":"1980-12-17T00:00:00Z","sal":800,"comm":null,"deptno":20},
  {"empno":7499,"ename":"ALLEN","job":"SALESMAN","mgr":7698,"hiredate":"1981-02-20T00:00:00Z","sal":1600,"comm":300,"deptno":30},
  {"empno":7521,"ename":"WARD","job":"SALESMAN","mgr":7698,"hiredate":"1981-02-22T00:00:00Z","sal":1250,"comm":500,"deptno":30},
  {"empno":7566,"ename":"JONES","job":"MANAGER","mgr":7839,"hiredate":"1981-04-01T23:00:00Z","sal":2975,"comm":null,"deptno":20},
  {"empno":7654,"ename":"MARTIN","job":"SALESMAN","mgr":7698,"hiredate":"1981-09-27T23:00:00Z","sal":1250,"comm":1400,"deptno":30},
  {"empno":7698,"ename":"BLAKE","job":"MANAGER","mgr":7839,"hiredate":"1981-04-30T23:00:00Z","sal":2850,"comm":null,"deptno":30},
  {"empno":7782,"ename":"CLARK","job":"MANAGER","mgr":7839,"hiredate":"1981-06-08T23:00:00Z","sal":2450,"comm":null,"deptno":10}
 ],
 "hasMore":true,
 "limit":7,
 "offset":0,
 "count":7,
 "links":[
  {"rel":"self","href":"http://localhost:8080/ords/ordstest/test/emp/"},
  {"rel":"describedby","href":"http://localhost:8080/metadata-catalog/test/emp/"},
  {"rel":"first","href":"http://localhost:8080/ords/ordstest/test/emp/"},
  {"rel":"next","href":"http://localhost:8080/ords/ordstest/test/emp/?offset=7"}
 ]
}

Further Reading

Please consult the Oracle REST Data Services Developer Guide for further information on developing, securing and accessing RESTful Services.