org.apache.commons.vfs2.Capability Java Examples

The following examples show how to use org.apache.commons.vfs2.Capability. 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: VfsClassLoaderTests.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Verify the package loaded with class loader.
 */
private void verifyPackage(final Package pack, final boolean sealed) {
    if (getBaseFolder().getFileSystem().hasCapability(Capability.MANIFEST_ATTRIBUTES)) {
        assertEquals("ImplTitle", pack.getImplementationTitle());
        assertEquals("ImplVendor", pack.getImplementationVendor());
        assertEquals("1.1", pack.getImplementationVersion());
        assertEquals("SpecTitle", pack.getSpecificationTitle());
        assertEquals("SpecVendor", pack.getSpecificationVendor());
        assertEquals("1.0", pack.getSpecificationVersion());
        assertEquals(sealed, pack.isSealed());
    } else {
        assertNull(pack.getImplementationTitle());
        assertNull(pack.getImplementationVendor());
        assertNull(pack.getImplementationVersion());
        assertNull(pack.getSpecificationTitle());
        assertNull(pack.getSpecificationVendor());
        assertNull(pack.getSpecificationVersion());
        assertFalse(pack.isSealed());
    }
}
 
Example #2
Source File: CanWriteFileFilter.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Checks to see if the file can be written to.
 *
 * @param fileInfo the File to check
 *
 * @return {@code true} if the file can be written to, otherwise {@code false}.
 * @throws FileSystemException Thrown for file system errors.
 */
@Override
public boolean accept(final FileSelectInfo fileInfo) throws FileSystemException {
    try (final FileObject file = fileInfo.getFile()) {
        final FileSystem fileSystem = file.getFileSystem();
        if (file.exists()) {
            if (!fileSystem.hasCapability(Capability.WRITE_CONTENT)) {
                return false;
            }
            return file.isWriteable();
        }
        if (!fileSystem.hasCapability(Capability.CREATE)) {
            return false;
        }
        return file.getParent().isWriteable();
    }
}
 
Example #3
Source File: VirtualFileSystem.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Adds the capabilities of this file system.
 */
@Override
protected void addCapabilities(final Collection<Capability> caps) {
    // TODO - this isn't really true
    caps.add(Capability.ATTRIBUTES);
    caps.add(Capability.CREATE);
    caps.add(Capability.DELETE);
    caps.add(Capability.GET_TYPE);
    caps.add(Capability.JUNCTIONS);
    caps.add(Capability.GET_LAST_MODIFIED);
    caps.add(Capability.SET_LAST_MODIFIED_FILE);
    caps.add(Capability.SET_LAST_MODIFIED_FOLDER);
    caps.add(Capability.LIST_CHILDREN);
    caps.add(Capability.READ_CONTENT);
    caps.add(Capability.SIGNING);
    caps.add(Capability.WRITE_CONTENT);
    caps.add(Capability.APPEND_CONTENT);
}
 
Example #4
Source File: MimeFileSystem.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
public Part createCommunicationLink() throws IOException, MessagingException {
    if (mimeStream != null) {
        closeMimeStream();
    }

    final FileObject parentLayer = getParentLayer();
    if (!parentLayer.exists()) {
        return null;
    }

    if (parentLayer.getFileSystem().hasCapability(Capability.RANDOM_ACCESS_READ)) {
        mimeStream = new SharedRandomContentInputStream(parentLayer);
    } else {
        mimeStream = getParentLayer().getContent().getInputStream();
    }
    return new MimeMessage(null, mimeStream);
}
 
Example #5
Source File: ProviderReadTests.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that folders have no content.
 */
public void testFolderContent() throws Exception {
    if (getFileSystem().hasCapability(Capability.DIRECTORY_READ_CONTENT)) {
        // test wont fail
        return;
    }

    // Try getting the content of a folder
    final FileObject folder = getReadFolderDir1();
    try {
        folder.getContent().getInputStream();
        fail();
    } catch (final FileSystemException e) {
        assertSameMessage("vfs.provider/read-not-file.error", folder, e);
    }
}
 
