in plugins/actions/ftp/src/main/java/org/apache/hop/workflow/actions/sftpput/ActionSftpPut.java [559:954]
public Result execute(Result previousResult, int nr) throws HopException {
Result result = previousResult;
List<RowMetaAndData> rows = result.getRows();
result.setResult(false);
if (isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "ActionSftpPut.Log.StartAction"));
}
ArrayList<FileObject> myFileList = new ArrayList<>();
if (copyingPrevious) {
if (rows.isEmpty()) {
if (isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "ActionSftpPut.ArgsFromPreviousNothing"));
}
result.setResult(true);
return result;
}
try {
RowMetaAndData resultRow = null;
// 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)) {
FileObject file = HopVfs.getFileObject(filePrevious);
if (!file.exists()) {
logError(
BaseMessages.getString(
PKG, "ActionSftpPut.Log.FilefromPreviousNotFound", filePrevious));
} else {
myFileList.add(file);
if (isDebug()) {
logDebug(
BaseMessages.getString(
PKG, "ActionSftpPut.Log.FilenameFromResult", filePrevious));
}
}
}
}
} catch (Exception e) {
logError(BaseMessages.getString(PKG, "ActionSftpPut.Error.ArgFromPrevious"));
result.setNrErrors(1);
// free resource
myFileList = null;
return result;
}
}
if (copyingPreviousFiles) {
List<ResultFile> resultFiles = result.getResultFilesList();
if (resultFiles == null || resultFiles.isEmpty()) {
if (isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "ActionSftpPut.ArgsFromPreviousNothingFiles"));
}
result.setResult(true);
return result;
}
try {
for (Iterator<ResultFile> it = resultFiles.iterator();
it.hasNext() && !parentWorkflow.isStopped(); ) {
ResultFile resultFile = it.next();
FileObject file = resultFile.getFile();
if (file != null) {
if (!file.exists()) {
logError(
BaseMessages.getString(
PKG, "ActionSftpPut.Log.FilefromPreviousNotFound", file.toString()));
} else {
myFileList.add(file);
if (isDebug()) {
logDebug(
BaseMessages.getString(
PKG, "ActionSftpPut.Log.FilenameFromResult", file.toString()));
}
}
}
}
} catch (Exception e) {
logError(BaseMessages.getString(PKG, "ActionSftpPut.Error.ArgFromPrevious"));
result.setNrErrors(1);
// free resource
myFileList = null;
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(remoteDirectory);
String realWildcard = resolve(wildcard);
String realLocalDirectory = resolve(localDirectory);
String realKeyFilename = null;
String realPassPhrase = null;
// Destination folder (Move to)
String realDestinationFolder = resolve(getDestinationFolder());
try {
// Let's perform some checks before starting
if (getAfterFtps() == AFTER_FTPSPUT_MOVE) {
if (Utils.isEmpty(realDestinationFolder)) {
logError(BaseMessages.getString(PKG, "ActionSSH2PUT.Log.DestinatFolderMissing"));
result.setNrErrors(1);
return result;
} else {
FileObject folder = null;
try {
folder = HopVfs.getFileObject(realDestinationFolder);
// Let's check if folder exists...
if (!folder.exists()) {
// Do we need to create it?
if (createDestinationFolder) {
folder.createFolder();
} else {
logError(
BaseMessages.getString(
PKG, "ActionSSH2PUT.Log.DestinatFolderNotExist", realDestinationFolder));
result.setNrErrors(1);
return result;
}
}
realDestinationFolder = HopVfs.getFilename(folder);
} catch (Exception e) {
throw new HopException(e);
} finally {
if (folder != null) {
try {
folder.close();
} catch (Exception e) {
/* Ignore */
}
}
}
}
}
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"));
result.setNrErrors(1);
return result;
}
realPassPhrase = resolve(getKeyPassPhrase());
}
// Create sftp client to host ...
sftpclient =
new SftpClient(
InetAddress.getByName(realServerName),
Const.toInt(realServerPort, 22),
realUsername,
realKeyFilename,
realPassPhrase);
if (isDetailed()) {
logDetailed(
BaseMessages.getString(
PKG,
"ActionSftpPut.Log.OpenedConnection",
realServerName,
"" + realServerPort,
realUsername));
}
// Set compression
sftpclient.setCompression(getCompression());
// Set proxy?
String realProxyHost = resolve(getProxyHost());
if (!Utils.isEmpty(realProxyHost)) {
// Set proxy
sftpclient.setProxy(
realProxyHost,
resolve(getProxyPort()),
resolve(getProxyUsername()),
resolve(getProxyPassword()),
getProxyType());
}
// login to ftp host ...
sftpclient.login(realPassword);
// Don't show the password in the logs, it's not good for security audits
// logDetailed("logged in using password "+realPassword); // Logging this seems a bad idea! Oh
// well.
// move to spool dir ...
if (!Utils.isEmpty(realSftpDirString)) {
boolean existfolder = sftpclient.folderExists(realSftpDirString);
if (!existfolder) {
if (!isCreateRemoteFolder()) {
throw new HopException(
BaseMessages.getString(
PKG, "ActionSftpPut.Error.CanNotFindRemoteFolder", realSftpDirString));
}
if (isDetailed()) {
logDetailed(
BaseMessages.getString(
PKG, "ActionSftpPut.Error.CanNotFindRemoteFolder", realSftpDirString));
}
// Let's create folder
sftpclient.createFolder(realSftpDirString);
if (isDetailed()) {
logDetailed(
BaseMessages.getString(
PKG, "ActionSftpPut.Log.RemoteFolderCreated", realSftpDirString));
}
}
sftpclient.chdir(realSftpDirString);
if (isDetailed()) {
logDetailed(
BaseMessages.getString(PKG, "ActionSftpPut.Log.ChangedDirectory", realSftpDirString));
}
} // end if
if (!copyingPrevious && !copyingPreviousFiles) {
// Get all the files in the local directory...
myFileList = new ArrayList<>();
FileObject localFiles = HopVfs.getFileObject(realLocalDirectory);
FileObject[] children = localFiles.getChildren();
if (children != null) {
for (int i = 0; i < children.length; i++) {
// Get filename of file or directory
if (children[i].getType().equals(FileType.FILE)) {
myFileList.add(children[i]);
}
} // end for
}
}
if (isDetailed()) {
logDetailed(
BaseMessages.getString(
PKG, "ActionSftpPut.Log.RowsFromPreviousResult", myFileList.size()));
}
Pattern pattern = null;
if (!copyingPrevious && !copyingPreviousFiles) {
if (!Utils.isEmpty(realWildcard)) {
pattern = Pattern.compile(realWildcard);
}
}
int nrFilesSent = 0;
int nrFilesMatched = 0;
// Get the files in the list and execute sftp.put() for each file
Iterator<FileObject> it = myFileList.iterator();
while (it.hasNext() && !parentWorkflow.isStopped()) {
FileObject myFile = it.next();
try {
String localFilename = myFile.toString();
String destinationFilename = myFile.getName().getBaseName();
boolean getIt = true;
// First see if the file matches the regular expression!
if (pattern != null) {
Matcher matcher = pattern.matcher(destinationFilename);
getIt = matcher.matches();
}
if (getIt) {
nrFilesMatched++;
if (isDebug()) {
logDebug(
BaseMessages.getString(
PKG, "ActionSftpPut.Log.PuttingFile", localFilename, realSftpDirString));
}
sftpclient.put(myFile, destinationFilename);
nrFilesSent++;
if (isDetailed()) {
logDetailed(
BaseMessages.getString(PKG, "ActionSftpPut.Log.TransferredFile", localFilename));
}
// We successfully uploaded the file
// what's next ...
switch (getAfterFtps()) {
case AFTER_FTPSPUT_DELETE:
myFile.delete();
if (isDetailed()) {
logDetailed(
BaseMessages.getString(PKG, "ActionSftpPut.Log.DeletedFile", localFilename));
}
break;
case AFTER_FTPSPUT_MOVE:
FileObject destination = null;
try {
destination =
HopVfs.getFileObject(
realDestinationFolder
+ Const.FILE_SEPARATOR
+ myFile.getName().getBaseName());
myFile.moveTo(destination);
if (isDetailed()) {
logDetailed(
BaseMessages.getString(
PKG, "ActionSftpPut.Log.FileMoved", myFile, destination));
}
} finally {
if (destination != null) {
destination.close();
}
}
break;
default:
if (addFilenameResut) {
// Add to the result files...
ResultFile resultFile =
new ResultFile(
ResultFile.FILE_TYPE_GENERAL,
myFile,
parentWorkflow.getWorkflowName(),
toString());
result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
if (isDetailed()) {
logDetailed(
BaseMessages.getString(
PKG,
"ActionSftpPut.Log.FilenameAddedToResultFilenames",
localFilename));
}
}
break;
}
}
} finally {
if (myFile != null) {
myFile.close();
}
}
} // end for
result.setResult(true);
// Abuse the nr of files retrieved vector in the results
//
result.setNrFilesRetrieved(nrFilesSent);
// If we didn't upload any files, say something about it...
//
if (nrFilesMatched == 0) {
if (isSuccessWhenNoFile()) {
// Just warn user
if (isBasic()) {
logBasic(BaseMessages.getString(PKG, "ActionSftpPut.Error.NoFileToSend"));
}
} else {
// Fail
logError(BaseMessages.getString(PKG, "ActionSftpPut.Error.NoFileToSend"));
result.setNrErrors(1);
result.setResult(false);
return result;
}
}
} catch (Exception e) {
result.setNrErrors(1);
logError(BaseMessages.getString(PKG, "ActionSftpPut.Exception", 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
} // end catch
myFileList = null;
} // end finally
return result;
} // JKU: end function execute()