com.sap.gateway.ip.core.customdev.util.Message
object, which the runtime passes to your Groovy function. The API documentation available for this interface is available here.getAttachments
and setBody
. The former returns a java.util.Map
object containing the message’s attachments, and the latter sets a new message body.Map
returned by getAttachments
are attachment names, and the values are javax.activation.DataHandler
objects. The DataHandler
objects contain the actual attachment data. Given the scenario, we assume that the Map
contains exactly one attachment, but we do not know its key ahead of time.Map
’s only value without knowing its associated key, I retrieve the Collection
of all the Map
’s values and iterate it once. The code looks like this (required import
statements not shown):Map<String, DataHandler> attachments = message.getAttachments()
Iterator<DataHandler> it = attachments.values().iterator()
DataHandler attachment = it.next()
DataHandler
class offers a couple of different ways to get at the wrapped data. We are going to use the getContent
method, which returns a java.lang.Object
object. Why? Because an Object
instance is what the setBody
method of the Message
class expects. Here is the code that replaces the message body:message.setBody(attachment.getContent())
import com.sap.gateway.ip.core.customdev.util.Message
import java.util.Map
import java.util.Iterator
import javax.activation.DataHandler
def Message processData(Message message) {
Map<String, DataHandler> attachments = message.getAttachments()
Iterator<DataHandler> it = attachments.values().iterator()
DataHandler attachment = it.next()
message.setBody(attachment.getContent())
return message
}
Map
returned by getAttachments
is empty. You do this by calling its isEmpty
method as follows:import com.sap.gateway.ip.core.customdev.util.Message
import java.util.Map
import java.util.Iterator
import javax.activation.DataHandler
def Message processData(Message message) {
Map<String, DataHandler> attachments = message.getAttachments()
if (attachments.isEmpty()) {
// Handling of missing attachment goes here
} else {
Iterator<DataHandler> it = attachments.values().iterator()
DataHandler attachment = it.next()
message.setBody(attachment.getContent())
}
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 | |
3 | |
3 | |
3 | |
3 | |
3 | |
3 | |
3 | |
3 | |
3 |