Skip to main content


         This documentation site is for previous versions. Visit our new documentation site for current releases.      
 

This content has been archived and is no longer being updated.

Links may not function; however, this content may be relevant to outdated versions of the product.

Configuring custom SFTP and FTPS implementations for Pega applications

Updated on September 1, 2021

The Pega 7 Platform supports FTP Secure (FTPS) for Pega 7.1.8 or later and supports SSH FTP (SFTP) for Pega 7.1.9 or later. The FTP Server data instance provides built-in support for transferring files from the Pega server file system to an SFTP server. You can create custom SSH FTP (SFTP) and FTP Secure (FTPS) implementations for applications when you need additional capabilities or if you are using earlier versions of the Pega 7 Platform or PRPC.

For information about built-in functionality, see About FTP Server data instances.

Implementing custom SSH FTP (SFTP)

SourceForge open source software provides a custom implementation of SFTP that uses the underlying implementation in the Java Secure Channel file, JSCH.jar. Download and install this file for Pega 7 Platform versions prior to 7.2.2 if it is not already installed on your system.

  1. Download the latest JSCH.jar file from either of these sites:
    • SourceForge
    • JCraft
  2. Place the JSCH.jar file in the application server directory where JAR files are stored for selection during class loading.
    For example, on a Tomcat server the directory is C:\apache-tomcat-7.0.22\lib.
  3. Optional: Review the Parameters tab of the Activity form to see how parameters pass values in the Java code:
    Parameters tab on the Activity form
  4. Create an activity with a Java step that specifies the following code:

    // Declare parameters
    String HostName= tools.getParamValue("HostName");
    String UserName = tools.getParamValue("UserName");
    String Password = tools.getParamValue("Password");
    String remotePath =tools.getParamValue("remotePath");
    String localFile = tools.getParamValue("LocalFileName");
    String fileName = (new java.io.File(localFile)).getName();

    //Trim values
    HostName=HostName.trim();UserName =UserName.trim();
    Password =Password.trim();remotePath =remotePath.trim();
    localFile =localFile.trim();

    java.io.InputStream inputStream = null;
    com.jcraft.jsch.JSch jsch = null;
    com.jcraft.jsch.ChannelSftp sftpChannel=null;
    com.jcraft.jsch.Session ftpSession=null;
    try {
    jsch = new com.jcraft.jsch.JSch();
    oLog.debug("Getting jsch Session starts");
    ftpSession = jsch.getSession(UserName,HostName,22);
    ftpSession.setPassword(Password);

    java.util.Properties config = new java.util.Properties();
    config.put("StrictHostKeyChecking", "no");
    ftpSession.setConfig(config);
    ftpSession.connect();

    oLog.debug("Getting jsch Session ends");
    try {
    sftpChannel = (com.jcraft.jsch.ChannelSftp) ftpSession.openChannel("sftp");
    sftpChannel.connect();
    sftpChannel.cd(remotePath);

    java.io.File fileF=new java.io.File(localFile);
    * try {inputStream = new java.io.FileInputStream(fileF);
    }
    catch (java.io.FileNotFoundException exp)
    {oLog.error(exp.toString());}*

    sftpChannel.put(localFile,fileName);
    }
    catch (com.jcraft.jsch.SftpException e)
    {oLog.error(e.toString());
    }
    }

    catch (com.jcraft.jsch.JSchException e) {
    oLog.error(e.toString());
    }

    oLog.debug(" End SFTWorker ->getfileContent()");
    if (sftpChannel != null && sftpChannel.isConnected()) {
    sftpChannel.disconnect();
    }
    if (ftpSession != null && ftpSession.isConnected()) {
    ftpSession.disconnect();
    }

  5. Refer to the Java Secure Channel code example that is on the JCraft website.

Implementing custom FTP Secure (FTPS)

