Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member

Many a times we have a requirement where the client wants that any file which is send to external system should be archived on local file system/ FTP folder.

To take a backup of target file we have few alternatives available like we can opt a conventional way by adding one more receiver and assign one file receiver channel to it and then simultaneously send the message to third party as well as to the client's local system (let it be NFS or FTP), OS commands can also be one of the alternative or we can write a adapter module to accomplish this requirement.

Recently I also came across the same situation, where SAP system is generating a message and after doing few transformations in PI I need to send transformed XML file message to external system and take a backup of the same target file on local FTP server.

Firstly I thought of adding one more "Business Component" (re-use the same mapping) and file receiver channel pointing to Local FTP folder in the existing scenario, but as expected client starts asking the same solution for multiple file interfaces :smile: . So to have a reusable kind of solution I created one generic adapter module which will create a backup of target message on local server and then send the same to external application.

So, the objective of this blog is to show how the target file message can be send to two different locations using one file receiver adapter.

Below module code has been defined in such a way that depending upon the parameters passed from channel it can back up a target message either on FTP or NFS server

package com.poc.sdn;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.rmi.RemoteException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.TimedObject;
import javax.ejb.Timer;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;
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.aii.af.service.auditlog.Audit;
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.XMLPayload;
import com.sap.engine.interfaces.messaging.api.auditlog.AuditLogStatus;
/*\*
\* @author amitsrivastava5
\*
*/
public class BackUpFilesBean implements SessionBean, TimedObject {
/\* (non-Javadoc)
\* @see javax.ejb.SessionBean#ejbActivate()
*/
@Override
public void ejbActivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub
}
/\* (non-Javadoc)
\* @see javax.ejb.SessionBean#ejbPassivate()
*/
@Override
public void ejbPassivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub
}
/\* (non-Javadoc)
\* @see javax.ejb.SessionBean#ejbRemove()
*/
@Override
public void ejbRemove() throws EJBException, RemoteException {
// TODO Auto-generated method stub
}
/\* (non-Javadoc)
\* @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
*/
@Override
public void setSessionContext(SessionContext arg0) throws EJBException,
RemoteException {
// TODO Auto-generated method stub
}
/\* (non-Javadoc)
\* @see javax.ejb.TimedObject#ejbTimeout(javax.ejb.Timer)
*/
@Override
public void ejbTimeout(Timer arg0) {
// TODO Auto-generated method stub
}
public void ejbCreate() throws javax.ejb.CreateException {
}
public ModuleData process(ModuleContext mc, ModuleData inputModuleData)
throws ModuleException {
Object obj = null;
Message msg = null;
MessageKey amk = null;
//Reading Type of Transport Protocol
String ServerType = (String) mc.getContextData("Server");
try { 
// Retrieves the current principle data, usually the message , Return type is Object
obj = inputModuleData.getPrincipalData();
// A Message is what an application sends or receives when interacting with the Messaging System.
msg = (Message) obj;
// MessageKey consists of a message Id string and the MessageDirection
amk = new MessageKey(msg.getMessageId(),msg.getMessageDirection());
//Reading file name from message header
MessagePropertyKey mpk = new MessagePropertyKey("FileName","http://sap.com/xi/XI/System/File");
String filename = msg.getMessageProperty(mpk);
Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, "filename is" \+filename );
// Returns the main document as XMLPayload.
XMLPayload xpld = msg.getDocument();
byte\[\] inpbyt = xpld.getContent();
Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, "Input file read successfully");
//Archiving target file on FTP server
if (ServerType.equals("FTP"))
{
String HostName = (String) mc.getContextData("HostName");
String FTPDirectory = (String) mc.getContextData("FTPDirectory");
String Username = (String) mc.getContextData("Username");
String Password = (String) mc.getContextData("pwd");
Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, "Connecting to FTP location");
FtpClient client = new FtpClient();
client.openServer(HostName);
client.login (Username,Password);
client.cd(FTPDirectory);
Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, "Connection to FTP location Successful");
TelnetOutputStream out = client.put("Backup_"+filename);
out.write(inpbyt);
out.flush();
out.close();
client.closeServer();
Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, "File written sucessfully");
}
//Archiving target file on SAP file system
else if (ServerType.equals("NFS"))
{
Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, "Write file on NFS location");
String NFSDirectory = (String) mc.getContextData("NFSDirectory");
File path = new File(NFSDirectory+"/" +filename);
FileOutputStream fos = new FileOutputStream(path); 
fos.write(inpbyt);
Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, "File written sucessfully")
}
else
{
throw new CustomException("ServerType Parameter Is Not Having Valid Value");
}
// Set content as byte array into payload
xpld.setContent(inpbyt);
// Sets the principle data that represents usually the message to be processed
inputModuleData.setPrincipalData(msg);
}catch (Exception e) {
Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,
"Module Exception caught:");
ModuleException me = new ModuleException(e);
throw me;
}
return inputModuleData;
}
}
class CustomException extends Exception
{
public CustomException(String message)
{
super(message);
}
}

Module configuration to Backup file on FTP server

If target file needs to be archived on FTP server then below module parameters need to be passed in file receiver channel

Note - "Server" parameter value ("FTP" or "NFS") will call the corresponding method in module to archive files on FTP or NFS server.

Module Configuration to Backup file on NFS server

15 Comments
Labels in this area