cancel
Showing results for 
Search instead for 
Did you mean: 

Maintaning the same file name as the source and zip the output in PI 7.11

Former Member
0 Kudos

Hello dear friends,

I've a scenario where I have to dynamically maintain the file name (test1.xml, test2.xml,...) and zip the output (test1.zip, test2.zip,...).

I use Adapter-Specific Message Attribute to dynamically determine the file name and I use PayloadZipBean (zip.mode - zipAll) to zip the files.

When I use both of these options in the receiver file communication channel I get a corrupt XML file as output but when I use those options separately it works fine.

I don't want to work with OS commands, is there another solution for this issue?

Your help in this is appreciated,

Thank you and Regards,

Accepted Solutions (0)

Answers (6)

Answers (6)

0 Kudos

Hi Indrajit,

If it is a pass through interface then can we create one dummy mapping just to add java mapping instead of creating custom module?

Former Member
0 Kudos

Hi Mark

Module development steps u will easily get in SCN. So I am providing the the code for the module

Module name : ZipFiles

Code :

package com.sap;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.rmi.RemoteException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;

import com.sap.aii.af.lib.mp.module.Module;
import com.sap.aii.af.lib.mp.module.ModuleContext;
import com.sap.aii.af.lib.mp.module.ModuleData;
import com.sap.aii.af.lib.mp.module.ModuleException;
import com.sap.engine.interfaces.messaging.api.Message;
import com.sap.engine.interfaces.messaging.api.MessageKey;
import com.sap.engine.interfaces.messaging.api.MessagePropertyKey;
import com.sap.engine.interfaces.messaging.api.PublicAPIAccess;
import com.sap.engine.interfaces.messaging.api.PublicAPIAccessFactory;
import com.sap.engine.interfaces.messaging.api.XMLPayload;
import com.sap.engine.interfaces.messaging.api.auditlog.AuditAccess;
import com.sap.engine.interfaces.messaging.api.auditlog.AuditLogStatus;
import com.sap.engine.interfaces.messaging.api.exception.MessagingException;

public class ZipFilesBean implements Module, SessionBean {

private static final long serialVersionUID = 1L;
private AuditAccess Audit = null;
private MessageKey amk = null;
static final int BUFFER = 1024 * 1000;

public ModuleData process(ModuleContext moduleContext,
   ModuleData inputModuleData) throws ModuleException {

  Message mes = null;

  XMLPayload xmlData = null;
  mes = (Message) inputModuleData.getPrincipalData();
  xmlData = mes.getDocument();
  int len = 0;
  byte buf[] = new byte[BUFFER];

  // For Audit Log Fetch the Audit Message key from the message.
  amk = new MessageKey(mes.getMessageId(), mes.getMessageDirection());
  PublicAPIAccess pa;
  try {
   pa = PublicAPIAccessFactory.getPublicAPIAccess();
  } catch (MessagingException me) {
   ModuleException e1 = new ModuleException(me);
   throw e1;
  }
  Audit = pa.getAuditAccess();
  Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,
    "Entering the GetFileName module");

  try {
   InputStream inStream = xmlData.getInputStream();
   BufferedInputStream bis = new BufferedInputStream(inStream);

   ByteArrayOutputStream myBytes = new ByteArrayOutputStream();

   ZipOutputStream zos = new ZipOutputStream(myBytes);

   MessagePropertyKey mpk = new MessagePropertyKey("FileName",
     "http://sap.com/xi/XI/System/File");
   String FileName = mes.getMessageProperty(mpk);
   String outfile = FileName.replaceAll(".csv", ".zip");
   mes.setMessageProperty(mpk, outfile);

   ZipEntry zipEntry = new ZipEntry(FileName);
   zos.putNextEntry(zipEntry);
   while ((len = bis.read(buf)) > 0) {
    zos.write(buf, 0, len);
   }
   zos.closeEntry();
   zos.close();

   byte[] docContent = myBytes.toByteArray();
   if (docContent != null) {
    xmlData.setContent(docContent);
    inputModuleData.setPrincipalData(mes);
   }

  } catch (Exception e) {
   e.printStackTrace();
  }

  return inputModuleData;

}

public void ejbActivate() throws EJBException, RemoteException {

}

public void ejbPassivate() throws EJBException, RemoteException {

}

public void ejbRemove() throws EJBException, RemoteException {

}

public void setSessionContext(SessionContext arg0) throws EJBException,
   RemoteException {

}

public void ejbCreate() throws CreateException {

}

}

Once you deploy the module , use the below config in communication channel

Please make sure that the JNDI name is ZipFiles.

markbernabe
Active Participant
0 Kudos

Thanks a lot Indrajit! Will try it

markbernabe
Active Participant
0 Kudos

Hi Indrajit,

I'm quite overwhelmed with the number of how to guides regarding the creation of adapter module. Is this the correct one for PI 7.1?

http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/c0b39e65-981e-2b10-1c9c-fc3f8e674...

Former Member
0 Kudos

Hi Mark

You can follow that document. Let me know if you need any help on certain steps.

