Monday, December 12, 2016

Using REST Adapter in 12c Service Bus with Query Parameters

In 12c Service Bus, we have REST adapter to invoke a REST based service. Earlier in 11g SOA Suite we used to use HTTP adapter for simple HTTP method based calls. With this adapter query parameters and XML were only means of data formats in communication. In 11g Oracle Service Bus we used HTTP based business service to achieve REST based communication. With 12c's REST adapter now we can invoke external service which is based on REST and we can send and receive data in JSON format.

Now, let us see a small 12c service bus program which invokes Google's GeoCode service to validate an address. In this demo, we send some address as input to the GeoCode API. In turn, GeoCode will send us the respective co-ordinates of the given address's location.

Before developing this program, please obtain a key from GeoCode by signing into your Google account. Just follow the steps that are provided by Google.
You can create a  GeoCode project and a key here.

Once you are ready with the GeoCode API key with your account, the end point which we need to invoke will look something like below.

https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=AIzaSy***********wlwpFuD**********Xk

I, just striked out few characters in the key with asterisk symbol as it is the key owned by me.
Basically this URL has 2 inputs. One is the address and the other is the Key.

Before invoking the GeoCode service, please download the security certificate from any browser and add the certificate to the weblogic server keystore.

In this demo, I am using 12c integrated weblogic server.

Steps to add a security certificate to the keystore :

  • Download the security certificate from any browser after hitting above URL. Say, the certificate name is GeoCodeCertificate.cer.
  • Copy the security certificate to the lib directory of your Jdeveloper home directory. In my case, it is, D:\oracleJdev122\wlserver\server\lib. Of course, you can save the certificate any other directory. Just to not forget I have added in lib directory.
  • Go to Java's JDK bin where keytool executable utility is present. The path is something like C:\Program Files\Java\jre1.8.0_45\bin.
  • Now execute below command.
C:\Program Files\Java\jdk1.8.0_45\bin>keytool -import -trustcacerts -alias geoco
decert -file D:\oracleJdev122\wlserver\server\lib\GeoCodeCertificate.cer -keysto
re D:\oracleJdev122\wlserver\server\lib\DemoTrust.jks -storepass DemoTrustKeySto
rePassPhrase
Certificate was added to keystore

You will see that the certificate is now added to the keystore as above.


Now we are ready to build our service bus program.



I am using 12.2.1.0 version of Jdevloper.

Once you open the Jdeveloper, create a service bus application. Inside that application, create a service bus project.