Use the custom implementation of FTPS that is provided by Apache Commons.

  1. Download the latest Apache Commons JAR file from the Apache Commons website.
  2. Find the FTPSClient Javadoc on the Apache Commons website.
  3. Manually specify the values of the highlighted lines of the following code to suit the requirements of your environment.

    boolean error = false;

    /** localFile variable holds reference to file on the PRPC server
    example:
    String localFile = "F:\\TestFile.txt";
    **/
    String localFile = "";
    org.apache.commons.net.ftp.FTPSClient ftps = new org.apache.commons.net.ftp.FTPSClient("SSL", true);
    try {
    int reply;
    ftps.setAuthValue("SSL");
    /** Enter the fully qualified name and port of the FTPS server
    example:
    ftps.connect("wpuripw7", 990);
    **/
    ftps.connect("", 990);
    /** Enter the user credentials to access FTPS server
    example:
    ftps.login("admin", "admin");
    **/
    ftps.login("", "");
    reply = ftps.getReplyCode();
    if (!org.apache.commons.net.ftp.FTPReply.isPositiveCompletion(reply)) {
    ftps.disconnect();
    oLog.info("FTP server refused connection.");
    throw new PRRuntimeException("Could not connect to the FTP server");
    }
    } catch (java.io.IOException e) {
    if (ftps.isConnected()) {
    try {
    oLog.info("ERROR. Disconnecting from the FTP server.");
    ftps.disconnect();
    } catch (java.io.IOException f) {
    // do nothing
    }
    }
    throw new PRRuntimeException("Error establishing connection with FTP server.", e);
    }
    try {
    ftps.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE); // can be BINARY or ASCII
    ftps.enterLocalPassiveMode();
    java.io.InputStream input = new java.io.FileInputStream(localFile);
    /** Name the file as you want it on the FTPS server, usually its the same as source
    ftps.storeFile("TestFile.txt", input);
    **/
    ftps.storeFile("", input);
    ftps.logout();
    } catch (org.apache.commons.net.ftp.FTPConnectionClosedException e) {
    error = true;
    oLog.error("Server closed connection.");
    throw new PRRuntimeException("Server closed connection", e);
    } catch (java.io.IOException e) {
    error = true;
    oLog.error("Error sending file: " + e.getMessage());
    throw new PRRuntimeException("Error sending file.", e);
    } finally {
    oLog.info("Logging out of the session.");
    if (ftps.isConnected()) {
    try {
    oLog.info("Disconnecting from the FTP server.");
    ftps.disconnect();
    } catch (java.io.IOException f) {
    // do nothing
    }
    }
    }

  4. Follow the comments in the code about the values that you need to specify:

    Line 9: String localFile, specify the file path on the PRPC server that needs to be sent over FTPS.
    Line 18: ftps.connect("", 990), specify the FTPS server and port.
    Line 23: ftps.login("", ""), specify the FTPS server credentials.
    Line 49: ftps.storeFile("", input), specify the name of the file that you want to be on the FTPS server.

  5. Include the edited code in the Java step of your activity.
  6. Install the latest Apache commons-net-3.3.jar file in the pr_engineclasses table by using the Import wizard, which provides access to the FTPSClient code.
  7. If you get a compilation error while saving the activity, do the following steps:

    1. In the System Management Application (SMA) ETier Runtime, look up the org.apache.commons.net.ftp.FTPSClient.
      FTPSClient Class Name and Location

    2. Open the Dynamic System Setting (DSS) compiler/defaultPaths and add the output location that you specified in Step 7a, for example:
      L:\pegaapps\v715oraclefresh\apache-tomcat-7.0.22\lib\commons-net-3.3.jar.

    3. In the System Management Application (SMA), click Advanced > Class Management > Refresh External JARs.

References

Have a question? Get answers now.

Visit the Support Center to ask questions, engage in discussions, share ideas, and help others.

Did you find this content helpful?

Want to help us improve this content?

We'd prefer it if you saw us at our best.

Pega.com is not optimized for Internet Explorer. For the optimal experience, please use:

Close Deprecation Notice
Contact us