Thursday, 8 September 2016

Enabling JSON based REST Invocation in SOA OSB 11g

Enabling JSON based REST Invocation in OSB 11g

Scenario: Service Provider is migrating from SOAP WS to REST Model. Service Consumer needs to be updated with REST Clients.
Solution: If all the communication happens via OSB Layer then SOAP messages can be translated to JSON messages so that Service Consumer don’t have to modify their WS clients.

In this post, we will discuss on how to leverage Java Callout capability in OSB to enable JSON based REST Invocation.
On a high level, OSB has two components called Proxy Service & Business Service

Proxy Service will define the service with message formats, communication protocols, Message Flow and target Business Service.
Business Service holds the information about Service Provider like URL, mode of communication, authentication mechanism, etc.
Service Consumer will have no idea about to which instance of Service Provider the request is redirected. It acts as an Abstraction layer, where service consumer & service provider were not tightly coupled.
Let’s come back to the scope, Enabling JSON based REST Invocation in OSB.
Ø  If the incoming request is REST message, then it is straight forward, you can define the proxy service’s service type as Messaging Service to accept the data as Text and pass it to business service where URL & corresponding http method will be configured, with which the request will be sent to the target Service Provider.
Ø  If the incoming request is SOAP message, then Java callout capability in OSB can be leveraged to convert the SOAP XML message to JSON message and pass on.
To perform any activity on OSB except read, you need to create a session which can be activated post your configuration is completed

Create Proxy Service:
1.       Under Create Resource Field, select Proxy Service.

2.       Expose the Proxy Service as SOAP Web Service so that SOAP based Service consumer will communicate to proxy in a seamless manner (In my case, Service Consumer was SOA BPEL Process).
Click Next, Select Protocol as http. Endpoint URI will be prepopulated, modify as needed.

3.       Click Last, Let’s skip to last page of this configuration leaving all other parameters with the default value. Then Click on Save button.

Before getting into Proxy Service Message Flow, let’s create the corresponding Business Service,

Create Business Service:
1.       Under Create Resource, select Business Service. Enter Service Name
2.       Select Service Type as Messaging Service for accepting json messages from proxy service. Click Next

3.       Select the Request Message Type & Response Message Type as Text and Click Next

4.       Select protocol as http and configure the Endpoint URI & add the same then Click Next

5.       Select the appropriate method as needed. If any basic authenication needed then configure a service account and tag here. Then Click Next.
Note: If you want to invoke GET method, then Request Message Type should be None, because GET method invocation cannot have a body. If you want to invoke HEAD method, then Response Message Type should be None.

6.       Click Last and save the configuration .




Linking Proxy Service to Business Service:
Now Proxy service & Business Service is created, In order to link the proxy service to business service, Click Edit Message Flow button to create a message flow for this proxy.

·         In the Message flow, add a Route to the service and click Edit Route


·         Click the link in the Route to field to configure the corresponding Business Service.
·         In the Request Action section,


External Activity: Develop an API to convert incoming XML object to JSON String as a static method which will take apache XMLObject as input param and return String as output. Upload the JAR file into OSB with all dependency jar files. Hint: Develop the api as Maven Project to manage dependencies effectively.
·         Click Java Callout to link to configure the static method in the api. Locate the JAR & implemented method.
ü  Configure Input Parameter as $body, so that the entire SOAP Body will be passed as input.
ü  Capture the return parameter (output) in result variable (run-time variable).


·         Add another action called Replace to replace the body content with temporary result variable
ü  Enter the variable as body
ü  Enter the with value as ‘$result’
·         Add another action called Replace to set the content-type of the request.
ü  Click XPath link and replace it with “./ctx:transport/ctx:request/tp:headers/http:Content-Type”
ü  Enter the variable as outbound
ü  Enter the with value as “<http:Content-Type>application/json</http:Content-Type>” or ‘application/json’
In case you are trying to perform GET operation, the default content type of business service is ‘binary/octet-stream’, so you need to update the content type in proxy service & send all the headers to business service.
·         Below screenshot will speak about setting up the Transport Headers (Add an action->Communication->Transport Headers/Routing options options) for enabling GET operation with respective Content-type. In case if you need to add some application data to URI for GET operation, then perform a concat operation using xquery & update URI field in Routing Options action. The updated URI will be overridden in the business service.

Likewise, perform the reverse activity to convert the incoming JSON message to appropriate XML format and set in the body content in the Response Action Section.


By this way you can enable JSON based REST Invocation in OSB Layer without impact the service consumer & service provider.

Friday, 29 July 2016

Integrating Spring Context into JDeveloper SOA 11g

Invoking REST Service API from SOA Suite 11g via Spring Context

Requirements:
a)      Oracle SOA Suite 11g (If you gonna use SOA Suite 12c, then this post is not useful, as 12c have implemented REST Adapters for JSON based REST API Invocation)
b)      JDeveloper 11g
c)       RCU & Database (I will come up with a separate post for this)

Steps:
1)      Open JDeveloper Application
2)      Select Tools -> Check for Updates -> Click Next -> Select all the checkboxes -> locate “SOA Composite Editor” and Install
3)      Select Tools -> Check for Updates -> Click Next -> Select all the checkboxes -> locate “Spring, Weblogic SCA Integration Extension” and Install
4)      Select File -> New -> Under General, Applications -> SOA Application -> Enter Application Name and Click Next
5)      Enter the Project Name, select Java & XML from Available to Selected and Click Next
6)      Enter the Composite Name, select Empty Composite and Click Finish.
7)      Open <Composite Name>.xml, select Design Tab
8)      In Component Palette, From Service Component section, click & drag BPEL Process into composite.xml
9)      It creates a client & a simple BPEL Workflow
10)    In Component Palette, From Service Component section, click & drag Spring Context into composite.xml
11)   Now we need to create Spring Bean for integrating Spring into BPEL Workflow
12)   Create a Java Interface and define methods
13)   Create a Java Class which implements the above created Interface and implement your logic
14)   Register the bean into Spring.xml configuration file as below,
<bean class="<fully qualified class name>" id="impl" />
<sca:service name="test" target="impl" type="<fully qualified interface name>" />
15)   In composite.xml, Connect the BPEL Component & Spring Component, necessary files (wsdl, componentType) will be generated by JDeveloper
16)   Now you can use the Spring Component inside BPEL Workflow, as needed you can invoke this Spring Component via Invoke activity with proper input & output variables assigned.
17)   In the Spring Implementation Class, you can play around with all capabilities of Java. As mentioned in the Topic, I’ve used it for Invoking a REST API by constructing a Jersey Client.
18)   Import necessary libraries by, Right-click Project -> Project Properties -> Libraries & Classpath -> Click Add Jar/Directory -> locate and import libraries
19)   There are two ways to deploy the artifacts into WebLogic App Server, post completion of development. Direct Deployment or export the application as SOA archive and deploy it via Enterprise Manager Console.

I have used Jersey 2.6 as SOA 11g supported by jdk6.
Jersey 2.6 API Dependency List,

If needed, i can share the sample code.

Recent Posts

Micro VMs & Unikernels

This post is a follow up of this post . In Previous Post, we discussed about Virtual Machines & Containers architecture. In this post, w...

Older Posts