Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
CarlosRoggan
Product and Topic Expert
Product and Topic Expert

This  blog is about Integration Gateway in SAP Mobile Platform 3.0.

It is a follow up of the introduction blog Integration Gateway: Understanding REST data source [1]: QUERY (very simplified)

In the first blog, the focus was to understand the structure of the string that we have to return in the script.

In order to keep it simple, we used a hardcoded string.

Now, in the current blog, we’ll go one step further and modify the response body of the REST service to give it the required format.

Note: I’ve attached the relevant files, the OData model and the script file, for your convenience.

Prerequisites

  • SAP Mobile Platform 3.0 SP 05
  • Eclipse Kepler with SAP Mobile Platform Tools installed
  • The REST tutorial part 1

OData model

Let’s start with the model.

Other than we did in our intro blog, we now have to create a model that matches the REST service.

Here are the rules:

Entity Type namearbitrary
Entity Set namearbitrary
Property namesProperty names in the OData model have to be exactly the same like in the REST service
Property count

The number of properties in the OData model compared to the number in REST service:

- OData model should have all properties that are in the REST service

- OData model must not have less properties than the REST service

- But OData model can have more properties than the REST service. Additional properties will be empty at runtime.

I’ve created the Entity as follows, according to the documentation of the REST service (http://www.gisgraphy.com/documentation/user-guide.htm#streetwebservice)

You can either type all properties, or import the model from the attached edmx file.

Note:

We don’t care about the types of the properties. Strings are ok for this tutorial.

Custom Code

Create binding and custom code (see the intro blog for details)

In the processResponseData () method,  we retrieve the response body of the call to the REST service.

Then we modify it to meet the expected structure.

This modification is easy in our example, because the REST service has a structure that is almost suitable.

The structure of the REST service is almost suitable, because it has 3 hierarchy levels, just like the structure that we have to provide.

What we have to do is:

  1. Remove the undesired informative nodes in the beginning
  2. Add the opening tag that was also removed in 1
  3. Rename the nodes according to the OData model

This can be done simply with string operations.

There are many possible implementations, my approach is the following:

Remove the whole beginning by searching for the first occurrence of <result>, which is the first entry.

Doing that, we have also removed the opening root node. So we have to add it, that’s what we do in the last line.

And the rename is done in the middle for the EntitySet and EntityType nodes.

We don’t have to care about the properties, as they are presented in a suitable format

This is how the code looks like (in Groovy):

def Message processResponseData(message) {

  

     String restResponse = message.getBody().toString();

   

       /* CONVERT PAYLOAD */


    // find the first relevant entry

       int index = restResponse.indexOf("<result>");
    // cut the undesired information in the beginning of REST response

    restResponse = restResponse.substring(index);

    // replace the REST node with the OData EntityType

    restResponse = restResponse.replaceAll("<result>", "<Street>");

    // and the closing tag

    restResponse = restResponse.replaceAll("</result>", "</Street>");

    //replace REST rootNode with OData EntitySet

    restResponse = restResponse.replaceAll("</results>", "</StreetSet>");

    //above, we've cut the root node, so add the opening EntitySet

    restResponse = "<StreetSet>" + restResponse;

   

       // set the converted payload in the expected structure

    message.setBody(restResponse);

    message.setHeader("Content-Type", new String("xml"));

   

       return message;

}

As usual, don't forget to add the required import statements:

import com.sap.gateway.ip.core.customdev.util.Message

Result

After generate & deploy & configure & run we can see the "odataisized" result in the browser:

Links

Preparing Eclipse for Groovy scripting: http://scn.sap.com/docs/DOC-61719

Introduction in REST datasource part 1: Understanding the return structure in xml

http://scn.sap.com/community/developer-center/mobility-platform/blog/2015/02/10/understanding-rest-d...

Introduction in REST datasource part 2: Understanding the return structure in json

http://scn.sap.com/community/developer-center/mobility-platform/blog/2015/02/11/integration-gateway-...

Installing SMP toolkit:

http://scn.sap.com/community/developer-center/mobility-platform/blog/2014/08/22/how-to-install-sap-m...

Tutorial for Integration Gateway:

http://scn.sap.com/community/developer-center/mobility-platform/blog/2014/06/10/creating-an-odata-se...