Following are the steps to hit the GeoCode REST service.

  1. GeoCode service can give us response either in XML or JSON format. We now handle JSON response in service bus. Before that copy paste the URL given previously in your browser. Once the URL is hit, there will be a JSON response from GeoCode service. Copy the JSON payload and save it.
  2. Now create an NXSD file based on the JSON payload. We use NXSD to convert the JSON to XML and use that XML as the output of our pipeline.
  3. NXSD creation screen shots.
          Right click on the project and click new. Now select NXSD.




         NXSD creation wizard will pop up.

       

       Click Next. Give a proper  name to your NXSD. 
     

     
      Click Next. Select JSON Interchange Format type.
                                


     Click Next. Please keep everything default. Don't change the Root Element name. Paste the JSON response payload from GeoCode n the sample section.

                             


       Click Next. Native schema will be generated and shown. You can test if you want.

                               


     Click Next. Click Finish.



       Now an NXSD file is created. We use this file in NXSD transformation action at later point of time in the pipeline. Using this we convert the response JSON from GeoCode API to XML.

      4.  Open the overview page of the project and you can see a canvas. On the right side of the canvas, under External Services section right click and add REST service. In below screenshot there is already one REST service created which is part of the demo.

         

      5.  Once you select REST, a wizard will pop up. Give a proper name to the REST reference. Please observe that I haven't checked the box below the Type option.

                   

   
                             

         6. Click Next. Add base URI and add resource path as shown below. We will see method configuration in next step.
            
       
        7. In Methods section, click the Plus Symbol. REST Method Definition wizard will pop up. In this, provide a method name which is of your own. Leave the Resource value to default. In HTTP Verb dropdown, select GET method. We will see Request and Response parameters in next step.

                           
       
       8. Now click on Plus symbol in Request tab. Once you click on plus symbol, you will see Create URI Parameter wizard. In this give a parameter name and keep the defaults Style as query and Type as String.

                 


    9. Create one more parameter for GeoCode key. Once you create the final REST Method Definition wizard looks as below. Click OK.
  
                            

       10. Now the final REST Binding Configuration Wizard looks as below.


         11. Now create a pipeline on the overview canvas. Give appropriate name to the pipeline.
                
                
                     
        12. Click Next and provide the configuration details. Here I am choosing HTTP protocol. Input and output messages to and from the pipeline are in XML format.

                               
                             
       13. Open the pipeline and add a pipeline pair. Inside the request pipeline add a service callout and in that service callout browse the business service that is generated for the REST service adapter that you have created earlier. Following screenshot shows the service callout configuration along with business service browsed and the input and output variables declared.

                           


       14. Our aim is to call the GeoCode API. The input parameters are the HTTP name-value parameters. These parameters will go to the GeoCode API through the GET method. To send the address and key values, I am setting HTTP parameters inside the service callout just before calling the REST business service. This is done using insert action as shown below.

                            

   In this insert action, I have given below XQuery expression inside Value.

   <http:query-parameters>
          <http:parameter name="address" value="{$body//address}"/>
          <http:parameter name="key" value="{$body//key}"/>
   </http:query-parameters>

   Here, I am constructing <http:query-parameter> element for address and key HTTP Parameters respectively. For each parameter the value comes from the input XML that is already present in $body variable.
  Eventually address and key parameters will be populated in $outbound variable which will go to the business service.
  The above XML expression is added as first child of below XML Path inside the $outbound variable.
./ctx:transport/ctx:request

 NOTE : address and key are the parameters that we already created inside the REST adapter.

Now we are all set to call the business service with the required input.

  
    15.  But the output which is coming from the REST service is in JSON format. We will convert the JSON to XML using the nXSD Transalte action.

           In the response flow of service callout, add the nXSD Translate and configure as shown in below screenshot.

           

  In the configuration, you can see that the Transalation type is Native to XML i.e., JSON to XML. Input to the nXSD Transalte action will come from $varOutput (output variable of the service callout action).
Browse the NXSD in the static section.
nXSD Transalte action is assigned to $body variable only after conversion.
  


Once the response flow is finished we can see the JSON to XML converted output in $body variable.

Finally, the overview page looks as below.





Now the pipeline development is complete.
Deploy the service on integrated weblogic service. (I am assuming you already familiar with deployment process from the JDev and not adding any screens for this).
Now test the service.



After opening the pipeline, click on the Launch Test Console button.


Provide the following input.


<addvalue>
<address>1600 Amphitheatre Parkway, Mountain View,CA</address>
<key>AIzaSyCgL1oRh93d0********DRVA51******LHXk</key>
</addvalue>

Once you have hit the service, you can see the $outbound variable as below.





Following screenshot shows the JSON response from GeoCode in the response $body and $varOutput variables within the service callout action in the flow trace.



And we can see the final XML output in $body variable as below. This is after nXSD Translate action execution.





This is how we can achieve the REST based communication in service bus.


While testing, if you see an error like  "...............HostNameVerification Failed...'
Please make sure you have below setting in Admin console.



In the default server's SSL settings, please go to Advanced settings and select Hostname Verification value as None.


In this demo it is a very crude way of sending input to the REST service by manually constructing the query parameters separated by Ampersand symbol.
In Next demo we will see an elegant way of sending parameters to the REST adapter using 'Expression' in REST adapter and we will send an XML as input to the REST adapter.

2 comments: