cancel
Showing results for 
Search instead for 
Did you mean: 

Simple java mapping to create zip files

ajeyagv
Participant
0 Kudos

Hello All,

I have a little complex scenario. The sender sends an IDoc and it is converted into a text file. This text file is supposed to have a specified filename along with the timestamp (the file name is to be of the form VT_KKO_*timestamp*.txt. After this file has been created, it has to be zipped and sent to SFTP file server. This zip file also is to have the same filename only the extension should be zip instead of txt.

So, I decided to go ahead with first dynamically naming the file with a udf and then use java mapping to zip this file.

Can anyone provide a simple java code to just take the inbound payload and zip it and pass it along to the receiver adapter. No other operation is to be conducted. All the sample programs I found are doing some additional work like, taking mulitple payloads etc. which is not needed for my requirement.

Kindly help me with this. Also if you have any better idea or solution then, please suggest that as well.

Thanks,

Ajeya G V

Accepted Solutions (1)

Accepted Solutions (1)

AlexanderApel
Participant
0 Kudos
former_member186851
Active Contributor
0 Kudos

Hello Ajeya,

Additionally check if Payload Beans can you help out

SAPTechnical.COM - Zipping Files/Payloads using Module PayloadZipBean

ajeyagv
Participant
0 Kudos

Hello Alexander,

Thanks for the reply. The code seems to be very simple. Just for my understand please tell me, what value should be passed for the parameter filename ? The name of the file that PI has to pick up or will PI use that string to name the file that is going to be inside the zip file ?

AlexanderApel
Participant

Answers (1)

Answers (1)

former_member182412
Active Contributor
0 Kudos

Hi Vaani,

You can set the dynamic FileName attribute in the mapping using dynamic configuration UDF. In the receiver file adapter module configuration like below.

The adapter module DynamicContentTypeBean code:


import javax.ejb.CreateException;

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.PublicAPIAccessFactory;

import com.sap.engine.interfaces.messaging.api.auditlog.AuditAccess;

import com.sap.engine.interfaces.messaging.api.auditlog.AuditLogStatus;

public class DynamicContentTypeBean implements SessionBean, Module {

  private static final long serialVersionUID = 1L;

  private SessionContext myContext;

  private AuditAccess audit;

  @Override

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

  String CLASS_NAME = getClass().getSimpleName();

  try {

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

  MessageKey key = new MessageKey(msg.getMessageId(), msg.getMessageDirection());

  audit = PublicAPIAccessFactory.getPublicAPIAccess().getAuditAccess();

  audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS, CLASS_NAME + ": Module Called");

  MessagePropertyKey KEY_FILENAME = new MessagePropertyKey("FileName", "http://sap.com/xi/XI/System/File");

  String fileName = msg.getMessageProperty(KEY_FILENAME);

  audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS, "FileName read successfully. fileName: " + fileName);

  fileName = fileName.replace(".zip", ".txt");

  String contentType = "text/plain;charset=\"utf-8\";name=\"" + fileName + "\"";

  msg.getMainPayload().setContentType(contentType);

  audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS, "ContentType was set to: " + contentType);

  } catch (Exception e) {

  throw new ModuleException(e.getClass() + ": " + e.getMessage());

  }

  return inputModuleData;

  }

  @Override

  public void ejbRemove() {

  }

  @Override

  public void ejbActivate() {

  }

  @Override

  public void ejbPassivate() {

  }

  @Override

  public void setSessionContext(SessionContext context) {

  setMyContext(context);

  }

  public void setMyContext(SessionContext myContext) {

  this.myContext = myContext;

  }

  public SessionContext getMyContext() {

  return myContext;

  }

  public void ejbCreate() throws CreateException {

  }

}

The created zip file:

Regards,

Praveen.