When the web service response comes dynamically for every request made, certain fields can be populated dynamically because of which response mapping cannot be done completely via mapping tool. Only static fields can be mapped using mapping editor and the dynamic fields can be mapped via custom scripts.

Steps from Design Time tool

  1. Create an OData Service Implementation Project
  2. Create an OData Model for an operation

/wp-content/uploads/2014/10/1_578300.png

      3. Right click on odatasvc and choose Select data source

      4. Select the entity set and choose the query CRUD operation. Select the data source SOAP Service.

/wp-content/uploads/2014/10/2_578302.png

     5. Specify the wsdl file and choose the operation from the list of soap operations and click on finish

/wp-content/uploads/2014/10/3_578303.png

     6. Right click on Query and Select Define ResponseMapping

/wp-content/uploads/2014/10/4_578305.png

     7. Map all the static fields as below

5.PNG

Here the fields under businessKeys property are populated only after a query request is fired. So, these dynamic fields are mapped via scripts

      8. Right click on Query and select Define Custom Code

/wp-content/uploads/2014/10/6_578307.png

     9. Get the entire response from message body, extract the dynamic field values and create a list of hashMaps as below. Set this list to custom header

function processResponseXML(message) {
importPackage(java.util);
importPackage(java.lang);
var payload = message.getBody().toString();
importPackage(com.sap.gateway.ip.core.customdev.logging);
log.logErrors(LogMessage.TechnicalError, payload.toString());
var tokens = payload.split(“(?=<)|(?<=>)”); var list = new ArrayList();
var innerMap = new HashMap();
for (var i = 0; i < tokens.length; i++) {
  if (tokens[i].contains(“firstName”)) {
   innerMap.put(“firstName”, tokens[i + 1]);
   i = i + 2;
  }
  else if (tokens[i].contains(“lastName”)) {
   innerMap.put(“lastName”, tokens[i + 1]);
   i = i + 2;
  }
  if (innerMap.containsKey(“firstName”) && innerMap.containsKey(“lastName”)) {
   list.add(innerMap); // Create a list of hashMaps
   innerMap = new HashMap();
  }
}
message.setHeader(“MappingOutput”, list); // Set the list of hashMaps to a
            // header
return message;
}


   10. From the previously set header get the list of hashmaps and from the message body get the list of hashmaps for all static fields which were mapped using mapping editor, combine them into a single list of hashMaps as below

function processResponseData(message) {
importPackage(java.util);
var data = message.getHeaders().get(“MappingOutput”);// Get the data from
               // previously set
               // header
importPackage(com.sap.gateway.ip.core.customdev.logging);
log.logErrors(LogMessage.TechnicalError, data);
var body = message.getBody();
var newData = null;
var newList = new ArrayList();
for (var i = 0; i < data.size(); i++) {
  newData = new HashMap();
  var dyData = data.get(i);// Getting dynamic data from the previously
         // set header
  var mappedData = body.get(i); // Getting the mapped data from mapping
          // tool
  newData.putAll(dyData);
  newData.putAll(mappedData);
  newList.add(newData); // Finally create a list of hashMaps
}
message.setBody(newList);
return message;
}

    11. Right Click on Project and select Generate and Deploy Integration Content. This will deploy the bundle.

Now fire an OData Request https://localhost:8083/gateway/odata/SAP/SAMPLE;v=1/QuerySet on the browser.

To report this post you need to login first.

5 Comments

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

    1. Tejesvi DVR

      hi Stan,

      we have inserted the images through the tool, this problem is happening randomly, any ways we will upload it again.

      thanks,

      tejesvi

      (0) 

Leave a Reply