Custom Script Functions for REST Services
The process flow chart depicts how the two custom script functions that are available for REST services work.

- The function ProcessRequestData is invoked after the OData request is converted to XML or JSON format by the Integration Gateway runtime. Use ProcessRequestData to modify OData request which is in XML or JSON format. For REST read operation, use this function to modify the value of RelativeUri (only in cases where the RelativeUri should be modified). For REST query operation, this function can be used to modify the header RelativeUri. In both query and read operations, the message body is empty. All other information is available as headers. See Message Headers.
Input: OData request in XML or JSON format depending on the configuration maintained in the API Toolkit for SAP Mobile Platform.
Output: Modified request body and headers in XML or JSON format.
Example 1: This custom script illustrates how to handle filter query option by changing the URI before the REST call is made. The URI formed is modified by appending the filter parameter. The modified URI must be REST compliant.def Message processRequestData(message) { Object obj = message.getHeaders().get("UriInfo"); UriInfo uriInfo = (UriInfo) obj; GetEntitySetUriInfo entitysetUriInfo = (GetEntitySetUriInfo) uriInfo; String relativeUri = (String) message.getHeaders().get("RelativeUri"); StringBuffer url = new StringBuffer(relativeUri); //Fetching the filter expression if (entitysetUriInfo.getFilter() != null) { url.append("?").append("\$filter").append('=').append(entitysetUriInfo.getFilter().getExpressionString()); } //Changing the RelativeUri for catering to filter operations String serviceUrl = url.toString(); serviceUrl = serviceUrl.replaceAll(" ","%20").replaceAll("'","%27"); //Setting the new RelativeUri header message.setHeader("RelativeUri", new String(serviceUrl)); return message; }Example 2: For operations like REST create and update, that requires request payload, modify the function ProcessRequestData to get a REST service compliant payload. You can convert it into XML or JSON format by using the Content-Type header.Input in XML Format:
<EntitySetName> <EntityName> <Key1>KeyValue1</Key1> <Key2>KeyValue2</Key2> <Property1>Value1</Property1> <Property2>Value2</Property2> ............ <EntityName> </EntitySetName>Input in JSON Format:
{"Key1":KeyValue1,"Key2":KeyValue2, "Property1":Value1, "Property2":Value2}Expected Output: REST Compliant Request Payload:
def Message processRequestData(message) { // Set Content-Type header to identify the content accepted by the service. message.setHeader("Content-Type", "application/atom+xml"); Object string = message.getBody(String.class); String requestBody=(String) string; //REQUEST PAYLOAD IN LOGS log.logErrors(LogMessage.TechnicalError, "\n Request Payload is:\n" + requestBody); // Convert to request payload using REST API String finalString="<?xml version=\"1.0\" encoding=\"utf-8\"?><entry xml:base=\"http://vmw4517.wdf.sap.corp:50000/sap/opu/odata/sap/zemp_srv/\" xmlns=\"http://www.w3.org/2005/Atom\" xmlns:m=\"http://schemas.microsoft.com/ado/2007/08/dataservices/metadata\" xmlns:d=\"http://schemas.microsoft.com/ado/2007/08/dataservices\"><content type=\"application/xml\"><m:properties>"; requestBody=requestBody.replaceAll("<Employee>", ""); requestBody=requestBody.replaceAll("<Employees>", ""); requestBody=requestBody.replaceAll("</Employees>", ""); requestBody=requestBody.replaceAll("</Employee>", ""); requestBody=requestBody.replaceAll("<", "<d:"); requestBody=requestBody.replaceAll("<d:/", "</d:"); finalString=finalString+requestBody; finalString=finalString+"</m:properties></content></entry>"; // CSRF token is not supported yet, hence adding the below header to bypass message.setHeader("X-Requested-With", new String("X")); message.setBody(finalString); return message; - The function ProcessResponseData is invoked after the response is received from the REST service. Use ProcessResponseData to modify the REST response and do response mapping. Message body contains the actual response from the REST in XML or JSON format. The message body should be modified into a corresponding XML or JSON format that adheres to the OData model as explained below.
Input: REST response in XML or JSON format.
Output: Modified REST response either in XML or JSON format which is converted to OData response by Integration Gateway runtime. For error scenario, the output is a formatted error response which is displayed to the end user.Expected Output in XML Format for REST Read and Create:<EntitySetName> <EntityName> <Key1>KeyValue1</Key1> <Key2>KeyValue2</Key2> <Property1>Value1</Property1> <Property2>Value2</Property2> ............ <EntityName> </EntitySetName>Expected Output in XML Format for REST Query:
<EntitySetName> <EntityName> <Key1>KeyValue1</Key1> <Key2>KeyValue2</Key2> <Property1>Value1</Property1> <Property2>Value2</Property2> ............ <EntityName> <EntityName>……</EntityName > <EntityName>……</EntityName > <EntityName>……</EntityName > <EntityName>……</EntityName > </EntitySetName>Expected Output in JSON Format for REST Read and Create:{"d":{"Key1":KeyValue1,"Key2":KeyValue2, "Property1":Value1, "Property2":Value2}}Expected Output in JSON Format for REST Query:{"d":{"results":[, {"Key1":KeyValue1,"Key2":KeyValue2, "Property1":Value1, "Property2":Value2}, {"Key1":KeyValue1,"Key2":KeyValue2, "Property1":Value1, "Property2":Value2} ]}}Example: The XML response from the REST service is in the form:
This response must be modified so that it can be handled in the Custom Processor which will convert it to an OData response. The highlighted part in the above response has to be removed using custom script, check the sample code below.function processResponseData(message) { importPackage(com.sap.gateway.ip.core.customdev.util); /* importPackage(java.lang); importPackage(java.io); importPackage(java.util); importPackage(org.apache.camel); importPackage(com.sap.gateway.core.ip.component.commons); importPackage(org.apache.olingo.odata2.api.edm); importPackage(org.apache.olingo.odata2.api.uri); importPackage(com.sap.gateway.ip.core.customdev.logging); var f; f = message.getBody(); //ORIGINAL PAYLOAD IN LOGS log.logErrors(LogMessage.TechnicalError, "\n Original Payload is:\n" + f); log.logErrors(LogMessage.TechnicalError, "\n +++++++++++++++++++++++++++++++ END OF TRACE OUTPUT ++++++++++++++++++++++++++++++++\n"); CONVERTING REST RESPONSE TO ODATA RESPONSE USING STRING API This is just a sample implementation consuming a REST service from SAP system. You have to rewrite the below code according to the service you are consuming. After the manipulations the Original payload should be converted into the following format <EntitySetName> <EntityTypeName> <PropertyName>propertyValue </PropertyName> <PropertyName>propertyValue </PropertyName> <EntityTypeName> . . . <EntityTypeName> <PropertyName>propertyValue </PropertyName> <PropertyName>propertyValue </PropertyName> <EntityTypeName> <EntitySetName> -------------------Sample Implementation--------------------------------------- f = f.replaceFirst("<item>", "<Product>").replaceFirst("</item>", "</Product>"); f = f.replaceFirst("<item>", "<Product>").replaceFirst("</item>", "</Product>"); f = f.replaceFirst("<item>", "<Product>").replaceFirst("</item>", "</Product>"); f = f.replaceFirst("<.*?<DATA>", "<Products>").replaceAll("<item>.*?</asx:abap>", "</Products>").replaceFirst("<.*?>", ""); f = f.replaceAll("\n",""); f = f.trim(); var index = f.indexOf("<"); f = f.substring(index); -------------------End Sample Implementation----------------------------------- //RESPONSE PAYLOAD IN LOGS log.logErrors(LogMessage.TechnicalError, "\n Response Payload is:\n" + f); log.logErrors(LogMessage.TechnicalError, "\n +++++++++++++++++++++++++++++++ END OF TRACE OUTPUT ++++++++++++++++++++++++++++++++\n"); //Setting the new response into the message body message.setBody(f); //This tag is mandatory in SAP Mobile Platform 3.0 SP05 release but optional from SP06. . message.setHeader("Content-Type", new java.lang.String("xml")); */ return message; }