Skip to Content
Dec 08, 2021 at 10:53 AM

How to modify Content-Type Header of Attachment object with Groovy in CPI?

116 Views Last edit Dec 08, 2021 at 12:38 PM 2 rev

We are receiving a SOAP message with an attachment in CPI. Unfortunately when trying to place the message on a JMS queue the following error occurs within the JMS Receiver Adapter.
Inbound processing in endpoint at /attachment/test failed with message "Fault:javax.mail.internet.ParseException: In Content-Type string <image;name="Nice_picture.jpg";>, expected '/', got ;", caused by "ParseException:In Content-Type string <image;name="Nice_picture.jpg";>, expected '/', got ;"

Looking at the raw message data we post on the SOAP endpoint we noticed the Media-Type in the Content-Type Header is not complete; the subtype is missing. This apparently is not sufficient for JMS Receiver Adapter.
Content-ID: <125303109>
Content-Disposition: attachment;filename="<125303109>.bin"
Content-Type: image;name="Nice_picture.jpg";
Content-Description: <125303109>

When changing the header of the attachment to image/jpeg and resending the message everything works fine.

To solve this issue in CPI I wanted to write a script which would fix the Media Type and replace the header. Sadly this doesn't work yet. It seems I am not able to modify the Content-Type header of the Attachment because the error persists with this script.

import java.util.Map
import org.apache.camel.*

def Message processData(Message message) {    
    Map<String, Attachment> attachments = message.getAttachmentWrapperObjects()  
    for (Attachment attachment : attachments.values()) {
        // Get the Content-Type header of this Attachment
        def attachmentContentType = attachment.getHeader('Content-Type')
            def attachmentContentTypeFixed = attachmentContentType.replaceFirst(/^image;/, "image/jpeg;")
        // Update header on attachment with fixed Media Type
        message.setAttachmentHeader('Content-Type', attachmentContentTypeFixed, attachment) 
       return message;

Checks later in the IFlow (e.g. message.setHeader('X_checkHeader, attachment.getHeader('Content-Type'))) show the header has been modified but when sending the message to a JMS queue, or an other SOAP endpoint, the header is reverted back the old erroneous value.

What I am doing wrong here and how could I solve the issue with the Content-Type parsing error in the JMS Receiver Adapter?