cancel
Showing results for 
Search instead for 
Did you mean: 

Unzip attachment Sender Mail Adapter PI 7.4

Former Member
0 Kudos

Hi All,

I'm trying unzip a file from mail adapter sender. I tried to set the Adapter Module as below, but it didn't work.

I'm in SAP PI 7.4 version single stack.

Could you help me?

Regards,

Erica

Accepted Solutions (1)

Accepted Solutions (1)

manoj_khavatkopp
Active Contributor
0 Kudos

Erica,

Firstly you need to add PaylaodSwapBean before then PaylodZip bean , so that the attachment and the mainpayload will be swapped.

Your current configuration is trying to unzip the mail body but not an attachment.

br,

Manoj

Former Member
0 Kudos

Are you saying it? Because it is not working too.

Thanks,

Erica

former_member182412
Active Contributor
0 Kudos

Hi Erica,

The parameter name is payload-name not Payload-Name.

Regards,

Praveen.

Former Member
0 Kudos

I tried as payload-name, but it is not working too.

Regards,

Erica

manoj_khavatkopp
Active Contributor
0 Kudos

Can you try with this parameters :

Swap.keyName = Content-Description.

Swap.Value = MailAttachment-1

once you have added this please provide the screenshot of the sender channel log.

Bdw what type of file do you have inside the zipfile ?

br,

Manoj

former_member182412
Active Contributor
0 Kudos

Hi Erika,

You only have one payload which is zip file, you don't need to use PayloadSwapBean to swap the payload. If you have more than one payload then you need to use. Try without payload swap bean.

Regards,

Praveen.

manoj_khavatkopp
Active Contributor
0 Kudos

Adding to Praveen's reply, you dont need CallSap adapter Module in the sender mail channel too.

Former Member
0 Kudos

The interface can receive XML files or ZIP files with this xml files zipped. So it iw why I'm using PayloadSwapBean.

Regards,

Erica

Former Member
0 Kudos

I tried but I received an error in communication channel. The content of zip is XML. I can receive this XML zipped or not.

former_member182412
Active Contributor
0 Kudos

Hi Erika,

What is your PI version, if your PI version support then use the configuration mentioned in the below note. You can process all the attachments as separate PI messages.

2040884 - Processing Mail attachments as PI messages

Regards,

Praveen.

manoj_khavatkopp
Active Contributor
0 Kudos

As per your screenshot from SXMB_MONI we see only one payload i.e Attachment so as already praveen Suggested you can remove the payloadswap bean . Give it a try and also provide the screenshot of the channel monitoring log which will be helpful to analyze if module execution is happening or not.

Praveen Gandepalli   : How can a mail have just an attachment atleast it should have an empty body right  ?i mean  to say how is it possible to have just one payload

Former Member
0 Kudos

Manoj,

I can receive XMLs files or XMLs in a ZIP file. However, I did the test you asked and it not worked too. The communication channel don't have error, but the unzip not worked.

manoj_khavatkopp
Active Contributor
0 Kudos

Your latest Screenshot shows you have 2 attachment , so any standard module Paylaodswap bean/Zip bean can work on one paylaod at a time , so these modules wont help in your case.

So :

  • Check the SAP Note which Praveen has suggested .
  • You may try this custom module

Br,

Manoj

Former Member
0 Kudos

HI Manoj,

Thanks for you help.

I tried to split the message as you told but anyway is working, as the sap note I don't see any difference and with the Splitter i got an error.

As the Sap note:

As the custom module

Regards,

Erica

manoj_khavatkopp
Active Contributor
0 Kudos

Erica,

Regarding the Custom module  you have to download the EAR file mentioned in the blog and then deploy it in your PI system after that only you can use this module.

Search in SCN you will get numerous blogs on how to deploy EAR file.

But wait lets see if any suggestions by any experts beacuse after you add custom module you have next query to unzip only the necessary files not all i.e XML this is but tricky

Br,

Manoj

Former Member
0 Kudos

But only one question, it will split the attachment in n messages, but not will unzip the attachment , correct?

What is the work around to unzip?

Regards.

Erica

former_member182412
Active Contributor
0 Kudos

Hi Erica,

  • What is your PI version, is the note is applicable to your PI version??
  • If yes then each attachment the new PI message will be created
  • So you can use PayloadZipBean in the receiver channel to unzip
  • Or if you want unzip and need to do the mapping then you can do it in java mapping
  • What is your full requirement??

Regards,

Praveen.

Former Member
0 Kudos

What is your PI version, is the note is applicable to your PI version??

I am in SAP PI 7.4, yes the note is applicable

