org.apache.commons.vfs2.Selectors Java Examples

The following examples show how to use org.apache.commons.vfs2.Selectors. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ProviderDeleteTests.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Sets up a scratch folder for the test to use.
 */
protected FileObject createScratchFolder() throws Exception {
    final FileObject scratchFolder = getWriteFolder();

    // Make sure the test folder is empty
    scratchFolder.delete(Selectors.EXCLUDE_SELF);
    scratchFolder.createFolder();

    final FileObject dir1 = scratchFolder.resolveFile("dir1");
    dir1.createFolder();
    final FileObject dir1file1 = dir1.resolveFile("a.txt");
    dir1file1.createFile();
    final FileObject dir2 = scratchFolder.resolveFile("dir2");
    dir2.createFolder();
    final FileObject dir2file1 = dir2.resolveFile("b.txt");
    dir2file1.createFile();

    return scratchFolder;
}
 
Example #2
Source File: VFSFileProvider.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
/**
 * @param file
 * @param toPath
 * @param overwrite
 * @return
 * @throws FileException
 */
@Override
public VFSFile copy( VFSFile file, String toPath, boolean overwrite ) throws FileException {
  try {
    FileObject fileObject = KettleVFS
      .getFileObject( file.getPath(), new Variables(), VFSHelper.getOpts( file.getPath(), file.getConnection() ) );
    FileObject copyObject =
      KettleVFS.getFileObject( toPath, new Variables(), VFSHelper.getOpts( file.getPath(), file.getConnection() ) );
    copyObject.copyFrom( fileObject, Selectors.SELECT_SELF );
    if ( file instanceof VFSDirectory ) {
      return VFSDirectory
        .create( copyObject.getParent().getPublicURIString(), fileObject, file.getConnection(), file.getDomain() );
    } else {
      return VFSFile
        .create( copyObject.getParent().getPublicURIString(), fileObject, file.getConnection(), file.getDomain() );
    }
  } catch ( KettleFileException | FileSystemException e ) {
    throw new FileException();
  }
}
 
Example #3
Source File: ProviderWriteTests.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Tests file copy to and from the same file system type. This was a problem w/ FTP.
 */
public void testCopySameFileSystem() throws Exception {
    final FileObject scratchFolder = createScratchFolder();

    // Create direct child of the test folder
    final FileObject file = scratchFolder.resolveFile("file1.txt");
    assertFalse(file.exists());

    // Create the source file
    final String content = "Here is some sample content for the file.  Blah Blah Blah.";
    final OutputStream os = file.getContent().getOutputStream();
    try {
        os.write(content.getBytes("utf-8"));
    } finally {
        os.close();
    }

    assertSameContent(content, file);

    // Make sure we can copy the new file to another file on the same filesystem
    final FileObject fileCopy = scratchFolder.resolveFile("file1copy.txt");
    assertFalse(fileCopy.exists());
    fileCopy.copyFrom(file, Selectors.SELECT_SELF);

    assertSameContent(content, fileCopy);
}
 
Example #4
Source File: ProviderCacheStrategyTests.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Test the on_call strategy
 */
public void testOnCallCache() throws Exception {
    final FileObject scratchFolder = getWriteFolder();
    if (FileObjectUtils.isInstanceOf(getBaseFolder(), RamFileObject.class)
            || scratchFolder.getFileSystem() instanceof VirtualFileSystem) {
        // cant check ram filesystem as every manager holds its own ram filesystem data
        return;
    }

    scratchFolder.delete(Selectors.EXCLUDE_SELF);

    final DefaultFileSystemManager fs = createManager();
    fs.setCacheStrategy(CacheStrategy.ON_CALL);
    fs.init();
    final FileObject foBase2 = getBaseTestFolder(fs);

    final FileObject cachedFolder = foBase2.resolveFile(scratchFolder.getName().getPath());

    FileObject[] fos = cachedFolder.getChildren();
    assertContainsNot(fos, "file1.txt");

    scratchFolder.resolveFile("file1.txt").createFile();

    fos = cachedFolder.getChildren();
    assertContains(fos, "file1.txt");
}
 
Example #5
Source File: OldVFSNotebookRepo.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
@Override
public void remove(String noteId, AuthenticationInfo subject) throws IOException {
  FileObject rootDir = fsManager.resolveFile(getPath("/"));
  FileObject noteDir = rootDir.resolveFile(noteId, NameScope.CHILD);

  if (!noteDir.exists()) {
    // nothing to do
    return;
  }

  if (!isDirectory(noteDir)) {
    // it is not look like zeppelin note savings
    throw new IOException("Can not remove " + noteDir.getName().toString());
  }

  noteDir.delete(Selectors.SELECT_SELF_AND_CHILDREN);
}
 
