Zip File Using PayloadZipBean Preserving Original File Name Inside The Zip
Introduction
The requirement is create a zip file in the target server but we need to create same file name in inside the zip file we are creating. File adapter we can only set the name of the zip file name not the inside file in Zip file, Using message transform bean we can set the static file name but not the dynamic file name, we need to write adapter module to set the file name dynamically.
I want to show how we can implement this in PI/PRO.
Design
Create the sender data type as below.
Create receiver data type like below.
Create two message types using above data types and create below two service interfaces.
Create message mapping for above sender message type to receiver message type.
fileName variable mapping:
I used reusable dynamic configuration UDF’s as shown in below blog.
Reuse FunctionLibrary for DynamicConfiguration and Message Header Attributes
Create operation mapping for above message mapping.
Configuration
Create the below IFLOW in NWDS.
Sender channel configuration:
Enable File Name attribute in adapter specific message attributes.
Receiver channel configuration:
Enable File Name attribute in adapter specific message attributes.
Maintain below modules in the receiver file channel.
I have used custom adapter module to change the content type.
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;
@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());
AuditAccess audit = PublicAPIAccessFactory.getPublicAPIAccess().getAuditAccess();
audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS, CLASS_NAME + ": Module Called");
String source = moduleContext.getContextData("source");
if (source == null) {
throw new ModuleException("Parameter source is missing");
}
audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS, "Read parameter source: " + source);
String target = moduleContext.getContextData("target");
if (target == null) {
throw new ModuleException("Parameter target is missing");
}
audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS, "Read parameter newString: " + target);
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(source, target);
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 {
}
}
Testing
I placed the below XML file in the source directory.
The content type was changed as we expected and Zip file was created in target directory.
Target ZIP file was created in the target folder.
Zip file contains the same file name inside.
The content of the file inside the zip file.
Conclusion
Using above custom adapter module we can create a zip file and create the same file name inside the zip file. I hope this helps to the community.
Hi Praveen,
I have a scenario where the sender will send zip file. The zip file contains many files(.txt,.csv). In target location I have to unzip the file and place the files inside that zip file. By seeing so many sites I came to know that I can achieve this by using java mapping or OS Coomands. As I am not from java background but I tried everything. If you have any Java Module for this scenario kindly share with me.
Thanks & Regards,
Lakshmi Mounika Manne
I used this module but problem is that code also changing the attachment name like untitled.xml ?