Skip to Content

Business requirement is like:

  • While triggering SAP-PI’s SOAP web-service from client system, zip attachments will be send along with SOAP Request Payload. Here, please note, one zip per web-service call is in scope.
  • This zip attachments has multiple CSV files, which needs to be extracted in SAP’s application directory
  • Once unzip is successful, return unzipped zip-File in SOAP-Response Message

To accomplish above business requirements, one Java UDF will be written in Request Message Mapping of SAP-PI’s respective (SOAP-to-File Synchronous Inbound) interface

Here, given example of SAP PI Java UDF (user defined function), has below functionalities:

  • To read zip attachment from Request Payload
  • Unzip attachment to one SAP application server directory
  • Using Dynamic-configuration, return unzipped zip file name

UDF Imports:

  • java.util.zip.*
    java.io.ByteArrayInputStream
public String UnZip(String upzipPath, Container container)
 throws StreamTransformationException{

/*
UDF to:
            1. Get ZipFile Attachment (i.e. get the input attachment from the source message)
            2. Unzip/extract ZipFile to SAP-Application directory
            3. Set unzipped attachment name to "FileName" variable using 'DynamicConfiguration'
*/

//File destDir = new File("/folde1/folde2/folde3/"); 
File destDir = new File(upzipPath); 
String resultStr = "";

//To add trace info
AbstractTrace trace;
trace = container.getTrace();
trace.addInfo("UDF to unzip ZipFile atatchment to the directory " + destDir );

//Get the input attachment from the source message)
GlobalContainer  globalContainer    = container.getGlobalContainer();
InputAttachments inputAttachments = globalContainer.getInputAttachments();
try
{
 //checks for the availability of attachments in the input message
 if(inputAttachments.areAttachmentsAvailable()){
 trace.addInfo("Attachments Available");
												 
 //Get the attachmentIds and store it in an Object array
 Collection<String> CollectionIDs = inputAttachments.getAllContentIds(true);
 Object[] arrayObj = CollectionIDs.toArray();
                  											
 String attachmentID = null;													
 String contentIDS = "";
												 
 //Loops at the input attachments to get the content of the attachment
 for(int i =0;i<arrayObj.length;i++){
  attachmentID =(String)arrayObj[i];   																				  
  resultStr = 	attachmentID;        //Get ZipAttachment Name

  //==============================================
  Attachment attachment = inputAttachments.getAttachment(attachmentID);      
  ZipInputStream zipIs = new ZipInputStream(new ByteArrayInputStream(attachment.getContent()));
  ZipEntry zEntry = null;
  try {
   while ((zEntry = zipIs.getNextEntry()) != null) {							
    // Write each file into directory -------------------------------
    trace.addInfo("zipFile content: " + zEntry.getName());  // Get each Zip content names		

    File destFile = new File(destDir, zEntry.getName());
    FileOutputStream fos = new FileOutputStream(destFile); // It creates blank file in dir
    OutputStream output = new BufferedOutputStream(fos);
    int len;
    byte[] buffer = new byte[2048];
    while ((len = zipIs.read(buffer)) > 0) {
     output.write(buffer, 0, len);
    }
    output.flush();
    output.close();
    // End of Write each file into directory ------------------------
   }
   zipIs.close();
  } catch (FileNotFoundException e) {
      //e.printStackTrace();
      trace.addInfo(" UnZip UDF Error: " +  e.getMessage());
  } catch (IOException e) {
      //e.printStackTrace();
      trace.addInfo(" UnZip UDF Error: " +  e.getMessage());
  }
  //=============================================
 }
 }          
}
catch (Exception e)
{
  //e.printStackTrace();
  trace.addInfo(" UnZip UDF Error: " +  e.getMessage());
}

//=====================================================================
//Return original name of unzipped attachment (zipFile name)
try
{
DynamicConfiguration conf = (DynamicConfiguration) container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);
DynamicConfigurationKey key = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File","FileName");
conf.put(key, resultStr);
}
catch (Exception e)
{
   //e.getMessage();
   trace.addInfo(" StoreAttachment UDF Error: " +  e.getMessage());
}
//=====================================================================
return   resultStr;
}//end of fn UnZip

 

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply