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

It is a follow-up of the previous blog, where I explained how to implement the CREATE operation for your OData service, based on a REST data source.

Since SMP SP07, the UPDATE operation is as well supported by Integration Gateway.

If you’re new to the topic, check the Links section below where you can find the tutorials which get you up to speed.

Update:

The sample REST service is now made available for public usage, so I’ve updated the tutorial to use it.

Also, the code is attached to this blog.

Prerequisites

I expect that you’ve gone through my previous tutorials, explaining REST data source – QUERY and READ and CREATE operation – based on XML payload.

Please check the Links section for the relevant blogs.

Furthermore, you need:

  • Eclipse with SAP Mobile Platform Tools installed
  • SMP SP07
  • Basic knowledge about OData provisioning using the Integration Gateway component of SMP

Preparation

REST Service

For this tutorial, we need a REST service that supports writing scenario.

I’m using a service that is public available, you only need to sign up, afterwards you can access it with your SCN  user and password.

Please see the following document for details:

Getting started with the SAP Netweaver Gateway Service Consumption System

Finally, you should be able to access it via the following URL:

https://sapes1.sapdevcenter.com/sap/opu/rest/address/companies


Destination

In your SMP, you need to create an HTTP destination to the following URL:

https://sapes1.sapdevcenter.com

Furthermore, you need to download the certificate and import it into your SMP keyStore.

Note:

For this Destination, it isn’t possible to do a “Test Connection”, as the server doesn’t send a valid response.

As a workaround, you can proceed as follows:

Create a second destination, which is only used to test if the target host can be reached.

This second destination points to a URL that actually can send a valid response.

For example, enter the following URL as destination URL:

https://sapes1.sapdevcenter.com/sap/opu/rest/address/companies

So, you use this second destination to do the “Test Connection”.

If this succeeds, then the first destination should be fine as well.

This first destination will be used to configure our OData service, that we create in this tutorial.

If you get an error message on connection test, you might consider the following:

Note:

You might need to enter proxy settings in your SMP:

https://localhost:8083/Admin/ -> Settings-> System

Note that you might need to restart the SMP server after changing the proxy settings.

OData Model

The OData model for this tutorial is the same like in the previous tutorial:

You can simply continue with the project that was created for the previous tutorial.

Bind data source

The Relative URI is the same like for the READ operation

/sap/opu/rest/address/companies/{ID}

The reason is:

When doing an UPDATE operation directly on the backend REST service, we use the following URL:

https://<host>:<port>/sap/opu/rest/address/companies/121

Since the number, which points to the company that has to be updated, is not fix, it has to be replaced by a variable.

The name of the variable has to be the name of the OData-property that identifies the company.

In our OData model, it is the key field, with name ID

That’s how we compose the last segment of the Relative URI, to be entered in the binding wizard in Eclipse:  /{ID}

Custom Code

Now generate the Custom Code for Groovy.

For the UPDATE operation, we have to implement only the processRequestData() method.

Why?

Because the OData specification doesn’t require that the UPDATE operation returns the payload of the modified resource.

Thus, since the response of our OData service will be empty for the UPDATE, we don’t have to provide anything in the processResponseData() method.

Implement processRequestData() method

Here I have some good news for you:

The implementation of the processRequestData() method of the UPDATE operation is exactly the same like for the CREATE operation.

Yes, you can just copy&paste the code (which of course leads to other questions, sure, how to avoid copy and paste).

So here’s again my implementation, based on string-operation (which looks better for demonstration)

