Search in sources :

Example 1 with FTPException

use of com.enterprisedt.net.ftp.FTPException in project pentaho-kettle by pentaho.

the class JobEntryFTP method hookInOtherParsers.

/**
 * Hook in known parsers, and then those that have been specified in the variable ftp.file.parser.class.names
 *
 * @param ftpClient
 * @throws FTPException
 * @throws IOException
 */
protected void hookInOtherParsers(FTPClient ftpClient) throws FTPException, IOException {
    if (log.isDebug()) {
        logDebug(BaseMessages.getString(PKG, "JobEntryFTP.DEBUG.Hooking.Parsers"));
    }
    String system = ftpClient.system();
    MVSFileParser parser = new MVSFileParser(log);
    if (log.isDebug()) {
        logDebug(BaseMessages.getString(PKG, "JobEntryFTP.DEBUG.Created.MVS.Parser"));
    }
    FTPFileFactory factory = new FTPFileFactory(system);
    if (log.isDebug()) {
        logDebug(BaseMessages.getString(PKG, "JobEntryFTP.DEBUG.Created.Factory"));
    }
    factory.addParser(parser);
    ftpClient.setFTPFileFactory(factory);
    if (log.isDebug()) {
        logDebug(BaseMessages.getString(PKG, "JobEntryFTP.DEBUG.Get.Variable.Space"));
    }
    VariableSpace vs = this.getVariables();
    if (vs != null) {
        if (log.isDebug()) {
            logDebug(BaseMessages.getString(PKG, "JobEntryFTP.DEBUG.Getting.Other.Parsers"));
        }
        String otherParserNames = vs.getVariable("ftp.file.parser.class.names");
        if (otherParserNames != null) {
            if (log.isDebug()) {
                logDebug(BaseMessages.getString(PKG, "JobEntryFTP.DEBUG.Creating.Parsers"));
            }
            String[] parserClasses = otherParserNames.split("|");
            String cName = null;
            Class<?> clazz = null;
            Object parserInstance = null;
            for (int i = 0; i < parserClasses.length; i++) {
                cName = parserClasses[i].trim();
                if (cName.length() > 0) {
                    try {
                        clazz = Class.forName(cName);
                        parserInstance = clazz.newInstance();
                        if (parserInstance instanceof FTPFileParser) {
                            if (log.isDetailed()) {
                                logDetailed(BaseMessages.getString(PKG, "JobEntryFTP.DEBUG.Created.Other.Parser", cName));
                            }
                            factory.addParser((FTPFileParser) parserInstance);
                        }
                    } catch (Exception ignored) {
                        if (log.isDebug()) {
                            ignored.printStackTrace();
                            logError(BaseMessages.getString(PKG, "JobEntryFTP.ERROR.Creating.Parser", cName));
                        }
                    }
                }
            }
        }
    }
}
Also used : FTPFileFactory(com.enterprisedt.net.ftp.FTPFileFactory) VariableSpace(org.pentaho.di.core.variables.VariableSpace) FileObject(org.apache.commons.vfs2.FileObject) FTPFileParser(com.enterprisedt.net.ftp.FTPFileParser) FTPException(com.enterprisedt.net.ftp.FTPException) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 2 with FTPException

use of com.enterprisedt.net.ftp.FTPException in project pentaho-kettle by pentaho.

the class JobEntryFTPPUT method hookInOtherParsers.

/**
 * Hook in known parsers, and then those that have been specified in the variable ftp.file.parser.class.names
 *
 * @param ftpClient
 * @throws FTPException
 * @throws IOException
 */
