Skip to Content

SFTP Adapter Module in SAP PI 7.0

We don’t have SFTP adapter Module in SAP PI 7.0 so if there is any requirement to connect a SFTP server then either we need to purchase an external adapter or we could create a module in JAVA.

I was having a requirement of placing a file over SFTP server so to solve that I wrote a JAVA code for SFTP adapter module.

Hopefully this will help others to create modules for their requirement.

Parameter Configuration in PI receiver Channel:

hahahah.PNG

/**
* @author ashutosh.a.upadhyay
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package com.sftp;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;

import javax.ejb.CreateException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.sap.aii.af.mp.module.Module;
import com.sap.aii.af.mp.module.ModuleContext;
import com.sap.aii.af.mp.module.ModuleData;
import com.sap.aii.af.mp.module.ModuleException;
import com.sap.aii.af.ra.ms.api.Message;
import com.sap.aii.af.ra.ms.api.MessageDirection;
import com.sap.aii.af.ra.ms.api.XMLPayload;
import com.sap.aii.af.service.auditlog.Audit;
import com.sap.aii.af.service.auditlog.AuditDirection;
import com.sap.aii.af.service.auditlog.AuditLogStatus;
import com.sap.aii.af.service.auditlog.AuditMessageKey;
/**
* @ejbHome <{com.sap.aii.af.mp.module.ModuleHome}>
* @ejbLocal <{com.sap.aii.af.mp.module.ModuleLocal}>
* @ejbLocalHome <{com.sap.aii.af.mp.module.ModuleLocalHome}>
* @ejbRemote <{com.sap.aii.af.mp.module.ModuleRemote}>
* @stateless
*/
public class SFTPAdapterModule implements SessionBean, Module {
private SessionContext myContext;

AuditMessageKey amk;

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 {
  try {
  Object obj = inputModuleData.getPrincipalData();
  Message msg = (Message) obj;
  String SFTPHOST = (String) moduleContext.getContextData(“SFTPHostorIP”);
  String SFTPPORT = (String) moduleContext.getContextData(“SFTPPort”);
  String SFTPUSER = (String) moduleContext.getContextData(“SFTPUserName”);
  String SFTPPASS = (String) moduleContext.getContextData(“SFTPpassword”);
  String destDir =  (String) moduleContext.getContextData(“TargetLocation”);
  String fileName = (String) moduleContext.getContextData(“SFTPFileName”);
    
    if (msg.getMessageDirection()== MessageDirection.INBOUND)
    amk = new AuditMessageKey(msg.getMessageId(),AuditDirection.INBOUND);
    else
    amk = new AuditMessageKey(msg.getMessageId(),AuditDirection.OUTBOUND);
     
    XMLPayload xp = msg.getDocument();
   
 
  Hashtable mp = (Hashtable) inputModuleData.getSupplementalData(“module-parameters”);
  if (fileName ==””){
   fileName = (String) mp.get (“FileName”);
  }
  if (destDir ==””){
    fileName = (String) mp.get (“Directory”);
  }  
    Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,”SFTP adapter module called….”);
  
    if (xp != null){
     Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,”Connecting to SFTP….”);
   byte by[] = convert(xp.getInputStream(),SFTPHOST,SFTPPORT,SFTPUSER,SFTPPASS, fileName, destDir);
     Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,”File placed successfully”);
     xp.setContent(by);
    }
  
    inputModuleData.setPrincipalData(msg);
    Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,”Principle data is set successfully.”);
    }
    catch (Exception e){
  Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,”Module Exception Caught .”);
  ModuleException me = new ModuleException(e);
  throw me;
    }
    return inputModuleData;
}

public byte [] convert(InputStream inputstream,String host,String port, String username,String password, String fileName, String dir) throws Exception {
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
String file =””;
  try
  {  
  Date date = new Date();
  SimpleDateFormat sdf = new SimpleDateFormat(“yyyyMMddHHmmssSSS”);
  String formattedDate = sdf.format(date);
  fileName = fileName+formattedDate;

  JSch jsch = new JSch();
  int portNo = Integer.parseInt(port);
  session = jsch.getSession(username,host,portNo);
  session.setPassword(password);
  java.util.Properties config = new java.util.Properties();
  config.put(“StrictHostKeyChecking”, “no”);
  Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,”Establishing Connection…”);
  session.setConfig(config);
  session.connect();
  Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,”Session Established”);
  channel = session.openChannel(“sftp”);
  Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,”Connecting the channel..” );
  channel.connect();
  channelSftp = (ChannelSftp) channel;
  Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,”Hitting the directory” );
 
  channelSftp.put(inputstream,fileName); 
  Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,”File placed..” );
     
  channelSftp.disconnect();
  Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,”Channel Disconnected…” );
    
  session.disconnect();
  sdf = new SimpleDateFormat(“dd/MM/yyyy”);
  formattedDate = sdf.format(date); 
  file = “<FileName> file placed to SFTP server at “+formattedDate;
 
  inputstream.close();
  Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,”Server Disconnected…” );
}
    catch (Exception e) {
    Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,”Exception in Convert Method.”);
    channel.disconnect();
  session.disconnect();
}
return file.getBytes();
}
}

/
hahahah.PNG
22 Comments
You must be Logged on to comment or reply to a post.