Search in sources :

Example 6 with FTPException

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

the class JobEntryFTPPUT method execute.

public Result execute(Result previousResult, int nr) {
    Result result = previousResult;
    result.setResult(false);
    long filesput = 0;
    if (log.isDetailed()) {
        logDetailed(BaseMessages.getString(PKG, "JobFTPPUT.Log.Starting"));
    }
    FTPClient ftpclient = null;
    try {
        // Create ftp client to host:port ...
        ftpclient = createAndSetUpFtpClient();
        // login to ftp host ...
        String realUsername = environmentSubstitute(userName);
        String realPassword = Encr.decryptPasswordOptionallyEncrypted(environmentSubstitute(password));
        ftpclient.connect();
        ftpclient.login(realUsername, realPassword);
        // set BINARY
        if (binaryMode) {
            ftpclient.setType(FTPTransferType.BINARY);
            if (log.isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobFTPPUT.Log.BinaryMode"));
            }
        }
        // Remove password from logging, you don't know where it ends up.
        if (log.isDetailed()) {
            logDetailed(BaseMessages.getString(PKG, "JobFTPPUT.Log.Logged", realUsername));
        }
        // Fix for PDI-2534 - add auxilliary FTP File List parsers to the ftpclient object.
        this.hookInOtherParsers(ftpclient);
        // move to spool dir ...
        String realRemoteDirectory = environmentSubstitute(remoteDirectory);
        if (!Utils.isEmpty(realRemoteDirectory)) {
            ftpclient.chdir(realRemoteDirectory);
            if (log.isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobFTPPUT.Log.ChangedDirectory", realRemoteDirectory));
            }
        }
        String realLocalDirectory = environmentSubstitute(localDirectory);
        if (realLocalDirectory == null) {
            throw new FTPException(BaseMessages.getString(PKG, "JobFTPPUT.LocalDir.NotSpecified"));
        } else {
            // handle file:/// prefix
            if (realLocalDirectory.startsWith("file:")) {
                realLocalDirectory = new URI(realLocalDirectory).getPath();
            }
        }
        final List<String> files;
        File localFiles = new File(realLocalDirectory);
        File[] children = localFiles.listFiles();
        if (children == null) {
            files = Collections.emptyList();
        } else {
            files = new ArrayList<String>(children.length);
            for (File child : children) {
                // Get filename of file or directory
                if (!child.isDirectory()) {
                    files.add(child.getName());
                }
            }
        }
        if (log.isDetailed()) {
            logDetailed(BaseMessages.getString(PKG, "JobFTPPUT.Log.FoundFileLocalDirectory", "" + files.size(), realLocalDirectory));
        }
        String realWildcard = environmentSubstitute(wildcard);
        Pattern pattern;
        if (!Utils.isEmpty(realWildcard)) {
            pattern = Pattern.compile(realWildcard);
        } else {
            pattern = null;
        }
        for (String file : files) {
            if (parentJob.isStopped()) {
                break;
            }
            boolean toBeProcessed = true;
            // First see if the file matches the regular expression!
            if (pattern != null) {
                Matcher matcher = pattern.matcher(file);
                toBeProcessed = matcher.matches();
            }
            if (toBeProcessed) {
                // File exists?
                boolean fileExist = false;
                try {
                    fileExist = ftpclient.exists(file);
                } catch (Exception e) {
                // Assume file does not exist !!
                }
                if (log.isDebug()) {
                    if (fileExist) {
                        logDebug(BaseMessages.getString(PKG, "JobFTPPUT.Log.FileExists", file));
                    } else {
                        logDebug(BaseMessages.getString(PKG, "JobFTPPUT.Log.FileDoesNotExists", file));
                    }
                }
                if (!fileExist || !onlyPuttingNewFiles) {
                    if (log.isDebug()) {
                        logDebug(BaseMessages.getString(PKG, "JobFTPPUT.Log.PuttingFileToRemoteDirectory", file, realRemoteDirectory));
                    }
                    String localFilename = realLocalDirectory + Const.FILE_SEPARATOR + file;
                    ftpclient.put(localFilename, file);
                    filesput++;
                    // Delete the file if this is needed!
                    if (remove) {
                        new File(localFilename).delete();
                        if (log.isDetailed()) {
                            logDetailed(BaseMessages.getString(PKG, "JobFTPPUT.Log.DeletedFile", localFilename));
                        }
                    }
                }
            }
        }
        result.setResult(true);
        if (log.isDetailed()) {
            logDebug(BaseMessages.getString(PKG, "JobFTPPUT.Log.WeHavePut", "" + filesput));
        }
    } catch (Exception e) {
        result.setNrErrors(1);
        logError(BaseMessages.getString(PKG, "JobFTPPUT.Log.ErrorPuttingFiles", e.getMessage()));
        logError(Const.getStackTracker(e));
    } finally {
        if (ftpclient != null && ftpclient.connected()) {
            try {
                ftpclient.quit();
            } catch (Exception e) {
                logError(BaseMessages.getString(PKG, "JobFTPPUT.Log.ErrorQuitingFTP", e.getMessage()));
            }
        }
        FTPClient.clearSOCKS();
    }
    return result;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) URI(java.net.URI) FTPClient(com.enterprisedt.net.ftp.FTPClient) 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) Result(org.pentaho.di.core.Result) File(java.io.File) 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