protected void hookInOtherParsers(FTPClient ftpClient) throws FTPException, IOException {
    if (log.isDebug()) {
        logDebug(BaseMessages.getString(PKG, "JobEntryFTP.DEBUG.Hooking.Parsers"));
    }
    String system = ftpClient.system();
    MVSFileParser parser = new MVSFileParser(log);
    if (log.isDebug()) {
        logDebug(BaseMessages.getString(PKG, "JobEntryFTP.DEBUG.Created.MVS.Parser"));
    }
    FTPFileFactory factory = new FTPFileFactory(system);
    if (log.isDebug()) {
        logDebug(BaseMessages.getString(PKG, "JobEntryFTP.DEBUG.Created.Factory"));
    }
    factory.addParser(parser);
    ftpClient.setFTPFileFactory(factory);
    if (log.isDebug()) {
        logDebug(BaseMessages.getString(PKG, "JobEntryFTP.DEBUG.Get.Variable.Space"));
    }
    VariableSpace vs = this.getVariables();
    if (vs != null) {
        if (log.isDebug()) {
            logDebug(BaseMessages.getString(PKG, "JobEntryFTP.DEBUG.Getting.Other.Parsers"));
        }
        String otherParserNames = vs.getVariable("ftp.file.parser.class.names");
        if (otherParserNames != null) {
            if (log.isDebug()) {
                logDebug(BaseMessages.getString(PKG, "JobEntryFTP.DEBUG.Creating.Parsers"));
            }
            String[] parserClasses = otherParserNames.split("|");
            String cName = null;
            Class<?> clazz = null;
            Object parserInstance = null;
            for (int i = 0; i < parserClasses.length; i++) {
                cName = parserClasses[i].trim();
                if (cName.length() > 0) {
                    try {
                        clazz = Class.forName(cName);
                        parserInstance = clazz.newInstance();
                        if (parserInstance instanceof FTPFileParser) {
                            if (log.isDetailed()) {
                                logDetailed(BaseMessages.getString(PKG, "JobEntryFTP.DEBUG.Created.Other.Parser", cName));
                            }
                            factory.addParser((FTPFileParser) parserInstance);
                        }
                    } catch (Exception ignored) {
                        if (log.isDebug()) {
                            ignored.printStackTrace();
                            logError(BaseMessages.getString(PKG, "JobEntryFTP.ERROR.Creating.Parser", cName));
                        }
                    }
                }
            }
        }
    }
}
Also used : MVSFileParser(org.pentaho.di.job.entries.ftp.MVSFileParser) FTPFileFactory(com.enterprisedt.net.ftp.FTPFileFactory) VariableSpace(org.pentaho.di.core.variables.VariableSpace) FTPFileParser(com.enterprisedt.net.ftp.FTPFileParser) FTPException(com.enterprisedt.net.ftp.FTPException) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) IOException(java.io.IOException)

Example 3 with FTPException

use of com.enterprisedt.net.ftp.FTPException in project pentaho-kettle by pentaho.

the class JobEntryFTP method execute.