Example #6
Source File: SharedRandomContentInputStream.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
private SharedRandomContentInputStream(final Set<SharedRandomContentInputStream> createdStreams,
        final FileObject fo, final long fileStart, final long fileEnd, final InputStream is)
        throws FileSystemException {
    super(is);

    if (!fo.getFileSystem().hasCapability(Capability.RANDOM_ACCESS_READ)) {
        throw new FileSystemException("vfs.util/missing-capability.error", Capability.RANDOM_ACCESS_READ);
    }

    this.fo = fo;
    this.fileStart = fileStart;
    this.fileEnd = fileEnd;
    this.createdStreams = createdStreams;

    synchronized (createdStreams) {
        createdStreams.add(this);
    }
}
 
Example #7
Source File: UrlStructureTests.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that folders have no content.
 */
public void testFolderURL() throws Exception {
    final FileObject folder = getReadFolder().resolveFile("dir1");
    if (folder.getFileSystem().hasCapability(Capability.DIRECTORY_READ_CONTENT)) {
        // test might not fail on e.g. HttpFileSystem as there are no direcotries.
        // A Directory do have a content on http. e.g a generated directory listing or the index.html page.
        return;
    }

    assertTrue(folder.exists());

    // Try getting the content of a folder
    try {
        folder.getURL().openConnection().getInputStream();
        fail();
    } catch (final IOException e) {
        assertSameMessage("vfs.provider/read-not-file.error", folder, e);
    }
}
 
Example #8
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 #9
Source File: Shell.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
private void info(final String[] cmd) throws Exception {
    if (cmd.length > 1) {
        info(cmd[1]);
    } else {
        System.out.println(
                "Default manager: \"" + mgr.getClass().getName() + "\" " + "version " + getVersion(mgr.getClass()));
        final String[] schemes = mgr.getSchemes();
        final List<String> virtual = new ArrayList<>();
        final List<String> physical = new ArrayList<>();
        for (final String scheme : schemes) {
            final Collection<Capability> caps = mgr.getProviderCapabilities(scheme);
            if (caps != null) {
                if (caps.contains(Capability.VIRTUAL) || caps.contains(Capability.COMPRESS)
                        || caps.contains(Capability.DISPATCHER)) {
                    virtual.add(scheme);
                } else {
                    physical.add(scheme);
                }
            }
        }
        if (!physical.isEmpty()) {
            System.out.println("  Provider Schemes: " + physical);
        }
        if (!virtual.isEmpty()) {
            System.out.println("   Virtual Schemes: " + virtual);
        }
    }
}
 
Example #10
Source File: AbstractFileObject.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Returns an input/output stream to use to read and write the content of the file in and random manner.
 *
 * @param mode The RandomAccessMode.
 * @return The RandomAccessContent.
 * @throws FileSystemException if an error occurs.
 */
public RandomAccessContent getRandomAccessContent(final RandomAccessMode mode) throws FileSystemException {
    /*
     * VFS-210 if (!getType().hasContent()) { throw new FileSystemException("vfs.provider/read-not-file.error",
     * name); }
     */

    if (mode.requestRead()) {
        if (!fileSystem.hasCapability(Capability.RANDOM_ACCESS_READ)) {
            throw new FileSystemException("vfs.provider/random-access-read-not-supported.error");
        }
        if (!isReadable()) {
            throw new FileSystemException("vfs.provider/read-not-readable.error", fileName);
        }
    }

    if (mode.requestWrite()) {
        if (!fileSystem.hasCapability(Capability.RANDOM_ACCESS_WRITE)) {
            throw new FileSystemException("vfs.provider/random-access-write-not-supported.error");
        }
        if (!isWriteable()) {
            throw new FileSystemException("vfs.provider/write-read-only.error", fileName);
        }
    }

    // Get the raw input stream
    try {
        return doGetRandomAccessContent(mode);
    } catch (final Exception exc) {
        throw new FileSystemException("vfs.provider/random-access.error", fileName, exc);
    }
}
 