If yes then each attachment the new PI message will be created

As the image this doesn't happened.

So you can use PayloadZipBean in the receiver channel to unzip

Or if you want unzip and need to do the mapping then you can do it in java mapping

What is your full requirement??

I need to receive a mail with multilple attachments between XMLs and XMLs in a zip file, if we receive the files in a ZIP , I need to extract and identify the XML files.

I already have a java mapping. This java is responsible to identiify some strings in the XMLs, so depend on the string (for example, "nfeProc"), then it is sent to ECC to record this XMLs files in the respectives folders. I tested the interface withou zip files and it is working. But, unfortunately, I don't know JAVA, I got this from an other project, but I believe I should change this java mapping code  to do the unzip and continue the java code as is working. What do you think? Can you give some help how I can start the java code do unzip the file? I found some code in scn but I don't know it should be applicable to this function. I requested a JAVA resource in the company to help me, but he doesn't know PI, so he is trying to deal with it, but we don't have any news yet.

My actual java function:

package com.b2b.inbound.att;

import java.io.BufferedReader;

import java.io.ByteArrayInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.io.UnsupportedEncodingException;

import java.util.Collection;

import java.util.Iterator;

import java.util.Map;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;

//import javax.xml.soap.Node;

import javax.xml.transform.Transformer;

import javax.xml.transform.TransformerConfigurationException;

import javax.xml.transform.TransformerException;

import javax.xml.transform.TransformerFactory;

import javax.xml.transform.dom.DOMSource;

import javax.xml.transform.stream.StreamResult;

//import org.omg.CORBA.DynamicImplementation;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

//import org.w3c.dom.NodeList;

import org.w3c.dom.Text;

import org.xml.sax.SAXException;

import com.sap.aii.mapping.api.AbstractTransformation;

import com.sap.aii.mapping.api.Attachment;

import com.sap.aii.mapping.api.DynamicConfiguration;

import com.sap.aii.mapping.api.DynamicConfigurationKey;

import com.sap.aii.mapping.api.InputAttachments;

import com.sap.aii.mapping.api.StreamTransformationException;

import com.sap.aii.mapping.api.TransformationInput;

import com.sap.aii.mapping.api.TransformationOutput;

//import com.sap.aii.mapping.api.OutputAttachments;

//import com.sap.aii.mapping.api.DynamicConfiguration;

import com.sap.aii.mapping.api.StreamTransformationConstants;

public class GetAttachments extends AbstractTransformation{

  InputStream in = null;

  OutputStream out = null;

  String NFe = "";

  //Object nfe = "";

  public void transform(TransformationInput input, TransformationOutput output)