public Result execute(Result previousResult, int nr) {
    log.logBasic(BaseMessages.getString(PKG, "JobEntryFTP.Started", serverName));
    Result result = previousResult;
    result.setNrErrors(1);
    result.setResult(false);
    NrErrors = 0;
    NrfilesRetrieved = 0;
    successConditionBroken = false;
    boolean exitjobentry = false;
    limitFiles = Const.toInt(environmentSubstitute(getLimit()), 10);
    // Here let's put some controls before stating the job
    if (movefiles) {
        if (Utils.isEmpty(movetodirectory)) {
            logError(BaseMessages.getString(PKG, "JobEntryFTP.MoveToFolderEmpty"));
            return result;
        }
    }
    if (isDetailed()) {
        logDetailed(BaseMessages.getString(PKG, "JobEntryFTP.Start"));
    }
    FTPClient ftpclient = null;
    String realMoveToFolder = null;
    try {
        // Create ftp client to host:port ...
        ftpclient = initFTPClient();
        String realServername = environmentSubstitute(serverName);
        String realServerPort = environmentSubstitute(port);
        ftpclient.setRemoteAddr(getInetAddress(realServername));
        if (!Utils.isEmpty(realServerPort)) {
            ftpclient.setRemotePort(Const.toInt(realServerPort, 21));
        }
        if (!Utils.isEmpty(proxyHost)) {
            String realProxy_host = environmentSubstitute(proxyHost);
            ftpclient.setRemoteAddr(InetAddress.getByName(realProxy_host));
            if (isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobEntryFTP.OpenedProxyConnectionOn", realProxy_host));
            }
            // FIXME: Proper default port for proxy
            int port = Const.toInt(environmentSubstitute(proxyPort), 21);
            if (port != 0) {
                ftpclient.setRemotePort(port);
            }
        } else {
            ftpclient.setRemoteAddr(getInetAddress(realServername));
            if (isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobEntryFTP.OpenedConnectionTo", realServername));
            }
        }
        // set activeConnection connectmode ...
        if (activeConnection) {
            ftpclient.setConnectMode(FTPConnectMode.ACTIVE);
            if (isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobEntryFTP.SetActive"));
            }
        } else {
            ftpclient.setConnectMode(FTPConnectMode.PASV);
            if (isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobEntryFTP.SetPassive"));
            }
        }
        // Set the timeout
        ftpclient.setTimeout(timeout);
        if (isDetailed()) {
            logDetailed(BaseMessages.getString(PKG, "JobEntryFTP.SetTimeout", String.valueOf(timeout)));
        }
        ftpclient.setControlEncoding(controlEncoding);
        if (isDetailed()) {
            logDetailed(BaseMessages.getString(PKG, "JobEntryFTP.SetEncoding", controlEncoding));
        }
        // If socks proxy server was provided
        if (!Utils.isEmpty(socksProxyHost)) {
            if (!Utils.isEmpty(socksProxyPort)) {
                FTPClient.initSOCKS(environmentSubstitute(socksProxyPort), environmentSubstitute(socksProxyHost));
            } else {
                throw new FTPException(BaseMessages.getString(PKG, "JobEntryFTP.SocksProxy.PortMissingException", environmentSubstitute(socksProxyHost), getName()));
            }
            // then if we have authentication information
            if (!Utils.isEmpty(socksProxyUsername) && !Utils.isEmpty(socksProxyPassword)) {
                FTPClient.initSOCKSAuthentication(environmentSubstitute(socksProxyUsername), Utils.resolvePassword(this, socksProxyPassword));
            } else if (!Utils.isEmpty(socksProxyUsername) && Utils.isEmpty(socksProxyPassword) || Utils.isEmpty(socksProxyUsername) && !Utils.isEmpty(socksProxyPassword)) {
                // we have a username without a password or vica versa
                throw new FTPException(BaseMessages.getString(PKG, "JobEntryFTP.SocksProxy.IncompleteCredentials", environmentSubstitute(socksProxyHost), getName()));
            }
        }
        // login to ftp host ...
        ftpclient.connect();
        String realUsername = environmentSubstitute(userName) + (!Utils.isEmpty(proxyHost) ? "@" + realServername : "") + (!Utils.isEmpty(proxyUsername) ? " " + environmentSubstitute(proxyUsername) : "");
        String realPassword = Utils.resolvePassword(this, password) + (!Utils.isEmpty(proxyPassword) ? " " + Utils.resolvePassword(this, proxyPassword) : "");
        ftpclient.login(realUsername, realPassword);
        // Remove password from logging, you don't know where it ends up.
        if (isDetailed()) {
            logDetailed(BaseMessages.getString(PKG, "JobEntryFTP.LoggedIn", realUsername));
        }
        // Fix for PDI-2534 - add auxilliary FTP File List parsers to the ftpclient object.
        this.hookInOtherParsers(ftpclient);
        // move to spool dir ...
        if (!Utils.isEmpty(ftpDirectory)) {
            String realFtpDirectory = environmentSubstitute(ftpDirectory);
            realFtpDirectory = normalizePath(realFtpDirectory);
            ftpclient.chdir(realFtpDirectory);
            if (isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobEntryFTP.ChangedDir", realFtpDirectory));
            }
        }
        // Create move to folder if necessary
        if (movefiles && !Utils.isEmpty(movetodirectory)) {
            realMoveToFolder = environmentSubstitute(movetodirectory);
            realMoveToFolder = normalizePath(realMoveToFolder);
            // Folder exists?
            boolean folderExist = true;
            if (isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobEntryFTP.CheckMoveToFolder", realMoveToFolder));
            }
            String originalLocation = ftpclient.pwd();
            try {
                // does not work for folders, see PDI-2567: folderExist=ftpclient.exists(realMoveToFolder);
                // try switching to the 'move to' folder.
                ftpclient.chdir(realMoveToFolder);
                // Switch back to the previous location.
                if (isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "JobEntryFTP.CheckMoveToFolderSwitchBack", originalLocation));
                }
                ftpclient.chdir(originalLocation);
            } catch (Exception e) {
                folderExist = false;
            // Assume folder does not exist !!
            }
            if (!folderExist) {
                if (createmovefolder) {
                    ftpclient.mkdir(realMoveToFolder);
                    if (isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "JobEntryFTP.MoveToFolderCreated", realMoveToFolder));
                    }
                } else {
                    logError(BaseMessages.getString(PKG, "JobEntryFTP.MoveToFolderNotExist"));
                    exitjobentry = true;
                    NrErrors++;
                }
            }
        }
        if (!exitjobentry) {
            // Get all the files in the current directory...
            FTPFile[] ftpFiles = ftpclient.dirDetails(null);
            if (isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobEntryFTP.FoundNFiles", String.valueOf(ftpFiles.length)));
            }
            // set transfertype ...
            if (binaryMode) {
                ftpclient.setType(FTPTransferType.BINARY);
                if (isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "JobEntryFTP.SetBinary"));
                }
            } else {
                ftpclient.setType(FTPTransferType.ASCII);
                if (isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "JobEntryFTP.SetAscii"));
                }
            }
            if (ftpFiles.length == 1) {
                String translatedWildcard = environmentSubstitute(wildcard);
                if (!Utils.isEmpty(translatedWildcard)) {
                    if (ftpFiles[0].getName().startsWith(translatedWildcard)) {
                        throw new FTPException(ftpFiles[0].getName());
                    }
                }
            }
            Pattern pattern = null;
            if (!Utils.isEmpty(wildcard)) {
                String realWildcard = environmentSubstitute(wildcard);
                pattern = Pattern.compile(realWildcard);
            }
            if (!getSuccessCondition().equals(SUCCESS_IF_NO_ERRORS)) {
                limitFiles = Const.toInt(environmentSubstitute(getLimit()), 10);
            }
            // Get the files in the list...
            for (FTPFile ftpFile : ftpFiles) {
                if (parentJob.isStopped()) {
                    exitjobentry = true;
                    throw new Exception(BaseMessages.getString(PKG, "JobEntryFTP.JobStopped"));
                }
                if (successConditionBroken) {
                    throw new Exception(BaseMessages.getString(PKG, "JobEntryFTP.SuccesConditionBroken", "" + NrErrors));
                }
                boolean getIt = true;
                String filename = ftpFile.getName();
                if (isDebug()) {
                    logDebug(BaseMessages.getString(PKG, "JobEntryFTP.AnalysingFile", filename));
                }
                // We get only files
                if (ftpFile.isDir()) {
                    // not a file..so let's skip it!
                    getIt = false;
                    if (isDebug()) {
                        logDebug(BaseMessages.getString(PKG, "JobEntryFTP.SkippingNotAFile", filename));
                    }
                }
                if (getIt) {
                    try {
                        // See if the file matches the regular expression!
                        if (pattern != null) {
                            Matcher matcher = pattern.matcher(filename);
                            getIt = matcher.matches();
                        }
                        if (getIt) {
                            downloadFile(ftpclient, filename, realMoveToFolder, parentJob, result);
                        }
                    } catch (Exception e) {
                        // Update errors number
                        updateErrors();
                        logError(BaseMessages.getString(PKG, "JobFTP.UnexpectedError", e.toString()));
                    }
                }
            }
        // end for
        }
    } catch (Exception e) {
        if (!successConditionBroken && !exitjobentry) {
            updateErrors();
        }
        logError(BaseMessages.getString(PKG, "JobEntryFTP.ErrorGetting", e.getMessage()));
    } finally {
        if (ftpclient != null) {
            try {
                ftpclient.quit();
            } catch (Exception e) {
                logError(BaseMessages.getString(PKG, "JobEntryFTP.ErrorQuitting", e.getMessage()));
            }
        }
        FTPClient.clearSOCKS();
    }
    result.setNrErrors(NrErrors);
    result.setNrFilesRetrieved(NrfilesRetrieved);
    if (getSuccessStatus()) {
        result.setResult(true);
    }
    if (exitjobentry) {
        result.setResult(false);
    }
    displayResults();
    return result;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) FTPFile(com.enterprisedt.net.ftp.FTPFile) FTPClient(com.enterprisedt.net.ftp.FTPClient) FTPException(com.enterprisedt.net.ftp.FTPException) FTPException(com.enterprisedt.net.ftp.FTPException) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) Result(org.pentaho.di.core.Result)

