on 05-16-2011 12:10 PM
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,
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
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?
Hi Mark
Yes you can download it from market place. Check the below steps
You need a user for the SAP service marketplace.
If you are unable to download then provide me your email id , I will send you the jars.
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,
============================================
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
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,
============================================
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
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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!
Hi,
We're you able to solve this problem? I have the same requirement.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Friends,
Is there someone who can provide me with some kind of clue or information about this issue?
Thank you,
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
Hi Stefan,
I've a question...
I've developed the adapter module where we can set the file name dynamically according to this code:
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
>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]
User | Count |
---|---|
76 | |
9 | |
8 | |
7 | |
6 | |
5 | |
5 | |
5 | |
5 | |
5 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.