markbernabe
Active Participant
0 Kudos

Hi Indrajit,

First of all, I'd like to say that I greatly appreciate your support. Thanks a lot!

I would like to start off now with the adapter module. Is there another way for me to obtain the library/jar files? I don't have access to our PI directory. Can I download them in service marketplace?

Former Member
0 Kudos

Hi Mark

Yes you can download it from market place. Check the below steps

From service marketplace

You need a user for the SAP service marketplace.

  1. Enter service.sap.com/patches as address  in your browser window.
  2. Click on the left side on: Entry by Application Group
  3. Navigate: Support Packages and Patches -> SAP NetWeaver -> SAP NETWEAVER -> SAP NETWEAVER 7.0/SAP NETWEAVER PI 7.1
    -> Entry by Component -> J2EE Adapter Engine (PI/XI)/Adapter Engine (Java EE)
  4. Expand XI ADAPTER FRAMEWORK CORE 7.00/7.10 -> #OS independent and download the SP level, that fits your PI installation
  5. Extract the downloaded .sca file recursively, until you get the .jar files.

If you are unable to download then provide me your email id , I will send you the jars.

markbernabe
Active Participant
0 Kudos

Hi Indrajit,

Here's my email: <removed by moderator>

Thanks a lot!

===============================================================================

Please comply with the SCN Rules of Engagement and do not propose private communications,

  1. i.e.,

============================================     

Good Community Etiquette and Behavior

   

     4.  Share experise transparently. SCN is a community site that encourages public knowledge sharing.

============================================

Message was edited by: Kenny Scott, SCN XI/PI Moderator

===============================================================================

Message was edited by: Kenny Scott, SCN XI/PI Moderator

markbernabe
Active Participant
0 Kudos

Hi Indrajit,

I've added the SCA into my download basket but it seems that it requires some approval. I have SP6. Can you send it to my mail please? Thanks a lot!

Former Member
0 Kudos

Hi Mark

Sent the jars, pls check.

markbernabe
Active Participant
0 Kudos

This message was moderated.

markbernabe
Active Participant
0 Kudos

Hi Indrajit,

Your mail didn't reach my gmail inbox. Can you send it to my other email also? <removed by moderator>

Thanks a lot!

===============================================================================

Please comply with the SCN Rules of Engagement and do not propose private communications,

  1. i.e.,

============================================     

Good Community Etiquette and Behavior

   

     4.  Share experise transparently. SCN is a community site that encourages public knowledge sharing.

============================================

Message was edited by: Kenny Scott, SCN XI/PI Moderator

===============================================================================

Message was edited by: Kenny Scott, SCN XI/PI Moderator

Former Member
0 Kudos

Hi Mark

I will send you the jars within 2 hours. I will let u know once I send.

markbernabe
Active Participant
0 Kudos

Hi Indrajit,

Got it now thanks! I've followed the steps and upon deployment, I got this error

Is it because the username/password I entered is not authorized?

Former Member
0 Kudos

Hi Mark

Yes looks like the user is not having enough autorization for deployment.

Check with your basis team and see if u can find something.

0 Kudos

Hi Indrajit,

Please confirm the channel config.Should i select ASMA in both the channels and keep * in file names for both the channels.

Former Member
0 Kudos

Hi Mark

For pass thru interfaces, we normally do not create any ESR objects. So we create only Integration directory objects and hence there will be no mapping where we can do this.

Only option left is to write an adapter module.Please create an adapter module and use the piece of code that I have provided by making little bit change.

Let me know if you need any help on the module code.

markbernabe
Active Participant
0 Kudos

Hi Indrajit,

Thanks for the info. Can you guide me on how to implement module codes as well? I haven't done that before. Appreciate it.

Former Member
0 Kudos

Hi 

Please create a java mapping and use the below code

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.*;
import java.util.zip.*;
import com.sap.aii.mapping.api.AbstractTransformation;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;
import com.sap.aii.mapping.api.StreamTransformationException;
import com.sap.aii.mapping.api.DynamicConfiguration;
import com.sap.aii.mapping.api.StreamTransformationConstants;
import com.sap.aii.mapping.api.DynamicConfigurationKey;


public class ZipFilesUsingJava extends AbstractTransformation {
static final int BUFFER = 1024*1000;

    public void transform(TransformationInput arg0, TransformationOutput arg1)
    throws StreamTransformationException {
 
    try
          {
               int len = 0;
               byte buf[] = new byte[BUFFER];
              
               DynamicConfiguration conf = arg0.getDynamicConfiguration();

               DynamicConfigurationKey dynKey = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File","FileName");
               String keyval = conf.get(dynKey);
              
               String outfile = keyval.replaceAll(".xml",".zip");
              
               conf.put(dynKey,outfile);
               BufferedInputStream bis = new BufferedInputStream(arg0.getInputPayload().getInputStream());
               ZipInputStream zis = new ZipInputStream(bis);
               ZipOutputStream zos = new ZipOutputStream(arg1.getOutputPayload().getOutputStream());
              
               ZipEntry zipEntry = new ZipEntry(keyval);
               zos.putNextEntry(zipEntry);
               while ((len = bis.read(buf)) > 0)
               {
               zos.write(buf, 0, len);
               }
               zos.closeEntry();
               zos.close();
              
        
          }
          catch(Exception e)
          {
               e.printStackTrace();
          }
 
}//end of transform         
               

}