Example 4 with FTPException

use of com.enterprisedt.net.ftp.FTPException in project pentaho-kettle by pentaho.

the class JobEntryFTPDelete method execute.

/**
 * Needed for the Vector coming from sshclient.ls() *
 */
@SuppressWarnings("unchecked")
public Result execute(Result previousResult, int nr) {
    log.logBasic(BaseMessages.getString(PKG, "JobEntryFTPDelete.Started", serverName));
    RowMetaAndData resultRow = null;
    Result result = previousResult;
    List<RowMetaAndData> rows = result.getRows();
    result.setResult(false);
    NrErrors = 0;
    NrfilesDeleted = 0;
    successConditionBroken = false;
    HashSet<String> list_previous_files = new HashSet<>();
    // Here let's put some controls before stating the job
    String realservername = environmentSubstitute(serverName);
    String realserverpassword = Utils.resolvePassword(this, password);
    String realFtpDirectory = environmentSubstitute(ftpDirectory);
    int realserverport = Const.toInt(environmentSubstitute(port), 0);
    String realUsername = environmentSubstitute(userName);
    String realPassword = Utils.resolvePassword(this, password);
    String realproxyhost = environmentSubstitute(proxyHost);
    String realproxyusername = environmentSubstitute(proxyUsername);
    String realproxypassword = Utils.resolvePassword(this, proxyPassword);
    int realproxyport = Const.toInt(environmentSubstitute(proxyPort), 0);
    String realkeyFilename = environmentSubstitute(keyFilename);
    String realkeyPass = environmentSubstitute(keyFilePass);
    // PDI The following is used to apply a path for SSH because the SFTPv3Client doesn't let us specify/change dirs
    String sourceFolder = "";
    if (isDetailed()) {
        logDetailed(BaseMessages.getString(PKG, "JobEntryFTPDelete.Start"));
    }
    if (copyprevious && rows.size() == 0) {
        if (isDetailed()) {
            logDetailed(BaseMessages.getString(PKG, "JobEntryFTPDelete.ArgsFromPreviousNothing"));
        }
        result.setResult(true);
        return result;
    }
    try {
        // Get all the files in the current directory...
        String[] filelist = null;
        if (protocol.equals(PROTOCOL_FTP)) {
            // If socks proxy server was provided
            if (!Utils.isEmpty(socksProxyHost)) {
                if (!Utils.isEmpty(socksProxyPort)) {
                    FTPClient.initSOCKS(environmentSubstitute(socksProxyPort), environmentSubstitute(socksProxyHost));
                } else {
                    throw new FTPException(BaseMessages.getString(PKG, "JobEntryFTPDelete.SocksProxy.PortMissingException", environmentSubstitute(socksProxyHost), getName()));
                }
                // then if we have authentication information
                if (!Utils.isEmpty(socksProxyUsername) && !Utils.isEmpty(socksProxyPassword)) {
                    FTPClient.initSOCKSAuthentication(environmentSubstitute(socksProxyUsername), Utils.resolvePassword(this, socksProxyPassword));
                } else if (!Utils.isEmpty(socksProxyUsername) && Utils.isEmpty(socksProxyPassword) || Utils.isEmpty(socksProxyUsername) && !Utils.isEmpty(socksProxyPassword)) {
                    // we have a username without a password or vica versa
                    throw new FTPException(BaseMessages.getString(PKG, "JobEntryFTPDelete.SocksProxy.IncompleteCredentials", environmentSubstitute(socksProxyHost), getName()));
                }
            }
            // establish the connection
            FTPConnect(realservername, realUsername, realPassword, realserverport, realFtpDirectory, realproxyhost, realproxyusername, realproxypassword, realproxyport, timeout);
            filelist = ftpclient.dir();
            // CHECK THIS !!!
            if (filelist.length == 1) {
                String translatedWildcard = environmentSubstitute(wildcard);
                if (!Utils.isEmpty(translatedWildcard)) {
                    if (filelist[0].startsWith(translatedWildcard)) {
                        throw new FTPException(filelist[0]);
                    }
                }
            }
        } else if (protocol.equals(PROTOCOL_FTPS)) {
            // establish the secure connection
            FTPSConnect(realservername, realUsername, realserverport, realPassword, realFtpDirectory, timeout);
            // Get all the files in the current directory...
            filelist = ftpsclient.getFileNames();
        } else if (protocol.equals(PROTOCOL_SFTP)) {
            // establish the secure connection
            SFTPConnect(realservername, realUsername, realserverport, realPassword, realFtpDirectory);
            // Get all the files in the current directory...
            filelist = sftpclient.dir();
        } else if (protocol.equals(PROTOCOL_SSH)) {
            // establish the secure connection
            SSHConnect(realservername, realserverpassword, realserverport, realUsername, realPassword, realproxyhost, realproxyusername, realproxypassword, realproxyport, realkeyFilename, realkeyPass);
            sourceFolder = ".";
            if (realFtpDirectory != null) {
                sourceFolder = realFtpDirectory + "/";
            } else {
                sourceFolder = "./";
            }
            // NOTE: Source of the unchecked warning suppression for the declaration of this method.
            Vector<SFTPv3DirectoryEntry> vfilelist = sshclient.ls(sourceFolder);
            if (vfilelist != null) {
                // Make one pass through the vfilelist to get an accurate count
                // Using the two-pass method with arrays is faster than using ArrayList
                int fileCount = 0;
                Iterator<SFTPv3DirectoryEntry> iterator = vfilelist.iterator();
                while (iterator.hasNext()) {
                    SFTPv3DirectoryEntry dirEntry = iterator.next();
                    if (dirEntry != null && !dirEntry.filename.equals(".") && !dirEntry.filename.equals("..") && !isDirectory(sshclient, sourceFolder + dirEntry.filename)) {
                        fileCount++;
                    }
                }
                // Now that we have the correct count, create and fill in the array
                filelist = new String[fileCount];
                iterator = vfilelist.iterator();
                int i = 0;
                while (iterator.hasNext()) {
                    SFTPv3DirectoryEntry dirEntry = iterator.next();
                    if (dirEntry != null && !dirEntry.filename.equals(".") && !dirEntry.filename.equals("..") && !isDirectory(sshclient, sourceFolder + dirEntry.filename)) {
                        filelist[i] = dirEntry.filename;
                        i++;
                    }
                }
            }
        }
        if (isDetailed()) {
            logDetailed("JobEntryFTPDelete.FoundNFiles", String.valueOf(filelist.length));
        }
        int found = filelist == null ? 0 : filelist.length;
        if (found == 0) {
            result.setResult(true);
            return result;
        }
        Pattern pattern = null;
        if (copyprevious) {
            // 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 file_previous = resultRow.getString(0, null);
                if (!Utils.isEmpty(file_previous)) {
                    list_previous_files.add(file_previous);
                }
            }
        } else {
            if (!Utils.isEmpty(wildcard)) {
                String realWildcard = environmentSubstitute(wildcard);
                pattern = Pattern.compile(realWildcard);
            }
        }
        if (!getSuccessCondition().equals(SUCCESS_IF_ALL_FILES_DOWNLOADED)) {
            limitFiles = Const.toInt(environmentSubstitute(getLimitSuccess()), 10);
        }
        // Get the files in the list...
        for (int i = 0; i < filelist.length && !parentJob.isStopped(); i++) {
            if (successConditionBroken) {
                throw new Exception(BaseMessages.getString(PKG, "JobEntryFTPDelete.SuccesConditionBroken"));
            }
            boolean getIt = false;
            if (isDebug()) {
                logDebug(BaseMessages.getString(PKG, "JobEntryFTPDelete.AnalysingFile", filelist[i]));
            }
            try {
                // First see if the file matches the regular expression!
                if (copyprevious) {
                    if (list_previous_files.contains(filelist[i])) {
                        getIt = true;
                    }
                } else {
                    if (pattern != null) {
                        Matcher matcher = pattern.matcher(filelist[i]);
                        getIt = matcher.matches();
                    }
                }
                if (getIt) {
                    // Delete file
                    if (protocol.equals(PROTOCOL_FTP)) {
                        ftpclient.delete(filelist[i]);
                    }
                    if (protocol.equals(PROTOCOL_FTPS)) {
                        // System.out.println( "---------------" + filelist[i] );
                        ftpsclient.deleteFile(filelist[i]);
                    } else if (protocol.equals(PROTOCOL_SFTP)) {
                        sftpclient.delete(filelist[i]);
                    } else if (protocol.equals(PROTOCOL_SSH)) {
                        sshclient.rm(sourceFolder + filelist[i]);
                    }
                    if (isDetailed()) {
                        logDetailed("JobEntryFTPDelete.RemotefileDeleted", filelist[i]);
                    }
                    updateDeletedFiles();
                }
            } catch (Exception e) {
                // Update errors number
                updateErrors();
                logError(BaseMessages.getString(PKG, "JobFTP.UnexpectedError", e.getMessage()));
                if (successConditionBroken) {
                    throw new Exception(BaseMessages.getString(PKG, "JobEntryFTPDelete.SuccesConditionBroken"));
                }
            }
        }
    // end for
    } catch (Exception e) {
        updateErrors();
        logError(BaseMessages.getString(PKG, "JobEntryFTPDelete.ErrorGetting", e.getMessage()));
        logError(Const.getStackTracker(e));
    } finally {
        if (ftpclient != null && ftpclient.connected()) {
            try {
                ftpclient.quit();
                ftpclient = null;
            } catch (Exception e) {
                logError(BaseMessages.getString(PKG, "JobEntryFTPDelete.ErrorQuitting", e.getMessage()));
            }
        }
        if (ftpsclient != null) {
            try {
                ftpsclient.disconnect();
            } catch (Exception e) {
                logError(BaseMessages.getString(PKG, "JobEntryFTPDelete.ErrorQuitting", e.getMessage()));
            }
        }
        if (sftpclient != null) {
            try {
                sftpclient.disconnect();
                sftpclient = null;
            } catch (Exception e) {
                logError(BaseMessages.getString(PKG, "JobEntryFTPDelete.ErrorQuitting", e.getMessage()));
            }
        }
        if (sshclient != null) {
            try {
                sshclient.close();
                sshclient = null;
            } catch (Exception e) {
                logError(BaseMessages.getString(PKG, "JobEntryFTPDelete.ErrorQuitting", e.getMessage()));
            }
        }
        FTPClient.clearSOCKS();
    }
    result.setResult(!successConditionBroken);
    result.setNrFilesRetrieved(NrfilesDeleted);
    result.setNrErrors(NrErrors);
    return result;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) FTPException(com.enterprisedt.net.ftp.FTPException) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) Result(org.pentaho.di.core.Result) SFTPv3DirectoryEntry(com.trilead.ssh2.SFTPv3DirectoryEntry) RowMetaAndData(org.pentaho.di.core.RowMetaAndData) Iterator(java.util.Iterator) Vector(java.util.Vector) FTPException(com.enterprisedt.net.ftp.FTPException) HashSet(java.util.HashSet)

