Wednesday, December 28, 2016

Using REST Adapter in 12c Service Bus with nXSD's XML as Input

In continuation to the previous post here, in this demo we will see how an XML fragment can be passed as input to the REST adapter in Service Bus instead of building query parameters and populate them in the $outbound variable.


Please follow everything in previous post until you create the REST adapter.
Now we will see what needs to be configure within the REST adapter.


1. Right click on the overview canvas and add REST service. Make sure that you have checked the check box down below the Type drop down. With this setting we can now call the REST adapter/service as a webservice. After completing the configuration, we can see WSDL file generated along with the WADL file corresponding to the REST adapter.

The main difference between previous demo and the present demo is this check box selection.


2.   In this step, we need to provide the URL, resource and operation details similar to the previous demo. Create a  new operation in Operation Bindings section of the window.
                                

3. In the new Operation window add a proper name to the operation and select the HTTP method as GET. In the Request parameter section, we need to specify the input which is now in XML format. To specify that we need to build the XML schema corresponding to the input XML. Now, click on the gear symbol next to the Schema URL text box.



4.  Native Format Builder wizard will pop up to build the schema, as below. Click Next


5.  Give a proper name to your schema. Click Next.


6. Based on the sample that we provide, a schema will be generated. Select XML Sample option and click Next.

7.  Provide a sample XML with required XML structure. And click Next.


8. The nXSD schema which is going to be generated will shown to you. Click Next. Once the REST adapter configuration is finished, you can see a new nXSD created for the input XML which will be input the REST adapter's business service.


9.  Now we have completed the schema. Click on Finish.



10.  Once the schema is built, the Operation Bindings window will look as below. In below screen shot, in parameter section, you can see Address and Key as the parameters. But GeoCode expects query parameters as lower case address and key. Please edit the parameters as lower case address and key.



11. In Response tab, select JSON and XML as the response format.

                   

12.  In previous demo we have created on nXSD for the response that is coming from GeoCode API. I am using the same nXSD here. Browse the nXSD in the search option just next to the Schema URL text box.


13. Once the schema is browsed, the Response tab looks as below. Since we have give the nXSD in the response section, the JSON response that is given by GeoCode will automatically be converted to XML.



 14. Finally the REST adapter window looks as below and click Finish.


15.  Now we need to invoke the REST adapter's business service from the pipeline. Create a pipeline. Inside the pipeline, add pipeline pair, stage and a service callout. In the service callout browse the Business Service corresponding to the REST adapter for GeoCode API.


16.  Now add an assign action inside the service callout's request action flow. With this assign action we will  populate $varInput variable. This input will be given to the GeoCode REST business service.



 For the elements AddressValue and KeyValue, the text value comes from the input payload of the pipeline.

17. Populate the output from REST adapter (from GeoCode API) in $body variable. This $body will be the output of the pipeline.

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.