From SMP 3.0 SP05 release onward there is a support of OData Query operation for a service provisioned based on a REST service. In this blog post I want to show how you can develop and deploy an OData service to the SMP3 which relies on an REST service. That means that the Integration Gateway component in SMP3 will convert on the fly data coming from the REST service into OData.


Tools Used: Eclipse Kepler

Prerequisite:

1. SAP Mobile Platform Tools plugins added to Eclipse Kepler (make sure its version should be 1.3.1 check this guide for more info)

2. SAP Mobile Platform 3.0 SP06 Runtime


I got one publicly available REST service (http://www.thomas-bayer.com/sqlrest/CUSTOMER/1), i am going to convert this information into OData.


              rest.PNG


Steps:


1. Create a new project in Eclipse Kepler: File>New>SAP Mobile Platform OData Implementation Project

    • Make sure you select Target Runtime server as SP06

                  1.PNG

    • Give any model name and Finish

        

                        restmodel.PNG

2. Create an Entity and add properties as below:

          entitytype.PNG

3. Right click .odatasrv > Select Data source as REST Service

    • Request URL : /sqlrest/CUSTOMER/1

    2.PNG

4. Expand .odatasrv > Right click Query > Define Custom code

          3.PNG    

5. Replace generated code with below code. (I have attached it in a file as well)


function getUriInfo(message) {
  importPackage(org.apache.olingo.odata2.api.uri);
  importPackage(com.sap.gateway.core.ip.component.commons);
  var uriInfo = message.getHeaders().get(ODataExchangeHeaderProperty.UriInfo.toString());
  return uriInfo;
}
function processRequestData(message) {
        return message;
}
function processResponseData(message) {
    importPackage(java.io);
    importPackage(java.lang);
    importPackage(java.util);
    importPackage(org.apache.camel);
    importPackage(com.sap.gateway.core.ip.component.commons);
    importPackage(com.sap.gateway.ip.core.customdev.util);
    importPackage(org.apache.olingo.odata2.api.edm);
    importPackage(org.apache.olingo.odata2.api.uri);
    importPackage(com.sap.gateway.ip.core.customdev.logging);
    var payload = message.getBody();
    log.logErrors(LogMessage.TechnicalError, "\n +++++++++++++++++++++++++++++++++++ TRACE OUTPUT +++++++++++++++++++++++++++++++++++\n");
log.logErrors(LogMessage.TechnicalError, "\n Original Payload is:\n" + payload);
    payload = payload.replace("\n", "");
    payload = payload.replaceFirst("<.xml .*?>", ""); // remove the line <?xml version="1.0"?>
    payload = "<CUSTOMERSet>" + payload + "</CUSTOMERSet>";
    log.logErrors(LogMessage.TechnicalError, "\n Response Payload is:\n" + payload);
    log.logErrors(LogMessage.TechnicalError, "\n +++++++++++++++++++++++++++++++ END OF TRACE OUTPUT ++++++++++++++++++++++++++++++++\n");
    message.setBody(payload);
    message.setHeader("Content-Type", new java.lang.String("xml"));
return message;
}



6. Right click project > Generate and Deploy Integration Content

          deployment.PNG

Creating a destination in Gateway cockpit:

7. Open Gateway cockpit (https://smpserver:adminport/gateway/cockpit) > Navigate to “Destinations” tab

    Destination URL : http://www.thomas-bayer.com

    /wp-content/uploads/2015/01/4_655505.png

8. Select newly created destination and check if you are able to ping it .(Test connection)

    destping.PNG

9. Navigate to SERVICES > select deployed OData service (ThomasREST)> Add destination

    adddest.PNG

10. Open service document

https://jk:8083/gateway/odata/SAP/THOMASRESTIGW;v=1

https://jk:8083/gateway/odata/SAP/THOMASRESTIGW;v=1/CUSTOMERSet

    output.PNG

For more info, check official guide.

Enabling REST Services as OData Services

Custom Scripting functions for REST services

Note: From SMP SP06, the READ operation is supported as well. Check this blog.

Thanks Mustafa Saglam and Bjoern Woppmann for your continuous support.

To report this post you need to login first.

4 Comments

You must be Logged on to comment or reply to a post.

  1. Nikhil Deshmukh

    Hi Jitendra,

    I have followed this document and was able to retrieve data from REST Service.

    We have a rest service that returns the following xml :

    contentXML.JPG

    so I have created a following OData model:

    odata.JPGPages is a complex type of pages. pages.item is also complex type of item.

    I deploy this service after adding the Javascript code.

    But when i run this service on SMP Gateway I get the following Data:

    content.JPG

    If you see, I only get the Last Data item that exist in the service.

    <d:vcKey>YMA85X-APIL</d:vcKey>……..

    So what can be done to get the complete data set of items?

    (0) 

Leave a Reply