Configuring custom SFTP and FTPS implementations for Pega applications
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.
- Download the latest JSCH.jar file from either of these sites:
- SourceForge
- JCraft
- 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. - Optional: Review the Parameters tab of the Activity form to see how parameters pass values in the Java code:
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();
}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.
- Download the latest Apache Commons JAR file from the Apache Commons website.
- Find the FTPSClient Javadoc on the Apache Commons website.
- 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
}
}
} 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.- Include the edited code in the Java step of your activity.
- 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.
If you get a compilation error while saving the activity, do the following steps:
In the System Management Application (SMA) ETier Runtime, look up the org.apache.commons.net.ftp.FTPSClient.
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
.In the System Management Application (SMA), click
.