[OSB 12c][Tutorial] OSB 12c SOAP webservice for temperature conversion i.e. Celsius to Fahrenheit and vice versa

This tutorial is intended for the audience who are completely new to OSB and want to try a simple hello world kind of application. Instead of sending and receiving Hello World!. We will be using simple temperature conversion i.e. from Celsius to Fahrenheit and vice versa. We will be using simple Xquery to do all conversion and logic.

High Level:

In this case, our OSB component will contain 2 parts proxy service and pipeline. Proxy service will expose SOAP endpoint to the client application and pipeline will perform various operations on input payload. Since this is just a Hello World application, we will not be using business services. XQuery will be used to implement mathematical logic and conditional logic. Service will take temperature as input and will perform either Fahrenheit to Celsius (FtoC) or Celsius to Fahrenheit (CtoF) temperature conversion. If the conversion type is invalid, then the service will return a failure status.

Mathematical Calculations:

Let temperature in Fahrenheit be X℉ and temperature in Celsius be Y℃.

Fahrenheit to Celsius (FtoC) 

X = (9/5*Y) + 32

Celsius to Fahrenheit (CtoF)

Y = (X-32)*5/9

Steps:

Note: Jdeveloper 12c(12.2.1.4) is used for creating OSB artifacts.

1. Click on File. Click on New and then on Application.

2. Click on Service Bus Application with Service Bus Project. Click on OK.

 3. Specify the Application Name and click on Next.


4. Specify the Project Name and click on Finish. Service Bus Application and Project will be created. 

5. Right-click on the project, click on New and then Click on XML Schema.
6. Specify the File Name, Directory, and Target Namespace for the XML schema. Click on OK. A new schema file will be created.

7. Replace content in the file with the below schema data.
<?xml version="1.0" encoding="windows-1252" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.example.org"
            targetNamespace="http://www.example.org" elementFormDefault="qualified">
  <xsd:element name="Request">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="temperature" type="xsd:double"/>
        <xsd:element name="unit" type="xsd:string"/>
        <xsd:element name="conversionType" type="xsd:string"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
  <xsd:element name="Response">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="status" type="xsd:string"/>
        <xsd:element name="statusDescription" type="xsd:string"/>
        <xsd:element name="temperature" type="xsd:double"/>
        <xsd:element name="unit" type="xsd:string"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>
8. Drag and drop pipeline from the component palette to Pipelines/Split Joins area. "Create Pipeline Service" window will appear.

6. Specify the name for the pipeline and click on Next.

9. Select the WSDL radio button and click on create WSDL button.

10. Use the schema created in step 7 and create WSDL using Create WSDL dialog box. Click on OK.
11. The details will be populated. Check the Expose as a Proxy Service. Specify the name of the proxy service and set Proxy Transport as HTTP. Click on Finish.
12. Double Click on the pipeline.
The pipeline window will open as below.
13. Drag pipeline pair from component pallet and drop below the entry point of the pipeline.
Pipeline pair will be added as below and 2 stages will be created, one each under request and response pipelines respectively. 

14. To implement transformation, we will be using XQuery. Create a new XQuery file with the name transformation_xquery.xqy under the Transformations folder.
xquery version "1.0" encoding "utf-8";

(:: OracleAnnotationVersion "1.0" ::)
declare namespace ns1="http://www.example.org";
(:: import schema at "../temperature.xsd" ::)
declare variable $Request as element() (:: schema-element(ns1:Request) ::) external;
declare function local:func($Request as element() (:: schema-element(ns1:Request) ::)) as element() (:: schema-element(ns1:Response) ::) {
    if($Request/ns1:conversionType ='CtoF') then(
    <ns1:Response>
        <ns1:status>SUCCESS</ns1:status>
        <ns1:statusDescription>Converted to Fahrenheit unit!</ns1:statusDescription>
        <ns1:temperature>{(xs:double($Request/ns1:temperature) * 9.0 div 5.0)+32}</ns1:temperature>
        <ns1:unit>F</ns1:unit>
    </ns1:Response>
        )
        else(
        if($Request/ns1:conversionType ='FtoC') then(
    <ns1:Response>
        <ns1:status>SUCCESS</ns1:status>
        <ns1:statusDescription>Converted to Celsius unit!</ns1:statusDescription>
        <ns1:temperature>{((xs:double($Request/ns1:temperature)-32)*5.0 div 9.0)}</ns1:temperature>
        <ns1:unit>C</ns1:unit>
    </ns1:Response>
        )
        else(
    <ns1:Response>
        <ns1:status>FAILURE</ns1:status>
        <ns1:statusDescription>INVALID conversion type</ns1:statusDescription>
        <ns1:temperature>{$Request/ns1:temperature}</ns1:temperature>
        <ns1:unit>{$Request/ns1:unit}</ns1:unit>
    </ns1:Response>
        )
        )
};
local:func($Request)

15. Add Replace activity to response pipeline under Stage 1.


16. Under replace properties, set location as the body. Location expression as ".". For value select XQuery resource. A window will open. Update the values as shown below. Click on OK.

17. Under Replace Option, Select "Replace Node Contents".

18. Open the proxy service and specify the endpoint for the service or project.

The Endpoint URI will be the relative URI and the hostname will be added before Endpoint URI for the final URL i.e. http://<hostname>:<port>/<Endpoint-URI>

19. We need to deploy the service to SBConsole. We can either create a jar and deploy it to the service bus console else we can directly deploy it from Jdeveloper if a server is configured in Jdevloper.

20. Login to the Service Bus console. Open the proxy service. Click on the Green button to launch the test console.
21. Specify the values for the XML elements and Click on Execute.

The service performs the temperature conversion based on inputs and returns the results as below.


We can test and play with different sets of values to check if the code is working fine and even enhance it further.

Please share your valuable feedback in the comments section below 😊



Comments

Post a Comment

Popular posts from this blog

DateTime formatting using xp20:format-dateTime ()

Create Delimited String from XML Nodes and Vice Versa in SOA 12c

Import and Export MDS artifacts in SOA 12c