Search in sources :

Example 81 with FileObject

use of org.apache.commons.vfs2.FileObject in project pentaho-kettle by pentaho.

the class JobEntryZipFile method processRowFile.

public boolean processRowFile(Job parentJob, Result result, String realZipfilename, String realWildcard, String realWildcardExclude, String realSourceDirectoryOrFile, String realMovetodirectory, boolean createparentfolder) {
    boolean Fileexists = false;
    File tempFile = null;
    File fileZip;
    boolean resultat = false;
    boolean renameOk = false;
    boolean orginExist = false;
    // Check if target file/folder exists!
    FileObject originFile = null;
    ZipInputStream zin = null;
    byte[] buffer;
    OutputStream dest = null;
    BufferedOutputStreamWithCloseDetection buff = null;
    ZipOutputStream out = null;
    ZipEntry entry;
    String localSourceFilename = realSourceDirectoryOrFile;
    try {
        originFile = KettleVFS.getFileObject(realSourceDirectoryOrFile, this);
        localSourceFilename = KettleVFS.getFilename(originFile);
        orginExist = originFile.exists();
    } catch (Exception e) {
    // Ignore errors
    } finally {
        if (originFile != null) {
            try {
                originFile.close();
            } catch (IOException ex) {
                logError("Error closing file '" + originFile.toString() + "'", ex);
            }
        }
    }
    String localrealZipfilename = realZipfilename;
    if (realZipfilename != null && orginExist) {
        FileObject fileObject = null;
        try {
            fileObject = KettleVFS.getFileObject(localrealZipfilename, this);
            localrealZipfilename = KettleVFS.getFilename(fileObject);
            // Check if Zip File exists
            if (fileObject.exists()) {
                Fileexists = true;
                if (log.isDebug()) {
                    logDebug(BaseMessages.getString(PKG, "JobZipFiles.Zip_FileExists1.Label") + localrealZipfilename + BaseMessages.getString(PKG, "JobZipFiles.Zip_FileExists2.Label"));
                }
            }
            // Let's see if we need to create parent folder of destination zip filename
            if (createparentfolder) {
                createParentFolder(localrealZipfilename);
            }
            // Let's start the process now
            if (ifZipFileExists == 3 && Fileexists) {
                // the zip file exists and user want to Fail
                resultat = false;
            } else if (ifZipFileExists == 2 && Fileexists) {
                // the zip file exists and user want to do nothing
                if (addFileToResult) {
                    // Add file to result files name
                    ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, fileObject, parentJob.getJobname(), toString());
                    result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
                }
                resultat = true;
            } else if (afterZip == 2 && realMovetodirectory == null) {
                // After Zip, Move files..User must give a destination Folder
                resultat = false;
                logError(BaseMessages.getString(PKG, "JobZipFiles.AfterZip_No_DestinationFolder_Defined.Label"));
            } else {
                // After Zip, Move files..User must give a destination Folder
                // Let's see if we deal with file or folder
                FileObject[] fileList;
                FileObject sourceFileOrFolder = KettleVFS.getFileObject(localSourceFilename, this);
                boolean isSourceDirectory = sourceFileOrFolder.getType().equals(FileType.FOLDER);
                final Pattern pattern;
                final Pattern patternExclude;
                if (isSourceDirectory) {
                    // 
                    if (!Utils.isEmpty(realWildcard)) {
                        pattern = Pattern.compile(realWildcard);
                    } else {
                        pattern = null;
                    }
                    if (!Utils.isEmpty(realWildcardExclude)) {
                        patternExclude = Pattern.compile(realWildcardExclude);
                    } else {
                        patternExclude = null;
                    }
                    // 
                    if (includingSubFolders) {
                        fileList = sourceFileOrFolder.findFiles(new ZipJobEntryPatternFileSelector(pattern, patternExclude));
                    } else {
                        fileList = sourceFileOrFolder.getChildren();
                    }
                } else {
                    pattern = null;
                    patternExclude = null;
                    // Target is a file
                    fileList = new FileObject[] { sourceFileOrFolder };
                }
                if (fileList.length == 0) {
                    resultat = false;
                    logError(BaseMessages.getString(PKG, "JobZipFiles.Log.FolderIsEmpty", localSourceFilename));
                } else if (!checkContainsFile(localSourceFilename, fileList, isSourceDirectory)) {
                    resultat = false;
                    logError(BaseMessages.getString(PKG, "JobZipFiles.Log.NoFilesInFolder", localSourceFilename));
                } else {
                    if (ifZipFileExists == 0 && Fileexists) {
                        // do we have already a .zip at the end?
                        if (localrealZipfilename.toLowerCase().endsWith(".zip")) {
                            // strip this off
                            localrealZipfilename = localrealZipfilename.substring(0, localrealZipfilename.length() - 4);
                        }
                        localrealZipfilename += "_" + StringUtil.getFormattedDateTimeNow(true) + ".zip";
                        if (log.isDebug()) {
                            logDebug(BaseMessages.getString(PKG, "JobZipFiles.Zip_FileNameChange1.Label") + localrealZipfilename + BaseMessages.getString(PKG, "JobZipFiles.Zip_FileNameChange1.Label"));
                        }
                    } else if (ifZipFileExists == 1 && Fileexists) {
                        // the zip file exists and user want to append
                        // get a temp file
                        fileZip = getFile(localrealZipfilename);
                        tempFile = File.createTempFile(fileZip.getName(), null);
                        // delete it, otherwise we cannot rename existing zip to it.
                        tempFile.delete();
                        renameOk = fileZip.renameTo(tempFile);
                        if (!renameOk) {
                            logError(BaseMessages.getString(PKG, "JobZipFiles.Cant_Rename_Temp1.Label") + fileZip.getAbsolutePath() + BaseMessages.getString(PKG, "JobZipFiles.Cant_Rename_Temp2.Label") + tempFile.getAbsolutePath() + BaseMessages.getString(PKG, "JobZipFiles.Cant_Rename_Temp3.Label"));
                        }
                        if (log.isDebug()) {
                            logDebug(BaseMessages.getString(PKG, "JobZipFiles.Zip_FileAppend1.Label") + localrealZipfilename + BaseMessages.getString(PKG, "JobZipFiles.Zip_FileAppend2.Label"));
                        }
                    }
                    if (log.isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "JobZipFiles.Files_Found1.Label") + fileList.length + BaseMessages.getString(PKG, "JobZipFiles.Files_Found2.Label") + localSourceFilename + BaseMessages.getString(PKG, "JobZipFiles.Files_Found3.Label"));
                    }
                    // Prepare Zip File
                    buffer = new byte[18024];
                    dest = KettleVFS.getOutputStream(localrealZipfilename, this, false);
                    buff = new BufferedOutputStreamWithCloseDetection(dest);
                    out = new ZipOutputStream(buff);
                    HashSet<String> fileSet = new HashSet<String>();
                    if (renameOk) {
                        // User want to append files to existing Zip file
                        // The idea is to rename the existing zip file to a temporary file
                        // and then adds all entries in the existing zip along with the new files,
                        // excluding the zip entries that have the same name as one of the new files.
                        zin = new ZipInputStream(new FileInputStream(tempFile));
                        entry = zin.getNextEntry();
                        while (entry != null) {
                            String name = entry.getName();
                            if (!fileSet.contains(name)) {
                                // Add ZIP entry to output stream.
                                out.putNextEntry(new ZipEntry(name));
                                // Transfer bytes from the ZIP file to the output file
                                int len;
                                while ((len = zin.read(buffer)) > 0) {
                                    out.write(buffer, 0, len);
                                }
                                fileSet.add(name);
                            }
                            entry = zin.getNextEntry();
                        }
                        // Close the streams
                        zin.close();
                    }
                    // Set the method
                    out.setMethod(ZipOutputStream.DEFLATED);
                    // Set the compression level
                    if (compressionRate == 0) {
                        out.setLevel(Deflater.NO_COMPRESSION);
                    } else if (compressionRate == 1) {
                        out.setLevel(Deflater.DEFAULT_COMPRESSION);
                    }
                    if (compressionRate == 2) {
                        out.setLevel(Deflater.BEST_COMPRESSION);
                    }
                    if (compressionRate == 3) {
                        out.setLevel(Deflater.BEST_SPEED);
                    }
                    // Specify Zipped files (After that we will move,delete them...)
                    FileObject[] zippedFiles = new FileObject[fileList.length];
                    int fileNum = 0;
                    // Get the files in the list...
                    for (int i = 0; i < fileList.length && !parentJob.isStopped(); i++) {
                        boolean getIt = true;
                        boolean getItexclude = false;
                        // ..only if target is a folder !
                        if (isSourceDirectory) {
                            // If we include sub-folders, we match on the whole name, not just the basename
                            // 
                            String filename;
                            if (includingSubFolders) {
                                filename = fileList[i].getName().getPath();
                            } else {
                                filename = fileList[i].getName().getBaseName();
                            }
                            if (pattern != null) {
                                // Matches the base name of the file (backward compatible!)
                                // 
                                Matcher matcher = pattern.matcher(filename);
                                getIt = matcher.matches();
                            }
                            if (patternExclude != null) {
                                Matcher matcherexclude = patternExclude.matcher(filename);
                                getItexclude = matcherexclude.matches();
                            }
                        }
                        // Get processing File
                        String targetFilename = KettleVFS.getFilename(fileList[i]);
                        if (sourceFileOrFolder.getType().equals(FileType.FILE)) {
                            targetFilename = localSourceFilename;
                        }
                        FileObject file = KettleVFS.getFileObject(targetFilename, this);
                        boolean isTargetDirectory = file.exists() && file.getType().equals(FileType.FOLDER);
                        if (getIt && !getItexclude && !isTargetDirectory && !fileSet.contains(targetFilename)) {
                            // We can add the file to the Zip Archive
                            if (log.isDebug()) {
                                logDebug(BaseMessages.getString(PKG, "JobZipFiles.Add_FilesToZip1.Label") + fileList[i] + BaseMessages.getString(PKG, "JobZipFiles.Add_FilesToZip2.Label") + localSourceFilename + BaseMessages.getString(PKG, "JobZipFiles.Add_FilesToZip3.Label"));
                            }
                            // Associate a file input stream for the current file
                            InputStream in = KettleVFS.getInputStream(file);
                            // Add ZIP entry to output stream.
                            // 
                            String relativeName;
                            String fullName = fileList[i].getName().getPath();
                            String basePath = sourceFileOrFolder.getName().getPath();
                            if (isSourceDirectory) {
                                if (fullName.startsWith(basePath)) {
                                    relativeName = fullName.substring(basePath.length() + 1);
                                } else {
                                    relativeName = fullName;
                                }
                            } else if (isFromPrevious) {
                                int depth = determineDepth(environmentSubstitute(storedSourcePathDepth));
                                relativeName = determineZipfilenameForDepth(fullName, depth);
                            } else {
                                relativeName = fileList[i].getName().getBaseName();
                            }
                            out.putNextEntry(new ZipEntry(relativeName));
                            int len;
                            while ((len = in.read(buffer)) > 0) {
                                out.write(buffer, 0, len);
                            }
                            out.flush();
                            out.closeEntry();
                            // Close the current file input stream
                            in.close();
                            // Get Zipped File
                            zippedFiles[fileNum] = fileList[i];
                            fileNum = fileNum + 1;
                        }
                    }
                    // Close the ZipOutPutStream
                    out.close();
                    buff.close();
                    dest.close();
                    if (log.isBasic()) {
                        logBasic(BaseMessages.getString(PKG, "JobZipFiles.Log.TotalZippedFiles", "" + zippedFiles.length));
                    }
                    // Delete Temp File
                    if (tempFile != null) {
                        tempFile.delete();
                    }
                    // -----Get the list of Zipped Files and Move or Delete Them
                    if (afterZip == 1 || afterZip == 2) {
                        // iterate through the array of Zipped files
                        for (int i = 0; i < zippedFiles.length; i++) {
                            if (zippedFiles[i] != null) {
                                // Delete, Move File
                                FileObject fileObjectd = zippedFiles[i];
                                if (!isSourceDirectory) {
                                    fileObjectd = KettleVFS.getFileObject(localSourceFilename, this);
                                }
                                // Here we can move, delete files
                                if (afterZip == 1) {
                                    // Delete File
                                    boolean deleted = fileObjectd.delete();
                                    if (!deleted) {
                                        resultat = false;
                                        logError(BaseMessages.getString(PKG, "JobZipFiles.Cant_Delete_File1.Label") + localSourceFilename + Const.FILE_SEPARATOR + zippedFiles[i] + BaseMessages.getString(PKG, "JobZipFiles.Cant_Delete_File2.Label"));
                                    }
                                    // File deleted
                                    if (log.isDebug()) {
                                        logDebug(BaseMessages.getString(PKG, "JobZipFiles.File_Deleted1.Label") + localSourceFilename + Const.FILE_SEPARATOR + zippedFiles[i] + BaseMessages.getString(PKG, "JobZipFiles.File_Deleted2.Label"));
                                    }
                                } else if (afterZip == 2) {
                                    // Move File
                                    FileObject fileObjectm = null;
                                    try {
                                        fileObjectm = KettleVFS.getFileObject(realMovetodirectory + Const.FILE_SEPARATOR + fileObjectd.getName().getBaseName(), this);
                                        fileObjectd.moveTo(fileObjectm);
                                    } catch (IOException e) {
                                        logError(BaseMessages.getString(PKG, "JobZipFiles.Cant_Move_File1.Label") + zippedFiles[i] + BaseMessages.getString(PKG, "JobZipFiles.Cant_Move_File2.Label") + e.getMessage());
                                        resultat = false;
                                    } finally {
                                        try {
                                            if (fileObjectm != null) {
                                                fileObjectm.close();
                                            }
                                        } catch (Exception e) {
                                            if (fileObjectm != null) {
                                                logError("Error closing file '" + fileObjectm.toString() + "'", e);
                                            }
                                        }
                                    }
                                    // File moved
                                    if (log.isDebug()) {
                                        logDebug(BaseMessages.getString(PKG, "JobZipFiles.File_Moved1.Label") + zippedFiles[i] + BaseMessages.getString(PKG, "JobZipFiles.File_Moved2.Label"));
                                    }
                                }
                            }
                        }
                    }
                    if (addFileToResult) {
                        // Add file to result files name
                        ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, fileObject, parentJob.getJobname(), toString());
                        result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
                    }
                    resultat = true;
                }
            }
        } catch (Exception e) {
            logError(BaseMessages.getString(PKG, "JobZipFiles.Cant_CreateZipFile1.Label") + localrealZipfilename + BaseMessages.getString(PKG, "JobZipFiles.Cant_CreateZipFile2.Label"), e);
            resultat = false;
        } finally {
            if (fileObject != null) {
                try {
                    fileObject.close();
                    fileObject = null;
                } catch (IOException ex) {
                    logError("Error closing file '" + fileObject.toString() + "'", ex);
                }
            }
            try {
                if (out != null) {
                    out.close();
                }
                if (buff != null) {
                    buff.close();
                }
                if (dest != null) {
                    dest.close();
                }
                if (zin != null) {
                    zin.close();
                }
            } catch (IOException ex) {
                logError("Error closing zip file entry for file '" + originFile.toString() + "'", ex);
            }
        }
    } else {
        resultat = false;
        if (localrealZipfilename == null) {
            logError(BaseMessages.getString(PKG, "JobZipFiles.No_ZipFile_Defined.Label"));
        }
        if (!orginExist) {
            logError(BaseMessages.getString(PKG, "JobZipFiles.No_FolderCible_Defined.Label", localSourceFilename));
        }
    }
    // return a verifier
    return resultat;
}
Also used : BufferedOutputStreamWithCloseDetection(org.pentaho.di.workarounds.BufferedOutputStreamWithCloseDetection) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipOutputStream(java.util.zip.ZipOutputStream) OutputStream(java.io.OutputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) ResultFile(org.pentaho.di.core.ResultFile) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) KettleFileException(org.pentaho.di.core.exception.KettleFileException) FileSystemException(org.apache.commons.vfs2.FileSystemException) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) ZipInputStream(java.util.zip.ZipInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) FileObject(org.apache.commons.vfs2.FileObject) ResultFile(org.pentaho.di.core.ResultFile) File(java.io.File) HashSet(java.util.HashSet)

