Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
MortenWittrock
SAP Mentor
SAP Mentor
18,144
In a previous blog post of mine, I discussed how you can replace the body of a Cloud Integration message with the contents of an attachment. t.vanrooijen commented on that post, asking how you can add a binary attachment to a Cloud Integration message, and have that attachment delivered by a receiver mail channel. Since the answer is probably of interest to others, I’m going to describe an approach to solving Tom’s problem in this blog post.

The addAttachmentObject method


Unsurprisingly, the key to adding an attachment to a message is the Message interface. I’ve previously blogged about the methods of this interface, and among them we find the addAttachmentObject method:
public void addAttachmentObject(java.lang.String id, org.apache.camel.Attachment content)

The method expects a string, which is the name of the attachment, and an object of a class implementing the Apache Camel Attachment interface. The Attachment interface is implemented by the DefaultAttachment class located in the org.apache.camel.impl package.

Creating an attachment object


In order to construct a DefaultAttachment object, we need an object wrapping the actual contents of the attachment. In the following, I will assume that we have the contents in a byte array. In Java we have a convenient way of wrapping a byte array: The ByteArrayDataSource class, which implements the DataSource interface. Luckily, this class is available in Cloud Integration. To construct a ByteArrayDataSource object, we need to provide the byte array and a string. The string is the MIME type of the attachment.

Configuring the receiver mail channel


In order for the receiver mail channel to pass our attachment on to the email’s recipient, we need to instruct it to add attachments from the message. To do so, go to the channel's Connection tab, and make sure that the Add Message Attachments checkbox is selected.

Assembling the pieces


We now know everything we need to know in order to add an attachment in code. In the Groovy code below, the attachment is a PNG image, which I decode from Base64 in order to get a byte array. The binary content can be anything else, of course (but remember to adjust the MIME type accordingly).
import com.sap.gateway.ip.core.customdev.util.Message
import org.apache.camel.impl.DefaultAttachment
import javax.mail.util.ByteArrayDataSource

def Message processData(Message message) {

// 1: Get the binary attachment content in the form of a byte array
def imageBytes = 'iVBORw0KGgoAAAANSUhEUgAAAFsAAAALCAIAAACf2mY5AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAEySURBVEhL7ZTBGcMgCIUzlwM5j9NkmQyTPggq+IGml6SH/idFwOcL7Xb+sZAje96YvOtlgJuxLnucuSQ+VUdHSVsqBy1lRkpqx3tORZYxKr/jBt9lIskewSLZTh2pPm/Vv8YtR3r518ODTwauW+n78YYOvJ58nkpxpmKQJK04aemIZBLX3VRda7C2g7N2RJfTfaOnC1CumtFXQnnck41aSeKKtrT5CMg2nhEuUpgnrR2RRwj2gXdgGVV4ySIp6mlPOvraQcOgqG1jR9DAzoVmaHdhgvb+SPGEPQNUoTRJq7jnHUeQ1GaE1t5MAXKkTgNlyM+HkyGANxdOSAbHDfa2jBUWqdFQV36A9sHpOdzuBqskFYbP9f8BKE0yIw9Cl6/9eI/HHYEh8Y/xF3h+Rn6b8/wA8YzyCnrn53gAAAAASUVORK5CYII='.decodeBase64()

// 2: Construct a ByteArrayDataSource object with the byte array and the content's MIME type
def dataSource = new ByteArrayDataSource(imageBytes, 'image/png')

// 3: Construct a DefaultAttachment object
def attachment = new DefaultAttachment(dataSource)

// 4: Add the attachment to the message
message.addAttachmentObject('hello-world.png', attachment)

return message

}

Execute the code in a script step in your integration flow, and the following image will be attached to the message and delivered by the receiver mail channel:

19 Comments
Labels in this area