Technical Articles
Use groovy script to extract attachment content from soap message in CPI
Today one customer called a soap api from a third party system. Some important information is in attachment of the soap message . Customer needs to extract message in soap message attachment . I had some test and successfully extract the data from soap message . Let me share the steps, which may help others .
I support the readers has installed Soap UI.
Let me share the steps :
Step 1 , Develope and deploy iflow in CPI .
Develope iflow
/* Refer the link below to learn more about the use cases of script.
https://help.sap.com/viewer/368c481cd6954bdfa5d0435479fd4eaf/Cloud/en-US/148851bf8192412cba1f9d2c17f4bd25.html
If you want to know more about the SCRIPT APIs, refer the link below
https://help.sap.com/doc/a56f52e1a58e4e2bac7f7adbf45b2e26/Cloud/en-US/index.html */
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
def Message processData(Message message) {
//Attach
def attach = message.getAttachments();
def datahandler = attach.values()[0];
def content = datahandler.getContent();
def messageLog = messageLogFactory.getMessageLog(message);
if(messageLog != null){
messageLog.setStringProperty("Logging", "Printing Payload As Attachment");
messageLog.addAttachmentAsString("Message#2", content.toString(), "text/plain");
}
message.setBody(content);
return message;
}
After depoyment , we can find the iflow runtime URL and download the wsdl file with following screen shot .
Step 2, Test with Soap UI .
Get CPI runtime Client ID and Client Secret from CPI runtime instance service key
Test with Soap UI
We can find that the attachment content has been successfully extracted into response body .
Then End
Best regards!
Jacky Liu
Hello Jacky Liu.
Thanks for sharing and congrats for the great content.
Please, do you know how we can delete an attachment?
I see we have the method "removeAttachment(String id)", but I'm not sure how to get that ID.
I would appreciate any help from you.
Thank you!
Fellipe
Hi, Fellipe,
I can not find the methd removeAttachment(String id) for class interface Message in the follwoing linkage . .
Maybe you can try with the code to get the attachment key .
def attach = message.getAttachments();
def attachkey = attach.keys()[0];
https://help.sap.com/doc/a56f52e1a58e4e2bac7f7adbf45b2e26/Cloud/en-US/com/sap/gateway/ip/core/customdev/util/Message.html
Best regards!
Jacky Liu
Hi, Fellipe,
We can use the following code to get the key:
def attach = message.getAttachments();
def attachkey = attach.keySet()[0];
But the method message.removeAttachment() does not exist .
Best regards!
Jacky
Hello Jacky Liu.
Thanks for the answer.
I saw the removeAttachment() method in the Interface Message documentation of the package "org.apache.camel" (link).
In the package "com.sap.gateway.ip.core.customdev.utilI" I also didn't find.
Thank you for your attention and support.
Fellipe
Thanks Jacky Liu,
I am looking for a generic wrapper groovy script, that will receive metadata/payload from SOAP request and move it forward, turn on/off, collect cash data. Thanks in advance