Dynamic filename in mail receiver adapter made easier
Update March 28, 2014:
I think the mail receiver adapter will soon be supporting dynamic filename, see my comment from today.
Recently I had a requirement in SAP PI to dynamically set filename of an email attachment on a mail receiver channel (outgoing message). I already knew this could be achieved by means of “Use Mail Package” option which is available when using message protocol XIPAYLOAD.
But is “Use Mail Package” really necessary ? Unfortunately the mail adapter does not provide an Adapter-Specific Message Attribute for setting Content-Type or Content-Disposition.
My next thought was to use MessageTransformBean which can indeed set attachment filename using transform.ContentDisposition or transform.ContentType – but only as static values, no support for dynamic properties. What a shame.
Finally I checked the SAP NW PI javadocs and found just what I was looking for ( works for 7.1x and 7.3x ):
So in a message mapping all you have to do is write a small UDF that uses method setContentType in a way where you set not only the Content-Type, but also charset and … ta da… filename!
For instance a single values UDF with three string arguments ContentType, CharSet and FileName:
Set Content-Type in output header, for instance to text/xml;charset=”UTF-8″;filename=”INVOICE_00000012114_00110514008.xml”
If the message is sent to a mail receiver channel the Content-Type in the output header will be used.
This way we can set a dynamic mail attachment filename without using mail package structure.
String CompositeContentType = ContentType + “;charset=\”” + CharSet + “\”;filename=\”” + FileName + “\””;
You would typically use this UDF on a mapping variable ( “Add variable” ) in your target message.
Or in a pure java mapping:
public void transform(TransformationInput arg0, TransformationOutput arg1) …
I prefer this rather than “Use Mail Package” option. Especially if some kind of twist or tweak needs to be done. For instance if you want to apply one or more (standard or custom) modules to the module chain of your mail channel to further manipulate your payload to make it ready for attachment. Then you would often be better off not having to deal with the extra level of complexity that the Mail Package brings.