Example usage for org.apache.commons.vfs FileType FILE

List of usage examples for org.apache.commons.vfs FileType FILE

Introduction

In this page you can find the example usage for org.apache.commons.vfs FileType FILE.

Prototype

FileType FILE

To view the source code for org.apache.commons.vfs FileType FILE.

Click Source Link

Document

A regular file.

Usage

From source file:org.pentaho.di.job.entries.mail.JobEntryMail.java

public Result execute(Result result, int nr) {
    File masterZipfile = null;//from  w  w w .  j  av a 2s. co  m

    // Send an e-mail...
    // create some properties and get the default Session
    Properties props = new Properties();
    if (Const.isEmpty(server)) {
        logError(BaseMessages.getString(PKG, "JobMail.Error.HostNotSpecified"));

        result.setNrErrors(1L);
        result.setResult(false);
        return result;
    }

    String protocol = "smtp";
    if (usingSecureAuthentication) {
        if (secureConnectionType.equals("TLS")) {
            // Allow TLS authentication
            props.put("mail.smtp.starttls.enable", "true");
        } else {

            protocol = "smtps";
            // required to get rid of a SSL exception :
            // nested exception is:
            // javax.net.ssl.SSLException: Unsupported record version Unknown
            props.put("mail.smtps.quitwait", "false");
        }

    }

    props.put("mail." + protocol + ".host", environmentSubstitute(server));
    if (!Const.isEmpty(port)) {
        props.put("mail." + protocol + ".port", environmentSubstitute(port));
    }

    if (log.isDebug()) {
        props.put("mail.debug", "true");
    }

    if (usingAuthentication) {
        props.put("mail." + protocol + ".auth", "true");

        /*
         * authenticator = new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new
         * PasswordAuthentication( StringUtil.environmentSubstitute(Const.NVL(authenticationUser, "")),
         * StringUtil.environmentSubstitute(Const.NVL(authenticationPassword, "")) ); } };
         */
    }

    Session session = Session.getInstance(props);
    session.setDebug(log.isDebug());

    try {
        // create a message
        Message msg = new MimeMessage(session);

        // set message priority
        if (usePriority) {
            String priority_int = "1";
            if (priority.equals("low")) {
                priority_int = "3";
            }
            if (priority.equals("normal")) {
                priority_int = "2";
            }

            msg.setHeader("X-Priority", priority_int); // (String)int between 1= high and 3 = low.
            msg.setHeader("Importance", importance);
            // seems to be needed for MS Outlook.
            // where it returns a string of high /normal /low.
            msg.setHeader("Sensitivity", sensitivity);
            // Possible values are normal, personal, private, company-confidential

        }

        // Set Mail sender (From)
        String sender_address = environmentSubstitute(replyAddress);
        if (!Const.isEmpty(sender_address)) {
            String sender_name = environmentSubstitute(replyName);
            if (!Const.isEmpty(sender_name)) {
                sender_address = sender_name + '<' + sender_address + '>';
            }
            msg.setFrom(new InternetAddress(sender_address));
        } else {
            throw new MessagingException(BaseMessages.getString(PKG, "JobMail.Error.ReplyEmailNotFilled"));
        }

        // set Reply to addresses
        String reply_to_address = environmentSubstitute(replyToAddresses);
        if (!Const.isEmpty(reply_to_address)) {
            // Split the mail-address: space separated
            String[] reply_Address_List = environmentSubstitute(reply_to_address).split(" ");
            InternetAddress[] address = new InternetAddress[reply_Address_List.length];
            for (int i = 0; i < reply_Address_List.length; i++) {
                address[i] = new InternetAddress(reply_Address_List[i]);
            }
            msg.setReplyTo(address);
        }

        // Split the mail-address: space separated
        String[] destinations = environmentSubstitute(destination).split(" ");
        InternetAddress[] address = new InternetAddress[destinations.length];
        for (int i = 0; i < destinations.length; i++) {
            address[i] = new InternetAddress(destinations[i]);
        }
        msg.setRecipients(Message.RecipientType.TO, address);

        String realCC = environmentSubstitute(getDestinationCc());
        if (!Const.isEmpty(realCC)) {
            // Split the mail-address Cc: space separated
            String[] destinationsCc = realCC.split(" ");
            InternetAddress[] addressCc = new InternetAddress[destinationsCc.length];
            for (int i = 0; i < destinationsCc.length; i++) {
                addressCc[i] = new InternetAddress(destinationsCc[i]);
            }

            msg.setRecipients(Message.RecipientType.CC, addressCc);
        }

        String realBCc = environmentSubstitute(getDestinationBCc());
        if (!Const.isEmpty(realBCc)) {
            // Split the mail-address BCc: space separated
            String[] destinationsBCc = realBCc.split(" ");
            InternetAddress[] addressBCc = new InternetAddress[destinationsBCc.length];
            for (int i = 0; i < destinationsBCc.length; i++) {
                addressBCc[i] = new InternetAddress(destinationsBCc[i]);
            }

            msg.setRecipients(Message.RecipientType.BCC, addressBCc);
        }
        String realSubject = environmentSubstitute(subject);
        if (!Const.isEmpty(realSubject)) {
            msg.setSubject(realSubject);
        }

        msg.setSentDate(new Date());
        StringBuffer messageText = new StringBuffer();
        String endRow = isUseHTML() ? "<br>" : Const.CR;
        String realComment = environmentSubstitute(comment);
        if (!Const.isEmpty(realComment)) {
            messageText.append(realComment).append(Const.CR).append(Const.CR);
        }
        if (!onlySendComment) {

            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.Job")).append(endRow);
            messageText.append("-----").append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.JobName") + "    : ")
                    .append(parentJob.getJobMeta().getName()).append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.JobDirectory") + "  : ")
                    .append(parentJob.getJobMeta().getRepositoryDirectory()).append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.JobEntry") + "   : ")
                    .append(getName()).append(endRow);
            messageText.append(Const.CR);
        }

        if (includeDate) {
            messageText.append(endRow).append(BaseMessages.getString(PKG, "JobMail.Log.Comment.MsgDate") + ": ")
                    .append(XMLHandler.date2string(new Date())).append(endRow).append(endRow);
        }
        if (!onlySendComment && result != null) {
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.PreviousResult") + ":")
                    .append(endRow);
            messageText.append("-----------------").append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.JobEntryNr") + "         : ")
                    .append(result.getEntryNr()).append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.Errors") + "               : ")
                    .append(result.getNrErrors()).append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.LinesRead") + "           : ")
                    .append(result.getNrLinesRead()).append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.LinesWritten") + "        : ")
                    .append(result.getNrLinesWritten()).append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.LinesInput") + "          : ")
                    .append(result.getNrLinesInput()).append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.LinesOutput") + "         : ")
                    .append(result.getNrLinesOutput()).append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.LinesUpdated") + "        : ")
                    .append(result.getNrLinesUpdated()).append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.LinesRejected") + "       : ")
                    .append(result.getNrLinesRejected()).append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.Status") + "  : ")
                    .append(result.getExitStatus()).append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.Result") + "               : ")
                    .append(result.getResult()).append(endRow);
            messageText.append(endRow);
        }

        if (!onlySendComment && (!Const.isEmpty(environmentSubstitute(contactPerson))
                || !Const.isEmpty(environmentSubstitute(contactPhone)))) {
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.ContactInfo") + " :")
                    .append(endRow);
            messageText.append("---------------------").append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.PersonToContact") + " : ")
                    .append(environmentSubstitute(contactPerson)).append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.Tel") + "  : ")
                    .append(environmentSubstitute(contactPhone)).append(endRow);
            messageText.append(endRow);
        }

        // Include the path to this job entry...
        if (!onlySendComment) {
            JobTracker jobTracker = parentJob.getJobTracker();
            if (jobTracker != null) {
                messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.PathToJobentry") + ":")
                        .append(endRow);
                messageText.append("------------------------").append(endRow);

                addBacktracking(jobTracker, messageText);
                if (isUseHTML()) {
                    messageText.replace(0, messageText.length(),
                            messageText.toString().replace(Const.CR, endRow));
                }
            }
        }

        MimeMultipart parts = new MimeMultipart();
        MimeBodyPart part1 = new MimeBodyPart(); // put the text in the
        // Attached files counter
        int nrattachedFiles = 0;

        // 1st part

        if (useHTML) {
            if (!Const.isEmpty(getEncoding())) {
                part1.setContent(messageText.toString(), "text/html; " + "charset=" + getEncoding());
            } else {
                part1.setContent(messageText.toString(), "text/html; " + "charset=ISO-8859-1");
            }
        } else {
            part1.setText(messageText.toString());
        }

        parts.addBodyPart(part1);

        if (includingFiles && result != null) {
            List<ResultFile> resultFiles = result.getResultFilesList();
            if (resultFiles != null && !resultFiles.isEmpty()) {
                if (!zipFiles) {
                    // Add all files to the message...
                    //
                    for (ResultFile resultFile : resultFiles) {
                        FileObject file = resultFile.getFile();
                        if (file != null && file.exists()) {
                            boolean found = false;
                            for (int i = 0; i < fileType.length; i++) {
                                if (fileType[i] == resultFile.getType()) {
                                    found = true;
                                }
                            }
                            if (found) {
                                // create a data source
                                MimeBodyPart files = new MimeBodyPart();
                                URLDataSource fds = new URLDataSource(file.getURL());

                                // get a data Handler to manipulate this file type;
                                files.setDataHandler(new DataHandler(fds));
                                // include the file in the data source
                                files.setFileName(file.getName().getBaseName());

                                // insist on base64 to preserve line endings
                                files.addHeader("Content-Transfer-Encoding", "base64");

                                // add the part with the file in the BodyPart();
                                parts.addBodyPart(files);
                                nrattachedFiles++;
                                logBasic("Added file '" + fds.getName() + "' to the mail message.");
                            }
                        }
                    }
                } else {
                    // create a single ZIP archive of all files
                    masterZipfile = new File(System.getProperty("java.io.tmpdir") + Const.FILE_SEPARATOR
                            + environmentSubstitute(zipFilename));
                    ZipOutputStream zipOutputStream = null;
                    try {
                        zipOutputStream = new ZipOutputStream(new FileOutputStream(masterZipfile));

                        for (ResultFile resultFile : resultFiles) {
                            boolean found = false;
                            for (int i = 0; i < fileType.length; i++) {
                                if (fileType[i] == resultFile.getType()) {
                                    found = true;
                                }
                            }
                            if (found) {
                                FileObject file = resultFile.getFile();
                                ZipEntry zipEntry = new ZipEntry(file.getName().getBaseName());
                                zipOutputStream.putNextEntry(zipEntry);

                                // Now put the content of this file into this archive...
                                BufferedInputStream inputStream = new BufferedInputStream(
                                        KettleVFS.getInputStream(file));
                                try {
                                    int c;
                                    while ((c = inputStream.read()) >= 0) {
                                        zipOutputStream.write(c);
                                    }
                                } finally {
                                    inputStream.close();
                                }
                                zipOutputStream.closeEntry();
                                nrattachedFiles++;
                                logBasic("Added file '" + file.getName().getURI()
                                        + "' to the mail message in a zip archive.");
                            }
                        }
                    } catch (Exception e) {
                        logError("Error zipping attachement files into file [" + masterZipfile.getPath()
                                + "] : " + e.toString());
                        logError(Const.getStackTracker(e));
                        result.setNrErrors(1);
                    } finally {
                        if (zipOutputStream != null) {
                            try {
                                zipOutputStream.finish();
                                zipOutputStream.close();
                            } catch (IOException e) {
                                logError("Unable to close attachement zip file archive : " + e.toString());
                                logError(Const.getStackTracker(e));
                                result.setNrErrors(1);
                            }
                        }
                    }

                    // Now attach the master zip file to the message.
                    if (result.getNrErrors() == 0) {
                        // create a data source
                        MimeBodyPart files = new MimeBodyPart();
                        FileDataSource fds = new FileDataSource(masterZipfile);
                        // get a data Handler to manipulate this file type;
                        files.setDataHandler(new DataHandler(fds));
                        // include the file in the data source
                        files.setFileName(fds.getName());
                        // add the part with the file in the BodyPart();
                        parts.addBodyPart(files);
                    }
                }
            }
        }

        int nrEmbeddedImages = 0;
        if (embeddedimages != null && embeddedimages.length > 0) {
            FileObject imageFile = null;
            for (int i = 0; i < embeddedimages.length; i++) {
                String realImageFile = environmentSubstitute(embeddedimages[i]);
                String realcontenID = environmentSubstitute(contentids[i]);
                if (messageText.indexOf("cid:" + realcontenID) < 0) {
                    if (log.isDebug()) {
                        log.logDebug("Image [" + realImageFile + "] is not used in message body!");
                    }
                } else {
                    try {
                        boolean found = false;
                        imageFile = KettleVFS.getFileObject(realImageFile, this);
                        if (imageFile.exists() && imageFile.getType() == FileType.FILE) {
                            found = true;
                        } else {
                            log.logError("We can not find [" + realImageFile + "] or it is not a file");
                        }
                        if (found) {
                            // Create part for the image
                            MimeBodyPart messageBodyPart = new MimeBodyPart();
                            // Load the image
                            URLDataSource fds = new URLDataSource(imageFile.getURL());
                            messageBodyPart.setDataHandler(new DataHandler(fds));
                            // Setting the header
                            messageBodyPart.setHeader("Content-ID", "<" + realcontenID + ">");
                            // Add part to multi-part
                            parts.addBodyPart(messageBodyPart);
                            nrEmbeddedImages++;
                            log.logBasic("Image '" + fds.getName() + "' was embedded in message.");
                        }
                    } catch (Exception e) {
                        log.logError(
                                "Error embedding image [" + realImageFile + "] in message : " + e.toString());
                        log.logError(Const.getStackTracker(e));
                        result.setNrErrors(1);
                    } finally {
                        if (imageFile != null) {
                            try {
                                imageFile.close();
                            } catch (Exception e) { /* Ignore */
                            }
                        }
                    }
                }
            }
        }

        if (nrEmbeddedImages > 0 && nrattachedFiles == 0) {
            // If we need to embedd images...
            // We need to create a "multipart/related" message.
            // otherwise image will appear as attached file
            parts.setSubType("related");
        }
        // put all parts together
        msg.setContent(parts);

        Transport transport = null;
        try {
            transport = session.getTransport(protocol);
            String authPass = getPassword(authenticationPassword);

            if (usingAuthentication) {
                if (!Const.isEmpty(port)) {
                    transport.connect(environmentSubstitute(Const.NVL(server, "")),
                            Integer.parseInt(environmentSubstitute(Const.NVL(port, ""))),
                            environmentSubstitute(Const.NVL(authenticationUser, "")), authPass);
                } else {
                    transport.connect(environmentSubstitute(Const.NVL(server, "")),
                            environmentSubstitute(Const.NVL(authenticationUser, "")), authPass);
                }
            } else {
                transport.connect();
            }
            transport.sendMessage(msg, msg.getAllRecipients());
        } finally {
            if (transport != null) {
                transport.close();
            }
        }
    } catch (IOException e) {
        logError("Problem while sending message: " + e.toString());
        result.setNrErrors(1);
    } catch (MessagingException mex) {
        logError("Problem while sending message: " + mex.toString());
        result.setNrErrors(1);

        Exception ex = mex;
        do {
            if (ex instanceof SendFailedException) {
                SendFailedException sfex = (SendFailedException) ex;

                Address[] invalid = sfex.getInvalidAddresses();
                if (invalid != null) {
                    logError("    ** Invalid Addresses");
                    for (int i = 0; i < invalid.length; i++) {
                        logError("         " + invalid[i]);
                        result.setNrErrors(1);
                    }
                }

                Address[] validUnsent = sfex.getValidUnsentAddresses();
                if (validUnsent != null) {
                    logError("    ** ValidUnsent Addresses");
                    for (int i = 0; i < validUnsent.length; i++) {
                        logError("         " + validUnsent[i]);
                        result.setNrErrors(1);
                    }
                }

                Address[] validSent = sfex.getValidSentAddresses();
                if (validSent != null) {
                    // System.out.println("    ** ValidSent Addresses");
                    for (int i = 0; i < validSent.length; i++) {
                        logError("         " + validSent[i]);
                        result.setNrErrors(1);
                    }
                }
            }
            if (ex instanceof MessagingException) {
                ex = ((MessagingException) ex).getNextException();
            } else {
                ex = null;
            }
        } while (ex != null);
    } finally {
        if (masterZipfile != null && masterZipfile.exists()) {
            masterZipfile.delete();
        }
    }

    if (result.getNrErrors() > 0) {
        result.setResult(false);
    } else {
        result.setResult(true);
    }

    return result;
}

