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.
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 name | arbitrary |
Entity Set name | arbitrary |
Property names | Property 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.
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:
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
After generate & deploy & configure & run we can see the "odataisized" result in the browser:
Preparing Eclipse for Groovy scripting: http://scn.sap.com/docs/DOC-61719
Introduction in REST datasource part 1: Understanding the return structure in xml
Introduction in REST datasource part 2: Understanding the return structure in json
Installing SMP toolkit:
Tutorial for Integration Gateway:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
18 | |
9 | |
9 | |
9 | |
8 | |
7 | |
6 | |
6 | |
6 | |
6 |