Example 82 with FileObject

use of org.apache.commons.vfs2.FileObject in project pentaho-kettle by pentaho.

the class JobEntryUnZip method processOneFile.

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 {
        if (fileObject.getType().equals(FileType.FILE)) {
            // We have to unzip one zip file
            if (!unzipFile(fileObject, realTargetdirectory, realWildcard, realWildcardExclude, result, parentJob, 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 (!Utils.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, 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;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) FileObject(org.apache.commons.vfs2.FileObject) IOException(java.io.IOException) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) FileSystemException(org.apache.commons.vfs2.FileSystemException) IOException(java.io.IOException)

Example 83 with FileObject

use of org.apache.commons.vfs2.FileObject in project pentaho-kettle by pentaho.

the class JobEntryUnZip method unzipFile.

private boolean unzipFile(FileObject sourceFileObject, String realTargetdirectory, String realWildcard, String realWildcardExclude, Result result, Job parentJob, FileObject movetodir, String realMovetodirectory) {
    boolean retval = false;
    String unzipToFolder = realTargetdirectory;
    try {
        if (log.isDetailed()) {
            logDetailed(BaseMessages.getString(PKG, "JobUnZip.Log.ProcessingFile", sourceFileObject.toString()));
        }
        // 
        if (rootzip) {
            String shortSourceFilename = sourceFileObject.getName().getBaseName();
            int lenstring = shortSourceFilename.length();
            int lastindexOfDot = shortSourceFilename.lastIndexOf('.');
            if (lastindexOfDot == -1) {
                lastindexOfDot = lenstring;
            }
            String foldername = realTargetdirectory + "/" + shortSourceFilename.substring(0, lastindexOfDot);
            FileObject rootfolder = KettleVFS.getFileObject(foldername, this);
            if (!rootfolder.exists()) {
                try {
                    rootfolder.createFolder();
                    if (log.isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "JobUnZip.Log.RootFolderCreated", foldername));
                    }
                } catch (Exception e) {
                    throw new Exception(BaseMessages.getString(PKG, "JobUnZip.Error.CanNotCreateRootFolder", foldername), e);
                }
            }
            unzipToFolder = foldername;
        }
        // Try to read the entries from the VFS object...
        // 
        String zipFilename = "zip:" + sourceFileObject.getName().getFriendlyURI();
        FileObject zipFile = KettleVFS.getFileObject(zipFilename, this);
        FileObject[] items = zipFile.findFiles(new AllFileSelector() {

            public boolean traverseDescendents(FileSelectInfo info) {
                return true;
            }

            public boolean includeFile(FileSelectInfo info) {
                // Never return the parent directory of a file list.
                if (info.getDepth() == 0) {
                    return false;
                }
                FileObject fileObject = info.getFile();
                return fileObject != null;
            }
        });
        Pattern pattern = null;
        if (!Utils.isEmpty(realWildcard)) {
            pattern = Pattern.compile(realWildcard);
        }
        Pattern patternexclude = null;
        if (!Utils.isEmpty(realWildcardExclude)) {
            patternexclude = Pattern.compile(realWildcardExclude);
        }
        for (FileObject item : items) {
            if (successConditionBroken) {
                if (!successConditionBrokenExit) {
                    logError(BaseMessages.getString(PKG, "JobUnZip.Error.SuccessConditionbroken", "" + NrErrors));
                    successConditionBrokenExit = true;
                }
                return false;
            }
            synchronized (KettleVFS.getInstance().getFileSystemManager()) {
                FileObject newFileObject = null;
                try {
                    if (log.isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "JobUnZip.Log.ProcessingZipEntry", item.getName().getURI(), sourceFileObject.toString()));
                    }
                    // get real destination filename
                    // 
                    String newFileName = unzipToFolder + Const.FILE_SEPARATOR + getTargetFilename(item);
                    newFileObject = KettleVFS.getFileObject(newFileName, this);
                    if (item.getType().equals(FileType.FOLDER)) {
                        // 
                        if (log.isDetailed()) {
                            logDetailed(BaseMessages.getString(PKG, "JobUnZip.CreatingDirectory.Label", newFileName));
                        }
                        // 
                        if (!newFileObject.exists()) {
                            newFileObject.createFolder();
                        }
                    } else {
                        // File
                        // 
                        boolean getIt = true;
                        boolean getItexclude = false;
                        // 
                        if (pattern != null) {
                            Matcher matcher = pattern.matcher(item.getName().getURI());
                            getIt = matcher.matches();
                        }
                        if (patternexclude != null) {
                            Matcher matcherexclude = patternexclude.matcher(item.getName().getURI());
                            getItexclude = matcherexclude.matches();
                        }
                        boolean take = takeThisFile(item, newFileName);
                        if (getIt && !getItexclude && take) {
                            if (log.isDetailed()) {
                                logDetailed(BaseMessages.getString(PKG, "JobUnZip.ExtractingEntry.Label", item.getName().getURI(), newFileName));
                            }
                            if (iffileexist == IF_FILE_EXISTS_UNIQ) {
                                // Create file with unique name
                                int lenstring = newFileName.length();
                                int lastindexOfDot = newFileName.lastIndexOf('.');
                                if (lastindexOfDot == -1) {
                                    lastindexOfDot = lenstring;
                                }
                                newFileName = newFileName.substring(0, lastindexOfDot) + StringUtil.getFormattedDateTimeNow(true) + newFileName.substring(lastindexOfDot, lenstring);
                                if (log.isDebug()) {
                                    logDebug(BaseMessages.getString(PKG, "JobUnZip.Log.CreatingUniqFile", newFileName));
                                }
                            }
                            // 
                            if (!newFileObject.getParent().exists()) {
                                // creates the whole path.
                                newFileObject.getParent().createFolder();
                            }
                            InputStream is = null;
                            OutputStream os = null;
                            try {
                                is = KettleVFS.getInputStream(item);
                                os = KettleVFS.getOutputStream(newFileObject, false);
                                if (is != null) {
                                    byte[] buff = new byte[2048];
                                    int len;
                                    while ((len = is.read(buff)) > 0) {
                                        os.write(buff, 0, len);
                                    }
                                    // Add filename to result filenames
                                    addFilenameToResultFilenames(result, parentJob, newFileName);
                                }
                            } finally {
                                if (is != null) {
                                    is.close();
                                }
                                if (os != null) {
                                    os.close();
                                }
                            }
                        }
                    // end if take
                    }
                } catch (Exception e) {
                    updateErrors();
                    logError(BaseMessages.getString(PKG, "JobUnZip.Error.CanNotProcessZipEntry", item.getName().getURI(), sourceFileObject.toString()), e);
                } finally {
                    if (newFileObject != null) {
                        try {
                            newFileObject.close();
                            if (setOriginalModificationDate) {
                                // Change last modification date
                                newFileObject.getContent().setLastModifiedTime(item.getContent().getLastModifiedTime());
                            }
                        } catch (Exception e) {
                        /* Ignore */
                        }
                    // ignore this
                    }
                    // Close file object
                    // close() does not release resources!
                    KettleVFS.getInstance().getFileSystemManager().closeFileSystem(item.getFileSystem());
                    if (items != null) {
                        items = null;
                    }
                }
            }
        // Synchronized block on KettleVFS.getInstance().getFileSystemManager()
        }
        // Unzip done...
        if (afterunzip > 0) {
            doUnzipPostProcessing(sourceFileObject, movetodir, realMovetodirectory);
        }
        retval = true;
    } catch (Exception e) {
        updateErrors();
        log.logError(BaseMessages.getString(PKG, "JobUnZip.ErrorUnzip.Label", sourceFileObject.toString(), e.getMessage()), e);
    }
    return retval;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) FileSystemException(org.apache.commons.vfs2.FileSystemException) IOException(java.io.IOException) FileSelectInfo(org.apache.commons.vfs2.FileSelectInfo) AllFileSelector(org.apache.commons.vfs2.AllFileSelector) FileObject(org.apache.commons.vfs2.FileObject)