Example #6
Source File: PermissionsTests.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
private FileObject createTestFile() throws Exception {
    // Get the scratch folder
    final FileObject scratchFolder = getWriteFolder();
    assertNotNull(scratchFolder);

    // Make sure the test folder is empty
    scratchFolder.delete(Selectors.EXCLUDE_SELF);
    scratchFolder.createFolder();

    // Create direct child of the test folder
    final FileObject file = scratchFolder.resolveFile(FILENAME);
    assertFalse(file.exists());

    // Create the source file
    final String content = "Here is some sample content for the file.  Blah Blah Blah.";

    final OutputStream os = file.getContent().getOutputStream();
    try {
        os.write(content.getBytes("utf-8"));
    } finally {
        os.close();
    }
    return file;
}
 
Example #7
Source File: UrlTests.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Tests FindFiles with a file name that has a hash sign in it.
 */
public void testHashFindFiles() throws Exception {
    final FileSystemManager fsManager = VFS.getManager();

    final FileObject[] foList = getBaseFolder().findFiles(Selectors.SELECT_FILES);

    boolean hashFileFound = false;
    for (final FileObject fo : foList) {
        if (fo.getURL().toString().contains("test-hash")) {
            hashFileFound = true;

            assertEquals(fo.toString(), UriParser.decode(fo.getURL().toString()));
        }
    }

    if (!hashFileFound) {
        fail("Test hash file containing 'test-hash' not found");
    }
}
 
Example #8
Source File: TarFileSystem.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
protected TarFileSystem(final AbstractFileName rootName, final FileObject parentLayer,
        final FileSystemOptions fileSystemOptions) throws FileSystemException {
    super(rootName, parentLayer, fileSystemOptions);

    // Make a local copy of the file
    file = parentLayer.getFileSystem().replicateFile(parentLayer, Selectors.SELECT_SELF);

    // Open the Tar file
    if (!file.exists()) {
        // Don't need to do anything
        tarFile = null;
        return;
    }

    // tarFile = createTarFile(this.file);
}
 
Example #9
Source File: MoveTask.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Handles a single source file.
 */
@Override
protected void handleOutOfDateFile(final FileObject srcFile, final FileObject destFile) throws FileSystemException {
    if (!tryRename || !srcFile.canRenameTo(destFile)) {
        super.handleOutOfDateFile(srcFile, destFile);

        log("Deleting " + srcFile.getPublicURIString());
        srcFile.delete(Selectors.SELECT_SELF);
    } else {
        log("Rename " + srcFile.getPublicURIString() + " to " + destFile.getPublicURIString());
        srcFile.moveTo(destFile);
        if (!isPreserveLastModified()
                && destFile.getFileSystem().hasCapability(Capability.SET_LAST_MODIFIED_FILE)) {
            destFile.getContent().setLastModifiedTime(System.currentTimeMillis());
        }
    }
}
 
Example #10
Source File: TestRunCelosServerModeEmbedded.java    From celos with Apache License 2.0 5 votes vote down vote up
public void copyRemoteDefaultsToLocal(String username, URI defaultsDirUri)
        throws URISyntaxException, FileSystemException {
    JScpWorker worker = new JScpWorker(username);
    if (defaultsDirUri != null) {
        FileObject remoteDefaultsDir = worker.getFileObjectByUri(defaultsDirUri);
        if (remoteDefaultsDir.exists()) {
            FileObject localDefaultsDir = worker.getFileObjectByUri(getCelosDefaultsDir());
            localDefaultsDir.copyFrom(remoteDefaultsDir, Selectors.SELECT_CHILDREN);
        }
    }
}
 
Example #11
Source File: VFSNotebookRepo.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
@Override
public void remove(String noteId, String notePath, AuthenticationInfo subject)
    throws IOException {
  LOGGER.info("Remove note: " + noteId + ", notePath: " + notePath);
  FileObject noteFile = rootNotebookFileObject.resolveFile(
      buildNoteFileName(noteId, notePath), NameScope.DESCENDENT);
  noteFile.delete(Selectors.SELECT_SELF);
}
 
Example #12
Source File: Shell.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Does a 'cp' command.
 */