This will provide you the desired output.

markbernabe
Active Participant
0 Kudos

Thanks Indrajit. Sorry but I haven't done Java mapping before. Can you guide me on how to implement this? Does this work like an adapter module?

Former Member
0 Kudos

Hi Mark

Please do the follwing steps

1. Create a java class using any editor, eclipse or NWDS or Jcreator. Compile the class and then make a zip or jar file.

2. Import that zip or jar file in the imported acrchive portion in ESR.

3. Create the ESR objects, data type , message type, oytbound and inbound service interface.

4. Create one operation mapping between outbound and inbound interface. Use java class as the mapping type and choose the class you have created.

5. Create the ID objects, receiver determination, interface determination, sender and receiver agreemeent or you can create an ICO.

Thats it you are good to go. Java mapping is more easy rather than adapter module. Adapter module you can not test locally where as java mapping you can. Also every time you need to deploy the module in server for any changes.

http://wiki.scn.sap.com/wiki/display/XI/Beginners+guide+to+Java+mapping+using+DOM+parser+in+SAP+XI

http://techplay.plozzle.com/?p=21

markbernabe
Active Participant
0 Kudos

Thanks a lot! I'll try it out

markbernabe
Active Participant
0 Kudos

Hi Indrajit,

One quick question. I have to do this in a file-file pass thru interface that involves CSV files only. I tried to implement it using dummy structures (message type)  but I encountered this error during actual execution: 'Unable to parse XML message payload to extract operation for receiver determinationorg.xml.sax.SAXParseException: Content is not allowed in prolog'. Any idea?

Thanks in advance!

0 Kudos

Hi Indrajit,

After completing the ESR part.For the ID part what config we need to do in comm channels.Should i use ASMA in both the channels and keep * in FileNames ?

markbernabe
Active Participant
0 Kudos

Hi,

We're you able to solve this problem? I have the same requirement.

Former Member
0 Kudos

Friends,

Is there someone who can provide me with some kind of clue or information about this issue?

Thank you,

stefan_grube
Active Contributor
0 Kudos

There is no standard way to do this. You cannot have a dynamic file name neither for the zipped files not for the target file unless you do some Java code by yourself.

See this blog for more details:

/people/stefan.grube/blog/2007/02/20/working-with-the-payloadzipbean-module-of-the-xi-adapter-framework

Former Member
0 Kudos

Thank you Stefan for your reply,

In that case we will use OS command line to realize this, as described in this blog:

/people/michal.krawczyk2/blog/2007/02/08/xipi-command-line-sample-functions

Regards,

Kamran

Edited by: K. Vakili on May 18, 2011 12:38 PM

Former Member
0 Kudos

Hi Stefan,

I've a question...

I've developed the adapter module where we can set the file name dynamically according to this code:

http://wiki.sdn.sap.com/wiki/display/XI/Adapter%20Module%20PI%207.0%20Set%20Attachment%20Name?bc=tru...

It all works fine and I get the file name dynamically. The only thing is that the output file is a .XML file and when I open it, it says its a corrupt file but when I change the extension to .ZIP I get the correct ZIP file with the dynamic file name (also for the content)...

So the only thing is that I get a wrong extension (XML instead of ZIP). Is it possible to add a line to the code and just change the extension of the file... Something like (see the bold line):

Message msg = (Message) inputModuleData.getPrincipalData();

Payload payload = msg.getDocument();

String fileName = msg.getMessageProperty("http://sap.com/xi/XI/System/File",

"FileName");

if(fileName == null) fileName="default.txt";

fileName.replace = ("XML" ; "ZIP")

payload.setContentType("text/plain;charset = \"UTF-8\";"

+ "filename=\"" + fileName + "\"");

inputModuleData.setPrincipalData(msg);

} catch (Exception e) {

throw new ModuleException(e);

}

return inputModuleData;

}

}

Or is there another way to do this?

Please help me with this issue... Its very strange that everything works fine except I get a wrong extension.

Thank you in advance,

Kind regards,

Kamran

Edited by: K. Vakili on Jun 27, 2011 7:40 PM

baskar_gopalakrishnan2
Active Contributor
0 Kudos

>fileName.replace = ("XML" ; "ZIP")

The above logic will work for replacing a char not a string. If you want to change the filename, you need to use java file package and use replaceTo method to change the file name to different name.

Example : Refer this [link|http://www.javadb.com/how-to-rename-a-file-change-file-name]

Former Member
0 Kudos

Hi Baskar,

I imported java file package and wanted to use replaceTo method but I can't find it. I used:

fileName.replaceTo (".xml", ".zip") and I get an error... Is that what you mean?

Thank you and Regards,