From source file:org.pentaho.di.job.entries.movefiles.JobEntryMoveFiles.java

private boolean ProcessFileFolder(String sourcefilefoldername, String destinationfilefoldername,
        String wildcard, Job parentJob, Result result, String MoveToFolder) {
    boolean entrystatus = false;
    FileObject sourcefilefolder = null;
    FileObject destinationfilefolder = null;
    FileObject movetofolderfolder = null;
    FileObject Currentfile = null;

    // Get real source, destination file and wildcard
    String realSourceFilefoldername = environmentSubstitute(sourcefilefoldername);
    String realDestinationFilefoldername = environmentSubstitute(destinationfilefoldername);
    String realWildcard = environmentSubstitute(wildcard);

    try {/*from  w w  w.j av a  2s. c o  m*/
        sourcefilefolder = KettleVFS.getFileObject(realSourceFilefoldername, this);
        destinationfilefolder = KettleVFS.getFileObject(realDestinationFilefoldername, this);
        if (!Const.isEmpty(MoveToFolder)) {
            movetofolderfolder = KettleVFS.getFileObject(MoveToFolder, this);
        }

        if (sourcefilefolder.exists()) {

            // Check if destination folder/parent folder exists !
            // If user wanted and if destination folder does not exist
            // PDI will create it
            if (CreateDestinationFolder(destinationfilefolder)) {

                // Basic Tests
                if (sourcefilefolder.getType().equals(FileType.FOLDER) && destination_is_a_file) {
                    // Source is a folder, destination is a file
                    // WARNING !!! CAN NOT MOVE FOLDER TO FILE !!!

                    log.logError(BaseMessages.getString(PKG, "JobMoveFiles.Log.Forbidden"),
                            BaseMessages.getString(PKG, "JobMoveFiles.Log.CanNotMoveFolderToFile",
                                    realSourceFilefoldername, realDestinationFilefoldername));

                    // Update Errors
                    updateErrors();
                } else {
                    if (destinationfilefolder.getType().equals(FileType.FOLDER)
                            && sourcefilefolder.getType().equals(FileType.FILE)) {
                        // Source is a file, destination is a folder
                        // return destination short filename
                        String shortfilename = sourcefilefolder.getName().getBaseName();

                        try {
                            shortfilename = getDestinationFilename(shortfilename);
                        } catch (Exception e) {
                            logError(BaseMessages.getString(PKG,
                                    BaseMessages.getString(PKG, "JobMoveFiles.Error.GettingFilename",
                                            sourcefilefolder.getName().getBaseName(), e.toString())));
                            return entrystatus;
                        }
                        // Move the file to the destination folder

                        String destinationfilenamefull = KettleVFS.getFilename(destinationfilefolder)
                                + Const.FILE_SEPARATOR + shortfilename;
                        FileObject destinationfile = KettleVFS.getFileObject(destinationfilenamefull, this);

                        entrystatus = MoveFile(shortfilename, sourcefilefolder, destinationfile,
                                movetofolderfolder, parentJob, result);
                        return entrystatus;
                    } else if (sourcefilefolder.getType().equals(FileType.FILE) && destination_is_a_file) {
                        // Source is a file, destination is a file

                        FileObject destinationfile = KettleVFS.getFileObject(realDestinationFilefoldername,
                                this);

                        // return destination short filename
                        String shortfilename = destinationfile.getName().getBaseName();
                        try {
                            shortfilename = getDestinationFilename(shortfilename);
                        } catch (Exception e) {
                            logError(BaseMessages.getString(PKG,
                                    BaseMessages.getString(PKG, "JobMoveFiles.Error.GettingFilename",
                                            sourcefilefolder.getName().getBaseName(), e.toString())));
                            return entrystatus;
                        }

                        String destinationfilenamefull = KettleVFS.getFilename(destinationfile.getParent())
                                + Const.FILE_SEPARATOR + shortfilename;
                        destinationfile = KettleVFS.getFileObject(destinationfilenamefull, this);

                        entrystatus = MoveFile(shortfilename, sourcefilefolder, destinationfile,
                                movetofolderfolder, parentJob, result);
                        return entrystatus;
                    } else {
                        // Both source and destination are folders
                        if (log.isDetailed()) {
                            logDetailed("  ");
                            logDetailed(BaseMessages.getString(PKG, "JobMoveFiles.Log.FetchFolder",
                                    sourcefilefolder.toString()));
                        }

                        FileObject[] fileObjects = sourcefilefolder.findFiles(new AllFileSelector() {
                            public boolean traverseDescendents(FileSelectInfo info) {
                                return true;
                            }

                            public boolean includeFile(FileSelectInfo info) {
                                FileObject fileObject = info.getFile();
                                try {
                                    if (fileObject == null) {
                                        return false;
                                    }
                                } catch (Exception ex) {
                                    // Upon error don't process the file.
                                    return false;
                                } finally {
                                    if (fileObject != null) {
                                        try {
                                            fileObject.close();
                                        } catch (IOException ex) { /* Ignore */
                                        }
                                    }

                                }
                                return true;
                            }
                        });

                        if (fileObjects != null) {
                            for (int j = 0; j < fileObjects.length && !parentJob.isStopped(); j++) {
                                // Success condition broken?
                                if (successConditionBroken) {
                                    if (!successConditionBrokenExit) {
                                        logError(BaseMessages.getString(PKG,
                                                "JobMoveFiles.Error.SuccessConditionbroken", "" + NrErrors));
                                        successConditionBrokenExit = true;
                                    }
                                    return false;
                                }
                                // Fetch files in list one after one ...
                                Currentfile = fileObjects[j];

                                if (!MoveOneFile(Currentfile, sourcefilefolder, realDestinationFilefoldername,
                                        realWildcard, parentJob, result, movetofolderfolder)) {
                                    // Update Errors
                                    updateErrors();
                                }

                            }
                        }
                    }

                }
                entrystatus = true;
            } else {
                // Destination Folder or Parent folder is missing
                logError(BaseMessages.getString(PKG, "JobMoveFiles.Error.DestinationFolderNotFound",
                        realDestinationFilefoldername));
            }
        } else {
            logError(BaseMessages.getString(PKG, "JobMoveFiles.Error.SourceFileNotExists",
                    realSourceFilefoldername));
        }
    } catch (Exception e) {
        logError(BaseMessages.getString(PKG, "JobMoveFiles.Error.Exception.MoveProcess",
                realSourceFilefoldername.toString(), destinationfilefolder.toString(), e.getMessage()));
    } finally {
        if (sourcefilefolder != null) {
            try {
                sourcefilefolder.close();
            } catch (IOException ex) {
                /* Ignore */
            }
        }
        if (destinationfilefolder != null) {
            try {
                destinationfilefolder.close();
            } catch (IOException ex) {
                /* Ignore */
            }
        }
        if (Currentfile != null) {
            try {
                Currentfile.close();
            } catch (IOException ex) {
                /* Ignore */
            }
        }
        if (movetofolderfolder != null) {
            try {
                movetofolderfolder.close();
            } catch (IOException ex) {
                /* Ignore */
            }
        }
    }
    return entrystatus;
}

