Show TOC

Custom Script Functions for REST ServicesLocate this document in the navigation structure

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

  1. 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.

    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: 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;
    }
  2. 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 XML format for REST Read:
    <EntitySetName>
      <EntityName>
        <Key1>KeyValue1</Key1>
        <Key2>KeyValue2</Key2>
        <Property1>Value1</Property1> 
        <Property2>Value2</Property2> 
       ............
      <EntityName>
    </EntitySetName> 
    Expected output 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 JSON format for REST Read:
    {"d":{"Key1":KeyValue1,"Key2":KeyValue2,
    "Property1":Value1, "Property2":Value2}}
    
    Expected output 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.
    Note Set the OData response to the message body using message.setBody(odataResponse);
    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;
    }