cancel
Showing results for 
Search instead for 
Did you mean: 

CPI Alert Notification Template in Groovy

stephen_xue
Active Participant
0 Kudos
150

This is a simple one. I just developed a simple program for generating alert notification mail body in HTML catching the exception raised by the Cloud Integration Framwork. 

You need to configure a separated mail receiver for pushing the content into whoever concerns. 🙂

One of the benifit of using this piece of code is that , the only input parameter is: mail-receiver.

or if you do not want to even configure it , you can put the receiver in the value mapping

valueMapApi.getMappedValue('CPI', 'cpi:office365:receiver', 'receiver', 'OFFICE365', 'cpi:office365:receiver')

Please read the code carefully before using it and you take your own responsibility for the consequence. 🙂

import com.sap.gateway.ip.core.customdev.util.Message
import org.apache.camel.Exchange
import org.apache.camel.builder.SimpleBuilder

import java.time.Instant
import java.time.ZoneId
import java.time.ZonedDateTime

import com.sap.it.api.ITApiFactory
import com.sap.it.api.mapping.ValueMappingApi

/* ************************************************************************
    Program     : AlertNotification.groovy
    Create Date : Mar-26-2025
    Author      : Stephen Xue
    Parameters  :
        message --> message reference from framework;
        testFlag--> true for test mode; default for production code mode;
    Function    :
        1.
    Source: HTTP Header alert-receiver
    Target: HTML
 *************************************************************************/
Message processData(Message message)
{
    def map       = message.getProperties();
    def msgID     = map.get("SAP_MessageProcessingLogID");
    def receivers = map.get("alert-receiver");;
    def exceptionMessage, exceptionCode,exceptionName, tenantType;
    
    // Tenant type: prod or non-prod
    def tenant = System.getenv("TENANT_NAME");
    if(tenant.contains('-non-')){
        tenantType = 'Non-Prod';
    }else{
        tenantType = 'Prod';
    }    
    
    // Get receiver from ValueMappingApi
    if(receivers == "" || receivers == null ){
        def valueMapApi = ITApiFactory.getApi(ValueMappingApi.class, null);
		try{
			receivers = valueMapApi.getMappedValue('CPI', 'cpi:office365:receiver', 'receiver', 'OFFICE365', 'cpi:office365:receiver');
		}catch(Exception e){
			receivers = "yourusername@example.com";
		}
    }
    message.setHeader('mail-receiver',receivers);

    // Get iflow name and subject
    Exchange exchange = message.exchange;
    def evaluateSimple = { simpleExpression ->
        SimpleBuilder.simple(simpleExpression).evaluate(exchange, String);
    }
    def iflowName = evaluateSimple('${camelId}');
    def subject = "[${tenantType}]iFlow: '${iflowName}' has error";

    // Get system time
    Instant instant   = Instant.now();
    ZoneId zoneId     = ZoneId.of( "Australia/Melbourne" );
    ZonedDateTime zdt = instant.atZone( zoneId );
    def systemTime = zdt.toString();

    // Get Message Link
    String url = System.getenv("TENANT_NAME")+"."+System.getenv("IT_SYSTEM_ID")+"."+System.getenv("IT_TENANT_UX_DOMAIN");
    def msgIDLink = "https://"+url+":443/itspaces/shell/monitoring/MessageDetails/%7B%22messageGuid%22%3A%22"+msgID+"%22%7D";
    def ex = map.get("CamelExceptionCaught");
    if (ex!=null)
    {   // Exception Handling in OData V2 Receiver Adapter
        if(ex.getClass().getCanonicalName().equals("com.sap.gateway.core.ip.component.odata.exception.OsciException")){
            exceptionMessage = ex.getMessage();
            exceptionCode    = message.getHeaders().get("CamelHttpResponseCode").toString();
            exceptionName    = ex.getClass().getCanonicalName();
        // Exception Handling in HTTP Receiver
        }else if(ex.getClass().getCanonicalName().equals("org.apache.camel.component.ahc.AhcOperationFailedException")){
            exceptionMessage = ex.getResponseBody();
            exceptionCode    = message.getHeaders().get("CamelHttpResponseCode").toString();
            exceptionName    = ex.getClass().getCanonicalName();
        }else{
            exceptionMessage = ex.getMessage();
            exceptionCode    = message.getHeaders().get("CamelHttpResponseCode").toString();
            exceptionName    = ex.getClass().getCanonicalName();
        }

        mailBody =  "<!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "<head>\n" +
                "  <meta charset=\"UTF-8\" />\n" +
                "  <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />\n" +
                "  <title>EMAIL</title>\n" +
                "  <style>\n" +
                "table, th, td {\n" +
                "  border: 1px solid black;\n" +
                "}\n" +
                "</style>\n" +
                "</head>\n" +
                "<body>\n" +
                "<p>Hi,<br><br>\n" +
                "Please find below details for Interface Message failure.<br>\n" +
                "<table>\n" +
                "  <tr>\n" +
                "    <td><span style=\"font-weight:bold\">Tenant Type</span></td>\n" +
                "    <td>${tenantType}</td>\n" +
                "  </tr>\n" +
                "  <tr>\n" +
                "    <td><span style=\"font-weight:bold\">iFlow Name</span></td>\n" +
                "    <td>${iflowName}</td>\n" +
                "  </tr>\n" +
                "  <tr>\n" +
                "    <td><span style=\"font-weight:bold\">Message ID</span></td>\n" +
                "    <td><a href=\"${msgIDLink}\">${msgID}</a></td>\n" +
                "  </tr>\n" +
                "  <tr>\n" +
                "    <td><span style=\"font-weight:bold\">HTTP Status</span></td>\n" +
                "    <td>${exceptionCode}</td>\n" +
                "  </tr>\n" +
                "  <tr>\n" +
                "    <td><span style=\"font-weight:bold\">Timestamp</span></td>\n" +
                "    <td>${systemTime}</td>\n" +
                "  </tr>\n" +
                "  <tr>\n" +
                "    <td><span style=\"font-weight:bold\">Exception</span></td>\n" +
                "    <td>${exceptionName}</td>\n" +
                "  </tr>\n" +
                "  <tr>\n" +
                "    <td><span style=\"font-weight:bold\">Error Message</span></td>\n" +
                "    <td>${exceptionMessage}</td> \n" +
                "  </tr>\n" +
                "</table><br>\n" +
                "Do Not Reply to this mail. Please contact SAP-Integration Team for further investigation.<br><br>\n" +
                "Regards,<br>\n" +
                "SAP Integration Team.\n" +
                "</p>\n" +
                "</body>\n" +
                "</html>";
        message.setHeader('subject',subject);
        message.setBody(mailBody);
    }
    message.setHeader('mime-type','text/html');
    return message;
}

 This is how the alert notification looks like in my mail box.

stephen_xue_0-1745454632634.png

feel free to modify the code and have fun. 

 

 

Accepted Solutions (0)

Answers (0)