in plugins/actions/ftp/src/main/java/org/apache/hop/workflow/actions/sftp/ActionSftp.java [422:699]
public Result execute(Result previousResult, int nr) {
Result result = previousResult;
List<RowMetaAndData> rows = result.getRows();
RowMetaAndData resultRow = null;
result.setResult(false);
long filesRetrieved = 0;
if (isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "ActionSftp.Log.StartAction"));
}
HashSet<String> listPreviousFilenames = new HashSet<>();
if (copyprevious) {
if (rows.isEmpty()) {
if (isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "ActionSftp.ArgsFromPreviousNothing"));
}
result.setResult(true);
return result;
}
try {
// Copy the input row to the (command line) arguments
for (int iteration = 0; iteration < rows.size(); iteration++) {
resultRow = rows.get(iteration);
// Get file names
String filePrevious = resultRow.getString(0, null);
if (!Utils.isEmpty(filePrevious)) {
listPreviousFilenames.add(filePrevious);
if (isDebug()) {
logDebug(
BaseMessages.getString(PKG, "ActionSftp.Log.FilenameFromResult", filePrevious));
}
}
}
} catch (Exception e) {
logError(BaseMessages.getString(PKG, "ActionSftp.Error.ArgFromPrevious"));
result.setNrErrors(1);
return result;
}
}
SftpClient sftpclient = null;
// String substitution..
String realServerName = resolve(serverName);
String realServerPort = resolve(serverPort);
String realUsername = resolve(userName);
String realPassword = Encr.decryptPasswordOptionallyEncrypted(resolve(password));
String realSftpDirString = resolve(sftpDirectory);
String realWildcard = resolve(wildcard);
String realTargetDirectory = resolve(targetDirectory);
String realKeyFilename = null;
String realPassPhrase = null;
FileObject targetFolder = null;
try {
// Let's perform some checks before starting
if (isUseKeyFile()) {
// We must have here a private keyfilename
realKeyFilename = resolve(getKeyFilename());
if (Utils.isEmpty(realKeyFilename)) {
// Error..Missing keyfile
logError(BaseMessages.getString(PKG, "ActionSftp.Error.KeyFileMissing"));
result.setNrErrors(1);
return result;
}
if (!HopVfs.fileExists(realKeyFilename)) {
// Error.. can not reach keyfile
logError(
BaseMessages.getString(PKG, "ActionSftp.Error.KeyFileNotFound", realKeyFilename));
result.setNrErrors(1);
return result;
}
realPassPhrase = resolve(getKeyPassPhrase());
}
if (!Utils.isEmpty(realTargetDirectory)) {
targetFolder = HopVfs.getFileObject(realTargetDirectory);
boolean targetFolderExists = targetFolder.exists();
if (targetFolderExists) {
if (isDetailed()) {
logDetailed(
BaseMessages.getString(
PKG, "ActionSftp.Log.TargetFolderExists", realTargetDirectory));
}
} else {
if (!createtargetfolder) {
// Error..Target folder can not be found !
logError(
BaseMessages.getString(
PKG, "ActionSftp.Error.TargetFolderNotExists", realTargetDirectory));
result.setNrErrors(1);
return result;
} else {
// create target folder
targetFolder.createFolder();
if (isDetailed()) {
logDetailed(
BaseMessages.getString(
PKG, "ActionSftp.Log.TargetFolderCreated", realTargetDirectory));
}
}
}
}
if (targetFolder != null) {
targetFolder.close();
targetFolder = null;
}
// Create sftp client to host ...
sftpclient =
new SftpClient(
InetAddress.getByName(realServerName),
Const.toInt(realServerPort, DEFAULT_PORT),
realUsername,
realKeyFilename,
realPassPhrase);
if (isDetailed()) {
logDetailed(
BaseMessages.getString(
PKG,
"ActionSftp.Log.OpenedConnection",
realServerName,
realServerPort,
realUsername));
}
// Set compression
sftpclient.setCompression(getCompression());
// Set proxy?
String realProxyHost = resolve(getProxyHost());
if (!Utils.isEmpty(realProxyHost)) {
// Set proxy
String password = getRealPassword(getProxyPassword());
sftpclient.setProxy(
realProxyHost,
resolve(getProxyPort()),
resolve(getProxyUsername()),
password,
getProxyType());
}
// login to ftp host ...
sftpclient.login(realPassword);
// Passwords should not appear in log files.
// logDetailed("logged in using password "+realPassword); // Logging this seems a bad idea! Oh
// well.
// move to spool dir ...
if (!Utils.isEmpty(realSftpDirString)) {
try {
sftpclient.chdir(realSftpDirString);
} catch (Exception e) {
logError(
BaseMessages.getString(
PKG, "ActionSftp.Error.CanNotFindRemoteFolder", realSftpDirString));
throw new Exception(e);
}
if (isDetailed()) {
logDetailed(
BaseMessages.getString(PKG, "ActionSftp.Log.ChangedDirectory", realSftpDirString));
}
}
Pattern pattern = null;
// Get all the files in the current directory...
String[] filelist = sftpclient.dir();
if (filelist == null) {
// Nothing was found !!! exit
result.setResult(true);
if (isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "ActionSftp.Log.Found", "" + 0));
}
return result;
}
if (isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "ActionSftp.Log.Found", "" + filelist.length));
}
if (!copyprevious) {
if (!Utils.isEmpty(realWildcard)) {
pattern = Pattern.compile(realWildcard);
}
}
// Get the files in the list...
for (int i = 0; i < filelist.length && !parentWorkflow.isStopped(); i++) {
boolean getIt = true;
if (copyprevious) {
// filenames list is send by previous action
// download if the current file is in this list
getIt = listPreviousFilenames.contains(filelist[i]);
} else {
// download files
// but before see if the file matches the regular expression!
if (pattern != null) {
Matcher matcher = pattern.matcher(filelist[i]);
getIt = matcher.matches();
}
}
if (getIt) {
if (isDebug()) {
logDebug(
BaseMessages.getString(
PKG, "ActionSftp.Log.GettingFiles", filelist[i], realTargetDirectory));
}
FileObject targetFile =
HopVfs.getFileObject(realTargetDirectory + Const.FILE_SEPARATOR + filelist[i]);
sftpclient.get(targetFile, filelist[i]);
filesRetrieved++;
if (isaddresult) {
// Add to the result files...
ResultFile resultFile =
new ResultFile(
ResultFile.FILE_TYPE_GENERAL,
targetFile,
parentWorkflow.getWorkflowName(),
toString());
result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
if (isDetailed()) {
logDetailed(
BaseMessages.getString(
PKG, "ActionSftp.Log.FilenameAddedToResultFilenames", filelist[i]));
}
}
if (isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "ActionSftp.Log.TransferedFile", filelist[i]));
}
// Delete the file if this is needed!
if (remove) {
sftpclient.delete(filelist[i]);
if (isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "ActionSftp.Log.DeletedFile", filelist[i]));
}
}
}
}
result.setResult(true);
result.setNrFilesRetrieved(filesRetrieved);
} catch (Exception e) {
result.setNrErrors(1);
logError(BaseMessages.getString(PKG, "ActionSftp.Error.GettingFiles", e.getMessage()));
logError(Const.getStackTracker(e));
} finally {
// close connection, if possible
try {
if (sftpclient != null) {
sftpclient.disconnect();
}
} catch (Exception e) {
// just ignore this, makes no big difference
}
try {
if (targetFolder != null) {
targetFolder.close();
targetFolder = null;
}
if (listPreviousFilenames != null) {
listPreviousFilenames = null;
}
} catch (Exception e) {
// Ignore errors
}
}
return result;
}