Search in sources :

Example 1 with SFTPv3Client

use of ch.ethz.ssh2.SFTPv3Client in project pentaho-kettle by pentaho.

the class JobEntryFTPDeleteDialog method checkFTPFolder.

private void checkFTPFolder() {
    boolean folderexists = false;
    String errmsg = "";
    try {
        String realfoldername = jobMeta.environmentSubstitute(wFtpDirectory.getText());
        if (!Utils.isEmpty(realfoldername)) {
            if (connect()) {
                if (wProtocol.getText().equals(JobEntryFTPDelete.PROTOCOL_FTP)) {
                    ftpclient.chdir(pwdFolder);
                    ftpclient.chdir(realfoldername);
                    folderexists = true;
                }
                if (wProtocol.getText().equals(JobEntryFTPDelete.PROTOCOL_FTPS)) {
                    ftpsclient.changeDirectory(pwdFolder);
                    ftpsclient.changeDirectory(realfoldername);
                    folderexists = true;
                } else if (wProtocol.getText().equals(JobEntryFTPDelete.PROTOCOL_SFTP)) {
                    sftpclient.chdir(pwdFolder);
                    sftpclient.chdir(realfoldername);
                    folderexists = true;
                } else if (wProtocol.getText().equals(JobEntryFTPDelete.PROTOCOL_SSH)) {
                    SFTPv3Client client = new SFTPv3Client(conn);
                    boolean folderexist = sshDirectoryExists(client, realfoldername);
                    client.close();
                    if (folderexist) {
                        // Folder exists
                        folderexists = true;
                    } else {
                        // we can not find folder
                        folderexists = false;
                    }
                }
            }
        }
    } catch (Exception e) {
        errmsg = e.getMessage();
    }
    if (folderexists) {
        MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_INFORMATION);
        mb.setMessage(BaseMessages.getString(PKG, "JobFTPDelete.FolderExists.OK", wFtpDirectory.getText()) + Const.CR);
        mb.setText(BaseMessages.getString(PKG, "JobFTPDelete.FolderExists.Title.Ok"));
        mb.open();
    } else {
        MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR);
        mb.setMessage(BaseMessages.getString(PKG, "JobFTPDelete.FolderExists.NOK", wFtpDirectory.getText()) + Const.CR + errmsg);
        mb.setText(BaseMessages.getString(PKG, "JobFTPDelete.FolderExists.Title.Bad"));
        mb.open();
    }
}
Also used : SFTPv3Client(com.trilead.ssh2.SFTPv3Client) MessageBox(org.eclipse.swt.widgets.MessageBox)

Example 2 with SFTPv3Client

use of ch.ethz.ssh2.SFTPv3Client in project pentaho-kettle by pentaho.

the class JobEntrySSH2GET method execute.