Example #11
Source File: AbstractProviderTestCase.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Runs the test. This implementation short-circuits the test if the provider being tested does not have the
 * capabilities required by this test.
 * <p>
 * TODO - Handle negative caps as well - ie, only run a test if the provider does not have certain caps.
 * </p>
 * <p>
 * TODO - Figure out how to remove the test from the TestResult if the test is skipped.
 * </p>
 */
@Override
protected void runTest() throws Throwable {
    // Check the capabilities
    final Capability[] caps = getRequiredCaps();
    if (caps != null) {
        for (final Capability cap2 : caps) {
            final Capability cap = cap2;
            final FileSystem fs = getFileSystem();
            if (!fs.hasCapability(cap)) {
                // String name = fs.getClass().getName();
                // int index = name.lastIndexOf('.');
                // String fsName = (index > 0) ? name.substring(index + 1) : name;
                // System.out.println("skipping " + getName() + " because " +
                // fsName + " does not have capability " + cap);
                return;
            }
        }
    }

    // Provider has all the capabilities - execute the test
    if (method != null) {
        try {
            method.invoke(this, (Object[]) null);
        } catch (final InvocationTargetException e) {
            throw e.getTargetException();
        }
    } else {
        super.runTest();
    }

    if (((AbstractFileSystem) readFolder.getFileSystem()).isOpen()) {
        String name = "unknown";
        if (method != null) {
            name = method.getName();
        }

        throw new IllegalStateException(getClass().getName() + ": filesystem has open streams after: " + name);
    }
}
 
Example #12
Source File: AbstractFileObject.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Prepares this file for writing. Makes sure it is either a file, or its parent folder exists. Returns an output
 * stream to use to write the content of the file to.
 *
 * @param bAppend true when append to the file.
 *            Note: If the underlying file system does not support appending, a FileSystemException is thrown.
 * @return An OutputStream where the new contents of the file can be written.
 * @throws FileSystemException if an error occurs; for example:
 *             bAppend is true, and the underlying FileSystem does not support it
 */
public OutputStream getOutputStream(final boolean bAppend) throws FileSystemException {
    /*
     * VFS-210 if (getType() != FileType.IMAGINARY && !getType().hasContent()) { throw new
     * FileSystemException("vfs.provider/write-not-file.error", name); } if (!isWriteable()) { throw new
     * FileSystemException("vfs.provider/write-read-only.error", name); }
     */

    if (bAppend && !fileSystem.hasCapability(Capability.APPEND_CONTENT)) {
        throw new FileSystemException("vfs.provider/write-append-not-supported.error", fileName);
    }

    if (getType() == FileType.IMAGINARY) {
        // Does not exist - make sure parent does
        final FileObject parent = getParent();
        if (parent != null) {
            parent.createFolder();
        }
    }

    // Get the raw output stream
    try {
        return doGetOutputStream(bAppend);
    } catch (final RuntimeException re) {
        throw re;
    } catch (final Exception exc) {
        throw new FileSystemException("vfs.provider/write.error", exc, fileName);
    }
}
 
Example #13
Source File: CopyTask.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Handles an out-of-date file.
 *
 * @param srcFile The source FileObject.
 * @param destFile The destination FileObject.
 */
@Override
protected void handleOutOfDateFile(final FileObject srcFile, final FileObject destFile) throws FileSystemException {
    log("Copying " + srcFile.getPublicURIString() + " to " + destFile.getPublicURIString());
    destFile.copyFrom(srcFile, Selectors.SELECT_SELF);
    if (preserveLastModified && srcFile.getFileSystem().hasCapability(Capability.GET_LAST_MODIFIED)
            && destFile.getFileSystem().hasCapability(Capability.SET_LAST_MODIFIED_FILE)) {
        final long lastModTime = srcFile.getContent().getLastModifiedTime();
        destFile.getContent().setLastModifiedTime(lastModTime);
    }
}
 