Example 84 with FileObject

use of org.apache.commons.vfs2.FileObject in project pentaho-kettle by pentaho.

the class FileDoesNotExistValidator method validate.

public boolean validate(CheckResultSourceInterface source, String propertyName, List<CheckResultInterface> remarks, ValidatorContext context) {
    String filename = ValidatorUtils.getValueAsString(source, propertyName);
    VariableSpace variableSpace = getVariableSpace(source, propertyName, remarks, context);
    boolean failIfExists = getFailIfExists(source, propertyName, remarks, context);
    if (null == variableSpace) {
        return false;
    }
    String realFileName = variableSpace.environmentSubstitute(filename);
    FileObject fileObject = null;
    try {
        fileObject = KettleVFS.getFileObject(realFileName, variableSpace);
        if (fileObject.exists() && failIfExists) {
            JobEntryValidatorUtils.addFailureRemark(source, propertyName, VALIDATOR_NAME, remarks, JobEntryValidatorUtils.getLevelOnFail(context, VALIDATOR_NAME));
            return false;
        }
        try {
            // Just being paranoid
            fileObject.close();
        } catch (IOException ignored) {
        // Ignore close errors
        }
    } catch (Exception e) {
        JobEntryValidatorUtils.addExceptionRemark(source, propertyName, VALIDATOR_NAME, remarks, e);
        return false;
    }
    return true;
}
Also used : VariableSpace(org.pentaho.di.core.variables.VariableSpace) FileObject(org.apache.commons.vfs2.FileObject) IOException(java.io.IOException) IOException(java.io.IOException)

