Quantcast
Channel: SCN : Blog List - Process Integration (PI) & SOA Middleware
Viewing all articles
Browse latest Browse all 676

Creating File Name from SOAP Attachment

$
0
0

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.

 

Source_FileName.JPG
Fig.1, Attachment sent through SOAP UI

Dynamic_Conf.JPG

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.

 

Filename1.JPG
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.

 

Soap_Sender_Channel1.JPG
Fig.4, Sender Communication channel configuration

 

Soap_Sender_Channel2.JPG

Soap_Sender_Channel3.JPG
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.

TempFileName.JPG

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:

Message_Mapping.JPG

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.

File_Receiver_Channel1.JPG

Fig.8, Receiver Communication channel 1

File_Receiver_Channel2.JPG
Fig.9, Receiver Communication channel 2

File_Receiver_Channel3.JPG

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.

Dynamic_Conf2.JPG

Fig.11, Dynamic Configuration

AuditLog.JPG

Fig.12, Audit log

 

The audit log show that the DynamicConfigurationBean module is used to get the attachment and PayloadSwapBean module swaps the attachment.

Target_File_Name.JPG

Target_File_Name1.JPG

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.


Viewing all articles
Browse latest Browse all 676

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>