cancel
Showing results for 
Search instead for 
Did you mean: 

Read zip attachments mixed with non-zip files

Former Member
0 Kudos

Hello

We have a requirement in the Brazil project related with Nfe B2B inbound process, it is the process responsible for receive the XMLs files of the vendors in a mailbox.

I was able to build a interface Mail to Proxy that is working, this interface is responsible to read the mailbox and by a java mapping identify in the content of xml, if it is related with NFe, CTe or Event. After, it is sent to ECC by ABAP proxy, then in ECC we are able to receive it and identify what folder it should be recorded, we have one folder for nfe, other for cte and etc..

It is working very well, but as not everything is easy, I should be able to set the interface to work with ZIP files, we can receive mix files in the same mailbox, I can receive zip and non-zip files in the same mail. Or differents mails, some with zip and some other with non-zip.

I tried with a JAVA resource a way to unzip the file by java, and we are able to do it, however it is not being reading by the java function to identify the content yet, but anyway, it is not the problem yet. The problem is: If I set the mail adapter in PI to work with ZIP files I’m not able to work with non-zip files anymore.

I tried in many ways, talked with some friends but I’m not able to do it, Maybe, I don’t know if has someone can help me.

Follow the set that I did, it is prepared to zip files:

Mail adapter settings:


Java mapping:


Message monitoring in SAP ECC IEG510 with a zip files(we have a error due to the java function to do the identification of content is not working with ZIP yet)

Regards,

Erica

Accepted Solutions (1)

Accepted Solutions (1)

Snavi
Active Participant
0 Kudos

Hi Erica,


you read the attachment name in your java mapping and proceed with different logic based on file extension if it is .zip or non zip.

Former Member
0 Kudos

Hi,

Thanks for your return, however could you give me a example how it could be done in the Java?

Should I keep the PayloadSwapBean in the communication channel? Because I tried to remove this and The function to unzip not works.



Regards,

Erica

Snavi
Active Participant
0 Kudos

Hi Erica,

here is the small code snippet that you can use in your java mapping to get the email attachments filenames

public void transform(TransformationInput in, TransformationOutput out)

throws StreamTransformationException

{

InputAttachments ia = in.getInputAttachments();

Collection<String> col = ia.getAllContentIds(true);

Object[] arrayobj = col.toArray();

String fileName = "";

for (int i = 0; i<arrayobj.length;i++)

{

String AttachmentID = (String) arrayobj[i];

Attachment attach = ia.getAttachment(AttachmentID);

Attachment a  = ia.getAttachmentarrayobj[i].toString()

fileName = a.getContentType().split("=")[1].replace('"',' ').trim();

you can use filename in condition and put the logic for zip or non zip attachments.

Former Member
0 Kudos

Thanks.

The condition that we are saying is in the java code?

Should I keep the PayloadSwapBean in the communication channel?


Regards

Erica

former_member190293
Active Contributor
0 Kudos

Hi Erica!

I think that you don't need PayloadSwapBean. Since you use java code to read and process attachments, you can unzip it right in your java function.

Regards, Evgeniy.

Answers (1)

Answers (1)

former_member190293
Active Contributor
0 Kudos

Hi Erica!

I'm not much experienced in using mail adapter but it seems to me that your zip attachments must have the same content type. If so, you can check it and proccess it accordingly.

Regards, Evgeniy.

Former Member
0 Kudos

It has the same content.

Regards

Erica

former_member182503
Active Contributor
0 Kudos

Erica,

what is your PI/PO version?

Depending on your system version you can use a different approach for this interface, using a new feature from PI 7.4 SP 09, described here Processing Mail Attachments as PI Messages Using the PI Sender Mail Adapter (New) - What's New in SA... and on OSS note 2040884.

However, I would stick to the approach you are currently doing. You don`t need the PayloadSwapBean on your Mail adapter, you just need to iterate over the attachments from your message, checking for their content type, doing something like this (tweaking Evgeniy`s code):


public static String getFileName(String contentType){

  String fileName = "";

  Pattern pattern = Pattern.compile("name=['\"](.*)[\"']");

  Matcher matcher = pattern.matcher(contentType);

  if (matcher.find())

      fileName = matcher.group(1);

  return fileName;

  }

  public static String getFileExtension(String fileName){

  if (fileName.lastIndexOf(".") > 0) {

  return fileName.substring(fileName.lastIndexOf(".")+1, fileName.length()).toLowerCase();

  } else {

  return "";

  }

  }

  public void transform(TransformationInput in, TransformationOutput out)

  throws StreamTransformationException

  {

      InputAttachments ia = in.getInputAttachments();

      Collection<String> col = ia.getAllContentIds(true);

      Object[] arrayobj = col.toArray();

      String fileName = "";

      String fileExt = "";

      

      for (int i = 0; i<arrayobj.length;i++) {

            String AttachmentID = (String) arrayobj[i];

            Attachment attach = ia.getAttachment(AttachmentID);

            fileName = getFileName(attach.getContentType());

            fileExt = getFileExtension(fileName);

          

            //If java >= 1.7

            switch (fileExt) {

    case "zip":

    //ZIP File found - unzip it and check for XML files...

    break;

    case "xml":

    //XML file found - proceed with processing

    break;

    default:

    //Other kind of file - just ignore

    break;

    }

            //if java < 1.7

            if (fileExt.equals("zip")) {

            //ZIP File found - unzip it and check for XML files...

            } else if(fileExt.equals("xml")){

          //XML file found - proceed with processing

    } else {

    //Other kind of file - just ignore

    }

          

      }

  }

Regards,

JN

Former Member
0 Kudos

Hi Jose,

Thanks for return,

About the sap note, I tried but It not solved my problem, because I can receive XML and ZIP files in the same mail, so, I cannot define application/xml or application/zip type in the adapter mail.

I will try the java function.

Regards,

Erica

kaus19d
Active Contributor
0 Kudos