Example 5 with FTPException

use of com.enterprisedt.net.ftp.FTPException in project pentaho-kettle by pentaho.

the class JobEntryFTPPUT method createAndSetUpFtpClient.

// package-local visibility for testing purposes
FTPClient createAndSetUpFtpClient() throws IOException, FTPException {
    String realServerName = environmentSubstitute(serverName);
    String realServerPort = environmentSubstitute(serverPort);
    FTPClient ftpClient = createFtpClient();
    ftpClient.setRemoteAddr(InetAddress.getByName(realServerName));
    if (!Utils.isEmpty(realServerPort)) {
        ftpClient.setRemotePort(Const.toInt(realServerPort, FTP_DEFAULT_PORT));
    }
    if (!Utils.isEmpty(proxyHost)) {
        String realProxyHost = environmentSubstitute(proxyHost);
        ftpClient.setRemoteAddr(InetAddress.getByName(realProxyHost));
        if (log.isDetailed()) {
            logDetailed(BaseMessages.getString(PKG, "JobEntryFTPPUT.OpenedProxyConnectionOn", realProxyHost));
        }
        // FIXME: Proper default port for proxy
        int port = Const.toInt(environmentSubstitute(proxyPort), FTP_DEFAULT_PORT);
        if (port != 0) {
            ftpClient.setRemotePort(port);
        }
    } else {
        if (log.isDetailed()) {
            logDetailed(BaseMessages.getString(PKG, "JobEntryFTPPUT.OpenConnection", realServerName));
        }
    }
    // set activeConnection connectmode ...
    if (activeConnection) {
        ftpClient.setConnectMode(FTPConnectMode.ACTIVE);
        if (log.isDetailed()) {
            logDetailed(BaseMessages.getString(PKG, "JobFTPPUT.Log.SetActiveConnection"));
        }
    } else {
        ftpClient.setConnectMode(FTPConnectMode.PASV);
        if (log.isDetailed()) {
            logDetailed(BaseMessages.getString(PKG, "JobFTPPUT.Log.SetPassiveConnection"));
        }
    }
    // Set the timeout
    if (timeout > 0) {
        ftpClient.setTimeout(timeout);
        if (log.isDetailed()) {
            logDetailed(BaseMessages.getString(PKG, "JobFTPPUT.Log.SetTimeout", "" + timeout));
        }
    }
    ftpClient.setControlEncoding(controlEncoding);
    if (log.isDetailed()) {
        logDetailed(BaseMessages.getString(PKG, "JobFTPPUT.Log.SetEncoding", controlEncoding));
    }
    // If socks proxy server was provided
    if (!Utils.isEmpty(socksProxyHost)) {
        // if a port was provided
        if (!Utils.isEmpty(socksProxyPort)) {
            FTPClient.initSOCKS(environmentSubstitute(socksProxyPort), environmentSubstitute(socksProxyHost));
        } else {
            // looks like we have a host and no port
            throw new FTPException(BaseMessages.getString(PKG, "JobFTPPUT.SocksProxy.PortMissingException", environmentSubstitute(socksProxyHost)));
        }
        // now if we have authentication information
        if (!Utils.isEmpty(socksProxyUsername) && Utils.isEmpty(socksProxyPassword) || Utils.isEmpty(socksProxyUsername) && !Utils.isEmpty(socksProxyPassword)) {
            // we have a username without a password or vica versa
            throw new FTPException(BaseMessages.getString(PKG, "JobFTPPUT.SocksProxy.IncompleteCredentials", environmentSubstitute(socksProxyHost), getName()));
        }
    }
    return ftpClient;
}
Also used : FTPClient(com.enterprisedt.net.ftp.FTPClient) FTPException(com.enterprisedt.net.ftp.FTPException)

Aggregations

FTPException (com.enterprisedt.net.ftp.FTPException)6 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)5 KettleException (org.pentaho.di.core.exception.KettleException)5 KettleXMLException (org.pentaho.di.core.exception.KettleXMLException)5 IOException (java.io.IOException)4 FTPClient (com.enterprisedt.net.ftp.FTPClient)3 Matcher (java.util.regex.Matcher)3 Pattern (java.util.regex.Pattern)3 Result (org.pentaho.di.core.Result)3 FTPFileFactory (com.enterprisedt.net.ftp.FTPFileFactory)2 FTPFileParser (com.enterprisedt.net.ftp.FTPFileParser)2 UnknownHostException (java.net.UnknownHostException)2 VariableSpace (org.pentaho.di.core.variables.VariableSpace)2 FTPFile (com.enterprisedt.net.ftp.FTPFile)1 SFTPv3DirectoryEntry (com.trilead.ssh2.SFTPv3DirectoryEntry)1 File (java.io.File)1 URI (java.net.URI)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 Vector (java.util.Vector)1