Search in sources :

Example 1 with SFTPClient

use of org.pentaho.di.job.entries.sftp.SFTPClient in project pentaho-kettle by pentaho.

the class SFTPPutDialogTest method connectToSFTP_SeveralTimes_AlwaysReturnTrue.

@Test
public void connectToSFTP_SeveralTimes_AlwaysReturnTrue() throws Exception {
    SFTPClient sftp = mock(SFTPClient.class);
    SFTPPutDialog sod = new SFTPPutDialog(mock(Shell.class), new SFTPPutMeta(), mock(TransMeta.class), "SFTPPutDialogTest");
    SFTPPutDialog sodSpy = spy(sod);
    doReturn(sftp).when(sodSpy).createSFTPClient();
    assertTrue(sodSpy.connectToSFTP(false, null));
    assertTrue(sodSpy.connectToSFTP(false, null));
}
Also used : Shell(org.eclipse.swt.widgets.Shell) TransMeta(org.pentaho.di.trans.TransMeta) SFTPClient(org.pentaho.di.job.entries.sftp.SFTPClient) SFTPPutMeta(org.pentaho.di.trans.steps.sftpput.SFTPPutMeta) Test(org.junit.Test)

Example 2 with SFTPClient

use of org.pentaho.di.job.entries.sftp.SFTPClient in project pentaho-kettle by pentaho.

the class JobEntrySFTPPUTDialog method connectToSFTP.

private boolean connectToSFTP(boolean checkFolder, String Remotefoldername) {
    boolean retval = false;
    try {
        if (sftpclient == null) {
            // Create sftp client to host ...
            sftpclient = new SFTPClient(InetAddress.getByName(jobMeta.environmentSubstitute(wServerName.getText())), Const.toInt(jobMeta.environmentSubstitute(wServerPort.getText()), 22), jobMeta.environmentSubstitute(wUserName.getText()), jobMeta.environmentSubstitute(wKeyFilename.getText()), jobMeta.environmentSubstitute(wkeyfilePass.getText()));
            // Set proxy?
            String realProxyHost = jobMeta.environmentSubstitute(wProxyHost.getText());
            if (!Utils.isEmpty(realProxyHost)) {
                // Set proxy
                sftpclient.setProxy(realProxyHost, jobMeta.environmentSubstitute(wProxyPort.getText()), jobMeta.environmentSubstitute(wProxyUsername.getText()), jobMeta.environmentSubstitute(wProxyPassword.getText()), wProxyType.getText());
            }
            // login to ftp host ...
            sftpclient.login(Utils.resolvePassword(jobMeta, wPassword.getText()));
            retval = true;
        }
        if (checkFolder) {
            retval = sftpclient.folderExists(Remotefoldername);
        }
    } catch (Exception e) {
        if (sftpclient != null) {
            try {
                sftpclient.disconnect();
            } catch (Exception ignored) {
            // We've tried quitting the SFTP Client exception
            // nothing else to be done if the SFTP Client was already disconnected
            }
            sftpclient = null;
        }
        MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR);
        mb.setMessage(BaseMessages.getString(PKG, "JobSFTPPUT.ErrorConnect.NOK", wServerName.getText(), e.getMessage()) + Const.CR);
        mb.setText(BaseMessages.getString(PKG, "JobSFTPPUT.ErrorConnect.Title.Bad"));
        mb.open();
    }
    return retval;
}
Also used : SFTPClient(org.pentaho.di.job.entries.sftp.SFTPClient) MessageBox(org.eclipse.swt.widgets.MessageBox)

Example 3 with SFTPClient

use of org.pentaho.di.job.entries.sftp.SFTPClient in project pentaho-kettle by pentaho.

the class SFTPPutTest method setUp.

@Before
public void setUp() throws Exception {
    SFTPClient clientMock = mock(SFTPClient.class);
    step = StepMockUtil.getStep(SFTPPut.class, SFTPPutMeta.class, "mock step");
    step = spy(step);
    doReturn(clientMock).when(step).createSftpClient(anyString(), anyString(), anyString(), anyString(), anyString());
}
Also used : SFTPClient(org.pentaho.di.job.entries.sftp.SFTPClient) Before(org.junit.Before)

Example 4 with SFTPClient

use of org.pentaho.di.job.entries.sftp.SFTPClient in project pentaho-kettle by pentaho.

the class JobEntryFTPDelete method SFTPConnect.

