Skip to Content
Technical Articles
Author's profile photo Raffael Herrmann

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]).

Assigned Tags

      4 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Eng Swee Yeoh
      Eng Swee Yeoh

      Very nicely done... and I love that it is a 2 minute read 😉

      Author's profile photo Raffael Herrmann
      Raffael Herrmann
      Blog Post Author

      Thanks for the kind words. And yes, I wanted to achieve a shorter article this time. Not every solution I blog about deserves to be a 1 hour read. 😉

      Author's profile photo Nero Zhang
      Nero Zhang

      Nicely blog!!!

      Author's profile photo Venu Ravipati
      Venu Ravipati

      Very nice Blog. Thanks for sharing..