@Override
public Result execute(Result previousResult, int nr) {
    Result result = previousResult;
    result.setResult(false);
    if (log.isRowLevel()) {
        logRowlevel(BaseMessages.getString(PKG, "JobSSH2GET.Log.GettingFieldsValue"));
    }
    // Get real variable value
    String realServerName = environmentSubstitute(serverName);
    int realServerPort = Const.toInt(environmentSubstitute(serverPort), 22);
    String realUserName = environmentSubstitute(userName);
    String realServerPassword = Encr.decryptPasswordOptionallyEncrypted(environmentSubstitute(password));
    // Proxy Host
    String realProxyHost = environmentSubstitute(httpProxyHost);
    int realProxyPort = Const.toInt(environmentSubstitute(httpproxyport), 22);
    String realproxyUserName = environmentSubstitute(httpproxyusername);
    String realProxyPassword = Encr.decryptPasswordOptionallyEncrypted(environmentSubstitute(httpProxyPassword));
    // Key file
    String realKeyFilename = environmentSubstitute(keyFilename);
    String relKeyFilepass = environmentSubstitute(keyFilePass);
    // target files
    String realLocalDirectory = environmentSubstitute(localDirectory);
    String realwildcard = environmentSubstitute(wildcard);
    // Remote source
    String realftpDirectory = environmentSubstitute(ftpDirectory);
    // Destination folder (Move to)
    String realDestinationFolder = environmentSubstitute(destinationfolder);
    try {
        // Remote source
        realftpDirectory = FTPUtils.normalizePath(realftpDirectory);
        // Destination folder (Move to)
        realDestinationFolder = FTPUtils.normalizePath(realDestinationFolder);
    } catch (Exception e) {
        logError(BaseMessages.getString(PKG, "JobSSH2GET.Log.CanNotNormalizePath", e.getMessage()));
        result.setNrErrors(1);
        return result;
    }
    // Check for mandatory fields
    if (log.isRowLevel()) {
        logRowlevel(BaseMessages.getString(PKG, "JobSSH2GET.Log.CheckingMandatoryFields"));
    }
    boolean mandatoryok = true;
    if (Utils.isEmpty(realServerName)) {
        mandatoryok = false;
        logError(BaseMessages.getString(PKG, "JobSSH2GET.Log.ServernameMissing"));
    }
    if (usehttpproxy) {
        if (Utils.isEmpty(realProxyHost)) {
            mandatoryok = false;
            logError(BaseMessages.getString(PKG, "JobSSH2GET.Log.HttpProxyhostMissing"));
        }
    }
    if (publicpublickey) {
        if (Utils.isEmpty(realKeyFilename)) {
            mandatoryok = false;
            logError(BaseMessages.getString(PKG, "JobSSH2GET.Log.KeyFileMissing"));
        } else {
            // Let's check if key file exists...
            if (!new File(realKeyFilename).exists()) {
                mandatoryok = false;
                logError(BaseMessages.getString(PKG, "JobSSH2GET.Log.KeyFileNotExist"));
            }
        }
    }
    if (Utils.isEmpty(realLocalDirectory)) {
        mandatoryok = false;
        logError(BaseMessages.getString(PKG, "JobSSH2GET.Log.LocalFolderMissing"));
    } else {
        // Check if target folder exists...
        if (!new File(realLocalDirectory).exists()) {
            if (createtargetfolder) {
                // Create Target folder
                if (!CreateFolder(realLocalDirectory)) {
                    mandatoryok = false;
                }
            } else {
                mandatoryok = false;
                logError(BaseMessages.getString(PKG, "JobSSH2GET.Log.LocalFolderNotExists", realLocalDirectory));
            }
        } else {
            if (!new File(realLocalDirectory).isDirectory()) {
                mandatoryok = false;
                logError(BaseMessages.getString(PKG, "JobSSH2GET.Log.LocalFolderNotFolder", realLocalDirectory));
            }
        }
    }
    if (afterFtpPut.equals("move_file")) {
        if (Utils.isEmpty(realDestinationFolder)) {
            mandatoryok = false;
            logError(BaseMessages.getString(PKG, "JobSSH2GET.Log.DestinatFolderMissing"));
        }
    }
    if (mandatoryok) {
        Connection conn = null;
        SFTPv3Client client = null;
        boolean good = true;
        try {
            // Create a connection instance
            conn = getConnection(realServerName, realServerPort, realProxyHost, realProxyPort, realproxyUserName, realProxyPassword);
            if (log.isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobSSH2GET.Log.ConnectionInstanceCreated"));
            }
            if (timeout > 0) {
                // Cache Host Key
                if (cachehostkey) {
                    conn.connect(new SimpleVerifier(database), 0, timeout * 1000);
                } else {
                    conn.connect(null, 0, timeout * 1000);
                }
            } else {
                // Cache Host Key
                if (cachehostkey) {
                    conn.connect(new SimpleVerifier(database));
                } else {
                    conn.connect();
                }
            }
            // Authenticate
            boolean isAuthenticated = false;
            if (publicpublickey) {
                isAuthenticated = conn.authenticateWithPublicKey(realUserName, new File(realKeyFilename), relKeyFilepass);
            } else {
                isAuthenticated = conn.authenticateWithPassword(realUserName, realServerPassword);
            }
            // LET'S CHECK AUTHENTICATION ...
            if (isAuthenticated == false) {
                logError(BaseMessages.getString(PKG, "JobSSH2GET.Log.AuthenticationFailed"));
            } else {
                if (log.isBasic()) {
                    logBasic(BaseMessages.getString(PKG, "JobSSH2GET.Log.Connected", serverName, userName));
                }
                client = new SFTPv3Client(conn);
                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "JobSSH2GET.Log.ProtocolVersion", "" + client.getProtocolVersion()));
                }
                // Check if ftp (source) directory exists
                if (!Utils.isEmpty(realftpDirectory)) {
                    if (!sshDirectoryExists(client, realftpDirectory)) {
                        good = false;
                        logError(BaseMessages.getString(PKG, "JobSSH2GET.Log.RemoteDirectoryNotExist", realftpDirectory));
                    } else if (log.isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "JobSSH2GET.Log.RemoteDirectoryExist", realftpDirectory));
                    }
                }
                if (!Utils.isEmpty(realDestinationFolder)) {
                    // Check now destination folder
                    if (!sshDirectoryExists(client, realDestinationFolder)) {
                        if (createdestinationfolder) {
                            if (!CreateRemoteFolder(client, realDestinationFolder)) {
                                good = false;
                            }
                        } else {
                            good = false;
                            logError(BaseMessages.getString(PKG, "JobSSH2GET.Log.DestinatFolderNotExist", realDestinationFolder));
                        }
                    }
                }
                if (good) {
                    Pattern pattern = null;
                    if (!Utils.isEmpty(realwildcard)) {
                        pattern = Pattern.compile(realwildcard);
                    }
                    if (includeSubFolders) {
                        if (log.isDetailed()) {
                            logDetailed(BaseMessages.getString(PKG, "JobSSH2GET.Log.RecursiveModeOn"));
                        }
                        copyRecursive(realftpDirectory, realLocalDirectory, client, pattern, parentJob);
                    } else {
                        if (log.isDetailed()) {
                            logDetailed(BaseMessages.getString(PKG, "JobSSH2GET.Log.RecursiveModeOff"));
                        }
                        GetFiles(realftpDirectory, realLocalDirectory, client, pattern, parentJob);
                    }
                    /**
                     ****************************** RESULT *******************
                     */
                    if (log.isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "JobSSH2GET.Log.Result.JobEntryEnd1"));
                        logDetailed(BaseMessages.getString(PKG, "JobSSH2GET.Log.Result.TotalFiles", "" + nbfilestoget));
                        logDetailed(BaseMessages.getString(PKG, "JobSSH2GET.Log.Result.TotalFilesPut", "" + nbgot));
                        logDetailed(BaseMessages.getString(PKG, "JobSSH2GET.Log.Result.TotalFilesError", "" + nbrerror));
                        logDetailed(BaseMessages.getString(PKG, "JobSSH2GET.Log.Result.JobEntryEnd2"));
                    }
                    if (nbrerror == 0) {
                        result.setResult(true);
                    /**
                     ****************************** RESULT *******************
                     */
                    }
                }
            }
        } catch (Exception e) {
            result.setNrErrors(nbrerror);
            logError(BaseMessages.getString(PKG, "JobSSH2GET.Log.Error.ErrorFTP", e.getMessage()));
        } finally {
            if (conn != null) {
                conn.close();
            }
            if (client != null) {
                client.close();
            }
        }
    }
    return result;
}
Also used : Pattern(java.util.regex.Pattern) Connection(com.trilead.ssh2.Connection) SFTPv3Client(com.trilead.ssh2.SFTPv3Client) File(java.io.File) 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)

