List of usage examples for org.apache.commons.vfs FileObject getContent
public FileContent getContent() throws FileSystemException;
From source file:org.ow2.proactive.scripting.helper.filetransfer.driver.SFTP_VFS_Driver.java
public void getFile(String remotePath, String destFolderPath) throws Exception { connect();/*from w w w.j a v a 2 s.c om*/ debug("Getting file " + remotePath + " to local folder " + destFolderPath); String fileName = (new File(remotePath).getName()); String localPath = destFolderPath + File.separator + fileName; // we first set strict key checking off FileSystemOptions fsOptions = new FileSystemOptions(); SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(fsOptions, "no"); // now we create a new filesystem manager // the url is of form sftp://user:pass@host/remotepath/ String uri = "sftp://" + _user + ":" + _pass + "@" + _host + "/" + remotePath; // get file object representing the local file FileObject fo = fsManager.resolveFile(uri, fsOptions); // open input stream from the remote file BufferedInputStream is = new BufferedInputStream(fo.getContent().getInputStream()); // open output stream to local file OutputStream os = new BufferedOutputStream(new FileOutputStream(localPath)); int c; // do copying while ((c = is.read()) != -1) { os.write(c); } os.close(); is.close(); // close the file object fo.close(); debug("File copied " + remotePath + " to local folder " + destFolderPath); // NOTE: if you close the file system manager, you won't be able to // use VFS again in the same VM. If you wish to copy multiple files, // make the fsManager static, initialize it once, and close just // before exiting the process. // fsManager.close(); //System.out.println("Finished copying the file"); disconnect(); }
From source file:org.ow2.proactive.scripting.helper.filetransfer.driver.SFTP_VFS_Driver.java
public void putFile(String localPathFile, String remoteFolder) throws Exception { if (remoteFolder == "") remoteFolder = "."; debug("Putting file " + localPathFile + " to " + remoteFolder); //--Setup the SCP connection connect();//from www . j a va2s . c om //--Define paths // String localFolder = FileTransfertUtils.getFolderFromPathfile(localPathFile); String fileName = new File(localPathFile).getName(); // we first set strict key checking off FileSystemOptions fsOptions = new FileSystemOptions(); SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(fsOptions, "no"); // now we create a new filesystem manager // the url is of form sftp://user:pass@host/remotepath/ String uri = "sftp://" + _user + ":" + _pass + "@" + _host + "/" + remoteFolder + "/" + fileName; // get file object representing the local file FileObject fo = fsManager.resolveFile(uri, fsOptions); fo.createFile(); OutputStream os = fo.getContent().getOutputStream(); BufferedInputStream is = new BufferedInputStream(new FileInputStream(new File(localPathFile))); int c; // do copying while ((c = is.read()) != -1) { os.write(c); } os.close(); is.close(); fo.close(); debug("File copied :" + localPathFile + " to " + remoteFolder); //--Logout and disconnect disconnect(); }
From source file:org.pentaho.agilebi.test.MetadataToMondrianVfsTest.java
@Test public void testVfs() throws Exception { ((DefaultFileSystemManager) VFS.getManager()).addProvider("mtm", new MetadataToMondrianVfs()); FileSystemManager fsManager = VFS.getManager(); FileObject fobj = fsManager.resolveFile("mtm:test-res/example_olap.xmi"); StringBuilder buf = new StringBuilder(1000); InputStream in = fobj.getContent().getInputStream(); int n;/*from w w w. j ava 2 s . c o m*/ while ((n = in.read()) != -1) { buf.append((char) n); } in.close(); String results = buf.toString(); Assert.assertTrue(results.indexOf("<Cube name=\"customer2 Table\">") >= 0); }
From source file:org.pentaho.di.core.row.ValueDataUtil.java
public static Object loadFileContentInBinary(ValueMetaInterface metaA, Object dataA) throws KettleValueException { if (dataA == null) { return null; }//from w w w. j a va2s . c om FileObject file = null; FileInputStream fis = null; try { file = KettleVFS.getFileObject(dataA.toString()); fis = (FileInputStream) ((LocalFile) file).getInputStream(); int fileSize = (int) file.getContent().getSize(); byte[] content = Const.createByteArray(fileSize); fis.read(content, 0, fileSize); return content; } catch (Exception e) { throw new KettleValueException(e); } finally { try { if (fis != null) { fis.close(); } fis = null; if (file != null) { file.close(); } file = null; } catch (Exception e) { // Ignore } } }
From source file:org.pentaho.di.core.vfs.KettleVFS.java
public static OutputStream getOutputStream(FileObject fileObject, boolean append) throws IOException { FileObject parent = fileObject.getParent(); if (parent != null) { if (!parent.exists()) { throw new IOException(BaseMessages.getString(PKG, "KettleVFS.Exception.ParentDirectoryDoesNotExist", getFilename(parent))); }// w ww . ja va2 s.c om } try { fileObject.createFile(); FileContent content = fileObject.getContent(); return content.getOutputStream(append); } catch (FileSystemException e) { // Perhaps if it's a local file, we can retry using the standard // File object. This is because on Windows there is a bug in VFS. // if (fileObject instanceof LocalFile) { try { String filename = getFilename(fileObject); return new FileOutputStream(new File(filename), append); } catch (Exception e2) { throw e; // throw the original exception: hide the retry. } } else { throw e; } } }
From source file:org.pentaho.di.core.xml.XMLCheck.java
/** * Checks an xml file is well formed./*from www .j av a 2s .c om*/ * * @param file * The file to check * @return true if the file is well formed. */ public static final boolean isXMLFileWellFormed(FileObject file) throws KettleException { boolean retval = false; try { retval = isXMLWellFormed(file.getContent().getInputStream()); } catch (Exception e) { throw new KettleException(e); } return retval; }
From source file:org.pentaho.di.job.entries.evalfilesmetrics.JobEntryEvalFilesMetrics.java
private void getFileSize(FileObject file, Result result, Job parentJob) { try {//from w w w . j a v a 2s .co m incrementFilesCount(); if (isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobEvalFilesMetrics.Log.GetFile", file.toString(), String.valueOf(getFilesCount()))); } switch (evaluationType) { case EVALUATE_TYPE_SIZE: BigDecimal fileSize = BigDecimal.valueOf(file.getContent().getSize()); evaluationValue = evaluationValue.add(fileSize); if (isDebug()) { logDebug(BaseMessages.getString(PKG, "JobEvalFilesMetrics.Log.AddedFileSize", String.valueOf(fileSize), file.toString())); } break; default: evaluationValue = evaluationValue.add(ONE); break; } } catch (Exception e) { incrementErrors(); logError(BaseMessages.getString(PKG, "JobEvalFilesMetrics.Error.GettingFileSize", file.toString(), e.toString())); } }
From source file:org.pentaho.di.job.entries.folderscompare.JobEntryFoldersCompare.java
public Result execute(Result previousResult, int nr) { Result result = previousResult; result.setResult(false);/* ww w. j a v a2 s . c o m*/ boolean ok = true; String realFilename1 = getRealFilename1(); String realFilename2 = getRealFilename2(); FileObject folder1 = null; FileObject folder2 = null; FileObject filefolder1 = null; FileObject filefolder2 = null; try { if (filename1 != null && filename2 != null) { // Get Folders/Files to compare folder1 = KettleVFS.getFileObject(realFilename1, this); folder2 = KettleVFS.getFileObject(realFilename2, this); if (folder1.exists() && folder2.exists()) { if (!folder1.getType().equals(folder2.getType())) { // pb...we try to compare file with folder !!! logError(BaseMessages.getString(PKG, "JobFoldersCompare.Log.CanNotCompareFilesFolders")); if (folder1.getType() == FileType.FILE) { logError(BaseMessages.getString(PKG, "JobFoldersCompare.Log.IsAFile", realFilename1)); } else if (folder1.getType() == FileType.FOLDER) { logError(BaseMessages.getString(PKG, "JobFoldersCompare.Log.IsAFolder", realFilename1)); } else { logError(BaseMessages.getString(PKG, "JobFoldersCompare.Log.IsUnknownFileType", realFilename1)); } if (folder2.getType() == FileType.FILE) { logError(BaseMessages.getString(PKG, "JobFoldersCompare.Log.IsAFile", realFilename2)); } else if (folder2.getType() == FileType.FOLDER) { logError(BaseMessages.getString(PKG, "JobFoldersCompare.Log.IsAFolder", realFilename2)); } else { logError(BaseMessages.getString(PKG, "JobFoldersCompare.Log.IsUnknownFileType", realFilename2)); } } else { if (folder1.getType() == FileType.FILE) { // simply compare 2 files .. if (equalFileContents(folder1, folder2)) { result.setResult(true); } else { result.setResult(false); } } else if (folder1.getType() == FileType.FOLDER) { // We compare 2 folders ... FileObject[] list1 = folder1.findFiles(new TextFileSelector(folder1.toString())); FileObject[] list2 = folder2.findFiles(new TextFileSelector(folder2.toString())); int lenList1 = list1.length; int lenList2 = list2.length; if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobFoldersCompare.Log.FolderContains", realFilename1, "" + lenList1)); logDetailed(BaseMessages.getString(PKG, "JobFoldersCompare.Log.FolderContains", realFilename2, "" + lenList2)); } if (lenList1 == lenList2) { HashMap<String, String> collection1 = new HashMap<String, String>(); HashMap<String, String> collection2 = new HashMap<String, String>(); for (int i = 0; i < list1.length; i++) { // Put files list1 in TreeMap collection1 collection1.put(list1[i].getName().getBaseName(), list1[i].toString()); } for (int i = 0; i < list2.length; i++) { // Put files list2 in TreeMap collection2 collection2.put(list2[i].getName().getBaseName(), list2[i].toString()); } // Let's now fetch Folder1 // and for each entry, we will search it in Folder2 // if the entry exists..we will compare file entry (file or folder?) // if the 2 entry are file (not folder), we will compare content Set<Map.Entry<String, String>> entrees = collection1.entrySet(); Iterator<Map.Entry<String, String>> iterateur = entrees.iterator(); while (iterateur.hasNext()) { Map.Entry<String, String> entree = iterateur.next(); if (!collection2.containsKey(entree.getKey())) { ok = false; if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobFoldersCompare.Log.FileCanNotBeFoundIn", entree.getKey().toString(), realFilename2)); } } else { if (log.isDebug()) { logDebug(BaseMessages.getString(PKG, "JobFoldersCompare.Log.FileIsFoundIn", entree.getKey().toString(), realFilename2)); } filefolder1 = KettleVFS.getFileObject(entree.getValue().toString(), this); filefolder2 = KettleVFS .getFileObject(collection2.get(entree.getKey()).toString(), this); if (!filefolder2.getType().equals(filefolder1.getType())) { // The file1 exist in the folder2..but they don't have the same type ok = false; if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobFoldersCompare.Log.FilesNotSameType", filefolder1.toString(), filefolder2.toString())); } if (filefolder1.getType() == FileType.FILE) { logError(BaseMessages.getString(PKG, "JobFoldersCompare.Log.IsAFile", filefolder1.toString())); } else if (filefolder1.getType() == FileType.FOLDER) { logError(BaseMessages.getString(PKG, "JobFoldersCompare.Log.IsAFolder", filefolder1.toString())); } else { logError(BaseMessages.getString(PKG, "JobFoldersCompare.Log.IsUnknownFileType", filefolder1.toString())); } if (filefolder2.getType() == FileType.FILE) { logError(BaseMessages.getString(PKG, "JobFoldersCompare.Log.IsAFile", filefolder2.toString())); } else if (filefolder2.getType() == FileType.FOLDER) { logError(BaseMessages.getString(PKG, "JobFoldersCompare.Log.IsAFolder", filefolder2.toString())); } else { logError(BaseMessages.getString(PKG, "JobFoldersCompare.Log.IsUnknownFileType", filefolder2.toString())); } } else { // Files are the same type ... if (filefolder2.getType() == FileType.FILE) { // Let's compare file size if (comparefilesize) { long filefolder1_size = filefolder1.getContent().getSize(); long filefolder2_size = filefolder2.getContent().getSize(); if (filefolder1_size != filefolder2_size) { ok = false; if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobFoldersCompare.Log.FilesNotSameSize", filefolder1.toString(), filefolder2.toString())); logDetailed(BaseMessages.getString(PKG, "JobFoldersCompare.Log.SizeFileIs", filefolder1.toString(), "" + filefolder1_size)); logDetailed(BaseMessages.getString(PKG, "JobFoldersCompare.Log.SizeFileIs", filefolder2.toString(), "" + filefolder2_size)); } } } if (ok) { // Let's compare files content.. if (comparefilecontent) { if (!equalFileContents(filefolder1, filefolder2)) { ok = false; if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobFoldersCompare.Log.FilesNotSameContent", filefolder1.toString(), filefolder2.toString())); } } } } } } } // logBasic(entree.getKey() + " - " + entree.getValue()); } result.setResult(ok); } else { // The 2 folders don't have the same files number if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "JobFoldersCompare.Log.FoldersDifferentFiles", realFilename1.toString(), realFilename2.toString())); } } } // else: File type unknown !! } } else { if (!folder1.exists()) { logError(BaseMessages.getString(PKG, "JobFileCompare.Log.FileNotExist", realFilename1)); } if (!folder2.exists()) { logError(BaseMessages.getString(PKG, "JobFileCompare.Log.FileNotExist", realFilename2)); } result.setResult(false); result.setNrErrors(1); } } else { logError(BaseMessages.getString(PKG, "JobFoldersCompare.Log.Need2Files")); } } catch (Exception e) { result.setResult(false); result.setNrErrors(1); logError(BaseMessages.getString(PKG, "JobFoldersCompare.Log.ErrorComparing", realFilename2, realFilename2, e.getMessage())); } finally { try { if (folder1 != null) { folder1.close(); folder1 = null; } if (folder2 != null) { folder2.close(); folder2 = null; } if (filefolder1 != null) { filefolder1.close(); filefolder1 = null; } if (filefolder2 != null) { filefolder2.close(); filefolder2 = null; } } catch (IOException e) { // Ignore errors } } return result; }
From source file:org.pentaho.di.job.entries.ftpsget.FTPSConnection.java
/** * * this method is used to upload a file to a remote host * * @param localFileName//from w ww .j av a2 s . c om * Local full filename * @param shortFileName * Filename in remote host * @throws KettleException */ public void uploadFile(String localFileName, String shortFileName) throws KettleException { FileObject file = null; try { file = KettleVFS.getFileObject(localFileName); this.connection.uploadStream(file.getContent().getInputStream(), new FTPFile(new File(shortFileName))); } catch (Exception e) { throw new KettleException(BaseMessages.getString(PKG, "JobFTPS.Error.UuploadingFile", localFileName), e); } finally { if (file != null) { try { file.close(); } catch (Exception e) { // Ignore close errors } } } }
From source file:org.pentaho.di.job.entries.hadooptransjobexecutor.DistributedCacheUtil.java
/** * Extract a zip archive to a directory. * * @param archive Zip archive to extract * @param dest Destination directory. This must not exist! * @return Directory the zip was extracted into * @throws IllegalArgumentException when the archive file does not exist or the destination directory already exists * @throws IOException//from ww w . j a v a 2 s . c om * @throws KettleFileException */ public FileObject extract(FileObject archive, FileObject dest) throws IOException, KettleFileException { if (!archive.exists()) { throw new IllegalArgumentException("archive does not exist: " + archive.getURL().getPath()); } if (dest.exists()) { throw new IllegalArgumentException("destination already exists"); } dest.createFolder(); try { byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; int len = 0; ZipInputStream zis = new ZipInputStream(archive.getContent().getInputStream()); try { ZipEntry ze; while ((ze = zis.getNextEntry()) != null) { FileObject entry = KettleVFS.getFileObject(dest + Const.FILE_SEPARATOR + ze.getName()); if (ze.isDirectory()) { entry.createFolder(); continue; } OutputStream os = KettleVFS.getOutputStream(entry, false); try { while ((len = zis.read(buffer)) > 0) { os.write(buffer, 0, len); } } finally { if (os != null) { os.close(); } } } } finally { if (zis != null) { zis.close(); } } } catch (Exception ex) { // Try to clean up the temp directory and all files if (!deleteDirectory(dest)) { throw new KettleFileException("Could not clean up temp dir after error extracting", ex); } throw new KettleFileException("error extracting archive", ex); } return dest; }