Thursday, May 12, 2022

Payload Logging using Groovy Script in SAP CPI.

For the future reference, we have a script to save the payload in the iflow and that can be viewed in the Tenant .

Go to Monitoring page in SAP CPI - Choose iflow - Logs - MPL Attachment - Click on "C4CRequestPayload" to view the payload for this example.


Groovy Scripts:

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

import java.util.HashMap;

def Message processData(Message message) 

{

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

    def messageLog = messageLogFactory.getMessageLog(message);

    if(messageLog != null){

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

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

     }

    return message;

}

(OR)

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

import java.util.HashMap;

import com.sap.it.api.mapping.*;

import java.text.SimpleDateFormat;

import java.util.Calendar;


def Message processData(Message message) {

map = message.getProperties();

property_ENABLE_PAYLOAD_LOGGING = "TRUE";

if (property_ENABLE_PAYLOAD_LOGGING.toUpperCase().equals("TRUE")) {

def header = message.getHeaders() as String;

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

String timeStamp = new SimpleDateFormat("HH:mm:ss.SSS").format(new Date());

                String logTitleH = timeStamp + " Response Headers ";

String logTitleB = timeStamp + " Response Body ";

 def messageLog = messageLogFactory.getMessageLog(message);

if (messageLog != null) {

messageLog.addAttachmentAsString(logTitleH, header, "text/xml");

messageLog.addAttachmentAsString(logTitleB, body, "text/xml");

}

}

return message;

}

No comments:

Post a Comment

The sender will pass the data in JSON format without converting it to XML format. We need to read the particular field.

The sender will pass the data in JSON format without converting it to XML format. We need to read the particular field to check the conditio...