Example 3 with SFTPv3Client

use of ch.ethz.ssh2.SFTPv3Client in project pentaho-kettle by pentaho.

the class JobEntrySSH2GETDialog method checkFTPFolder.

private void checkFTPFolder() {
    boolean folderexists = false;
    String errmsg = "";
    try {
        String realfoldername = jobMeta.environmentSubstitute(wFtpDirectory.getText());
        if (!Utils.isEmpty(realfoldername)) {
            if (connect()) {
                SFTPv3Client client = new SFTPv3Client(conn);
                boolean folderexist = sshDirectoryExists(client, realfoldername);
                client.close();
                if (folderexist) {
                    // Folder exists
                    folderexists = true;
                } else {
                    // we cannot find folder
                    folderexists = false;
                }
            }
        }
    } catch (Exception e) {
        errmsg = e.getMessage();
    }
    if (folderexists) {
        MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_INFORMATION);
        mb.setMessage(BaseMessages.getString(PKG, "JobSSH2GET.FolderExists.OK", wFtpDirectory.getText()) + Const.CR);
        mb.setText(BaseMessages.getString(PKG, "JobSSH2GET.FolderExists.Title.Ok"));
        mb.open();
    } else {
        MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR);
        mb.setMessage(BaseMessages.getString(PKG, "JobSSH2GET.FolderExists.NOK", wFtpDirectory.getText()) + Const.CR + errmsg);
        mb.setText(BaseMessages.getString(PKG, "JobSSH2GET.FolderExists.Title.Bad"));
        mb.open();
    }
}
Also used : SFTPv3Client(com.trilead.ssh2.SFTPv3Client) MessageBox(org.eclipse.swt.widgets.MessageBox)

