public Boolean fileExists(ServerDetailsDTO conDetails, String filePath) { FileObject fileObject = null; StandardFileSystemManager manager = null; try { manager = new StandardFileSystemManager(); manager.init(); UserAuthenticator auth = new StaticUserAuthenticator(null, conDetails.getUserName(), conDetails.getPassword()); FileSystemOptions opts = new FileSystemOptions(); DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth); SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no"); String fileUri = buildUri(conDetails, filePath); fileObject = manager.resolveFile(fileUri, opts); return fileObject.exists() && fileObject.isReadable(); } catch (FileSystemException e) { log.error("cannot access the server", e); } finally { try { if (fileObject != null) { ((SftpFileSystem) fileObject.getFileSystem()).closeCommunicationLink(); FileSystem fs = null; fs = fileObject.getFileSystem(); manager.closeFileSystem(fs); } } finally { } } return null; }
public byte[] readFile(ServerDetailsDTO conDetails, String filePath) throws IOException { FileObject fileObject = null; StandardFileSystemManager manager = null; try { manager = new StandardFileSystemManager(); manager.init(); UserAuthenticator auth = new StaticUserAuthenticator(null, conDetails.getUserName(), conDetails.getPassword()); FileSystemOptions opts = new FileSystemOptions(); DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth); SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no"); String fileUri = buildUri(conDetails, filePath); fileObject = manager.resolveFile(fileUri, opts); if (fileObject.isReadable()) { InputStream is = fileObject.getContent().getInputStream(); int size = (int) fileObject.getContent().getSize(); byte[] fileContent = new byte[size]; for (int index = 0; size > 0; index += 4000, size -= 4000) { is.read(fileContent, index, size < 4000 ? size : 4000); if (Thread.currentThread().isInterrupted()) { return null; } } return fileContent; } return null; } finally { try { if (fileObject != null) { fileObject.getContent().close(); ((SftpFileSystem) fileObject.getFileSystem()).closeCommunicationLink(); FileSystem fs = null; fs = fileObject.getFileSystem(); manager.closeFileSystem(fs); } } finally { } } }
public Result execute(Result previousResult, int nr, Repository rep, Job parentJob) { LogWriter log = LogWriter.getInstance(); Result result = previousResult; result.setResult(false); if (filename != null) { String realFilename = getRealFilename(); try { FileObject file = KettleVFS.getFileObject(realFilename); if (file.exists() && file.isReadable()) { log.logDetailed( toString(), Messages.getString("JobEntryFileExists.File_Exists", realFilename)); // $NON-NLS-1$ result.setResult(true); } else { log.logDetailed( toString(), Messages.getString( "JobEntryFileExists.File_Does_Not_Exist", realFilename)); // $NON-NLS-1$ } } catch (IOException e) { result.setNrErrors(1); log.logError( toString(), Messages.getString( "JobEntryFileExists.ERROR_0004_IO_Exception", e.toString())); // $NON-NLS-1$ } } else { result.setNrErrors(1); log.logError( toString(), Messages.getString("JobEntryFileExists.ERROR_0005_No_Filename_Defined")); // $NON-NLS-1$ } return result; }
public Object execute(final CommandContext context) throws Exception { assert context != null; IO io = context.getIo(); FileObject file = resolveFile(context, path); io.println("URL: {}", file.getURL()); io.println("Name: {}", file.getName()); io.println("BaseName: {}", file.getName().getBaseName()); io.println("Extension: {}", file.getName().getExtension()); io.println("Path: {}", file.getName().getPath()); io.println("Scheme: {}", file.getName().getScheme()); io.println("URI: {}", file.getName().getURI()); io.println("Root URI: {}", file.getName().getRootURI()); io.println("Parent: {}", file.getName().getParent()); io.println("Type: {}", file.getType()); io.println("Exists: {}", file.exists()); io.println("Readable: {}", file.isReadable()); io.println("Writeable: {}", file.isWriteable()); io.println("Root path: {}", file.getFileSystem().getRoot().getName().getPath()); if (file.exists()) { FileContent content = file.getContent(); FileContentInfo contentInfo = content.getContentInfo(); io.println("Content type: {}", contentInfo.getContentType()); io.println("Content encoding: {}", contentInfo.getContentEncoding()); try { // noinspection unchecked Map<String, Object> attrs = content.getAttributes(); if (attrs != null && !attrs.isEmpty()) { io.println("Attributes:"); for (Map.Entry<String, Object> entry : attrs.entrySet()) { io.println(" {}='{}'", entry.getKey(), entry.getValue()); } } } catch (FileSystemException e) { io.println("File attributes are NOT supported"); } try { Certificate[] certs = content.getCertificates(); if (certs != null && certs.length != 0) { io.println("Certificate:"); for (Certificate cert : certs) { io.println(" {}", cert); } } } catch (FileSystemException e) { io.println("File certificates are NOT supported"); } if (file.getType().equals(FileType.FILE)) { io.println("Size: {} bytes", content.getSize()); } else if (file.getType().hasChildren() && file.isReadable()) { FileObject[] children = file.getChildren(); io.println("Directory with {} files", children.length); for (int iterChildren = 0; iterChildren < children.length; iterChildren++) { io.println("#{}:{}", iterChildren, children[iterChildren].getName()); if (iterChildren > 5) { break; } } } io.println( "Last modified: {}", DateFormat.getInstance().format(new Date(content.getLastModifiedTime()))); } else { io.println("The file does not exist"); } FileObjects.close(file); return Result.SUCCESS; }