Post date: Jul 6, 2015 9:05:24 AM
package com.xyz.adaptermodule;
//Classes for EJB
import javax.ejb.CreateException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
// Classes for Module development & Trace
import com.sap.aii.af.lib.mp.module.*;
import com.sap.engine.interfaces.messaging.api.*;
import com.sap.engine.interfaces.messaging.api.auditlog.*;
//extra imports
import java.util.Date;
import java.util.Enumeration;
public class FTPSArchiveBean implements SessionBean, Module {
private SessionContext myContext;
public void ejbRemove() {}
public void ejbActivate() {}
public void ejbPassivate() {}
public void setSessionContext(SessionContext context) { myContext = context;}
public void ejbCreate() throws CreateException {}
public ModuleData process(ModuleContext moduleContext,ModuleData inputModuleData)throws ModuleException {
AuditAccess audit = null;
Object obj = null;
Message msg = null;
MessageKey key = null;
try {
msg = (Message) inputModuleData.getPrincipalData();
key = new MessageKey(msg.getMessageId(), msg.getMessageDirection());
audit = PublicAPIAccessFactory.getPublicAPIAccess().getAuditAccess();
audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS, "FTPSArchive: Module called");
MessagePropertyKey FileKey = new MessagePropertyKey("FileName", "http://sap.com/xi/XI/System/File");
MessagePropertyKey DirectoryKey = new MessagePropertyKey("Directory", "http://sap.com/xi/XI/System/File");
String SourceFileName = ""; String ArchiveFileName = ""; String status = "";
String Mode = (String) moduleContext.getContextData("Mode");
RenameFile rnf = new RenameFile();
//**********************
String addTimeStamp = "true";
Enumeration < String > paramList = moduleContext.getContextDataKeys();
while (paramList.hasMoreElements()) {
if (paramList.nextElement().equals("addTimeStamp")) {
addTimeStamp = (String) moduleContext.getContextData("addTimeStamp"); break;
}
}
if (addTimeStamp.equals("true"))
addTimeStamp = new java.text.SimpleDateFormat("_ddMMyyHHmmSS").format(new Date());
else addTimeStamp = "";
//**********************
if (Mode.equals("FTP")) {
String Host = (String) moduleContext.getContextData("Host");
int Port = Integer.parseInt(moduleContext.getContextData("Port"));
String User = (String) moduleContext.getContextData("User");
String Pwd = (String) moduleContext.getContextData("pwd");
SourceFileName = msg.getMessageProperty(DirectoryKey) + msg.getMessageProperty(FileKey);
ArchiveFileName = SourceFileName.replace((String) moduleContext.getContextData("ArchiveFrom"), (String) moduleContext.getContextData("ArchiveTo"));
status = rnf.onFTP(Host, Port, User, Pwd, SourceFileName, ArchiveFileName, addTimeStamp);
}
if (Mode.equals("NFS")) {
SourceFileName = msg.getMessageProperty(DirectoryKey) + "/" + msg.getMessageProperty(FileKey);
ArchiveFileName = SourceFileName.replace((String) moduleContext.getContextData("ArchiveFrom"), (String) moduleContext.getContextData("ArchiveTo"));
status = rnf.onNFS(SourceFileName, ArchiveFileName, addTimeStamp);
}
String DynTargetFolder = msg.getMessageProperty(DirectoryKey).replaceFirst((String) moduleContext.getContextData("TargetFrom"), (String) moduleContext.getContextData("TargetTo"));
if (Mode.equals("NONE")) {
String KeyValue = msg.getMessageProperty(FileKey).split("_")[4].replaceAll("0", "").substring(0, 2);
DynTargetFolder = DynTargetFolder.replaceFirst((String) moduleContext.getContextData("Key"), KeyValue);
audit.addAuditLogEntry(key,AuditLogStatus.SUCCESS,"FTPSArchive: Mode-NONE-TargetFolder:" + DynTargetFolder);
}
audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS, "FTPSArchive: Source File Name :" + SourceFileName);
if (!status.contains("failed")) audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS, "FTPSArchive: " + status);
else audit.addAuditLogEntry(key, AuditLogStatus.ERROR, "FTPSArchive: " + status);
msg.setMessageProperty(DirectoryKey, DynTargetFolder);
} catch (Exception e) {
audit.addAuditLogEntry(key, AuditLogStatus.ERROR, "FTPSArchive: Cannot read the module context or config data");
ModuleException me = new ModuleException(e);
throw me;
}
return inputModuleData;
}
}
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
package com.xyz.adaptermodule;
import java.io.File;
import java.io.IOException;
import java.net.SocketException;
import java.util.Date;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPSClient;
public class RenameFile {
FTPSClient ftpClient = null;
String status = "";
public String onFTP(String host, int port, String username,
String password, String ArchiveFrom, String ArchiveTo, String addTimeStamp) {
try {
ftpClient = new FTPSClient("TLS", false);
ftpClient.connect(host, port);
ftpClient.login(username, password);
ftpClient.enterLocalPassiveMode();
if (ftpClient.getReplyCode() == 230) {
ftpClient.sendCommand("OPTS UTF8 ON");
ftpClient.execPBSZ(0);
ftpClient.execPROT("P");
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.setUseEPSVwithIPv4(true);
} else {
status = "FTP login failed";
return status;
}
String ArchiveFileName = ArchiveTo.split("/")[ArchiveTo.split("/").length - 1];
String ArchiveDirectory = ArchiveTo.replace(ArchiveFileName, "");
if (!ftpClient.changeWorkingDirectory(ArchiveDirectory)) {
if (makeDirectories(ArchiveDirectory)) status = status + "Created Archive folder " + ArchiveDirectory;
else {
status = status + "Archive folder creation failed" + ArchiveDirectory;
ftpClient.logout();
return status;
}
}
String extn = "";
if (ArchiveTo.contains(".")) extn = "." + ArchiveTo.split("\\.")[ArchiveTo.split("\\.").length - 1];
ArchiveTo = ArchiveTo.replace(extn, "") + addTimeStamp + extn;
if (ftpClient.rename(ArchiveFrom, ArchiveTo)) status = status + " Archived Successfully as " + ArchiveTo;
else status = status + " Archive failed!";
ftpClient.logout();
} catch (SocketException e) { e.printStackTrace(); }
catch (IOException e) { e.printStackTrace(); }
return status;
}
boolean makeDirectories(String dirPath) throws IOException {
String[] pathElements = dirPath.split("/");
if (pathElements != null && pathElements.length > 0) {
for (String singleDir: pathElements) {
if (!ftpClient.changeWorkingDirectory(singleDir)) {
if (ftpClient.makeDirectory(singleDir)) ftpClient.changeWorkingDirectory(singleDir);
else return false;
}
}
}
return true;
}
public String onNFS(String ArchiveFrom, String ArchiveTo, String addTimeStamp) {
String status = "";
String ArchiveFileName = ArchiveTo.split("/")[ArchiveTo.split("/").length - 1];
String ArchiveDirectory = ArchiveTo.replace(ArchiveFileName, "");
File f_ArchiveDirectory = new File(ArchiveDirectory);
if (!f_ArchiveDirectory.exists())
if (f_ArchiveDirectory.mkdirs()) status = "CreatedArchiveFolder " + ArchiveDirectory;
else {
status = "Archive folder creation failed";
return status;
}
String extn = "";
if (ArchiveTo.contains(".")) extn = "." + ArchiveTo.split("\\.")[ArchiveTo.split("\\.").length - 1];
ArchiveTo = ArchiveTo.replace(extn, "") + addTimeStamp + extn;
File f_ArchiveFrom = new File(ArchiveFrom);
File f_ArchiveTo = new File(ArchiveTo);
if (f_ArchiveFrom.renameTo(f_ArchiveTo)) status = status + " Archived Successfully as " + ArchiveTo;
else status = status + " Archive failed!";
return status;
}
}