Search in sources :

Example 6 with SFTPv3Client

use of com.trilead.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 7 with SFTPv3Client

use of com.trilead.ssh2.SFTPv3Client in project pentaho-kettle by pentaho.

the class JobEntrySSH2GET method GetFiles.

/**
 * copy a directory from the remote host to the local one.
 *
 * @param sourceLocation
 *          the source directory on the remote host
 * @param targetLocation
 *          the target directory on the local host
 * @param sftpClient
 *          is an instance of SFTPv3Client that makes SFTP client connection over SSH-2
 * @return the number of files successfully copied
 * @throws Exception
 */
@SuppressWarnings("unchecked")
private void GetFiles(String sourceLocation, String targetLocation, SFTPv3Client sftpClient, Pattern pattern, Job parentJob) throws Exception {
    String sourceFolder = ".";
    if (!Utils.isEmpty(sourceLocation)) {
        sourceFolder = sourceLocation + FTPUtils.FILE_SEPARATOR;
    } else {
        sourceFolder += FTPUtils.FILE_SEPARATOR;
    }
    Vector<SFTPv3DirectoryEntry> filelist = sftpClient.ls(sourceFolder);
    if (filelist != null) {
        Iterator<SFTPv3DirectoryEntry> iterator = filelist.iterator();
        while (iterator.hasNext() && !parentJob.isStopped()) {
            SFTPv3DirectoryEntry dirEntry = iterator.next();
            if (dirEntry == null) {
                continue;
            }
            if (dirEntry.filename.equals(".") || dirEntry.filename.equals("..") || isDirectory(sftpClient, sourceFolder + dirEntry.filename)) {
                continue;
            }
            if (getFileWildcard(dirEntry.filename, pattern)) {
                // Copy file from remote host
                copyFile(sourceFolder + dirEntry.filename, targetLocation + FTPUtils.FILE_SEPARATOR + dirEntry.filename, sftpClient);
            }
        }
    }
}
Also used : SFTPv3DirectoryEntry(com.trilead.ssh2.SFTPv3DirectoryEntry)

Example 8 with SFTPv3Client

use of com.trilead.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 9 with SFTPv3Client

use of com.trilead.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 10 with SFTPv3Client

use of com.trilead.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)

Aggregations

TypesWriter (com.trilead.ssh2.packets.TypesWriter)8 SFTPv3Client (com.trilead.ssh2.SFTPv3Client)6 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)6 KettleException (org.pentaho.di.core.exception.KettleException)6 KettleXMLException (org.pentaho.di.core.exception.KettleXMLException)6 Connection (com.trilead.ssh2.Connection)3 SFTPv3DirectoryEntry (com.trilead.ssh2.SFTPv3DirectoryEntry)3 File (java.io.File)3 Pattern (java.util.regex.Pattern)3 MessageBox (org.eclipse.swt.widgets.MessageBox)3 Result (org.pentaho.di.core.Result)3 FTPException (com.enterprisedt.net.ftp.FTPException)2 SFTPv3FileHandle (com.trilead.ssh2.SFTPv3FileHandle)2 TypesReader (com.trilead.ssh2.packets.TypesReader)2 IOException (java.io.IOException)2 Matcher (java.util.regex.Matcher)2 KettleFileException (org.pentaho.di.core.exception.KettleFileException)2 HTTPProxyData (com.trilead.ssh2.HTTPProxyData)1 BufferedInputStream (java.io.BufferedInputStream)1 FileOutputStream (java.io.FileOutputStream)1