Search in sources :

Example 6 with FTPClient

use of com.enterprisedt.net.ftp.FTPClient 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)

Example 7 with FTPClient

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

the class JobEntryFTPDialog method connectToFTP.

private boolean connectToFTP(boolean checkfolder, boolean checkmoveToFolder) {
    boolean retval = false;
    String realServername = null;
    try {
        if (ftpclient == null || !ftpclient.connected()) {
            // Create ftp client to host:port ...
            ftpclient = new FTPClient();
            realServername = jobMeta.environmentSubstitute(wServerName.getText());
            int realPort = Const.toInt(jobMeta.environmentSubstitute(wPort.getText()), 21);
            ftpclient.setRemoteAddr(InetAddress.getByName(realServername));
            ftpclient.setRemotePort(realPort);
            if (!Utils.isEmpty(wProxyHost.getText())) {
                String realProxy_host = jobMeta.environmentSubstitute(wProxyHost.getText());
                ftpclient.setRemoteAddr(InetAddress.getByName(realProxy_host));
                int port = Const.toInt(jobMeta.environmentSubstitute(wProxyPort.getText()), 21);
                if (port != 0) {
                    ftpclient.setRemotePort(port);
                }
            }
            // login to ftp host ...
            ftpclient.connect();
            String realUsername = jobMeta.environmentSubstitute(wUserName.getText()) + (!Utils.isEmpty(wProxyHost.getText()) ? "@" + realServername : "") + (!Utils.isEmpty(wProxyUsername.getText()) ? " " + jobMeta.environmentSubstitute(wProxyUsername.getText()) : "");
            String realPassword = Utils.resolvePassword(jobMeta, wPassword.getText()) + (!Utils.isEmpty(wProxyPassword.getText()) ? " " + Utils.resolvePassword(jobMeta, wProxyPassword.getText()) : "");
            // login now ...
            ftpclient.login(realUsername, realPassword);
            pwdFolder = ftpclient.pwd();
        }
        String realFtpDirectory = "";
        if (!Utils.isEmpty(wFtpDirectory.getText())) {
            realFtpDirectory = jobMeta.environmentSubstitute(wFtpDirectory.getText());
        }
        if (checkfolder) {
            if (pwdFolder != null) {
                ftpclient.chdir(pwdFolder);
            }
            // move to spool dir ...
            if (!Utils.isEmpty(realFtpDirectory)) {
                ftpclient.chdir(realFtpDirectory);
            }
        }
        if (checkmoveToFolder) {
            if (pwdFolder != null) {
                ftpclient.chdir(pwdFolder);
            }
            if (!Utils.isEmpty(wMoveToDirectory.getText())) {
                String realMoveDirectory = jobMeta.environmentSubstitute(wMoveToDirectory.getText());
                realMoveDirectory = realFtpDirectory + "/" + realMoveDirectory;
                ftpclient.chdir(realMoveDirectory);
            }
        }
        retval = true;
    } catch (Exception e) {
        MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR);
        mb.setMessage(BaseMessages.getString(PKG, "JobFTP.ErrorConnect.NOK", realServername, e.getMessage()) + Const.CR);
        mb.setText(BaseMessages.getString(PKG, "JobFTP.ErrorConnect.Title.Bad"));
        mb.open();
    }
    return retval;
}
Also used : FTPClient(com.enterprisedt.net.ftp.FTPClient) MessageBox(org.eclipse.swt.widgets.MessageBox)

Aggregations

FTPClient (com.enterprisedt.net.ftp.FTPClient)7 FTPException (com.enterprisedt.net.ftp.FTPException)3 MessageBox (org.eclipse.swt.widgets.MessageBox)3 IOException (java.io.IOException)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 Result (org.pentaho.di.core.Result)2 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)2 KettleException (org.pentaho.di.core.exception.KettleException)2 KettleXMLException (org.pentaho.di.core.exception.KettleXMLException)2 SFTPClient (org.pentaho.di.job.entries.sftp.SFTPClient)2 FTPFile (com.enterprisedt.net.ftp.FTPFile)1 File (java.io.File)1 URI (java.net.URI)1 UnknownHostException (java.net.UnknownHostException)1