Example #14
Source File: GoogleDriveFileSystemTest.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@Test public void testFileSystem() {
  FileName fileName = mock( FileName.class );
  FileSystemOptions options = new FileSystemOptions();
  GoogleDriveFileSystem fileSystem = new GoogleDriveFileSystem( fileName, new FileSystemOptions() );
  Collection<Capability> fileSystemCapabilities = new ArrayList<>();
  fileSystem.addCapabilities( fileSystemCapabilities );
  assertTrue( capabilities.containsAll( fileSystemCapabilities ) );
}
 
Example #15
Source File: LastModifiedTests.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Tests setting the last modified time of a folder.
 *
 * @throws FileSystemException if error occurred
 */
public void testSetLastModifiedFolder() throws FileSystemException {
    final long yesterday = System.currentTimeMillis() - 24 * 60 * 60 * 1000;

    if (getReadFolder().getFileSystem().hasCapability(Capability.SET_LAST_MODIFIED_FOLDER)) {
        // Try a folder
        final FileObject folder = getReadFolder().resolveFile("dir1");
        folder.getContent().setLastModifiedTime(yesterday);
        final long lastModTimeAccuracy = (long) folder.getFileSystem().getLastModTimeAccuracy();
        // folder.refresh(); TODO: does not work with SSH VFS-563
        final long lastModifiedTime = folder.getContent().getLastModifiedTime();
        assertDelta("set/getLastModified on Folder", yesterday, lastModifiedTime, lastModTimeAccuracy);
    }
}
 
Example #16
Source File: LastModifiedTests.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Tests setting the last modified time of file.
 *
 * @throws FileSystemException if error occurred
 */
public void testSetLastModifiedFile() throws FileSystemException {
    final long yesterday = System.currentTimeMillis() - 24 * 60 * 60 * 1000;

    if (getReadFolder().getFileSystem().hasCapability(Capability.SET_LAST_MODIFIED_FILE)) {
        // Try a file
        final FileObject file = getReadFolder().resolveFile("file1.txt");
        file.getContent().setLastModifiedTime(yesterday);
        final long lastModTimeAccuracy = (long) file.getFileSystem().getLastModTimeAccuracy();
        // folder.refresh(); TODO: does not work with SSH VFS-563
        final long lastModifiedTime = file.getContent().getLastModifiedTime();
        assertDelta("set/getLastModified on File", yesterday, lastModifiedTime, lastModTimeAccuracy);
    }
}
 
Example #17
Source File: Shell.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
private void info(final String scheme) throws Exception {
    System.out.println("Provider Info for scheme \"" + scheme + "\":");
    Collection<Capability> caps;
    caps = mgr.getProviderCapabilities(scheme);
    if (caps != null && !caps.isEmpty()) {
        System.out.println("  capabilities: " + caps);
    }
    final FileOperationProvider[] ops = mgr.getOperationProviders(scheme);
    if (ops != null && ops.length > 0) {
        System.out.println("  operations: " + Arrays.toString(ops));
    }
}
 
Example #18
Source File: ProviderRandomReadTests.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the capabilities required by the tests of this test case.
 */
@Override
protected Capability[] getRequiredCaps() {
    return new Capability[] { Capability.GET_TYPE, Capability.RANDOM_ACCESS_READ };
}
 
Example #19
Source File: LastModifiedTests.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the capabilities required by the tests of this test case.
 */
@Override
protected Capability[] getRequiredCaps() {
    return new Capability[] { Capability.GET_LAST_MODIFIED };
}
 
Example #20
Source File: UriTests.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the capabilities required by the tests of this test case.
 */
@Override
protected Capability[] getRequiredCaps() {
    return new Capability[] { Capability.URI };
}
 
Example #21
Source File: ProviderRandomReadWriteTests.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the capabilities required by the tests of this test case.
 */
