Wednesday, August 14, 2019

ORDS : AutoREST Enabling Objects



ORDS : AutoREST Enabling Objects



AutoREST is a feature in ORDS that allows us to interact with oracle database without writing any code. It provides predefined REST handlers (GET, POST, PUT, DELETE) to access a database object.

Handlers are nothing but the set of action that needs to be performed when a particular REST operation is called on a resource. For example, if we call a REST web service with GET method, ORDS needs to know what action it is supposed to perform on the resource with GET method. This action is defined with the help of handlers.

At very high level, meaning of each REST method can be defined as (though it can vary depending on the resource) –

1. GET - Retrieve records with or without search criteria

2. POST - Insert new record

3. PUT - Update existing record

4. DELETE - Delete existing record

Let’s start AutoREST enabling a database table. But before that I am assuming below things –

  • You have access to oracle database.
  • You have ORDS installed on oracle database.


For this demonstration, I am using SQL Developer and I have installed ORDS on Oracle XE Database installed on my local machine (i.e. localhost).


Step 1 : REST enable a database schema.



When ORDS is installed on database, it enables a ‘REST Data Services’ menu option in connection tree as shown below. Expand this menu






Under the connection tree, right click on schema name, select REST Services -> Enable REST Services option.



Mark the Enable schema checkbox. Give alias to your database schema if you don’t want to reveal the schema name otherwise keep it same as schema name. Click on Next.



Click on Finish.



Step 2 : REST enable database table.

Right click on a table name that you want to REST enable and select Enable REST Services.



Make sure the Enable object checkbox is marked. Give object alias. This will be part of a URI that we will be using to access DEPARTMENTS table. Click on Next.




Click on Finish.



We can also perform above two steps using APIs provided by ORDS as shown below –

Step 1 : REST enable a schema

BEGIN
    ORDS.ENABLE_SCHEMA(p_enabled => TRUE,
                       p_schema => 'HR',
                       p_url_mapping_type => 'BASE_PATH',
                       p_url_mapping_pattern => 'hr',
                       p_auto_rest_auth => FALSE);
    commit;
END;

Step 2 : REST enable a database table

BEGIN
    ORDS.ENABLE_OBJECT(p_enabled => TRUE,
                       p_schema => 'HR',
                       p_object => 'DEPARTMENTS',
                       p_object_type => 'TABLE',
                       p_object_alias => 'departments',
                       p_auto_rest_auth => FALSE);
    commit;
END;

That’s it. We have REST enabled a table DEPARTMENTS. Now let’s try accessing it from REST client. Here we will be using Postman – addon by google chrome which acts as a REST client. A simple web browser like Mozilla Firefox also works as a REST client but it has limitation of being able to call only GET methods. 


1) GET :- We call GET method on departments table (which is REST enabled) with URI below - http://localhost:9090/ords/hr/departments/

It tells the ORDS installed on database running at localhost:9090 to connect to the ‘hr’ schema to query the ‘departments’ table. Below is the output in JSON format we get in browser -




We can also query single record by just appending a primary key column value to the URI (as the one returned in above output as href link). It also includes additional links to capture metadata information.






2) POST :- We can insert new record in departments table with POST method. For this, we will use Postman. URI will remain same as the one in GET method. o   Here we need to select POST method and give input values to the columns in departments table as key:value pair in body section. 


  • Here we need to select POST method and give input values to the columns in departments table as key:value pair in body section.
  • Then Click on send. We will get 201 response code which means record is created successfully.



3) PUT :- We can update an existing record with PUT method. Follow the below steps –
  • Select PUT method.
  • URI will remain same here as well except one change that we need to give the primary key id of the record that we want to update in the URI itself.
  • Specify the values for fields that we intend to update as key:value pair in the body section.
  • Click on send. We will get response code 200 which means the operation is performed successfully.


4) DELETE :- We can delete an existing record using DELETE method with below steps - 
  • Select DELETE method.
  •  Here URI will have search criteria (query) included in it as shown below (Here I’ve want to delete the department with department_id 300)
  • Keep the Content-Type header attribute value as application/x-www-form-urlencoded


