, , ,

SAP CPI – Custom Headers – Reusable Groovy Script

By.

min read

Share

How do I use a single script across my tenant to log all my Custom Headers? How do I ensure that I do not have a messy landscape with every developer building his own groovy script for Custom Headers in each Iflow?

The Idea

  • Define all Custom Headers you want to Log in your iFlow as properties with a prefix “CH_
  • Use this Script as script collection.
  • The Script Collection iterates through all properties that startsWith CH_ and logs them as Custom Headers.
  • As Simple as That!

Usage in CPI

Groovy Script

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

def Message processData(Message message) {
    
	def messageLog = messageLogFactory.getMessageLog(message);
	def properties = message.getProperties();
	if(messageLog != null){
        properties.each { key, value ->
            if (key.startsWith("CH_")) {
                messageLog.addCustomHeaderProperty(key.replaceAll("CH_",""), value);
            }
        }
	}
	return message;
}

Script Collection

Use the Script Collection in your Iflow

Final Thoughts

  • Use this script to log your Custom Headers through a common script
  • If you do not want to hardcode the prefix, (CH_) in my case
    • define the prefix as a property in your iflow or
    • Even at your tenant level using PartnerDirectory ( See this post ) for example.
  • The Script Expects all parameters to be of Type “String”. Ensure your properties are of Type String.