From source file:org.pentaho.di.job.entries.pgpdecryptfiles.JobEntryPGPDecryptFiles.java

private boolean ProcessFileFolder(String sourcefilefoldername, String passPhrase,
        String destinationfilefoldername, String wildcard, Job parentJob, Result result, String MoveToFolder) {
    boolean entrystatus = false;
    FileObject sourcefilefolder = null;
    FileObject destinationfilefolder = null;
    FileObject movetofolderfolder = null;
    FileObject Currentfile = null;

    // Get real source, destination file and wildcard
    String realSourceFilefoldername = environmentSubstitute(sourcefilefoldername);
    String realDestinationFilefoldername = environmentSubstitute(destinationfilefoldername);
    String realWildcard = environmentSubstitute(wildcard);

    try {/*www  . j a v a  2  s . co  m*/

        sourcefilefolder = KettleVFS.getFileObject(realSourceFilefoldername);
        destinationfilefolder = KettleVFS.getFileObject(realDestinationFilefoldername);
        if (!Const.isEmpty(MoveToFolder)) {
            movetofolderfolder = KettleVFS.getFileObject(MoveToFolder);
        }

        if (sourcefilefolder.exists()) {

            // Check if destination folder/parent folder exists !
            // If user wanted and if destination folder does not exist
            // PDI will create it
            if (CreateDestinationFolder(destinationfilefolder)) {

                // Basic Tests
                if (sourcefilefolder.getType().equals(FileType.FOLDER) && destination_is_a_file) {
                    // Source is a folder, destination is a file
                    // WARNING !!! CAN NOT MOVE FOLDER TO FILE !!!

                    logError(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.Forbidden"),
                            BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.CanNotMoveFolderToFile",
                                    realSourceFilefoldername, realDestinationFilefoldername));

                    // Update Errors
                    updateErrors();
                } else {
                    if (destinationfilefolder.getType().equals(FileType.FOLDER)
                            && sourcefilefolder.getType().equals(FileType.FILE)) {
                        // Source is a file, destination is a folder
                        // return destination short filename
                        String shortfilename = sourcefilefolder.getName().getBaseName();

                        try {
                            shortfilename = getDestinationFilename(sourcefilefolder.getName().getBaseName());
                        } catch (Exception e) {
                            logError(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Error.GettingFilename",
                                    sourcefilefolder.getName().getBaseName(), e.toString()));
                            return entrystatus;
                        }
                        // Move the file to the destination folder

                        String destinationfilenamefull = destinationfilefolder.toString() + Const.FILE_SEPARATOR
                                + shortfilename;
                        FileObject destinationfile = KettleVFS.getFileObject(destinationfilenamefull);

                        entrystatus = DecryptFile(shortfilename, sourcefilefolder, passPhrase, destinationfile,
                                movetofolderfolder, parentJob, result);

                    } else if (sourcefilefolder.getType().equals(FileType.FILE) && destination_is_a_file) {
                        // Source is a file, destination is a file

                        FileObject destinationfile = KettleVFS.getFileObject(realDestinationFilefoldername);

                        // return destination short filename
                        String shortfilename = destinationfile.getName().getBaseName();
                        try {
                            shortfilename = getDestinationFilename(destinationfile.getName().getBaseName());
                        } catch (Exception e) {
                            logError(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Error.GettingFilename",
                                    sourcefilefolder.getName().getBaseName(), e.toString()));
                            return entrystatus;
                        }

                        String destinationfilenamefull = destinationfilefolder.getParent().toString()
                                + Const.FILE_SEPARATOR + shortfilename;
                        destinationfile = KettleVFS.getFileObject(destinationfilenamefull);

                        entrystatus = DecryptFile(shortfilename, sourcefilefolder, passPhrase, destinationfile,
                                movetofolderfolder, parentJob, result);

                    } else {
                        // Both source and destination are folders
                        if (isDetailed()) {
                            logDetailed("  ");
                            logDetailed(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.FetchFolder",
                                    sourcefilefolder.toString()));
                        }

                        FileObject[] fileObjects = sourcefilefolder.findFiles(new AllFileSelector() {
                            public boolean traverseDescendents(FileSelectInfo info) {
                                return info.getDepth() == 0 || include_subfolders;
                            }

                            public boolean includeFile(FileSelectInfo info) {
                                FileObject fileObject = info.getFile();
                                try {
                                    if (fileObject == null) {
                                        return false;
                                    }
                                } catch (Exception ex) {
                                    // Upon error don't process the file.
                                    return false;
                                } finally {
                                    if (fileObject != null) {
                                        try {
                                            fileObject.close();
                                            fileObject = null;
                                        } catch (IOException ex) { /* Ignore */
                                        }
                                    }
                                }
                                return true;
                            }
                        });

                        if (fileObjects != null) {
                            for (int j = 0; j < fileObjects.length && !parentJob.isStopped(); j++) {
                                // Success condition broken?
                                if (successConditionBroken) {
                                    if (!successConditionBrokenExit) {
                                        logError(BaseMessages.getString(PKG,
                                                "JobPGPDecryptFiles.Error.SuccessConditionbroken",
                                                "" + NrErrors));
                                        successConditionBrokenExit = true;
                                    }
                                    return false;
                                }
                                // Fetch files in list one after one ...
                                Currentfile = fileObjects[j];

                                if (!DecryptOneFile(Currentfile, sourcefilefolder, passPhrase,
                                        realDestinationFilefoldername, realWildcard, parentJob, result,
                                        movetofolderfolder)) {
                                    // Update Errors
                                    updateErrors();
                                }

                            }
                        }
                    }
                }
                entrystatus = true;
            } else {
                // Destination Folder or Parent folder is missing
                logError(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Error.DestinationFolderNotFound",
                        realDestinationFilefoldername));
            }
        } else {
            logError(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Error.SourceFileNotExists",
                    realSourceFilefoldername));
        }
    } catch (Exception e) {
        logError(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Error.Exception.MoveProcess",
                realSourceFilefoldername.toString(), destinationfilefolder.toString(), e.getMessage()));
        // Update Errors
        updateErrors();
    } finally {
        if (sourcefilefolder != null) {
            try {
                sourcefilefolder.close();
            } catch (IOException ex) { /* Ignore */
            }
        }
        if (destinationfilefolder != null) {
            try {
                destinationfilefolder.close();
            } catch (IOException ex) { /* Ignore */
            }
        }
        if (Currentfile != null) {
            try {
                Currentfile.close();
            } catch (IOException ex) { /* Ignore */
            }
        }
        if (movetofolderfolder != null) {
            try {
                movetofolderfolder.close();
            } catch (IOException ex) { /* Ignore */
            }
        }
    }
    return entrystatus;
}