@Override
protected Capability[] getRequiredCaps() {
    return new Capability[] { Capability.GET_TYPE, Capability.CREATE, Capability.RANDOM_ACCESS_READ,
            Capability.RANDOM_ACCESS_WRITE };
}
 
Example #22
Source File: ProviderCacheStrategyTests.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the capabilities required by the tests of this test case.
 */
@Override
protected Capability[] getRequiredCaps() {
    return new Capability[] { Capability.CREATE, Capability.GET_TYPE, Capability.LIST_CHILDREN, };
}
 
Example #23
Source File: VfsClassLoaderTests.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the capabilities required by the tests of this test case.
 */
@Override
protected Capability[] getRequiredCaps() {
    return new Capability[] { Capability.READ_CONTENT, Capability.URI };
}
 
Example #24
Source File: TgzFileProvider.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
@Override
public Collection<Capability> getCapabilities() {
    return capabilities;
}
 
Example #25
Source File: HdfsFileSystem.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
/**
 * @see org.apache.commons.vfs2.provider.AbstractFileSystem#addCapabilities(Collection)
 */
@Override
protected void addCapabilities(final Collection<Capability> capabilities) {
    capabilities.addAll(HdfsFileProvider.CAPABILITIES);
}
 
Example #26
Source File: AbstractFileObject.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
/**
 * Moves (rename) the file to another one.
 *
 * @param destFile The target FileObject.
 * @throws FileSystemException if an error occurs.
 */
@Override
public void moveTo(final FileObject destFile) throws FileSystemException {
    if (canRenameTo(destFile)) {
        if (!getParent().isWriteable()) {
            throw new FileSystemException("vfs.provider/rename-parent-read-only.error", getName(),
                    getParent().getName());
        }
    } else {
        if (!isWriteable()) {
            throw new FileSystemException("vfs.provider/rename-read-only.error", getName());
        }
    }

    if (destFile.exists() && !isSameFile(destFile)) {
        destFile.deleteAll();
        // throw new FileSystemException("vfs.provider/rename-dest-exists.error", destFile.getName());
    }

    if (canRenameTo(destFile)) {
        // issue rename on same filesystem
        try {
            attach();
            // remember type to avoid attach
            final FileType srcType = getType();

            doRename(destFile);

            FileObjectUtils.getAbstractFileObject(destFile).handleCreate(srcType);
            destFile.close(); // now the destFile is no longer imaginary. force reattach.

            handleDelete(); // fire delete-events. This file-object (src) is like deleted.
        } catch (final RuntimeException re) {
            throw re;
        } catch (final Exception exc) {
            throw new FileSystemException("vfs.provider/rename.error", exc, getName(), destFile.getName());
        }
    } else {
        // different fs - do the copy/delete stuff

        destFile.copyFrom(this, Selectors.SELECT_SELF);

        if ((destFile.getType().hasContent()
                && destFile.getFileSystem().hasCapability(Capability.SET_LAST_MODIFIED_FILE)
                || destFile.getType().hasChildren()
                        && destFile.getFileSystem().hasCapability(Capability.SET_LAST_MODIFIED_FOLDER))
                && fileSystem.hasCapability(Capability.GET_LAST_MODIFIED)) {
            destFile.getContent().setLastModifiedTime(this.getContent().getLastModifiedTime());
        }

        deleteSelf();
    }

}
 
Example #27
Source File: UrlFileProvider.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
@Override
public Collection<Capability> getCapabilities() {
    return capabilities;
}
 
Example #28
Source File: UrlFileSystem.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the capabilities of this file system.
 */
@Override
protected void addCapabilities(final Collection<Capability> caps) {
    caps.addAll(UrlFileProvider.capabilities);
}
 
Example #29
Source File: ResourceFileProvider.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
@Override
public Collection<Capability> getCapabilities() {
    return capabilities;
}
 
Example #30
Source File: Http4FileSystem.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
@Override
protected void addCapabilities(final Collection<Capability> caps) {
    caps.addAll(Http4FileProvider.capabilities);
}