public SimpleJsonExtractor(WorkUnitState workUnitState) throws FileSystemException { this.workUnitState = workUnitState; // Resolve the file to pull if (workUnitState.getPropAsBoolean(ConfigurationKeys.SOURCE_CONN_USE_AUTHENTICATION, false)) { // Add authentication credential if authentication is needed UserAuthenticator auth = new StaticUserAuthenticator(workUnitState.getProp(ConfigurationKeys.SOURCE_CONN_DOMAIN, ""), workUnitState.getProp(ConfigurationKeys.SOURCE_CONN_USERNAME), PasswordManager.getInstance(workUnitState) .readPassword(workUnitState.getProp(ConfigurationKeys.SOURCE_CONN_PASSWORD))); FileSystemOptions opts = new FileSystemOptions(); DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth); this.fileObject = VFS.getManager().resolveFile(workUnitState.getProp(SOURCE_FILE_KEY), opts); } else { this.fileObject = VFS.getManager().resolveFile(workUnitState.getProp(SOURCE_FILE_KEY)); } // Open the file for reading LOGGER.info("Opening file " + this.fileObject.getURL().toString()); this.bufferedReader = this.closer.register(new BufferedReader(new InputStreamReader(this.fileObject.getContent().getInputStream(), ConfigurationKeys.DEFAULT_CHARSET_ENCODING))); }
fileContent = replaceWinEOL( fileContent ); tempFile.createFile(); OutputStream outputStream = tempFile.getContent().getOutputStream(); outputStream.write( fileContent.getBytes() ); outputStream.close(); if ( !isWindows ) { String tempFilename = KettleVFS.getFilename( tempFile );
throwsErrorOnFileNotFound( file ); is = KettleVFS.getInputStream( file ); int fileSize = (int) file.getContent().getSize(); content = Const.createByteArray( fileSize ); is.read( content, 0, fileSize ); } catch ( KettleFileNotFoundException e ) { if ( failIfNoFile ) {
/** * Returns a attribute value for the given domain (namespace) and attribute-name. Some generic attribute domains and * names are defined as constants in the {@link LibRepositoryBoot} class. * * @param domain the attribute domain. * @param key the name of the attribute. * @return the value or null, if the content-entity does not have a value for this attribute. */ public Object getAttribute( final String domain, final String key ) { try { if ( LibRepositoryBoot.REPOSITORY_DOMAIN.equals( domain ) ) { if ( LibRepositoryBoot.SIZE_ATTRIBUTE.equals( key ) ) { return new Long( backend.getContent().getSize() ); } else if ( LibRepositoryBoot.VERSION_ATTRIBUTE.equals( key ) ) { return new Date( backend.getContent().getLastModifiedTime() ); } } } catch ( FileSystemException ex ) { throw new RuntimeException( ex ); } return null; }
/** * Prepare file-dependent data for fill additional fields. */ protected void fillFileAdditionalFields( D data, FileObject file ) throws FileSystemException { data.shortFilename = file.getName().getBaseName(); data.path = KettleVFS.getFilename( file.getParent() ); data.hidden = file.isHidden(); data.extension = file.getName().getExtension(); data.uriName = file.getName().getURI(); data.rootUriName = file.getName().getRootURI(); if ( file.getType().hasContent() ) { data.lastModificationDateTime = new Date( file.getContent().getLastModifiedTime() ); data.size = file.getContent().getSize(); } else { data.lastModificationDateTime = null; data.size = null; } } }
@Test public void testOpenConnectionUseKey_2() throws Exception { when( fileObject.exists() ).thenReturn( true ); when( fileObject.getContent() ).thenReturn( fileContent ); when( fileContent.getSize() ).thenReturn( 1000L ); when( fileContent.getInputStream() ).thenReturn( new ByteArrayInputStream( new byte[] { 1, 2, 3, 4, 5 } ) ); when( variableSpace.environmentSubstitute( passPhrase ) ).thenReturn( passPhrase ); when( connection.authenticateWithPublicKey( eq( username ), Matchers.<char[]>any(), eq( passPhrase ) ) ).thenReturn( true ); SSHData.OpenConnection( server, port, username, null, true, keyFilePath, passPhrase, 0, variableSpace, null, 0, null, null ); verify( connection ).connect(); verify( connection ).authenticateWithPublicKey( eq( username ), Matchers.<char[]>any(), eq( passPhrase ) ); }
String shortSourceFilename = sourceFileObject.getName().getBaseName(); int lenstring = shortSourceFilename.length(); int lastindexOfDot = shortSourceFilename.lastIndexOf( '.' ); String zipFilename = "zip:" + sourceFileObject.getName().getFriendlyURI(); FileObject zipFile = KettleVFS.getFileObject( zipFilename, this ); FileObject[] items = zipFile.findFiles( new AllFileSelector() { if ( log.isDetailed() ) { logDetailed( BaseMessages.getString( PKG, "JobUnZip.Log.ProcessingZipEntry", item.getName().getURI(), sourceFileObject.toString() ) ); int len; while ( ( len = is.read( buff ) ) > 0 ) { os.write( buff, 0, len ); is.close(); os.close(); if ( setOriginalModificationDate ) { newFileObject.getContent().setLastModifiedTime( item.getContent().getLastModifiedTime() ); KettleVFS.getInstance().getFileSystemManager().closeFileSystem( item.getFileSystem() ); if ( items != null ) { items = null;
private InputStream createNonEmptyFile() throws FileSystemException, IOException { final FileObject root = manager.resolveFile("ram://file"); root.createFile(); final FileContent content = root.getContent(); final OutputStream output = this.closeOnTearDown(content.getOutputStream()); output.write(1); output.write(2); output.write(3); output.flush(); output.close(); return this.closeOnTearDown(content.getInputStream()); }
/** * Tests concurrent reads on a file. */ public void testReadSingleSequencial() throws Exception { final FileObject file = getReadFolder().resolveFile("file1.txt"); assertTrue(file.exists()); file.getContent().getInputStream().close(); file.getContent().getInputStream().close(); }
@Test public void testMarkingWorks() throws Exception { final File temp = File.createTempFile("temp-file-name", ".tmp"); final FileSystemManager fileSystemManager = VFS.getManager(); try (FileObject file = fileSystemManager.resolveFile(temp.getAbsolutePath())) { try (OutputStream outputStream = file.getContent().getOutputStream()) { outputStream.write(expected.getBytes()); outputStream.flush(); } try (InputStream stream = file.getContent().getInputStream()) { if (stream.markSupported()) { for (int i = 0; i < 10; i++) { stream.mark(0); final byte[] data = new byte[100]; stream.read(data, 0, 7); Assert.assertEquals(expected, new String(data).trim()); stream.reset(); } } } } }
private void resolveAndOpenCloseInputStream() throws IOException, FileSystemException { try (final FileObject zipFileObject = manager.resolveFile(zipFileUri)) { zipFileObject.getContent().getInputStream().close(); } }
private void loadLogFileAsContent(String data, TabWithName target) throws IOException { final FileObject tempFileWithClipboard = VFS.getManager().resolveFile("clipboard://clipboard_"+System.currentTimeMillis()); tempFileWithClipboard.createFile(); final OutputStream outputStream = tempFileWithClipboard.getContent().getOutputStream(); outputStream.write(data.getBytes()); outputStream.flush(); outputStream.close(); final LogImporter logImporter = logParserComboBox.getItemAt(logParserComboBox.getSelectedIndex()); if (target.getLogDataCollector().isPresent()){ final LogViewPanelI logViewPanelI = target.getLogDataCollector().get(); getOtrosApplication().getLogLoader().startLoading(new VfsSource(tempFileWithClipboard),logImporter,logViewPanelI); } else { final String tabTitle = new SimpleDateFormat("HH:mm:ss").format(new Date()); new TailLogActionListener(getOtrosApplication(), logImporter) .openFileObjectInTailMode(tempFileWithClipboard, "Clipboard " + tabTitle); } }
private HSSFWorkbook createWorkbook( FileObject file ) throws Exception { HSSFWorkbook wb = null; OutputStream os = null; try { os = file.getContent().getOutputStream(); wb = new HSSFWorkbook(); wb.createSheet( SHEET_NAME ); wb.write( os ); } finally { os.flush(); os.close(); } return wb; }
InputStream imageStream = null; try ( FileObject imageFile = KettleVFS.getFileObject( data.realHeaderImage ) ) { if ( !imageFile.exists() ) { throw new KettleException( BaseMessages.getString( PKG, "ExcelInputLog.ImageFileNotExists", data.realHeaderImage ) ); data.headerImageHeight = m.getHeight() * 0.0625; byte[] imageData = new byte[(int) imageFile.getContent().getSize()]; imageStream = KettleVFS.getInputStream( imageFile ); imageStream.read( imageData );
private void writeFile(FileObject file, String content) throws IOException { if (!file.isWriteable()) { throw new IOException("Can't write into file"); } try (OutputStream outputStream = file.getContent().getOutputStream()) { if (content != null) { outputStream.write(content.getBytes(Charset.forName("UTF-8"))); //$NON-NLS-1$ } } }
private static String createVirtualFile( String filename, String... rows ) throws Exception { String virtualFile = TestUtils.createRamFile( filename ); StringBuilder content = new StringBuilder(); if ( rows != null ) { for ( String row : rows ) { content.append( row ); } } ByteArrayOutputStream bos = new ByteArrayOutputStream(); bos.write( content.toString().getBytes() ); OutputStream os = KettleVFS.getFileObject( virtualFile ).getContent().getOutputStream(); try { IOUtils.copy( new ByteArrayInputStream( bos.toByteArray() ), os ); } finally { os.close(); } return virtualFile; }
@Override public RepositoryObject getObjectInformation( ObjectId objectId, RepositoryObjectType objectType ) throws KettleException { try { String filename = calcDirectoryName( null ); if ( objectId.getId().startsWith( "/" ) ) { filename += objectId.getId().substring( 1 ); } else { filename += objectId.getId(); } FileObject fileObject = KettleVFS.getFileObject( filename ); if ( !fileObject.exists() ) { return null; } FileName fname = fileObject.getName(); String name = fname.getBaseName(); if ( !Utils.isEmpty( fname.getExtension() ) && name.length() > fname.getExtension().length() ) { name = name.substring( 0, name.length() - fname.getExtension().length() - 1 ); } String filePath = fileObject.getParent().getName().getPath(); final FileObject baseDirObject = KettleVFS.getFileObject( repositoryMeta.getBaseDirectory() ); final int baseDirObjectPathLength = baseDirObject.getName().getPath().length(); final String dirPath = baseDirObjectPathLength <= filePath.length() ? filePath.substring( baseDirObjectPathLength ) : "/"; RepositoryDirectoryInterface directory = loadRepositoryDirectoryTree().findDirectory( dirPath ); Date lastModified = new Date( fileObject.getContent().getLastModifiedTime() ); return new RepositoryObject( objectId, name, directory, "-", lastModified, objectType, "", false ); } catch ( Exception e ) { throw new KettleException( "Unable to get object information for object with id=" + objectId, e ); } }
FileSystemManager fsManager = VFS.getManager(); if (fsManager == null) { throw newError("Cannot get virtual file system manager"); FileObject file = fsManager.resolveFile(userDir, url); FileContent fileContent = null; try { file.refresh(); && !file.getName().getURI().equals(url)) fsManager.getFilesCache().removeFile( file.getFileSystem(), file.getName()); file = fsManager.resolveFile(userDir, url); return fileContent.getInputStream();
@Override public List<RepositoryElementMetaInterface> getJobObjects( ObjectId id_directory, boolean includeDeleted ) throws KettleException { try { List<RepositoryElementMetaInterface> list = new ArrayList<RepositoryElementMetaInterface>(); RepositoryDirectoryInterface tree = loadRepositoryDirectoryTree(); RepositoryDirectoryInterface directory = tree.findDirectory( id_directory ); String folderName = calcDirectoryName( directory ); FileObject folder = KettleVFS.getFileObject( folderName ); for ( FileObject child : folder.getChildren() ) { if ( child.getType().equals( FileType.FILE ) ) { if ( !child.isHidden() || !repositoryMeta.isHidingHiddenFiles() ) { String name = child.getName().getBaseName(); if ( name.endsWith( EXT_JOB ) ) { String jobName = name.substring( 0, name.length() - 4 ); ObjectId id = new StringObjectId( calcObjectId( directory, jobName, EXT_JOB ) ); Date date = new Date( child.getContent().getLastModifiedTime() ); list.add( new RepositoryObject( id, jobName, directory, "-", date, RepositoryObjectType.JOB, "", false ) ); } } } } return list; } catch ( Exception e ) { throw new KettleException( "Unable to get list of jobs in folder with id : " + id_directory, e ); } }
@Override public InputStream getInputStream(final URL url) throws ConfigurationException { FileObject file; try { final FileSystemOptions opts = getOptions(url.getProtocol()); file = (opts == null) ? VFS.getManager().resolveFile(url.toString()) : VFS.getManager().resolveFile(url.toString(), opts); if (file.getType() != FileType.FILE) { throw new ConfigurationException("Cannot load a configuration from a directory"); } final FileContent content = file.getContent(); if (content == null) { final String msg = "Cannot access content of " + file.getName().getFriendlyURI(); throw new ConfigurationException(msg); } return content.getInputStream(); } catch (final FileSystemException fse) { final String msg = "Unable to access " + url.toString(); throw new ConfigurationException(msg, fse); } }