From source file:org.pentaho.di.job.entries.pgpencryptfiles.GPG.java

/**
 * Constructs a new GnuPG//w w w  .ja va2s .c om
 *
 * @param gpgFilename
 *          gpg program location
 * @param logInterface
 *          LogChannelInterface
 * @throws KettleException
 */
public GPG(String gpgFilename, LogChannelInterface logInterface) throws KettleException {
    this.log = logInterface;
    this.gpgexe = gpgFilename;
    // Let's check GPG filename
    if (Const.isEmpty(getGpgExeFile())) {
        // No filename specified
        throw new KettleException(BaseMessages.getString(PKG, "GPG.GPGFilenameMissing"));
    }
    // We have a filename, we need to check
    FileObject file = null;
    try {
        file = KettleVFS.getFileObject(getGpgExeFile());

        if (!file.exists()) {
            throw new KettleException(BaseMessages.getString(PKG, "GPG.GPGFilenameNotFound"));
        }
        // The file exists
        if (!file.getType().equals(FileType.FILE)) {
            throw new KettleException(BaseMessages.getString(PKG, "GPG.GPGNotAFile", getGpgExeFile()));
        }

        // Ok we have a real file
        // Get the local filename
        this.gpgexe = KettleVFS.getFilename(file);

    } catch (Exception e) {
        throw new KettleException(BaseMessages.getString(PKG, "GPG.ErrorCheckingGPGFile", getGpgExeFile()), e);
    } finally {
        try {
            if (file != null) {
                file.close();
            }
        } catch (Exception e) {
            // Ignore close errors
        }
    }
}