Example 85 with FileObject

use of org.apache.commons.vfs2.FileObject in project pentaho-kettle by pentaho.

the class PentahoReportingOutput method processReport.

@VisibleForTesting
public void processReport(Object[] r, String sourceFilename, String targetFilename, ProcessorType outputProcessorType, Boolean createParentFolder) throws KettleException {
    try {
        // Load the master report from the PRPT
        // 
        MasterReport report = loadMasterReport(sourceFilename, getTrans());
        // Set the parameters values that are present in the various fields...
        // 
        ReportParameterValues values = report.getParameterValues();
        ReportParameterDefinition definition = report.getParameterDefinition();
        for (String parameterName : meta.getParameterFieldMap().keySet()) {
            String fieldName = meta.getParameterFieldMap().get(parameterName);
            if (fieldName != null) {
                int index = getInputRowMeta().indexOfValue(fieldName);
                if (index < 0) {
                    throw new KettleException(BaseMessages.getString(PKG, "PentahoReportingOutput.Exception.CanNotFindField", fieldName));
                }
                Class<?> clazz = findParameterClass(definition, parameterName);
                Object value = null;
                if (clazz != null) {
                    if (clazz.equals(String.class)) {
                        value = getInputRowMeta().getString(r, index);
                    } else if (clazz.equals((new String[0]).getClass())) {
                        value = getInputRowMeta().getString(r, index).split("\t");
                    } else if (clazz.equals(Date.class)) {
                        value = getInputRowMeta().getDate(r, index);
                    } else if (clazz.equals(byte.class) || clazz.equals(Byte.class)) {
                        value = getInputRowMeta().getInteger(r, index).byteValue();
                    } else if (clazz.equals(Short.class) || clazz.equals(short.class)) {
                        value = getInputRowMeta().getInteger(r, index).shortValue();
                    } else if (clazz.equals(Integer.class) || clazz.equals(int.class)) {
                        value = getInputRowMeta().getInteger(r, index).intValue();
                    } else if (clazz.equals(Long.class) || clazz.equals(long.class)) {
                        value = getInputRowMeta().getInteger(r, index);
                    } else if (clazz.equals(Double.class) || clazz.equals(double.class)) {
                        value = getInputRowMeta().getNumber(r, index);
                    } else if (clazz.equals(Float.class) || clazz.equals(float.class)) {
                        value = getInputRowMeta().getNumber(r, index).floatValue();
                    } else if (clazz.equals(Number.class)) {
                        value = getInputRowMeta().getBigNumber(r, index).floatValue();
                    } else if (clazz.equals(Boolean.class) || clazz.equals(boolean.class)) {
                        value = getInputRowMeta().getBoolean(r, index);
                    } else if (clazz.equals(BigDecimal.class)) {
                        value = getInputRowMeta().getBigNumber(r, index);
                    } else if (clazz.equals((new byte[0]).getClass())) {
                        value = getInputRowMeta().getBinary(r, index);
                    } else {
                        value = getInputRowMeta().getValueMeta(index).convertToNormalStorageType(r[index]);
                    }
                    values.put(parameterName, value);
                } else {
                    // This parameter was not found, log this as a warning...
                    // 
                    logBasic(BaseMessages.getString(PKG, "PentahoReportingOutput.Log.ParameterNotFoundInReport", parameterName, sourceFilename));
                }
            }
        }
        Runnable exportTask;
        PentahoReportingSwingGuiContext context = new PentahoReportingSwingGuiContext();
        switch(outputProcessorType) {
            case PDF:
                exportTask = new ReportExportTask(report, context, targetFilename, createParentFolder) {

                    protected ReportProcessor createReportProcessor(OutputStream fout) throws Exception {
                        PdfOutputProcessor outputProcessor = new PdfOutputProcessor(report.getConfiguration(), fout, report.getResourceManager());
                        return new PageableReportProcessor(report, outputProcessor);
                    }
                };
                break;
            case CSV:
                exportTask = new ReportExportTask(report, context, targetFilename, createParentFolder) {

                    protected ReportProcessor createReportProcessor(OutputStream fout) throws Exception {
                        ReportStructureValidator validator = new ReportStructureValidator();
                        if (validator.isValidForFastProcessing(report) == false) {
                            StreamCSVOutputProcessor target = new StreamCSVOutputProcessor(fout);
                            return new StreamReportProcessor(report, target);
                        } else {
                            return new FastCsvExportProcessor(report, fout);
                        }
                    }
                };
                break;
            case Excel:
                exportTask = new ReportExportTask(report, context, targetFilename, createParentFolder) {

                    protected ReportProcessor createReportProcessor(OutputStream fout) throws Exception {
                        ReportStructureValidator validator = new ReportStructureValidator();
                        if (validator.isValidForFastProcessing(report) == false) {
                            final FlowExcelOutputProcessor target = new FlowExcelOutputProcessor(report.getConfiguration(), fout, report.getResourceManager());
                            target.setUseXlsxFormat(false);
                            return new FlowReportProcessor(report, target);
                        } else {
                            return new FastExcelExportProcessor(report, fout, false);
                        }
                    }
                };
                break;
            case Excel_2007:
                exportTask = new ReportExportTask(report, context, targetFilename, createParentFolder) {

                    protected ReportProcessor createReportProcessor(OutputStream fout) throws Exception {
                        ReportStructureValidator validator = new ReportStructureValidator();
                        if (validator.isValidForFastProcessing(report) == false) {
                            final FlowExcelOutputProcessor target = new FlowExcelOutputProcessor(report.getConfiguration(), fout, report.getResourceManager());
                            target.setUseXlsxFormat(true);
                            return new FlowReportProcessor(report, target);
                        } else {
                            return new FastExcelExportProcessor(report, fout, true);
                        }
                    }
                };
                break;
            case StreamingHTML:
                exportTask = new ReportExportTask(report, context, targetFilename, createParentFolder) {

                    protected String filename, suffix;

                    protected ContentLocation targetRoot;

                    @Override
                    protected void execute() throws Exception {
                        FileObject targetDirectory = targetFile.getParent();
                        FileObjectRepository targetRepository = new FileObjectRepository(targetDirectory);
                        targetRoot = targetRepository.getRoot();
                        suffix = getSuffix(targetPath);
                        filename = IOUtils.getInstance().stripFileExtension(targetFile.getName().toString());
                        ReportProcessor reportProcessor = createReportProcessor(null);
                        try {
                            reportProcessor.processReport();
                        } finally {
                            reportProcessor.close();
                        }
                    }

                    protected ReportProcessor createReportProcessor(OutputStream fout) throws Exception {
                        ReportStructureValidator validator = new ReportStructureValidator();
                        if (validator.isValidForFastProcessing(report) == false) {
                            final HtmlOutputProcessor outputProcessor = new StreamHtmlOutputProcessor(report.getConfiguration());
                            final HtmlPrinter printer = new AllItemsHtmlPrinter(report.getResourceManager());
                            printer.setContentWriter(targetRoot, new DefaultNameGenerator(targetRoot, filename, suffix));
                            // $NON-NLS-1$
                            printer.setDataWriter(null, null);
                            printer.setUrlRewriter(new FileSystemURLRewriter());
                            outputProcessor.setPrinter(printer);
                            return new StreamReportProcessor(report, outputProcessor);
                        } else {
                            FastHtmlContentItems printer = new FastHtmlContentItems();
                            printer.setContentWriter(targetRoot, new DefaultNameGenerator(targetRoot, filename, suffix));
                            // $NON-NLS-1$
                            printer.setDataWriter(null, null);
                            printer.setUrlRewriter(new FileSystemURLRewriter());
                            return new FastHtmlExportProcessor(report, printer);
                        }
                    }
                };
                break;
            case PagedHTML:
                exportTask = new ReportExportTask(report, context, targetFilename, createParentFolder) {

                    protected String filename, suffix;

                    protected ContentLocation targetRoot;

                    @Override
                    protected void execute() throws Exception {
                        FileObject targetDirectory = targetFile.getParent();
                        FileObjectRepository targetRepository = new FileObjectRepository(targetDirectory);
                        targetRoot = targetRepository.getRoot();
                        suffix = getSuffix(targetPath);
                        Path p = Paths.get(targetFile.getName().getPath());
                        filename = IOUtils.getInstance().stripFileExtension(p.getFileName().toString());
                        ReportProcessor reportProcessor = createReportProcessor(null);
                        try {
                            reportProcessor.processReport();
                        } finally {
                            reportProcessor.close();
                        }
                    }

                    protected ReportProcessor createReportProcessor(OutputStream fout) throws Exception {
                        final FlowHtmlOutputProcessor outputProcessor = new FlowHtmlOutputProcessor();
                        final HtmlPrinter printer = new AllItemsHtmlPrinter(report.getResourceManager());
                        printer.setContentWriter(targetRoot, new DefaultNameGenerator(targetRoot, filename, suffix));
                        printer.setDataWriter(targetRoot, new DefaultNameGenerator(targetRoot, "content"));
                        printer.setUrlRewriter(new FileSystemURLRewriter());
                        outputProcessor.setPrinter(printer);
                        return new FlowReportProcessor(report, outputProcessor);
                    }
                };
                break;
            case RTF:
                exportTask = new ReportExportTask(report, context, targetFilename, createParentFolder) {

                    protected ReportProcessor createReportProcessor(OutputStream fout) throws Exception {
                        StreamRTFOutputProcessor target = new StreamRTFOutputProcessor(report.getConfiguration(), fout, report.getResourceManager());
                        return new StreamReportProcessor(report, target);
                    }
                };
                break;
            default:
                exportTask = null;
                break;
        }
        if (exportTask != null) {
            exportTask.run();
        }
        if (context.getStatusType() == StatusType.ERROR) {
            KettleVFS.getFileObject(targetFilename, getTransMeta()).delete();
            if (context.getCause() != null) {
                throw context.getCause();
            }
            throw new KettleStepException(context.getMessage());
        }
        ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, KettleVFS.getFileObject(targetFilename, getTransMeta()), getTransMeta().getName(), getStepname());
        resultFile.setComment("This file was created with a Pentaho Reporting Output step");
        addResultFile(resultFile);
    } catch (Throwable e) {
        throw new KettleException(BaseMessages.getString(PKG, "PentahoReportingOutput.Exception.UnexpectedErrorRenderingReport", sourceFilename, targetFilename, outputProcessorType.getDescription()), e);
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) StreamCSVOutputProcessor(org.pentaho.reporting.engine.classic.core.modules.output.table.csv.StreamCSVOutputProcessor) KettleStepException(org.pentaho.di.core.exception.KettleStepException) FlowExcelOutputProcessor(org.pentaho.reporting.engine.classic.core.modules.output.table.xls.FlowExcelOutputProcessor) OutputStream(java.io.OutputStream) FastHtmlContentItems(org.pentaho.reporting.engine.classic.core.modules.output.fast.html.FastHtmlContentItems) StreamRTFOutputProcessor(org.pentaho.reporting.engine.classic.core.modules.output.table.rtf.StreamRTFOutputProcessor) ReportStructureValidator(org.pentaho.reporting.engine.classic.core.modules.output.fast.validator.ReportStructureValidator) FileObjectRepository(org.pentaho.di.trans.steps.pentahoreporting.urlrepository.FileObjectRepository) StreamHtmlOutputProcessor(org.pentaho.reporting.engine.classic.core.modules.output.table.html.StreamHtmlOutputProcessor) FlowHtmlOutputProcessor(org.pentaho.reporting.engine.classic.core.modules.output.table.html.FlowHtmlOutputProcessor) HtmlOutputProcessor(org.pentaho.reporting.engine.classic.core.modules.output.table.html.HtmlOutputProcessor) ReportParameterDefinition(org.pentaho.reporting.engine.classic.core.parameters.ReportParameterDefinition) PageableReportProcessor(org.pentaho.reporting.engine.classic.core.modules.output.pageable.base.PageableReportProcessor) FileSystemURLRewriter(org.pentaho.reporting.engine.classic.core.modules.output.table.html.FileSystemURLRewriter) FlowReportProcessor(org.pentaho.reporting.engine.classic.core.modules.output.table.base.FlowReportProcessor) FastHtmlExportProcessor(org.pentaho.reporting.engine.classic.core.modules.output.fast.html.FastHtmlExportProcessor) FileObject(org.apache.commons.vfs2.FileObject) FastCsvExportProcessor(org.pentaho.reporting.engine.classic.core.modules.output.fast.csv.FastCsvExportProcessor) ReportParameterValues(org.pentaho.reporting.engine.classic.core.util.ReportParameterValues) Path(java.nio.file.Path) PdfOutputProcessor(org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.PdfOutputProcessor) DefaultNameGenerator(org.pentaho.reporting.libraries.repository.DefaultNameGenerator) FlowHtmlOutputProcessor(org.pentaho.reporting.engine.classic.core.modules.output.table.html.FlowHtmlOutputProcessor) FlowReportProcessor(org.pentaho.reporting.engine.classic.core.modules.output.table.base.FlowReportProcessor) ReportProcessor(org.pentaho.reporting.engine.classic.core.layout.output.ReportProcessor) PageableReportProcessor(org.pentaho.reporting.engine.classic.core.modules.output.pageable.base.PageableReportProcessor) StreamReportProcessor(org.pentaho.reporting.engine.classic.core.modules.output.table.base.StreamReportProcessor) ResultFile(org.pentaho.di.core.ResultFile) BigDecimal(java.math.BigDecimal) ResourceLoadingException(org.pentaho.reporting.libraries.resourceloader.ResourceLoadingException) KettleFileException(org.pentaho.di.core.exception.KettleFileException) ResourceCreationException(org.pentaho.reporting.libraries.resourceloader.ResourceCreationException) ResourceKeyCreationException(org.pentaho.reporting.libraries.resourceloader.ResourceKeyCreationException) KettleStepException(org.pentaho.di.core.exception.KettleStepException) KettleException(org.pentaho.di.core.exception.KettleException) MalformedURLException(java.net.MalformedURLException) ResourceException(org.pentaho.reporting.libraries.resourceloader.ResourceException) FastExcelExportProcessor(org.pentaho.reporting.engine.classic.core.modules.output.fast.xls.FastExcelExportProcessor) MasterReport(org.pentaho.reporting.engine.classic.core.MasterReport) AllItemsHtmlPrinter(org.pentaho.reporting.engine.classic.core.modules.output.table.html.AllItemsHtmlPrinter) ContentLocation(org.pentaho.reporting.libraries.repository.ContentLocation) StreamHtmlOutputProcessor(org.pentaho.reporting.engine.classic.core.modules.output.table.html.StreamHtmlOutputProcessor) FileObject(org.apache.commons.vfs2.FileObject) StreamReportProcessor(org.pentaho.reporting.engine.classic.core.modules.output.table.base.StreamReportProcessor) AllItemsHtmlPrinter(org.pentaho.reporting.engine.classic.core.modules.output.table.html.AllItemsHtmlPrinter) HtmlPrinter(org.pentaho.reporting.engine.classic.core.modules.output.table.html.HtmlPrinter) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

FileObject (org.apache.commons.vfs2.FileObject)646 KettleException (org.pentaho.di.core.exception.KettleException)206 IOException (java.io.IOException)203 FileSystemException (org.apache.commons.vfs2.FileSystemException)173 KettleXMLException (org.pentaho.di.core.exception.KettleXMLException)104 KettleFileException (org.pentaho.di.core.exception.KettleFileException)97 Test (org.junit.Test)82 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)68 File (java.io.File)60 InputStream (java.io.InputStream)48 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)37 KettleStepException (org.pentaho.di.core.exception.KettleStepException)36 ArrayList (java.util.ArrayList)35 ResultFile (org.pentaho.di.core.ResultFile)33 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)32 Result (org.pentaho.di.core.Result)32 OutputStream (java.io.OutputStream)29 FileName (org.apache.commons.vfs2.FileName)29 KettleValueException (org.pentaho.di.core.exception.KettleValueException)29 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)28