// This is Groovy Flowstep Version 2.x, running with Groovy runtime 4, Downgrade the script if older behaviour needed.
// This is Groovy Flowstep Version 2.x, running with Groovy runtime 4, Downgrade the script if older behaviour needed.
import com.sap.it.script.v2.api.Message;
import java.io.StringWriter
import java.io.PrintWriter
def Message processData(Message message) {
def messageLog = messageLogFactory.getMessageLog(message);
if(messageLog != null){
def value ;
/*
value = message.getHeaders().get("id");
if(value!=null){
messageLog.addCustomHeaderProperty("zid", value);
}
value = message.getHeaders().get("sourceapplication");
if(value!=null){
messageLog.addCustomHeaderProperty("zsourceapplication", value);
}
*/
value = message.getHeaders().get("SapAuthenticatedUserName");
if(value!=null){
messageLog.addCustomHeaderProperty("zSapAuthenticatedUserName", value);
}
}
/* Consider loggin tese headers from the sender https adapter
x-ssl-client-issuer-dn L0M9REUvTz1FLk9OIFNFL0NOPUVPTiBJbnRlcm5hbCBUTFMgQ0EgdjYgRzE=
x-ssl-client-notafter 261009131220Z
x-ssl-client-notbefore 251009131220Z
x-ssl-client-root-ca-dn L0M9REUvTz1FLk9OIFNFL0NOPUVPTiBJbnRlcm5hbCBSb290IENBIEcx
x-ssl-client-session-id 51260EF252E08E9052C2D9A97D0D7E2E07C6DB5A57113A4E4D0E1D6E7CC14561
x-ssl-client-subject-cn bzE3MDYucGFydG5lcnMuZW9uLnNl
x-ssl-client-subject-dn b64encoded
*/
// get a map of properties
def map = message.getProperties();
// get an exception java class instance
def ex = map.get("CamelExceptionCaught");
if (ex!=null) {
messageLog.addCustomHeaderProperty("zexCanonicalName", ex.getClass().getCanonicalName());
// an http adapter throws an instance of com.sap.it.script.v2.api.exceptions.HttpOperationFailedException
if (ex.getClass().getCanonicalName().equals("com.sap.it.script.v2.api.exceptions.HttpOperationFailedException")) {
// this is the old version of the Groovy Flowstep
// save the http error response as a message attachment
//def messageLog = messageLogFactory.getMessageLog(message);
messageLog.addAttachmentAsString("zhttp.ResponseBody", ex.getResponseBody(), "text/plain");
// copy the http error response to an exchange property
message.setProperty("zhttp.ResponseBody",ex.getResponseBody());
// copy the http error response to the message body
message.setBody(ex.getResponseBody());
// copy the value of http error code (i.e. 500) to a property
message.setProperty("zhttp.StatusCode",ex.getStatusCode());
messageLog.addCustomHeaderProperty("zhttp.StatusCode", "" + ex.getStatusCode());
// copy the value of http error text (i.e. "Internal Server Error") to a property
message.setProperty("zhttp.StatusText",ex.getStatusText());
messageLog.addCustomHeaderProperty("zhttp.StatusText", ex.getStatusText());
} else if (ex.getClass().getCanonicalName().equals("com.sap.it.script.v2.api.exceptions.HttpOperationFailedException")) {
// Groovy Flowstep Version 2.x, running with Groovy runtime 4
//com.sap.it.script.v2.api.exceptions.HttpOperationFailedException
// save the http error response as a message attachment
//def messageLog = messageLogFactory.getMessageLog(message);
if("true".equalsIgnoreCase(map.get("zExceptionTrace"))){
messageLog.addAttachmentAsString("zhttp.ResponseBody", ex.getResponseBody(), "text/plain");
}
// copy the http error response to an exchange property
message.setProperty("http.ResponseBody",ex.getResponseBody());
// copy the http error response to the message body
//message.setBody(ex.getResponseBody() + "\nThe MPL ID for the failed message is : " + message.getHeader("SAP_MessageProcessingLogID"));
message.setBody(ex.getResponseBody());
// copy the value of http error code (i.e. 500) to a property
message.setProperty("http.StatusCode",ex.getStatusCode());
messageLog.addCustomHeaderProperty("zhttp.StatusCode", "" + ex.getStatusCode());
// copy the value of http error text (i.e. "Internal Server Error") to a property
message.setProperty("http.StatusText",ex.getStatusText());
messageLog.addCustomHeaderProperty("zhttp.StatusText", ex.getStatusText());
// ex.getResponseHeaders()
messageLog.addCustomHeaderProperty("zhttp.Message", ex.getMessage());
StringWriter sw = new StringWriter()
PrintWriter pw = new PrintWriter(sw)
ex.printStackTrace(pw)
def stackTraceString = sw.toString()
//ex.getStackTrace()
//messageLog.addCustomHeaderProperty("zhttp.StackTrace", stackTraceString);
if("true".equalsIgnoreCase(map.get("zExceptionTrace"))){
messageLog.addAttachmentAsString("zhttp.StackTrace", stackTraceString, "text/plain");
}
//
// } else if (ex.getClass().getCanonicalName().equals("org.apache.camel.component.ahc.AhcOperationFailedException")) {
} else if (ex.getClass().getCanonicalName().equals("com.google.gson.stream.MalformedJsonException")) {
StringWriter sw = new StringWriter()
PrintWriter pw = new PrintWriter(sw)
ex.printStackTrace(pw)
def stackTraceString = sw.toString()
if("true".equalsIgnoreCase(map.get("zExceptionTrace"))){
def msg = message.getBody(java.lang.String);
if(msg){
messageLog.addAttachmentAsString("zMsgBody", message.getBody(java.lang.String), "text/plain");
}
messageLog.addAttachmentAsString("zExStackTrace", stackTraceString, "text/plain");
}
if(ex.getMessage()){
messageLog.addCustomHeaderProperty("zExMessage", ex.getMessage());
}
if(ex.getCause()){
messageLog.addCustomHeaderProperty("zExCause", ex.getCause());
}
message.setHeader("CamelHttpResponseCode", "500");
messageLog.addCustomHeaderProperty("zhttp.StatusCode", "500");
message.setHeader("Content-Type", "text/plain");
message.setBody(ex.getClass().getCanonicalName() + "\nThe MPL ID for the failed message is : " + message.getHeader("SAP_MessageProcessingLogID"));
}else{
messageLog.addCustomHeaderProperty("zhttp.StatusCode", "501");
messageLog.addCustomHeaderProperty("zEx", "unknown");
throw ex;
}
}else{
messageLog.addCustomHeaderProperty("zhttp.StatusCode", "502");
messageLog.addCustomHeaderProperty("zEx", "null");
throw new Exception("ex null is not expected" + " The MPL ID for the failed message is : " + message.getHeader("SAP_MessageProcessingLogID"));
}
//throw ex;
return message;
}