private void SFTPConnect(String realservername, String realusername, int realport, String realpassword, String realFTPDirectory) throws Exception {
    // Create sftp client to host ...
    sftpclient = new SFTPClient(InetAddress.getByName(realservername), realport, realusername);
    // login to ftp host ...
    sftpclient.login(realpassword);
    // move to spool dir ...
    if (!Utils.isEmpty(realFTPDirectory)) {
        sftpclient.chdir(realFTPDirectory);
        if (isDetailed()) {
            logDetailed("Changed to directory [" + realFTPDirectory + "]");
        }
    }
}
Also used : SFTPClient(org.pentaho.di.job.entries.sftp.SFTPClient)

Example 5 with SFTPClient

use of org.pentaho.di.job.entries.sftp.SFTPClient in project pentaho-kettle by pentaho.

the class JobEntrySFTPPUT method execute.

public Result execute(Result previousResult, int nr) throws KettleException {
    Result result = previousResult;
    List<RowMetaAndData> rows = result.getRows();
    result.setResult(false);
    // Set Embedded NamedCluter MetatStore Provider Key so that it can be passed to VFS
    if (parentJobMeta.getNamedClusterEmbedManager() != null) {
        parentJobMeta.getNamedClusterEmbedManager().passEmbeddedMetastoreKey(this, parentJobMeta.getEmbeddedMetastoreProviderKey());
    }
    if (log.isDetailed()) {
        logDetailed(BaseMessages.getString(PKG, "JobSFTPPUT.Log.StartJobEntry"));
    }
    ArrayList<FileObject> myFileList = new ArrayList<FileObject>();
    if (copyprevious) {
        if (rows.size() == 0) {
            if (log.isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobSFTPPUT.ArgsFromPreviousNothing"));
            }
            result.setResult(true);
            return result;
        }
        try {
            RowMetaAndData resultRow = null;
            // 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)) {
                    FileObject file = KettleVFS.getFileObject(file_previous, this);
                    if (!file.exists()) {
                        logError(BaseMessages.getString(PKG, "JobSFTPPUT.Log.FilefromPreviousNotFound", file_previous));
                    } else {
                        myFileList.add(file);
                        if (log.isDebug()) {
                            logDebug(BaseMessages.getString(PKG, "JobSFTPPUT.Log.FilenameFromResult", file_previous));
                        }
                    }
                }
            }
        } catch (Exception e) {
            logError(BaseMessages.getString(PKG, "JobSFTPPUT.Error.ArgFromPrevious"));
            result.setNrErrors(1);
            // free resource
            myFileList = null;
            return result;
        }
    }
    if (copypreviousfiles) {
        List<ResultFile> resultFiles = result.getResultFilesList();
        if (resultFiles == null || resultFiles.size() == 0) {
            if (log.isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobSFTPPUT.ArgsFromPreviousNothingFiles"));
            }
            result.setResult(true);
            return result;
        }
        try {
            for (Iterator<ResultFile> it = resultFiles.iterator(); it.hasNext() && !parentJob.isStopped(); ) {
                ResultFile resultFile = it.next();
                FileObject file = resultFile.getFile();
                if (file != null) {
                    if (!file.exists()) {
                        logError(BaseMessages.getString(PKG, "JobSFTPPUT.Log.FilefromPreviousNotFound", file.toString()));
                    } else {
                        myFileList.add(file);
                        if (log.isDebug()) {
                            logDebug(BaseMessages.getString(PKG, "JobSFTPPUT.Log.FilenameFromResult", file.toString()));
                        }
                    }
                }
            }
        } catch (Exception e) {
            logError(BaseMessages.getString(PKG, "JobSFTPPUT.Error.ArgFromPrevious"));
            result.setNrErrors(1);
            // free resource
            myFileList = null;
            return result;
        }
    }
    SFTPClient sftpclient = null;
    // String substitution..
    String realServerName = environmentSubstitute(serverName);
    String realServerPort = environmentSubstitute(serverPort);
    String realUsername = environmentSubstitute(userName);
    String realPassword = Encr.decryptPasswordOptionallyEncrypted(environmentSubstitute(password));
    String realSftpDirString = environmentSubstitute(sftpDirectory);
    String realWildcard = environmentSubstitute(wildcard);
    String realLocalDirectory = environmentSubstitute(localDirectory);
    String realKeyFilename = null;
    String realPassPhrase = null;
    // Destination folder (Move to)
    String realDestinationFolder = environmentSubstitute(getDestinationFolder());
    try {
        if (getAfterFTPS() == AFTER_FTPSPUT_MOVE) {
            if (Utils.isEmpty(realDestinationFolder)) {
                logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.DestinatFolderMissing"));
                result.setNrErrors(1);
                return result;
            } else {
                FileObject folder = null;
                try {
                    folder = KettleVFS.getFileObject(realDestinationFolder, this);
                    // Let's check if folder exists...
                    if (!folder.exists()) {
                        // Do we need to create it?
                        if (createDestinationFolder) {
                            folder.createFolder();
                        } else {
                            logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.DestinatFolderNotExist", realDestinationFolder));
                            result.setNrErrors(1);
                            return result;
                        }
                    }
                    realDestinationFolder = KettleVFS.getFilename(folder);
                } catch (Exception e) {
                    throw new KettleException(e);
                } finally {
                    if (folder != null) {
                        try {
                            folder.close();
                        } catch (Exception e) {
                        /* Ignore */
                        }
                    }
                }
            }
        }
        if (isUseKeyFile()) {
            // We must have here a private keyfilename
            realKeyFilename = environmentSubstitute(getKeyFilename());
            if (Utils.isEmpty(realKeyFilename)) {
                // Error..Missing keyfile
                logError(BaseMessages.getString(PKG, "JobSFTP.Error.KeyFileMissing"));
                result.setNrErrors(1);
                return result;
            }
            if (!KettleVFS.fileExists(realKeyFilename)) {
                // Error.. can not reach keyfile
                logError(BaseMessages.getString(PKG, "JobSFTP.Error.KeyFileNotFound"));
                result.setNrErrors(1);
                return result;
            }
            realPassPhrase = environmentSubstitute(getKeyPassPhrase());
        }
        // Create sftp client to host ...
        sftpclient = new SFTPClient(InetAddress.getByName(realServerName), Const.toInt(realServerPort, 22), realUsername, realKeyFilename, realPassPhrase);
        if (log.isDetailed()) {
            logDetailed(BaseMessages.getString(PKG, "JobSFTPPUT.Log.OpenedConnection", realServerName, "" + realServerPort, realUsername));
        }
        // Set compression
        sftpclient.setCompression(getCompression());
        // Set proxy?
        String realProxyHost = environmentSubstitute(getProxyHost());
        if (!Utils.isEmpty(realProxyHost)) {
            // Set proxy
            sftpclient.setProxy(realProxyHost, environmentSubstitute(getProxyPort()), environmentSubstitute(getProxyUsername()), environmentSubstitute(getProxyPassword()), getProxyType());
        }
        // login to ftp host ...
        sftpclient.login(realPassword);
        // move to spool dir ...
        if (!Utils.isEmpty(realSftpDirString)) {
            boolean existfolder = sftpclient.folderExists(realSftpDirString);
            if (!existfolder) {
                if (!isCreateRemoteFolder()) {
                    throw new KettleException(BaseMessages.getString(PKG, "JobSFTPPUT.Error.CanNotFindRemoteFolder", realSftpDirString));
                }
                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "JobSFTPPUT.Error.CanNotFindRemoteFolder", realSftpDirString));
                }
                // Let's create folder
                sftpclient.createFolder(realSftpDirString);
                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "JobSFTPPUT.Log.RemoteFolderCreated", realSftpDirString));
                }
            }
            sftpclient.chdir(realSftpDirString);
            if (log.isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobSFTPPUT.Log.ChangedDirectory", realSftpDirString));
            }
        }
        if (!copyprevious && !copypreviousfiles) {
            // Get all the files in the local directory...
            myFileList = new ArrayList<FileObject>();
            FileObject localFiles = KettleVFS.getFileObject(realLocalDirectory, this);
            FileObject[] children = localFiles.getChildren();
            if (children != null) {
                for (int i = 0; i < children.length; i++) {
                    // Get filename of file or directory
                    if (children[i].getType().equals(FileType.FILE)) {
                        // myFileList.add(children[i].getAbsolutePath());
                        myFileList.add(children[i]);
                    }
                }
            // end for
            }
        }
        if (myFileList == null || myFileList.size() == 0) {
            if (isSuccessWhenNoFile()) {
                // Just warn user
                if (isBasic()) {
                    logBasic(BaseMessages.getString(PKG, "JobSFTPPUT.Error.NoFileToSend"));
                }
            } else {
                // Fail
                logError(BaseMessages.getString(PKG, "JobSFTPPUT.Error.NoFileToSend"));
                result.setNrErrors(1);
                return result;
            }
        }
        if (log.isDetailed()) {
            logDetailed(BaseMessages.getString(PKG, "JobSFTPPUT.Log.RowsFromPreviousResult", myFileList.size()));
        }
        Pattern pattern = null;
        if (!copyprevious && !copypreviousfiles) {
            if (!Utils.isEmpty(realWildcard)) {
                pattern = Pattern.compile(realWildcard);
            }
        }
        // Get the files in the list and execute sftp.put() for each file
        Iterator<FileObject> it = myFileList.iterator();
        while (it.hasNext() && !parentJob.isStopped()) {
            FileObject myFile = it.next();
            try {
                String localFilename = myFile.toString();
                String destinationFilename = myFile.getName().getBaseName();
                boolean getIt = true;
                // First see if the file matches the regular expression!
                if (pattern != null) {
                    Matcher matcher = pattern.matcher(destinationFilename);
                    getIt = matcher.matches();
                }
                if (getIt) {
                    if (log.isDebug()) {
                        logDebug(BaseMessages.getString(PKG, "JobSFTPPUT.Log.PuttingFile", localFilename, realSftpDirString));
                    }
                    sftpclient.put(myFile, destinationFilename);
                    if (log.isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "JobSFTPPUT.Log.TransferedFile", localFilename));
                    }
                    // what's next ...
                    switch(getAfterFTPS()) {
                        case AFTER_FTPSPUT_DELETE:
                            myFile.delete();
                            if (log.isDetailed()) {
                                logDetailed(BaseMessages.getString(PKG, "JobSFTPPUT.Log.DeletedFile", localFilename));
                            }
                            break;
                        case AFTER_FTPSPUT_MOVE:
                            FileObject destination = null;
                            try {
                                destination = KettleVFS.getFileObject(realDestinationFolder + Const.FILE_SEPARATOR + myFile.getName().getBaseName(), this);
                                myFile.moveTo(destination);
                                if (log.isDetailed()) {
                                    logDetailed(BaseMessages.getString(PKG, "JobSFTPPUT.Log.FileMoved", myFile, destination));
                                }
                            } finally {
                                if (destination != null) {
                                    destination.close();
                                }
                            }
                            break;
                        default:
                            if (addFilenameResut) {
                                // Add to the result files...
                                ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, myFile, parentJob.getJobname(), toString());
                                result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
                                if (log.isDetailed()) {
                                    logDetailed(BaseMessages.getString(PKG, "JobSFTPPUT.Log.FilenameAddedToResultFilenames", localFilename));
                                }
                            }
                            break;
                    }
                }
            } finally {
                if (myFile != null) {
                    myFile.close();
                }
            }
        }
        // end for
        result.setResult(true);
    // JKU: no idea if this is needed...!
    // result.setNrFilesRetrieved(filesRetrieved);
    } catch (Exception e) {
        result.setNrErrors(1);
        logError(BaseMessages.getString(PKG, "JobSFTPPUT.Exception", e.getMessage()));
        logError(Const.getStackTracker(e));
    } finally {
        // close connection, if possible
        try {
            if (sftpclient != null) {
                sftpclient.disconnect();
            }
        } catch (Exception e) {
        // just ignore this, makes no big difference
        }
        // end catch
        myFileList = null;
    }
    return result;
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) SFTPClient(org.pentaho.di.job.entries.sftp.SFTPClient) ResultFile(org.pentaho.di.core.ResultFile) 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) RowMetaAndData(org.pentaho.di.core.RowMetaAndData) FileObject(org.apache.commons.vfs2.FileObject)

Aggregations

SFTPClient (org.pentaho.di.job.entries.sftp.SFTPClient)9 MessageBox (org.eclipse.swt.widgets.MessageBox)3 Shell (org.eclipse.swt.widgets.Shell)2 Test (org.junit.Test)2 TransMeta (org.pentaho.di.trans.TransMeta)2 SFTPPutMeta (org.pentaho.di.trans.steps.sftpput.SFTPPutMeta)2 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 FileObject (org.apache.commons.vfs2.FileObject)1 Before (org.junit.Before)1 Result (org.pentaho.di.core.Result)1 ResultFile (org.pentaho.di.core.ResultFile)1 RowMetaAndData (org.pentaho.di.core.RowMetaAndData)1 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)1 KettleException (org.pentaho.di.core.exception.KettleException)1 KettleXMLException (org.pentaho.di.core.exception.KettleXMLException)1