cancel
Showing results for 
Search instead for 
Did you mean: 

Problem logging the payload as an Attachment

01-23-2019 10:49 AM
4048 views 10 comments
SAP Managed Tags
Subscribe

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

Accepted Solutions (0)

Answers (4)

Answers (4)

Former Member
0 Likes

sorry, that was a typo

def body = message.getBody();
BufferedInputStream bis =new BufferedInputStream(body);
0 Likes

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;
}
former_member26700
Participant
0 Likes

Also it is important to review logs size limiTations. Please review below KBA door more details.

https://launchpad.support.sap.com/#/notes/2593825

Bhargav

0 Likes

Thanks Bhargav.

Did not knew it.

Best Regards

Sravan

Former Member
0 Likes

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>
0 Likes

Hi Vijay,

Thanks for explaining.

I will try it and check.

Best Regards

Sravan

0 Likes

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;
}
Former Member
0 Likes

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();
}
0 Likes

Hi Vijay,

Sorry for my bad understanding. I am new to using SCPI.

Should i add the above code in my script as well ?

Best Regards

Sravan