Dear all,
I have been using Groovy scripting to log my Payload as an attachment in SCPI. (SAP Cloud Platform Integration)
Sometimes the script doesn't log my payload as my attachment.But sometimes it does.
Is there any other setting to be enabled in SCPI or SAP Cloud Platform to enable this ?
Some kind of Permissions or Roles is required ?
Below is the script I am using:
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
def Message processData(Message message) {
def bodyAsString = message.getBody(String.class);
def messageLog = messageLogFactory.getMessageLog(message);
messageLog.setStringProperty("Logging#1", "Printing Payload As Attachment");
messageLog.addAttachmentAsString("Log Payload",bodyAsString,"text/xml");
return message;
}
I am just using the script after the request reply as below:

Even in Logs, I am not getting Logging#1", "Printing Payload As Attachment

Request clarification before answering.
sorry, that was a typo
def body = message.getBody();
BufferedInputStream bis =new BufferedInputStream(body);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I am getting this error:
javax.script.ScriptException: java.lang.Exception: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: java.io.BufferedInputStream(java.lang.String)@ line 7 in script1.groovy, cause: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: java.io.BufferedInputStream(java.lang.String)
Script I am using is
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import java.io.BufferedInputStream;
def Message processData(Message message) {
def body = message.getBody();
BufferedInputStream bis = new BufferedInputStream(body);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
int result = bis.read();
while(result != -1) {
buf.write((byte) result);
result = bis.read();
}
def out_string = buf.toString("UTF-8");
def messageLog = messageLogFactory.getMessageLog(message);
messageLog.setStringProperty("Logging#1", "Printing Payload As Attachment");
messageLog.addAttachmentAsString("Log Payload",buf,"text/xml");
return message;
}
Also it is important to review logs size limiTations. Please review below KBA door more details.
https://launchpad.support.sap.com/#/notes/2593825
Bhargav
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Sravan,
Yes, we need to add.
def bodyAsString =message.getBody(String.class);In the statement you are trying to convert from message type to String type but actually i think it is coming as Bufferd stream so we need to add the code.
BufferedInputStream bis = new BufferedInputStream(message);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
int result = bis.read();
while(result != -1) {
buf.write((byte) result);
result = bis.read();
}
// StandardCharsets.UTF_8.name() > JDK 7
return buf.toString("UTF-8");
<br>
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Should the script be changes as following ?
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
def Message processData(Message message) {
BufferedInputStream bis = new BufferedInputStream(message);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
int result = bis.read();
while(result != -1) {
buf.write((byte) result);
result = bis.read();
}
return buf.toString("UTF-8");
// def bodyAsString = message.getBody(String.class);
def messageLog = messageLogFactory.getMessageLog(message);
messageLog.setStringProperty("Logging#1", "Printing Payload As Attachment");
//messageLog.addAttachmentAsString("Log Payload",bodyAsString,"text/xml");
messageLog.addAttachmentAsString("Log Payload",buf,"text/xml");
return message;
}
Hi Sravan,
The reason for this is, when the payload comes as BufferedReader to Stream, try the below to convert to string.
String newLine = System.getProperty("line.separator");
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder result = new StringBuilder();
boolean flag = false;
for (String line; (line = reader.readLine()) != null; ) {
result.append(flag? newLine: "").append(line);
flag = true;
}
return result.toString();
/////////////////////////////////////////////////////////////////////////////////
(Or)
BufferedInputStream bis = new BufferedInputStream(inputStream);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
int result = bis.read();
while(result != -1) {
buf.write((byte) result);
result = bis.read();
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 5 | |
| 4 | |
| 4 | |
| 3 | |
| 2 | |
| 2 | |
| 2 | |
| 2 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.