Oracle REST Data Services (ORDS)


Oracle REST Data Services (ORDS)


What is ORDS?


In this blog I will be introducing you to Oracle REST Data Services, a technology by oracle commonly referred to as ORDS. In most simple words, ORDS can be defined as ‘a java application which enables us to expose oracle database as a RESTful web service’. As its name implies, it uses REST architecture. Now the question is why specifically REST when we have many more web based protocols like SOAP? The answer is - REST is easy.


  • It comes with small and uniform set of operations : GET, PUT, POST, DELETE
  • Small and uniform set of status codes : 200(Success), 401(Unauthorized), 500(Server error) etc. There are few more, but I can only think of these at the time.
  • It exposes a resource rather than behaviour/action. For example, let say if I have two numbers a and b. What SOAP offers is – addition web service, web service for subtraction, division, multiplication and so on. But what REST will say is – hey, I have a calculator to offer with set of operation including add, subtract, divide, multiply. Tell me what operation you want to perform on these two numbers.
  • REST has become a de-facto standard on which most of the web/internet is built on. (There is no written rule, but it is commonly accepted architecture for building web applications.)

ORDS Architecture
As I said above, ORDS comes as a java application which is installed on top of oracle database. ORDS always runs within an application server which traverses requests/response to and from database. We can use ORDS either in standalone mode or with an application server like Apache tomcat, WebLogic or Glassfish server. When ran in standalone mode, it runs within its own embedded application server called Jetty.

NOTE : Standalone mode is recommended to be used only on Non-Prod/Dev/Test environments.

ORDS comes with .war configuration file and not as an executable (.exe). We need to place it somewhere on application server and install using command line interface. Alternately, we can also install ORDS through SQL Developer. Recent versions of SQL Developer like 4.1 and onwards, give us flexibility to install and use ORDS through wizards or in other words it provides us GUI so that we can enable our database objects as RESTFul web service with minimal or almost zero coding.





More on ORDS architecture –

Below picture depicts how ORDS works. Here client can be any application having some means to call HTTP protocol or it can be simply an end user accessing ORDS web service through a web browser.



Client requests an access to particular resource using URI i.e Universal Resource Identifier assigned to each REST enabled object in database by ORDS. URI is made up of –

  • HTTP : It tells ORDS that it has to use http protocol to transfer information over the network.
  • Hostname + Port : Hostname identifies a particular database server and a port at which it is listening for incoming requests/connections.
  • Basepath : HTTP://hostname:9090/ords/ this becomes our base path to which ORDS decrypts as – use http messaging protocol and connect to the database server identified by ‘hostname’ and the DB is running at port 9090.


Since ORDS is a java-based application, it uses JDBC connection pool to connect to a particular database. When a request comes to ORDS, it decomposes the URI as explained above to identify a database it is supposed to connect to. It then borrows one connection from its internal JDBC connection pool and connects to the database.

It then does mapping and binding of URI to database object and transforms the request into sql/plsql query. Database runs the query and sends result back to ORDS.

ORDS again transforms the result into JSON format before sending it to the client. 



Uses

a. Exposing data restfully
The primary use case is to expose existing data as REST services. One area where I have seen this used extensively is exposing Oracle e-Business suite data as REST services. For ex. we can make our customer/master items data easily available for other applications like SFDC.

 b. Data capture
ORDS is a fast and light weight method of allowing you to capture data. Think IOT (Internet of Things). There is a vast amount of data coming at us and we need a way to easily capture it and store it using fast industry standard protocols.

 c. Integration middleware
Having provided the ability to capture and expose data, oracle database along with ORDS can function as low-cost integration middleware. (Purpose of integration middleware is to send the data back and forth between applications - exactly what ORDS enabled us to do with oracle DB)

That was all the theory about ORDS. We will see ORDS implementation with some practical examples in next blog à Link is here.