From source file:org.pentaho.di.job.entries.pgpencryptfiles.JobEntryPGPEncryptFiles.java

private boolean ProcessFileFolder(int actionType, String sourcefilefoldername, String userID,
        String destinationfilefoldername, String wildcard, Job parentJob, Result result, String MoveToFolder) {
    boolean entrystatus = false;
    FileObject sourcefilefolder = null;
    FileObject destinationfilefolder = null;
    FileObject movetofolderfolder = null;
    FileObject Currentfile = null;

    // Get real source, destination file and wildcard
    String realSourceFilefoldername = environmentSubstitute(sourcefilefoldername);
    String realuserID = environmentSubstitute(userID);
    String realDestinationFilefoldername = environmentSubstitute(destinationfilefoldername);
    String realWildcard = environmentSubstitute(wildcard);

    try {/*from w w  w  .jav a2s. c  om*/

        sourcefilefolder = KettleVFS.getFileObject(realSourceFilefoldername);
        destinationfilefolder = KettleVFS.getFileObject(realDestinationFilefoldername);
        if (!Const.isEmpty(MoveToFolder)) {
            movetofolderfolder = KettleVFS.getFileObject(MoveToFolder);
        }

        if (sourcefilefolder.exists()) {

            // Check if destination folder/parent folder exists !
            // If user wanted and if destination folder does not exist
            // PDI will create it
            if (CreateDestinationFolder(destinationfilefolder)) {

                // Basic Tests
                if (sourcefilefolder.getType().equals(FileType.FOLDER) && destination_is_a_file) {
                    // Source is a folder, destination is a file
                    // WARNING !!! CAN NOT MOVE FOLDER TO FILE !!!

                    logError(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.Forbidden"),
                            BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.CanNotMoveFolderToFile",
                                    realSourceFilefoldername, realDestinationFilefoldername));

                    // Update Errors
                    updateErrors();
                } else {
                    if (destinationfilefolder.getType().equals(FileType.FOLDER)
                            && sourcefilefolder.getType().equals(FileType.FILE)) {
                        // Source is a file, destination is a folder
                        // return destination short filename
                        String shortfilename = sourcefilefolder.getName().getBaseName();

                        try {
                            shortfilename = getDestinationFilename(sourcefilefolder.getName().getBaseName());
                        } catch (Exception e) {
                            logError(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Error.GettingFilename",
                                    sourcefilefolder.getName().getBaseName(), e.toString()));
                            return entrystatus;
                        }
                        // Move the file to the destination folder

                        String destinationfilenamefull = destinationfilefolder.toString() + Const.FILE_SEPARATOR
                                + shortfilename;
                        FileObject destinationfile = KettleVFS.getFileObject(destinationfilenamefull);

                        entrystatus = EncryptFile(actionType, shortfilename, sourcefilefolder, realuserID,
                                destinationfile, movetofolderfolder, parentJob, result);

                    } else if (sourcefilefolder.getType().equals(FileType.FILE) && destination_is_a_file) {

                        // Source is a file, destination is a file

                        FileObject destinationfile = KettleVFS.getFileObject(realDestinationFilefoldername);

                        // return destination short filename
                        String shortfilename = destinationfile.getName().getBaseName();
                        try {
                            shortfilename = getDestinationFilename(destinationfile.getName().getBaseName());
                        } catch (Exception e) {
                            logError(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Error.GettingFilename",
                                    sourcefilefolder.getName().getBaseName(), e.toString()));
                            return entrystatus;
                        }

                        String destinationfilenamefull = destinationfilefolder.getParent().toString()
                                + Const.FILE_SEPARATOR + shortfilename;
                        destinationfile = KettleVFS.getFileObject(destinationfilenamefull);

                        entrystatus = EncryptFile(actionType, shortfilename, sourcefilefolder, realuserID,
                                destinationfile, movetofolderfolder, parentJob, result);

                    } else {
                        // Both source and destination are folders
                        if (isDetailed()) {
                            logDetailed("  ");
                            logDetailed(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.FetchFolder",
                                    sourcefilefolder.toString()));
                        }

                        FileObject[] fileObjects = sourcefilefolder.findFiles(new AllFileSelector() {
                            public boolean traverseDescendents(FileSelectInfo info) {
                                return info.getDepth() == 0 || include_subfolders;
                            }

                            public boolean includeFile(FileSelectInfo info) {
                                FileObject fileObject = info.getFile();
                                try {
                                    if (fileObject == null) {
                                        return false;
                                    }
                                } catch (Exception ex) {
                                    // Upon error don't process the file.
                                    return false;
                                } finally {
                                    if (fileObject != null) {
                                        try {
                                            fileObject.close();
                                            fileObject = null;
                                        } catch (IOException ex) { /* Ignore */
                                        }
                                    }
                                }
                                return true;
                            }
                        });

                        if (fileObjects != null) {
                            for (int j = 0; j < fileObjects.length && !parentJob.isStopped(); j++) {
                                // Success condition broken?
                                if (successConditionBroken) {
                                    if (!successConditionBrokenExit) {
                                        logError(BaseMessages.getString(PKG,
                                                "JobPGPEncryptFiles.Error.SuccessConditionbroken",
                                                "" + NrErrors));
                                        successConditionBrokenExit = true;
                                    }
                                    return false;
                                }
                                // Fetch files in list one after one ...
                                Currentfile = fileObjects[j];

                                if (!EncryptOneFile(actionType, Currentfile, sourcefilefolder, realuserID,
                                        realDestinationFilefoldername, realWildcard, parentJob, result,
                                        movetofolderfolder)) {
                                    // Update Errors
                                    updateErrors();
                                }

                            }
                        }
                    }
                }
                entrystatus = true;
            } else {
                // Destination Folder or Parent folder is missing
                logError(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Error.DestinationFolderNotFound",
                        realDestinationFilefoldername));
            }
        } else {
            logError(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Error.SourceFileNotExists",
                    realSourceFilefoldername));
        }
    } catch (Exception e) {
        logError(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Error.Exception.MoveProcess",
                realSourceFilefoldername.toString(), destinationfilefolder.toString(), e.getMessage()));
        // Update Errors
        updateErrors();
    } finally {
        if (sourcefilefolder != null) {
            try {
                sourcefilefolder.close();
            } catch (IOException ex) { /* Ignore */
            }
        }
        if (destinationfilefolder != null) {
            try {
                destinationfilefolder.close();
            } catch (IOException ex) { /* Ignore */
            }
        }
        if (Currentfile != null) {
            try {
                Currentfile.close();
            } catch (IOException ex) { /* Ignore */
            }
        }
        if (movetofolderfolder != null) {
            try {
                movetofolderfolder.close();
            } catch (IOException ex) { /* Ignore */
            }
        }
    }
    return entrystatus;
}

