Skip to Content

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

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

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • Dec 25, 2017 at 07:11 PM

    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

    Add comment
    10|10000 characters needed characters exceeded