Creating File Name from SOAP Attachment
In this blog, I would like to share the steps to save SOAP attachment in a folder, with the same name. The SOAP adapter will change attachment name to ‘attachment-1’ in default, so we can’t get the original name directly. I was referring a blog, Creating File Name from Mail Attachment using Standard Beans, which must be working fine for Mail to File scenario. When I tried the same for a SOAP adapter, using MultipartHeaderBean module, the attachment name came with some special characters, which is shown in Fig 2.
Fig.1, Attachment sent through SOAP UI
Fig.2, Details of the attached file in PI after using MultipartHeaderBean Module
I used DynamicConfigurationBean and PayloadSwapBean in receiver adapter(File). The attachment name has “<” and “>”, which are not permitted in the creation of file name, so they are replaced with underscores “_”, which is shown in Fig 3.
Fig.3, Target file name with underscores
The underscores are making the attachment as an unknown file. I tried all the possible ways in Integration Builder, but unable to fix this. I then referred the blog Retaining SOAP Adapter Attachment Names, by making some minor changes in the UDF, I was able to get the correct name.
Configuration Steps
1. Sender Communication Channel
Create the necessary Data types, message types, service interfaces and mappings. In the sender SOAP communication channel, the parameter “keep attachments” should be checked. I didn’t use ASMA and MultipartHeaderBean module. Please refer Fig 4 and 5 for sender channel configuration.
Fig.4, Sender Communication channel configuration
Fig.5, Sender Communication channel – advanced options
2. Message Mapping
UDF to get the attachment name:
String attachmentID = null;
AbstractTrace trace;
GlobalContainer globalContainer = container.getGlobalContainer();
InputAttachments inputAttachments = globalContainer.getInputAttachments();
OutputAttachments outputAttachments = globalContainer.getOutputAttachments();
trace = container.getTrace();
DynamicConfiguration conf = (DynamicConfiguration) container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);
try
{
if(inputAttachments.areAttachmentsAvailable())
{
Collection<String> CollectionIDs = inputAttachments.getAllContentIds(true);
Object[] arrayObj = CollectionIDs.toArray();
for(int i =0;i<arrayObj.length;i++)
{
attachmentID =(String)arrayObj[i];
Attachment attachment = inputAttachments.getAttachment(attachmentID);
byte[] attachmentBytes = attachment.getContent();
Attachment renameAttachment = outputAttachments.create(attachmentID, attachmentBytes);
DynamicConfigurationKey key = DynamicConfigurationKey.create(“http://sap.com/xi/XI/System/File“,”TempFileName”);
attachmentID =”” + renameAttachment;
conf.put(key, attachmentID);
}
}
}
catch (Exception e)
{
trace.addWarning(“caught exception: ” + e + ” – probably no adapter-specific message attribute.”);
e.printStackTrace();
}
return attachmentID;
I used the Dynamic configuration syntax to show the attachment name generated by the above UDF, which is given in the below figure.
Fig. 6, Attachment Name with it’s attributes
I used replace string and trim function to get the file name alone. After that use the below UDF to create the correct attachment name in Dynamic Configuration.
UDF to create the attachment name in Dynamic Configuration:
DynamicConfiguration conf = (DynamicConfiguration) container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);
DynamicConfigurationKey key = DynamicConfigurationKey.create(“http://sap.com/xi/XI/System/File“,”FileName”);
String MyFileName = var1;
conf.put(key,MyFileName);
return var1;
Message Mapping Definition:
Fig.7, Message Mapping definition for getting the file name
Note: Please make sure that “Read Attachment” option is enabled in Operation Mapping.
3. Receiver Communication Channel
I used Dynamic Configuration and Payload swap modules in the receiver file communication channel. Please refer Fig. 8,9 and 10.
Fig.8, Receiver Communication channel 1
Fig.9, Receiver Communication channel 2
Fig.10, Receiver Communication channel 3
Testing
I sent the same Test_File.txt, using SOAP UI tool. Please refer the below screen shots of Dynamic Configuration, audit logs and target file.
Fig.11, Dynamic Configuration
Fig.12, Audit log
The audit log show that the DynamicConfigurationBean module is used to get the attachment and PayloadSwapBean module swaps the attachment.
Fig.13, Target file
As shown in the above figure the attachment is saved in the file location with the same name. I’m able to do the same with .jpg, .pdf, .doc, .xlsx, .csv and .xml files.
Hi Deepak,
This blog is informative. Thanks! I was looking for this option.
Regards,
Gopi
Thank you gopi
Hi Deepak,
Instead of using payloadswap bean and dynamic configuration module beans,
won't it be simple to get attachmentID (attachment name) using UDF and set Dynamic Configuration for FileName with the read attachment name in the same UDF. And finally, set ASMA for FileName in receiver file channel?
Just to add, in case of binary files (jpg, pdf etc..), use a java mapping to write attachment content as main payload and have equivalent java mapping code for UDF to set FileName for receiver file channel
Thanks,
Praveen Gujjeti
Hi Praveen,
Thank you for the valuable suggestion. I will try as you mentioned and will share the result.
Regards,
Deepak R
Great Start Deepak.. Keep Going 🙂
Hi Deepak,
I have a synchronous scenario and I have to set the name of excel attachment which is getting created in PI itself.
SOAP-PI_PROXY
Binary data received from ECC and creating attachment out of data in one of the fields.
It's an excel file.
https://scn.sap.com/thread/3955194
Please Advise.
Thanks,
Indu Khurana.