The process flow chart depicts how the two custom script functions that are available for REST services work.
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.
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; }
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.
<EntitySetName> <EntityName> <Key1>KeyValue1</Key1> <Key2>KeyValue2</Key2> <Property1>Value1</Property1> <Property2>Value2</Property2> ............ <EntityName> </EntitySetName>
<EntitySetName> <EntityName> <Key1>KeyValue1</Key1> <Key2>KeyValue2</Key2> <Property1>Value1</Property1> <Property2>Value2</Property2> ............ <EntityName> <EntityName>……</EntityName > <EntityName>……</EntityName > <EntityName>……</EntityName > <EntityName>……</EntityName > </EntitySetName>
{"d":{"Key1":KeyValue1,"Key2":KeyValue2, "Property1":Value1, "Property2":Value2}}
{"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:
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; }