  throws StreamTransformationException {

  // TODO Auto-generated method stub

  try{

  in = input.getInputPayload().getInputStream();

  out = output.getOutputPayload().getOutputStream();

  InputAttachments ipAtt = input.getInputAttachments();

  if (ipAtt.areAttachmentsAvailable()){

  Collection<String> colAtt = ipAtt.getAllContentIds(true);

  Iterator<String> itAtt = colAtt.iterator();

  String attachId = "";

  Attachment attach = null;

  //monta XML de saida

  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

  DocumentBuilder db = dbf.newDocumentBuilder();

  TransformerFactory tf = TransformerFactory.newInstance();

  Transformer transform = tf.newTransformer();

  //Cria escopo do XML

  Document docout = db.newDocument();

  Element root = docout.createElement("ns0:msgType_MultiAttachments");

  root.setAttribute("xmlns:ns0", "http://genmills.com/xi/Mail/Inbound");

  docout.appendChild(root);

  while(itAtt.hasNext()){

  attachId = itAtt.next();

  attach= ipAtt.getAttachment(attachId);

  String attType = "";

     attType = attach.getContentType();

     if (attType.contains("xml")){   

    

  NFe = this.getUTF8(attach.getContent());

  String accessKey = "";

  accessKey = NFe.substring(NFe.indexOf("<chNFe>")+7,NFe.indexOf("<chNFe>")+51);

  //OutputAttachments outAtt = output.getOutputAttachments();

  //Cria elemento Atachments

  Element attachments = docout.createElement("Attachments");

  root.appendChild(attachments);

  if (NFe.contains("nfeProc")){

  //Cria elemento Attachment Name

  Element attachmentName = docout.createElement("Attachments_Name");

  attachments.appendChild(attachmentName);

  Text attachmentNameText = docout.createTextNode("NFe_" +accessKey+ ".xml"); //Cria valor campo

  attachmentName.appendChild(attachmentNameText);

  }

  if (NFe.contains("procEventoNFe")){

  //Cria elemento Attachment Name

  Element attachmentName = docout.createElement("Attachments_Name");

  attachments.appendChild(attachmentName);

  Text attachmentNameText = docout.createTextNode("Event_" +accessKey+ ".xml"); //Cria valor campo

  attachmentName.appendChild(attachmentNameText);

  }

  if (NFe.contains("cteProc")){

  accessKey = NFe.substring(NFe.indexOf("<chCTe>")+7,NFe.indexOf("<chCTe>")+51);

  //Cria elemento Attachment Name

  Element attachmentName = docout.createElement("Attachments_Name");

  attachments.appendChild(attachmentName);

  Text attachmentNameText = docout.createTextNode("CTe_" +accessKey+ ".xml"); //Cria valor campo

  attachmentName.appendChild(attachmentNameText);

  }

  if (NFe.contains("eventoCTe")){

  accessKey = NFe.substring(NFe.indexOf("<chCTe>")+7,NFe.indexOf("<chCTe>")+51);

  //Cria elemento Attachment Name

  Element attachmentName = docout.createElement("Attachments_Name");

  attachments.appendChild(attachmentName);

  Text attachmentNameText = docout.createTextNode("EventCTe_" +accessKey+ ".xml"); //Cria valor campo

  attachmentName.appendChild(attachmentNameText);

  }

  //Cria elemento Attachment Content

  Element attachmentContent = docout.createElement("Attachments_Content");

  attachments.appendChild(attachmentContent);

  //Text attachmentContentText = docout.createTextNode(attType); //Cria valor campo

  Text attachmentContentText = docout.createTextNode(NFe); //Cria valor campo

  attachmentContent.appendChild(attachmentContentText);

     }

  }

  DOMSource domS = new DOMSource(docout);

  transform.transform((domS), new StreamResult(out));

  itAtt = null;

  colAtt = null;

  }

  //Tranforma mensagem de entrada em String

  String inptxml = this.stream2string(in);

  String fileName = "";

  Document docIn = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(inptxml.getBytes("utf-8")));

  org.w3c.dom.Node from = docIn.getElementsByTagName("From").item(0).getFirstChild();

  fileName = from.toString();

  //Dynamic Configuration para setar o nome do arquivo

  Map mapParameters = (Map) input.getInputHeader().getAll();

  mapParameters.put(DynamicConfigurationKey.create("http://sap.com/xi/XI/Dynamic", StreamTransformationConstants.DYNAMIC_CONFIGURATION), "");

  DynamicConfiguration conf = (DynamicConfiguration) mapParameters.get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);

  DynamicConfigurationKey key = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File", "FileName");

  conf.put(key, (fileName + ".xml"));

  } catch (ParserConfigurationException e) {

  // TODo Auto-generated catch block

  e.printStackTrace();

  } catch (TransformerConfigurationException e) {

  // TODo Auto-generated catch block

  e.printStackTrace();

  } catch (TransformerException e) {

  // TODo Auto-generated catch block

  e.printStackTrace();

  } catch (UnsupportedEncodingException e) {

  // TODo Auto-generated catch block

  e.printStackTrace();

  } catch (SAXException e) {

  // TODo Auto-generated catch block

  e.printStackTrace();

  } catch (IOException e) {

  // TODo Auto-generated catch block

  e.printStackTrace();

  }

  }

  private String stream2string (InputStream _in){

  BufferedReader inpxml = new BufferedReader(new InputStreamReader(in));

  StringBuffer buffer = new StringBuffer();

  String line = "";

  try {

  while ((line = inpxml.readLine()) != null) {

  buffer.append(line);

  }

  } catch (IOException e) {

  e.printStackTrace();

  }

  String inptxml = buffer.toString();

  return inptxml;

  }

  public String byte2String(byte[] _bytes)

  {

     String file_string = "";

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

     {

         file_string += (char)_bytes[i];

     }

     return file_string;   

  }

  private String getUTF8(byte[] Content){

  String ret;

  try {

  ret = new String(Content, "UTF-8");

  } catch (UnsupportedEncodingException e) {

  ret = "";

  }

  return ret;

  }

}

Regards

Erica

Answers (1)

Answers (1)

0 Kudos

Erica,

I used the following unzip module to extract the ZIP file. I used this in receiver communication channel. Please try this.

Former Member
0 Kudos

Thanks, Unfortunately , I cannot use it in the receiver, As you can see I have to pass by a java mapping to identify same strings, I already tried it in the sender , but it doesn't work. The zip file not is extracted.

Erica