Skip to Content
Technical Articles

How to handle SAP CPI attachments via Apache Simple in Content Modifier

Recently I was asked if there is a way to process message attachments in the SAP CPI directly in the Content Modifier without reading the attachments in a Groovy script first. Since I was not quite sure, I built a small proof of concept and what should I say? It works!

In the following article I want to show you how to access message attachments directly in the Content Modifier (or anywhere else where you can use simple expressions) using Apache Simple  expression syntax.

The test scenario

As a test scenario I designed a simple, synchronous SOAP interface. The interface accepts a SOAP message including SOAP attachment, maps/transforms the SOAP body and attachment into the response message and then returns it to the caller.

The following SoapUI screenshot shows the request message and the expected result.

(Click on the screenshot to enlarge it.)

The corresponding IFlow consists of only one sender channel and one content modifier. Groovy scripts or similar coding is not necessary.

Now that the test setup is clear, we can look at how to address attachments in the Content Modifier.

The implementation

In contrast to the body, which can be accessed via ${body}, or for example to properties that can be accessed via ${property.propertyName}, there is no direct access to the attachments. However, a look at the Apache Simple documentation tells us that we can access the Exchange container by use of ${exchange}. Starting from the Exchange Container we can then work our way through to the attachments.

With this knowledge we can now access the attachments directly in the Content Modifier. We can read the names using the getAttachmentNames() method and the data itself using the getAttachments() method.

<root>
  <body>
    <![CDATA[ ${body} ]]>
  </body>
  <attachmentName>${exchange.getIn().getAttachmentNames()[0]}</attachmentName>
  <attachmentContent>
    <![CDATA[ ${exchange.getIn().getAttachments().values()[0].getContent()} ]]>
  </attachmentContent>
</root>

With ${exchange} you get the Camel Exchange container as “DefaultExchange” interface. From there on you can call all functions the DefaultExchange class offers to you.

Also have a look to the JavaDOCs of the following classes to get a better understanding:

Conclusion

In general it is possible to read attachments directly using Apache Simple expressions. But since it is not possible to write loops or iterate over entries of arrays/maps by use of Simple expressions, this kind of access only makes sense if the number of attachments is known/fixed. (Otherwise you couldn’t access the entries via their indexes e.g. getAttachmentsNames()[0]).

4 Comments
You must be Logged on to comment or reply to a post.