As per thread: SOAP Sender attachment named attachment-1 discussion, SOAP sender Adapter always replaces the incoming attachment names with attachment-1, attachment-2 etc.. And as stefan.grube mentioned in the thread, this is the default SOAP adapter behavior since it was hardcoded in SOAP adapter for incoming attachments to XI. But most of the times, the business needs the original attachment names at the receiving target application. So, how can we achieve this.
In this blog, I am providing UDF code snippet using which we can retain original attachment names in the mapping by reading incoming attachments using mapping API for attachments (applicable from PI7.1 onwards). Also, with this procedure we can overwrite existing attachments with any content as bytes. Note:- the same can be achieved with a java mapping/XSLT with java enhancements as well.
String attachmentID = null;
java.util.Map map;
AbstractTrace trace;
//gets the input attachment from the source message
GlobalContainer globalContainer = container.getGlobalContainer();
InputAttachments inputAttachments = globalContainer.getInputAttachments();
OutputAttachments outputAttachments = globalContainer.getOutputAttachments();
trace = container.getTrace();
//map = globalContainer.getParameters();
try
{
//checks for the availability of attachments in the input message
if(inputAttachments.areAttachmentsAvailable())
{
trace.addInfo("Attachments Available");
//gets the attachmentIds and store it in an Object array
Collection<String> CollectionIDs = inputAttachments.getAllContentIds(true);
Object[] arrayObj = CollectionIDs.toArray();
//Loops at the input attachments to get the content of the attachment
for(int i =0;i<arrayObj.length;i++)
{
attachmentID =(String)arrayObj[i];
trace.addInfo((i+1) + ") Attachment Name: " + attachmentID);
Attachment attachment = inputAttachments.getAttachment(attachmentID);
String contentType = attachment.getContentType();
//To retain attachment names in mail adapter (receiver). Updated on 3:35 PM 09/30/2013 IST.
//Use charset only in case of textual attachments e.g., XML, flatfiles
contentType = contentType + ";charset = \"UTF-8\";"+ "name=\"" + attachmentID + "\"";
byte[] attachmentBytes = attachment.getContent();
Attachment renameAttachment = outputAttachments.create(attachmentID, contentType, attachmentBytes);
outputAttachments.setAttachment(renameAttachment);
//remove each attachment if required. Uncommonent below
//trace.addInfo("Removing Attachment no: " + (i+1) + " & Name: " + attachmentID);
//outputAttachments.removeAttachment(attachmentID);
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
return var1;
Attachments sent from SOAP UI as SOAP message to XI.
With the above UDF in the graphical mapping, the results will be as shown below for the inbound/mapping pipeline stages in MONI
Update: 3:35 PM 09/30/2013 IST
Code updated, to retain attachments names in case of receiver mail adapter. Line no: 30 in above UDF code snippet should be used in either of the below forms.
1) Code for textual attachments e.g., XML, flat etc.. with required encoding e.g., UTF-8
contentType = contentType + ";charset = \"UTF-8\";"+ "name=\"" + attachmentID + "\"";
2) Code for binary attachments e.g., pdf's, images etc..:
contentType = contentType + ";"+ "name=\"" + attachmentID + "\"";
Regards,
Praveen Gujjeti
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
4 | |
4 | |
4 | |
3 | |
3 | |
3 | |
3 | |
3 | |
3 | |
2 |