private void cp(final String[] cmd) throws Exception {
    if (cmd.length < 3) {
        throw new Exception("USAGE: cp <src> <dest>");
    }

    final FileObject src = mgr.resolveFile(cwd, cmd[1]);
    FileObject dest = mgr.resolveFile(cwd, cmd[2]);
    if (dest.exists() && dest.getType() == FileType.FOLDER) {
        dest = dest.resolveFile(src.getName().getBaseName());
    }

    dest.copyFrom(src, Selectors.SELECT_ALL);
}
 
Example #13
Source File: Shell.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Does an 'rm' command.
 */
private void rm(final String[] cmd) throws Exception {
    if (cmd.length < 2) {
        throw new Exception("USAGE: rm <path>");
    }

    final FileObject file = mgr.resolveFile(cwd, cmd[1]);
    file.delete(Selectors.SELECT_SELF);
}
 
Example #14
Source File: Webdav4VersioningTests.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Sets up a scratch folder for the test to use.
 */
protected FileObject createScratchFolder() throws Exception {
    final FileObject scratchFolder = getWriteFolder();

    // Make sure the test folder is empty
    scratchFolder.delete(Selectors.EXCLUDE_SELF);
    scratchFolder.createFolder();

    return scratchFolder;
}
 
Example #15
Source File: ProviderRandomReadWriteTests.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Sets up a scratch folder for the test to use.
 */
protected FileObject createScratchFolder() throws Exception {
    final FileObject scratchFolder = getWriteFolder();

    // Make sure the test folder is empty
    scratchFolder.delete(Selectors.EXCLUDE_SELF);
    scratchFolder.createFolder();

    return scratchFolder;
}
 
Example #16
Source File: ProviderRandomSetLengthTests.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Sets up a scratch folder for the test to use.
 */
protected FileObject createScratchFolder() throws Exception {
    final FileObject scratchFolder = this.getWriteFolder();

    // Make sure the test folder is empty
    scratchFolder.delete(Selectors.EXCLUDE_SELF);
    scratchFolder.createFolder();

    return scratchFolder;
}
 
Example #17
Source File: ProviderRenameTests.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Sets up a scratch folder for the test to use.
 */
protected FileObject createScratchFolder() throws Exception {
    final FileObject scratchFolder = getWriteFolder();

    // Make sure the test folder is empty
    scratchFolder.delete(Selectors.EXCLUDE_SELF);
    scratchFolder.createFolder();

    return scratchFolder;
}
 
Example #18
Source File: ProviderWriteAppendTests.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Tests create-delete-create-a-file sequence on the same file system.
 */
public void testAppendContent() throws Exception {
    final FileObject scratchFolder = createScratchFolder();

    // Create direct child of the test folder
    final FileObject file = scratchFolder.resolveFile("file1.txt");
    assertFalse(file.exists());

    // Create the source file
    final String content = "Here is some sample content for the file.  Blah Blah Blah.";
    final String contentAppend = content + content;

    final OutputStream os = file.getContent().getOutputStream();
    try {
        os.write(content.getBytes("utf-8"));
    } finally {
        os.close();
    }
    assertSameContent(content, file);

    // Append to the new file
    final OutputStream os2 = file.getContent().getOutputStream(true);
    try {
        os2.write(content.getBytes("utf-8"));
    } finally {
        os2.close();
    }
    assertSameContent(contentAppend, file);

    // Make sure we can copy the new file to another file on the same filesystem
    final FileObject fileCopy = scratchFolder.resolveFile("file1copy.txt");
    assertFalse(fileCopy.exists());
    fileCopy.copyFrom(file, Selectors.SELECT_SELF);

    assertSameContent(contentAppend, fileCopy);

    // Delete the file.
    assertTrue(fileCopy.exists());
    assertTrue(fileCopy.delete());
}
 
Example #19
Source File: ProviderWriteAppendTests.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Sets up a scratch folder for the test to use.
 */
protected FileObject createScratchFolder() throws Exception {
    final FileObject scratchFolder = getWriteFolder();

    // Make sure the test folder is empty
    scratchFolder.delete(Selectors.EXCLUDE_SELF);
    scratchFolder.createFolder();

    return scratchFolder;
}
 
Example #20
Source File: ProviderWriteTests.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Tests create-delete-create-a-file sequence on the same file system.
 */
