on 2017 Feb 15 6:14 PM
Hello,
I have a scenario where I collect an email by IMAP. This mail will have one XML-file attachment. I need to map this XML file. But I don’t know how to access this attachment and route it forward instead of the message body.
I tried to access those with ${in.attachments} and ${in.attachment} but this doesen’t work. If I just forward the whole message to an email receiver, I get the attachment again. Therefore, the attachment is there but I just do not know how to access it and forward it to a mapping.
Can anybody may help me?
Thanks Florian
Hi Florian
You can access the attachment in a script step, and replace the message body with its content. Give the following code a try:
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() // [1]
Iterator<DataHandler> it = attachments.values().iterator() // [2]
Object attachment = it.next().getContent() // [3]
message.setBody(attachment) // [4]
return message
}
Most of the code deals with the fact, that the attachments are returned as a Map object, and we don't know the key of the only value.
In [1] I get the Map of all the message's attachments (we're assuming there's only one). In [2] I create an Iterator, that will iterate over the values, which are DataHandler objects. In [3] I get the content of the first value, and finally in [4] I set the message's body to that content.
I've only tested this with SOAP attachments, but the principles are the same.
Let me know how it works out 🙂
Regards,
Morten
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you very much Morten!
This is working. I just need to add the ";".
If I forward this message by mail, I get the XML in the mail body. Exactly how I expected it.
To test the XML structure I just add a message mapping where the input is the same as the output structure. Then I added the mapping for every field.
But then I get the following error
com.sap.xi.mapping.camel.XiMappingException:
com.sap.aii.mappingtool.tf7.IllegalInstanceException: Cannot produce target
element /ns1:JournalEntryBulkCreateRequest/MessageHeader. Queue has not enougth
values in context. Target xsd requires a value for this element, but target
field mapping does not produce one. Probably the xml-instance is not valid to
the source xsd, or the target field mapping does not fulfill the requirement of
the target xsd., cause: com.sap.aii.mappingtool.tf7.IllegalInstanceException:
Cannot produce target element /ns1:JournalEntryBulkCreateRequest/MessageHeader.
Queue has not enougth values in context. Target xsd requires a value for this
element, but target field mapping does not produce one. Probably the
xml-instance is not valid to the source xsd, or the target field mapping does
not fulfill the requirement of the target xsd.
But this element is there in the source message. So do I need to make some kind of MIME encoding that the payload is treated like XML?
Could you may also help me with that? 🙂
Thank you and regards
Florian
Hi Florian. I take it you have an XML Schema for the XML format. Is the message valid according to that schema?
User | Count |
---|---|
57 | |
11 | |
7 | |
6 | |
6 | |
6 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.