def Message processRequestData(message) {

       message.setHeader(“Content-Type”, “application/atom+xml”);

       message.setHeader(“x-requested-with”, “XMLHTTPRequest”);

  

             // convert OData request body to a string that the backend REST service understands

       String odataRequestBody = message.getBody(String.class);

  

       odataRequestBody = odataRequestBody.replaceAll(“<Companies>”, “<asx:values>”);

       odataRequestBody = odataRequestBody.replaceAll(“</Companies>”, “</asx:values>”);

       odataRequestBody = odataRequestBody.replaceAll(“<Company>”, “<COMPANY>”);

       odataRequestBody = odataRequestBody.replaceAll(“</Company>”, “</COMPANY>”);

       odataRequestBody = “<asx:abap version=\”1.0\”>” + odataRequestBody +“</asx:abap>”;

  

       message.setBody(odataRequestBody);

       return message;

}

Result

In order to test the UPDATE operation, proceed as described in the previous tutorial.

The response status code should be 204 and the response body should be empty, as can be seen in the screenshot below.

Summary

In this tutorial, we’ve learned how to implement the UPDATE operation in a Groovy script.

We’ve seen that it’s easy:

The processRequestData() method is the same as in CREATE

Appendix

If you use the attached scripts, you have to add the following Required Bundles section to your manifest.mf file:

Require-Bundle: org.apache.httpcomponents.httpclient;bundle-version=”4

  .1.3″,org.apache.httpcomponents.httpcore;bundle-version=”4.1.4″,org.s

  lf4j.api;bundle-version=”1.7.2″,org.slf4j.jcl;bundle-version=”[1.7.2,

  1.7.3)”,org.slf4j.jul;bundle-version=”[1.7.2,1.7.3)”,org.slf4j.log4j;

  bundle-version=”[1.7.2,1.7.3)”,olingo-odata2-api;bundle-version=”2.0.

  2″,olingo-odata2-core;bundle-version=”2.0.2″,com.sap.gw.rt.camel.comp

  onents.custom-development;bundle-version=”1.7.0″,com.sap.gw.rt.ip.com

  mons.camel-commons;bundle-version=”1.7.0″,com.sap.gw.rt.ip.commons.ca

  mel-odata-rest;bundle-version=”1.7.0″,org.apache.camel.camel-core;bun

  dle-version=”2.12.4″,com.sap.it.commons;bundle-version=”1.11.0″,com.s

  ap.it.commons.logging.slf4j;bundle-version=”1.11.0″,com.springsource.

  org.apache.commons.io;bundle-version=”1.4.0″

Links

Installing SMP Toolkit:

http://scn.sap.com/community/developer-center/mobility-platform/blog/2014/08/22/how-to-install-sap-mobile-platform-tools-for-integration-gateway-in-eclipse-kepler

Tutorial for OData provisioning in SMP:

http://scn.sap.com/community/developer-center/mobility-platform/blog/2014/06/10/creating-an-odata-service-based-on-sap-gateway-soap-jdbc-and-jpa-data-sources-ba

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-data-source-in-integration-gateway-1-query-very-simplified

Introduction in REST data source part 2: Understanding the return structure in json

http://scn.sap.com/community/developer-center/mobility-platform/blog/2015/02/11/integration-gateway-understanding-rest-data-source-2-query–json-very-simplified

Introduction in REST data source part 3: Implementing the QUERY operation

http://scn.sap.com/community/developer-center/mobility-platform/blog/2015/02/12/integration-gateway-understanding-rest-data-source-3-query–xml-standard

Introduction in REST data source part 7: Implementing the READ operation

http://scn.sap.com/community/developer-center/mobility-platform/blog/2015/03/06/integration-gateway-understanding-rest-data-source-7-read–xml

Introduction in REST data source part 9: Implementing the CREATE operation

http://scn.sap.com/community/developer-center/mobility-platform/blog/2015/04/21/integration-gateway-understanding-rest-data-source-9-create-operation

Overview of all REST blogs

http://scn.sap.com/community/developer-center/mobility-platform/blog/2015/04/08/integration-gateway-rest-data-source-overview-of-blogs

The official Documentation: http://help.sap.com/mobile-platform/

To report this post you need to login first.

Be the first to leave a comment

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

Leave a Reply