public void testCreateDeleteCreateSameFileSystem() throws Exception {
    final FileObject scratchFolder = createScratchFolder();

    // Create direct child of the test folder
    final FileObject file = scratchFolder.resolveFile("file1.txt");
    assertFalse(file.exists());

    // Create the source file
    final String content = "Here is some sample content for the file.  Blah Blah Blah.";
    final OutputStream os = file.getContent().getOutputStream();
    try {
        os.write(content.getBytes("utf-8"));
    } finally {
        os.close();
    }

    assertSameContent(content, file);

    // Make sure we can copy the new file to another file on the same filesystem
    final FileObject fileCopy = scratchFolder.resolveFile("file1copy.txt");
    assertFalse(fileCopy.exists());
    fileCopy.copyFrom(file, Selectors.SELECT_SELF);

    assertSameContent(content, fileCopy);

    // Delete the file.
    assertTrue(fileCopy.exists());
    assertTrue(fileCopy.delete());

    // Make sure we can copy the same new file to the same target file on the same filesystem
    assertFalse(fileCopy.exists());
    fileCopy.copyFrom(file, Selectors.SELECT_SELF);

    assertSameContent(content, fileCopy);
}
 
Example #21
Source File: ProviderWriteTests.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Tests overwriting a file on the same file system.
 */
public void testCopyFromOverwriteSameFileSystem() throws Exception {
    final FileObject scratchFolder = createScratchFolder();

    // Create direct child of the test folder
    final FileObject file = scratchFolder.resolveFile("file1.txt");
    assertFalse(file.exists());

    // Create the source file
    final String content = "Here is some sample content for the file.  Blah Blah Blah.";
    final OutputStream os = file.getContent().getOutputStream();
    try {
        os.write(content.getBytes("utf-8"));
    } finally {
        os.close();
    }

    assertSameContent(content, file);

    // Make sure we can copy the new file to another file on the same filesystem
    final FileObject fileCopy = scratchFolder.resolveFile("file1copy.txt");
    assertFalse(fileCopy.exists());
    fileCopy.copyFrom(file, Selectors.SELECT_SELF);

    assertSameContent(content, fileCopy);

    // Make sure we can copy the same new file to the same target file on the same filesystem
    assertTrue(fileCopy.exists());
    fileCopy.copyFrom(file, Selectors.SELECT_SELF);

    assertSameContent(content, fileCopy);
}
 
Example #22
Source File: DataSpaceNodeConfigurationAgent.java    From scheduling with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void run() {
    try {
        long invalidationPeriod = getCacheInvalidationPeriod();
        long currentTime = System.currentTimeMillis();
        // lock the timer in write mode, this will prevent any Task to start during the cleaning process
        if (cacheCleaningRWLock.writeLock().tryLock()) {
            try {
                FileObject rootFO = fileSystemManager.resolveFile(rootCacheUri);
                if (!rootFO.exists()) {
                    rootFO.createFolder();
                }
                FileObject[] files = rootFO.findFiles(Selectors.EXCLUDE_SELF);
                if (files != null) {
                    for (FileObject file : files) {
                        if (currentTime - file.getContent().getLastModifiedTime() > invalidationPeriod) {
                            logger.info("[Cache Space cleaner] deleting " + file);
                            file.delete();
                        }
                    }
                }
            } finally {
                cacheCleaningRWLock.writeLock().unlock();
            }
        }
    } catch (Exception e) {
        logger.error("Error when cleaning files in cache", e);
    }
}
 
Example #23
Source File: ProviderWriteTests.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Sets up a scratch folder for the test to use.
 */
protected FileObject createScratchFolder() throws Exception {
    final FileObject scratchFolder = getWriteFolder();

    // Make sure the test folder is empty
    scratchFolder.delete(Selectors.EXCLUDE_SELF);
    scratchFolder.createFolder();

    return scratchFolder;
}
 
Example #24
Source File: ProviderCacheStrategyTests.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Test the on_resolve strategy
 */
