Sunday, 31 March 2019

Form ODATA V2 query parameters like filter,top,skip dynamically

This groovy script describes about the formation of the filter, top, skip query option in the receiver ODATA adapter for the V2 protocol.

Groovy script:


import com.sap.gateway.ip.core.customdev.util.Message;

import java.util.HashMap;

import org.apache.olingo.odata2.api.uri.UriInfo;

import com.sap.gateway.ip.core.customdev.logging.*;


def Message processData(Message message) {   

def body = message.getBody(java.lang.String) as String;

    def messageLog = messageLogFactory.getMessageLog(message);

def uriInfo = message.getHeaders().get("UriInfo");

/*

The following script copies the content of UriInfo into the respective fields for OData receiver.

Reference for UriInfo:com.sap.gateway.core.ip.provider.data.UriInfoImpl.java

In OData Reciever use {header.odataEntity}?{header.odataURI}

*/

def odataURI = new StringBuilder();

def urlDelimiter = "&";

def urlConcat = "?";

def entityName = uriInfo.getTargetEntitySet().getName();

log.logErrors(LogMessage.TechnicalError, "Entity Name::"+entityName);

/*

if (uriInfo.getTop() != null){

def top = uriInfo.getTop();

if(odataURI.size()!=0)

odataURI.append(urlDelimiter);

odataURI.append("\$top=").append(top);

log.logErrors(LogMessage.TechnicalError, "Top value:"+top);

}

if (uriInfo.getSkip() != null){

def skip = uriInfo.getSkip();

if(odataURI.size()!=0)

odataURI.append(urlDelimiter);

odataURI.append("\$skip=").append(skip);

log.logErrors(LogMessage.TechnicalError, "Skip value:"+skip);

}

*/

if (uriInfo.getFilter() != null){

def filter = uriInfo.getFilter().getExpressionString();

if(odataURI.size()!=0)

odataURI.append(urlDelimiter);

odataURI.append("\$filter=").append(filter);

log.logErrors(LogMessage.TechnicalError, "Filter value:"+filter);

}

log.logErrors(LogMessage.TechnicalError, "URI value:"+ odataURI.toString());



if(messageLog != null){

messageLog.setStringProperty("Logging#1", "Printing Payload As Attachment")

messageLog.addAttachmentAsString("IncidentMappingOUTPayload:", body, "text/plain");

}

//Set Headers

message.setHeader("odataEntity",entityName);

message.setHeader("odataURI",odataURI.toString());

return message;




No comments:

Post a Comment