cancel
Showing results for 
Search instead for 
Did you mean: 

SAP CPI: modify filename of an attachment during email processing?

iglmarkus
Explorer
0 Kudos

I have a artifact reading Mailbox and send mail using XI Adapter to proxy implementation.

What happens: if the attachment name includes space the the XI Adapter will fail with this response message "org.apache.cxf.binding.soap.SoapFault: Error during conversion of XI message"

If the attachment does not have any space in filename - all works fine.

So now my idea is repacing the filename within the email using a Groovy script - or is there a smarter way in order hand over to the proxy class using XI Adapter?

Back to the Groovy script, could the datahandler manipulate the Filename and write it back to the email?

Is there a example for such a groovy arround?

thanks for your feedback.

Markus

Accepted Solutions (0)

Answers (2)

Answers (2)

iglmarkus
Explorer
0 Kudos

HI Alan,

thanks for your proposal, but this is not exactly what i need.

I have already a script reading the attachments from the mail using DataHandler.

All filenames from email attachments will be returned in an array from the DataHandler, now all spaces in the filenames shall be replaced "\\s+", "_" if this is done update the mail with the new filenames (array).

that's the idea.

BR Markus

iglmarkus
Explorer
0 Kudos

import com.sap.gateway.ip.core.customdev.util.Message

import java.util.HashMap

import javax.activation.DataHandler

def Message processData(Message message) {

// Create a new Map of attachments and store it in a property.

def attachments = new HashMap<String, DataHandler>(message.getAttachments())

message.setProperty('Attachments', attachments)

// All done.

return message

}

you will see in the property Attachments is the list of all email attachments.

S0016260963
Participant
0 Kudos

Hi Markus,

Apologies for the misunderstanding. I'm still not 100% clear...

Based on your code, the property Attachments will now contain the contents of hashmap attachments. Does each key-value pair in the hashmap contain the filename (key) and the file content (value, presumably in Base64?) - i.e. does the output look like this?

{filename1=filecontent1_abcde, filename2=filecontent2_fghij, filename3=filecontent3_klmno, etc} 

Ref for info about hashmaps: https://blogs.sap.com/2019/07/08/using-hashmaps-for-storing-and-retrieving-data-in-i-flows-cpi/

Again, apologies if I'm not understanding this correctly.

Thanks,

Alan

S0016260963
Participant
0 Kudos

Hi Markus,

If I understand your question, you want to replace the spaces in a filename with something else (e.g. an underscore).

I did this recently - simply put the filename in an Exchange Property, and then use the following groovy script to read the property, replace the spaces with underscore, and write it back to the same property:

import com.sap.it.api.mapping.*;
import com.sap.gateway.ip.core.customdev.util.Message;

def Message processData(Message message) {
    
/* replace space in filename with underscore */
	
	// get filename from property
        filename = message.getProperty("PROP_filename");
		
	// replace spaces withunderscore 
	// note: this replaces consecutive whitespaces with a single underscore
	filename_adj = filename.replaceAll("\\s+", "_");
	
	// return property
	message.setProperty("PROP_filename",filename_adj);
    
    return message;
}

I hope this is what you mean.

Cheers,

Alan