Skip to Content

More often we have faced challenges for using SFTP protocol in SAP PI, especially the days when we did not have standard solution for it. Back then we have used workarounds using shell scripts or bought third party adapters. Now that with SP 8.0 and higher service pack for PI 7.11 sap itself has provided standard adapters for SFTP solution (Here), the need of using SFTP protocol in SAP PI has become simpler.

However, Older service packs still have to rely on custom solutions or 3rd party adapters, This blog would stand helpful for older service packs. In this article I will explain how SFTP can be done using adapter module.

For simplicity I created two separate modules, One for sender (to read files from SFTP server) and one for Receiver (to create files on SFTP server). This blog would cover the Receiver Module, I will be creating another blog for the Sender (Here).

To build these modules and to connect SFTP server I have used JSch jcraft jars, mainly because of its various methods and easiness in using them in custom classes. Moreover I found JSCH is capable of handling various authentication methods that are Password based, Public key and Host based. There are lots of example on http://www.jcraft.com/jsch to use these classes. For instance below is snippet of one of the method(Authenticating with Password and connecting to SFTP server) that I have used in the receiver module.


import com.jcraft.jsch.*;import java.io.*;
 try
   { JSch jsch = new JSch(); Session session = jsch.getSession(Username, HostName, Portno);
 session.setPassword(Password);
 session.setConfig("StrictHostKeyChecking", "no");     
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
sftpChannel.cd(Directory);
Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,"Connection to SFTP location Successful");
// Creating file with applied filename
String outfname = filename;
if (Outfilename != null) {
    outfname = Outfilename;
            }
if (Timestamp != null) {
    Date date = new Date();
// get the current date
    SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");
// set the format for date
    String dfmt = dateFormatter.format(date);
    dfmt = dfmt + ".";
    outfname = outfname.replace(".", dfmt);
            }
        String toutfname = outfname;
    if (tempfname != null) {
    toutfname = toutfname + ".tmp";
    Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,"Writing file with temporary name");
            }
    sftpChannel.put(isXML, toutfname);
    if (tempfname != null) {
    try {
    sftpChannel.rename(toutfname, outfname);
    Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,
    "File Renaming is succesful");
    } catch (Exception e) {
    Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,"Module Exception caught:");
    ModuleException me = new ModuleException(e);
    throw me;
            }
            }
    sftpChannel.exit();
    session.disconnect();
    Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, outfname + "File written sucessfully in " + Directory);
            }
    // 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;
        }














Likewise we can do various operations, for example creating the file with temporary name while it is being written and renaming it to final file name once file writing is completed and so on. Once the module code is completed with required operations and is deployed in SAP PI, Next step is to use it. I used it in module section of standard File adapter, with this my interface now will call the custom adapter module to post file on sftp server but additionally it will also call standard processing sequence of File adapter to create a file at file location mentioned in Parameters tab of standard file adapter. On a loner run these additional file, created by standard file adapter if needed can be used as an archive repository of data sent to sftp server or if not we can simply pass empty payload to this additional file for so any data security reason (The module code will have to be modified accordingly to pass empty payload to standard file adapter processing  sequence CallSapAdapter of the module section).


For content conversion we can use standard bean StrictXml2PlainBean and can include in module section of file adapter before our custom sftp module.


/wp-content/uploads/2014/07/2014_07_10_182635_495452.jpg


And for monitoring, We can add audit logs at every step inside module code and same will be visible in communication channel monitoring logs.

References :

SFTP using custom adapter module Part2 – Sender

http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/c0b39e65-981e-2b10-1c9c-fc3f8e6747fa?overridelayout=t…

JSch – Java Secure Channel – Examples

To report this post you need to login first.

2 Comments

You must be Logged on to comment or reply to a post.

Leave a Reply