public void testOnResolveCache() throws Exception {
    final FileObject scratchFolder = getWriteFolder();
    if (FileObjectUtils.isInstanceOf(getBaseFolder(), RamFileObject.class)
            || scratchFolder.getFileSystem() instanceof VirtualFileSystem) {
        // cant check ram filesystem as every manager holds its own ram filesystem data
        return;
    }

    scratchFolder.delete(Selectors.EXCLUDE_SELF);

    final DefaultFileSystemManager fs = createManager();
    fs.setCacheStrategy(CacheStrategy.ON_RESOLVE);
    fs.init();
    final FileObject foBase2 = getBaseTestFolder(fs);

    FileObject cachedFolder = foBase2.resolveFile(scratchFolder.getName().getPath());

    FileObject[] fos = cachedFolder.getChildren();
    assertContainsNot(fos, "file1.txt");

    scratchFolder.resolveFile("file1.txt").createFile();

    fos = cachedFolder.getChildren();
    assertContainsNot(fos, "file1.txt");

    cachedFolder = foBase2.resolveFile(scratchFolder.getName().getPath());
    fos = cachedFolder.getChildren();
    assertContains(fos, "file1.txt");
}
 
Example #25
Source File: ProviderCacheStrategyTests.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Test the manual cache strategy
 */
public void testManualCache() throws Exception {
    final FileObject scratchFolder = getWriteFolder();
    if (FileObjectUtils.isInstanceOf(getBaseFolder(), RamFileObject.class)
            || scratchFolder.getFileSystem() instanceof VirtualFileSystem) {
        // cant check ram filesystem as every manager holds its own ram filesystem data
        return;
    }

    scratchFolder.delete(Selectors.EXCLUDE_SELF);

    final DefaultFileSystemManager fs = createManager();
    fs.setCacheStrategy(CacheStrategy.MANUAL);
    fs.init();
    final FileObject foBase2 = getBaseTestFolder(fs);

    final FileObject cachedFolder = foBase2.resolveFile(scratchFolder.getName().getPath());

    FileObject[] fos = cachedFolder.getChildren();
    assertContainsNot(fos, "file1.txt");

    scratchFolder.resolveFile("file1.txt").createFile();

    fos = cachedFolder.getChildren();
    assertContainsNot(fos, "file1.txt");

    cachedFolder.refresh();
    fos = cachedFolder.getChildren();
    assertContains(fos, "file1.txt");
}
 
Example #26
Source File: WorkflowFilesDeployer.java    From celos with Apache License 2.0 5 votes vote down vote up
private void deployJSFile(URI dirUri, String fileName) throws URISyntaxException, IOException {
    if (dirUri != null) {
        URI fileUri = getTargetJsFileUri(dirUri);
        File localFile = new File(config.getDeployDir(), fileName);

        if (localFile.exists()) {
            parser.validateJsSyntax(new FileReader(localFile), localFile.getName());

            FileObject sftpFile = jScpWorker.getFileObjectByUri(fileUri);
            FileObject localFileObject = jScpWorker.getFileObjectByUri(localFile.getAbsolutePath());
            sftpFile.copyFrom(localFileObject, Selectors.SELECT_SELF);
        }
    }
}
 
Example #27
Source File: SftpFileTransferLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenUploadFileUsingApacheVfs_thenSuccess() throws IOException {
    FileSystemManager manager = VFS.getManager();
    FileObject local = manager.resolveFile(System.getProperty("user.dir") + "/" + localFile);
    FileObject remote = manager.resolveFile("sftp://" + username + ":" + password + "@" + remoteHost + "/" + remoteDir + "vfsFile.txt");
    remote.copyFrom(local, Selectors.SELECT_SELF);
    local.close();
    remote.close();
}
 
Example #28
Source File: SftpFileTransferLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenDownloadFileUsingApacheVfs_thenSuccess() throws IOException {
    FileSystemManager manager = VFS.getManager();
    FileObject local = manager.resolveFile(System.getProperty("user.dir") + "/" + localDir + "vfsFile.txt");
    FileObject remote = manager.resolveFile("sftp://" + username + ":" + password + "@" + remoteHost + "/" + remoteFile);
    local.copyFrom(remote, Selectors.SELECT_SELF);
    local.close();
    remote.close();
}
 
Example #29
Source File: AbstractFileObject.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Returns an iterator over a set of all FileObject in this file object.
 *
 * @return an Iterator.
 */
@Override
public Iterator<FileObject> iterator() {
    try {
        return listFiles(Selectors.SELECT_ALL).iterator();
    } catch (final FileSystemException e) {
        throw new IllegalStateException(e);
    }
}
 
Example #30
Source File: WebdavVersioningTests.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Sets up a scratch folder for the test to use.
 */
protected FileObject createScratchFolder() throws Exception {
    final FileObject scratchFolder = getWriteFolder();

    // Make sure the test folder is empty
    scratchFolder.delete(Selectors.EXCLUDE_SELF);
    scratchFolder.createFolder();

    return scratchFolder;
}