From source file:org.pentaho.di.job.entries.sftp.SFTPClient.java

public FileType getFileType(String filename) throws KettleJobException {
    try {//  ww  w  .  j  a v a  2 s  . c om
        SftpATTRS attrs = c.stat(filename);
        if (attrs == null) {
            return FileType.IMAGINARY;
        }

        if ((attrs.getFlags() & SftpATTRS.SSH_FILEXFER_ATTR_PERMISSIONS) == 0) {
            throw new KettleJobException("Unknown permissions error");
        }

        if (attrs.isDir()) {
            return FileType.FOLDER;
        } else {
            return FileType.FILE;
        }
    } catch (Exception e) {
        throw new KettleJobException(e);
    }
}

From source file:org.pentaho.di.job.entries.sftpput.JobEntrySFTPPUT.java

public Result execute(Result previousResult, int nr) throws KettleException {
    Result result = previousResult;
    List<RowMetaAndData> rows = result.getRows();
    result.setResult(false);//from   w  w w .j  a v a 2s . c o  m

    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 (!Const.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 {
        // Let's perform some checks before starting

        if (getAfterFTPS() == AFTER_FTPSPUT_MOVE) {
            if (Const.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 (Const.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 (!Const.isEmpty(realProxyHost)) {
            // Set proxy
            sftpclient.setProxy(realProxyHost, environmentSubstitute(getProxyPort()),
                    environmentSubstitute(getProxyUsername()), environmentSubstitute(getProxyPassword()),
                    getProxyType());
        }

        // login to ftp host ...
        sftpclient.login(realPassword);
        // Don't show the password in the logs, it's not good for security audits
        // logDetailed("logged in using password "+realPassword); // Logging this seems a bad idea! Oh well.

        // move to spool dir ...
        if (!Const.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));
            }
        } // end if

        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 (!Const.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));
                    }

                    // We successfully uploaded the file
                    // 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;
    } // end finally

    return result;
}