Example 4 with SFTPv3Client

use of ch.ethz.ssh2.SFTPv3Client in project pentaho-kettle by pentaho.

the class JobEntrySSH2PUTDialog method checkFTPFolder.

private void checkFTPFolder() {
    String realfoldername = jobMeta.environmentSubstitute(wFtpDirectory.getText());
    if (!Utils.isEmpty(realfoldername)) {
        boolean folderexists = false;
        String errmsg = "";
        try {
            if (connect()) {
                SFTPv3Client client = new SFTPv3Client(conn);
                boolean folderexist = sshDirectoryExists(client, realfoldername);
                client.close();
                if (folderexist) {
                    // Folder exists
                    folderexists = true;
                } else {
                    // we cannot find folder
                    folderexists = false;
                }
            }
        } catch (Exception e) {
            errmsg = e.getMessage();
        }
        if (folderexists) {
            MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_INFORMATION);
            mb.setMessage(BaseMessages.getString(PKG, "JobSSH2PUT.FolderExists.OK", wFtpDirectory.getText()) + Const.CR);
            mb.setText(BaseMessages.getString(PKG, "JobSSH2PUT.FolderExists.Title.Ok"));
            mb.open();
        } else {
            MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR);
            mb.setMessage(BaseMessages.getString(PKG, "JobSSH2PUT.FolderExists.NOK", wFtpDirectory.getText()) + Const.CR + errmsg);
            mb.setText(BaseMessages.getString(PKG, "JobSSH2PUT.FolderExists.Title.Bad"));
            mb.open();
        }
    }
}
Also used : SFTPv3Client(com.trilead.ssh2.SFTPv3Client) MessageBox(org.eclipse.swt.widgets.MessageBox)

Example 5 with SFTPv3Client

use of ch.ethz.ssh2.SFTPv3Client in project agileway by fangjinuo.

the class Ssh2Connection method openSftpSession.

@Override
public SftpSession openSftpSession() throws SshException {
    try {
        SFTPv3Client sftpClient = new SFTPv3Client(this.delegate);
        Ssh2SftpSession session = new Ssh2SftpSession(sftpClient);
        session.setSshConnection(this);
        return session;
    } catch (Throwable ex) {
        throw new SshException(ex.getMessage(), ex);
    }
}
Also used : Ssh2SftpSession(com.jn.agileway.ssh.client.impl.ganymedssh2.sftp.Ssh2SftpSession) SFTPv3Client(ch.ethz.ssh2.SFTPv3Client) SshException(com.jn.agileway.ssh.client.SshException)

Aggregations

SFTPv3Client (com.trilead.ssh2.SFTPv3Client)7 Connection (com.trilead.ssh2.Connection)3 MessageBox (org.eclipse.swt.widgets.MessageBox)3 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)3 KettleException (org.pentaho.di.core.exception.KettleException)3 KettleXMLException (org.pentaho.di.core.exception.KettleXMLException)3 SshException (com.jn.agileway.ssh.client.SshException)2 File (java.io.File)2 Pattern (java.util.regex.Pattern)2 Result (org.pentaho.di.core.Result)2 SFTPv3Client (ch.ethz.ssh2.SFTPv3Client)1 FTPException (com.enterprisedt.net.ftp.FTPException)1 Ssh2SftpSession (com.jn.agileway.ssh.client.impl.ganymedssh2.sftp.Ssh2SftpSession)1 Ssh2SftpSession (com.jn.agileway.ssh.client.impl.trileadssh2.sftp.Ssh2SftpSession)1 HTTPProxyData (com.trilead.ssh2.HTTPProxyData)1 IOException (java.io.IOException)1 Matcher (java.util.regex.Matcher)1 FileObject (org.apache.commons.vfs2.FileObject)1 KettleFileException (org.pentaho.di.core.exception.KettleFileException)1 FTPSConnection (org.pentaho.di.job.entries.ftpsget.FTPSConnection)1