
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)
Attachment
interface. The Attachment
interface is implemented by the DefaultAttachment
class located in the org.apache.camel.impl
package.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.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
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
5 | |
4 | |
4 | |
4 | |
4 | |
3 | |
3 | |
3 | |
3 | |
3 |