From source file:org.pentaho.di.job.entries.ssh2get.JobEntrySSH2GET.java

/**
 * Check existence of a local file/* ww  w  . j  ava  2 s . c om*/
 *
 * @param filename
 * @return true, if file exists
 */
public boolean FileExists(String filename) {

    FileObject file = null;
    try {
        file = KettleVFS.getFileObject(filename, this);
        if (!file.exists()) {
            return false;
        } else {
            if (file.getType() == FileType.FILE) {
                return true;
            } else {
                return false;
            }
        }
    } catch (Exception e) {
        return false;
    }
}

From source file:org.pentaho.di.job.entries.ssh2put.JobEntrySSH2PUT.java

private List<FileObject> getFiles(String localfolder) throws KettleFileException {
    try {//from ww  w.ja  v a 2 s . c o m
        List<FileObject> myFileList = new ArrayList<FileObject>();

        // Get all the files in the local directory...

        FileObject localFiles = KettleVFS.getFileObject(localfolder, 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]);

                }
            } // end for
        }

        return myFileList;
    } catch (IOException e) {
        throw new KettleFileException(e);
    }

}

From source file:org.pentaho.di.job.entries.unzip.JobEntryUnZip.java

private boolean processOneFile(Result result, Job parentJob, FileObject fileObject, String realTargetdirectory,
        String realWildcard, String realWildcardExclude, FileObject movetodir, String realMovetodirectory,
        String realWildcardSource) {
    boolean retval = false;

    try {/*from  w ww  . j  a v  a  2s .c o  m*/
        if (fileObject.getType().equals(FileType.FILE)) {
            // We have to unzip one zip file
            if (!unzipFile(fileObject, realTargetdirectory, realWildcard, realWildcardExclude, result,
                    parentJob, fileObject, movetodir, realMovetodirectory)) {
                updateErrors();
            } else {
                updateSuccess();
            }
        } else {
            // Folder..let's see wildcard
            FileObject[] children = fileObject.getChildren();

            for (int i = 0; i < children.length && !parentJob.isStopped(); i++) {
                if (successConditionBroken) {
                    if (!successConditionBrokenExit) {
                        logError(BaseMessages.getString(PKG, "JobUnZip.Error.SuccessConditionbroken",
                                "" + NrErrors));
                        successConditionBrokenExit = true;
                    }
                    return false;
                }
                // Get only file!
                if (!children[i].getType().equals(FileType.FOLDER)) {
                    boolean unzip = true;

                    String filename = children[i].getName().getPath();

                    Pattern patternSource = null;

                    if (!Const.isEmpty(realWildcardSource)) {
                        patternSource = Pattern.compile(realWildcardSource);
                    }

                    // First see if the file matches the regular expression!
                    if (patternSource != null) {
                        Matcher matcher = patternSource.matcher(filename);
                        unzip = matcher.matches();
                    }
                    if (unzip) {
                        if (!unzipFile(children[i], realTargetdirectory, realWildcard, realWildcardExclude,
                                result, parentJob, fileObject, movetodir, realMovetodirectory)) {
                            updateErrors();
                        } else {
                            updateSuccess();
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        updateErrors();
        logError(BaseMessages.getString(PKG, "JobUnZip.Error.Label", e.getMessage()));
    } finally {
        if (fileObject != null) {
            try {
                fileObject.close();
            } catch (IOException ex) { /* Ignore */
            }
        }
    }
    return retval;
}