Skip to Content
0

SAP HCI-PI: How to store message body content into an attachment

Dec 25, 2017 at 04:42 PM

306

avatar image

Hello All,

I need to write/store message body content into an attachment. So that I can use that attachment in mail adapter.

Hope this would be achieved by using script. Can anyone provide ideas on this.?

Also can you help me on how to find the size of message body content.? (Needed for defining condition in router based on message body size)

Regards,

Deva

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

1 Answer

Morten Wittrock Dec 25, 2017 at 07:11 PM
0

Hi Deva

I've covered the basics of what you need in this blog.

That was about binary content, though. If your message body is XML, you can add it as an attachment as follows:

import com.sap.gateway.ip.core.customdev.util.Message
import org.apache.camel.impl.DefaultAttachment
import javax.activation.DataHandler

def Message processData(Message message) {

   // 1: Construct a DataHandler object with the message body and XML MIME type
   def dataHandler = new DataHandler(message.getBody(), 'application/xml')

   // 2: Construct a DefaultAttachment object
   def attachment = new DefaultAttachment(dataHandler)

   // 3: Add the attachment to the message
   message.addAttachmentObject('message-body.xml', attachment)
   
   return message

}

As for the size of the message body, you can check the number of characters with:

message.getBody(java.lang.String).length()

Alternatively, you can count the number of bytes (using the default encoding) with:

message.getBody(java.lang.String).getBytes().length

Regards,

Morten

Show 2 Share
10 |10000 characters needed characters left characters exceeded

Thanks a lot Morten.

It works..

Regards,

Deva

1

That's good to hear, Deva. Please keep in mind that closing the question and accepting an answer is two separate actions in the new community site. Regards, Morten.

0