Java Code Examples for org.apache.commons.vfs2.FileSystemException#requireNonNull()

The following examples show how to use org.apache.commons.vfs2.FileSystemException#requireNonNull() . 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: DefaultFileOperations.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * @param operationClass The Class that performs the operation.
 * @return The FileOperation.
 * @throws FileSystemException if an error occurs.
 */
@Override
public FileOperation getOperation(final Class<? extends FileOperation> operationClass) throws FileSystemException {

    final String scheme = fileObject.getURL().getProtocol();
    final FileOperationProvider[] providers = fsmanager.getOperationProviders(scheme);

    FileSystemException.requireNonNull(providers, "vfs.operation/operation-not-supported.error", operationClass);

    FileOperation resultOperation = null;

    for (final FileOperationProvider provider : providers) {
        resultOperation = provider.getOperation(fileObject, operationClass);

        if (resultOperation != null) {
            break;
        }
    }

    return FileSystemException.requireNonNull(resultOperation, "vfs.operation/operation-not-supported.error", operationClass);
}
 
Example 2
Source File: FtpFileObject.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Creates an output stream to write the file content to.
 */
@Override
protected OutputStream doGetOutputStream(final boolean bAppend) throws Exception {
    final FtpClient client = getAbstractFileSystem().getClient();
    try {
        OutputStream out = null;
        if (bAppend) {
            out = client.appendFileStream(relPath);
        } else {
            out = client.storeFileStream(relPath);
        }

        FileSystemException.requireNonNull(out, "vfs.provider.ftp/output-error.debug", this.getName(),
                client.getReplyString());

        return new FtpOutputStream(client, out);
    } catch (final Exception e) {
        getAbstractFileSystem().putClient(client);
        throw e;
    }
}
 
Example 3
Source File: ResourceFileProvider.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Locates a file object, by absolute URI.
 *
 * @param baseFile The base file.
 * @param uri The URI of the file to locate.
 * @param fileSystemOptions The FileSystem options.
 * @return the FileObject.
 * @throws FileSystemException if an error occurs.
 */
@Override
public FileObject findFile(final FileObject baseFile, final String uri, final FileSystemOptions fileSystemOptions)
        throws FileSystemException {
    final FileName fileName;
    if (baseFile != null) {
        fileName = parseUri(baseFile.getName(), uri);
    }
    else {
        fileName = parseUri(null, uri);
    }
    final String resourceName = fileName.getPath();

    ClassLoader classLoader = ResourceFileSystemConfigBuilder.getInstance().getClassLoader(fileSystemOptions);
    if (classLoader == null) {
        classLoader = getClass().getClassLoader();
    }
    FileSystemException.requireNonNull(classLoader, "vfs.provider.url/badly-formed-uri.error", uri);
    final URL url = classLoader.getResource(resourceName);

    FileSystemException.requireNonNull(url, "vfs.provider.url/badly-formed-uri.error", uri);

    return getContext().getFileSystemManager().resolveFile(url.toExternalForm());
}
 
Example 4
Source File: StandardFileSystemManager.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Initializes this manager. Adds the providers and replicator.
 *
 * @throws FileSystemException if an error occurs.
 */
@Override
public void init() throws FileSystemException {
    // Set the replicator and temporary file store (use the same component)
    final DefaultFileReplicator replicator = createDefaultFileReplicator();
    setReplicator(new PrivilegedFileReplicator(replicator));
    setTemporaryFileStore(replicator);

    if (configUri == null) {
        // Use default config
        final URL url = getClass().getResource(CONFIG_RESOURCE);
        FileSystemException.requireNonNull(url, "vfs.impl/find-config-file.error", CONFIG_RESOURCE);
        configUri = url;
    }

    configure(configUri);
    configurePlugins();

    // Initialise super-class
    super.init();
}
 
Example 5
Source File: RamFileSystem.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Delete a file
 *
 * @param file
 * @throws FileSystemException
 */
void delete(final RamFileObject file) throws FileSystemException {
    // root is read only check
    FileSystemException.requireNonNull(file.getParent(), "unable to delete root");

    // Remove reference from cache
    this.cache.remove(file.getName());
    // Notify the parent
    final RamFileObject parent = (RamFileObject) this.resolveFile(file.getParent().getName());
    parent.getData().removeChild(file.getData());
    parent.close();
    // Close the file
    file.getData().clear();
    file.close();
}
 
Example 6
Source File: RamFileData.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Add a child.
 *
 * @param data The file data.
 * @throws FileSystemException if an error occurs.
 */
void addChild(final RamFileData data) throws FileSystemException {
    if (!this.getType().hasChildren()) {
        throw new FileSystemException("A child can only be added in a folder");
    }

    FileSystemException.requireNonNull(data, "No child can be null");

    if (this.children.contains(data)) {
        throw new FileSystemException("Child already exists. " + data);
    }

    this.children.add(data);
    updateLastModified();
}
 
Example 7
Source File: FtpFileObject.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
FtpInputStream getInputStream(final long filePointer) throws IOException {
    final FtpClient client = getAbstractFileSystem().getClient();
    try {
        final InputStream instr = client.retrieveFileStream(relPath, filePointer);
        FileSystemException.requireNonNull(instr, "vfs.provider.ftp/input-error.debug", this.getName(),
                client.getReplyString());
        return new FtpInputStream(client, instr);
    } catch (final IOException e) {
        getAbstractFileSystem().putClient(client);
        throw e;
    }
}
 
Example 8
Source File: HttpFileObject.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the last modified time of this file.
 * <p>
 * This implementation throws an exception.
 * </p>
 */
@Override
protected long doGetLastModifiedTime() throws Exception {
    final Header header = method.getResponseHeader("last-modified");
    FileSystemException.requireNonNull(header, "vfs.provider.http/last-modified.error", getName());
    return DateUtil.parseDate(header.getValue()).getTime();
}
 
Example 9
Source File: DelegatingFileSystemOptionsBuilder.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Creates the list of all set*() methods for the given scheme
 */
private Map<String, List<Method>> createSchemeMethods(final String scheme) throws FileSystemException {
    final FileSystemConfigBuilder fscb = getManager().getFileSystemConfigBuilder(scheme);
    FileSystemException.requireNonNull(fscb, "vfs.provider/no-config-builder.error", scheme);

    final Map<String, List<Method>> schemeMethods = new TreeMap<>();

    final Method[] methods = fscb.getClass().getMethods();
    for (final Method method : methods) {
        if (!Modifier.isPublic(method.getModifiers())) {
            continue;
        }

        final String methodName = method.getName();
        if (!methodName.startsWith("set")) {
            // not a setter
            continue;
        }

        final String key = methodName.substring(3).toLowerCase();

        List<Method> configSetter = schemeMethods.get(key);
        if (configSetter == null) {
            configSetter = new ArrayList<>(2);
            schemeMethods.put(key, configSetter);
        }
        configSetter.add(method);
    }

    return schemeMethods;
}
 
Example 10
Source File: DefaultFileSystemManager.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Resolve the uri to a file name.
 *
 * @param uri The URI to resolve.
 * @return The FileName of the file.
 * @throws FileSystemException if an error occurs.
 */
@Override
public FileName resolveURI(final String uri) throws FileSystemException {
    UriParser.checkUriEncoding(uri);

    if (uri == null) {
        throw new IllegalArgumentException();
    }

    // Extract the scheme
    final String scheme = UriParser.extractScheme(getSchemes(), uri);
    if (scheme != null) {
        // An absolute URI - locate the provider
        final FileProvider provider = providers.get(scheme);
        if (provider != null) {
            return provider.parseUri(null, uri);
        }

        // Otherwise, assume a local file
    }

    // Handle absolute file names
    if (localFileProvider != null && localFileProvider.isAbsoluteLocalName(uri)) {
        return localFileProvider.parseUri(null, uri);
    }

    if (scheme != null) {
        // An unknown scheme - hand it to the default provider
        FileSystemException.requireNonNull(defaultProvider, "vfs.impl/unknown-scheme.error", scheme, uri);
        return defaultProvider.parseUri(null, uri);
    }

    // Assume a relative name - use the supplied base file
    FileSystemException.requireNonNull(baseFile, "vfs.impl/find-rel-file.error", uri);

    return resolveName(baseFile.getName(), uri, NameScope.FILE_SYSTEM);
}
 
Example 11
Source File: DefaultFileSystemManager.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
/**
 * Resolves a URI, relative to a base file with specified FileSystem configuration.
 *
 * @param baseFile The base file.
 * @param uri The file name. May be a fully qualified or relative path or a url.
 * @param fileSystemOptions Options to pass to the file system.
 * @return A FileObject representing the target file.
 * @throws FileSystemException if an error occurs accessing the file.
 */
public FileObject resolveFile(final FileObject baseFile, final String uri,
        final FileSystemOptions fileSystemOptions) throws FileSystemException {
    final FileObject realBaseFile;
    if (baseFile != null && VFS.isUriStyle() && baseFile.getName().isFile()) {
        realBaseFile = baseFile.getParent();
    } else {
        realBaseFile = baseFile;
    }
    // TODO: use resolveName and use this name to resolve the fileObject

    UriParser.checkUriEncoding(uri);

    if (uri == null) {
        throw new IllegalArgumentException();
    }

    // Extract the scheme
    final String scheme = UriParser.extractScheme(getSchemes(), uri);
    if (scheme != null) {
        // An absolute URI - locate the provider
        final FileProvider provider = providers.get(scheme);
        if (provider != null) {
            return provider.findFile(realBaseFile, uri, fileSystemOptions);
        }
        // Otherwise, assume a local file
    }

    // Handle absolute file names
    if (localFileProvider != null && localFileProvider.isAbsoluteLocalName(uri)) {
        return localFileProvider.findLocalFile(uri);
    }

    if (scheme != null) {
        // An unknown scheme - hand it to the default provider
        FileSystemException.requireNonNull(defaultProvider, "vfs.impl/unknown-scheme.error", scheme, uri);
        return defaultProvider.findFile(realBaseFile, uri, fileSystemOptions);
    }

    // Assume a relative name - use the supplied base file
    FileSystemException.requireNonNull(realBaseFile, "vfs.impl/find-rel-file.error", uri);

    return realBaseFile.resolveFile(uri);
}
 
Example 12
Source File: DefaultFileSystemManager.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
/**
 * Resolves a name, relative to the root.
 *
 * @param base the base file name
 * @param name the name
 * @param scope the {@link NameScope}
 * @return The FileName of the file.
 * @throws FileSystemException if an error occurs.
 */
@Override
public FileName resolveName(final FileName base, final String name, final NameScope scope)
        throws FileSystemException {
    FileSystemException.requireNonNull(base, "Invalid base FileName.");
    FileSystemException.requireNonNull(name, "Invalid name FileName.");
    final FileName realBase;
    if (VFS.isUriStyle() && base.isFile()) {
        realBase = base.getParent();
    } else {
        realBase = base;
    }

    final StringBuilder buffer = new StringBuilder(name);

    // Adjust separators
    UriParser.fixSeparators(buffer);
    String scheme = UriParser.extractScheme(getSchemes(), buffer.toString());

    // Determine whether to prepend the base path
    if (name.length() == 0 || (scheme == null && buffer.charAt(0) != FileName.SEPARATOR_CHAR)) {
        // Supplied path is not absolute
        if (!VFS.isUriStyle()) {
            // when using URIs the parent already do have the trailing "/"
            buffer.insert(0, FileName.SEPARATOR_CHAR);
        }
        buffer.insert(0, realBase.getPath());
    }

    // Normalise the path
    final FileType fileType = UriParser.normalisePath(buffer);

    // Check the name is ok
    final String resolvedPath = buffer.toString();
    if (!AbstractFileName.checkName(realBase.getPath(), resolvedPath, scope)) {
        throw new FileSystemException("vfs.provider/invalid-descendent-name.error", name);
    }

    String fullPath;
    if (scheme != null) {
        fullPath = resolvedPath;
    } else {
        scheme = realBase.getScheme();
        fullPath = realBase.getRootURI() + resolvedPath;
    }
    final FileProvider provider = providers.get(scheme);
    if (provider != null) {
        // TODO: extend the file name parser to be able to parse
        // only a pathname and take the missing informations from
        // the base. Then we can get rid of the string operation.
        // // String fullPath = base.getRootURI() +
        // resolvedPath.substring(1);

        return provider.parseUri(realBase, fullPath);
    }

    // An unknown scheme - hand it to the default provider - if possible
    if (scheme != null && defaultProvider != null) {
        return defaultProvider.parseUri(realBase, fullPath);
    }

    // TODO: avoid fallback to this point
    // this happens if we have a virtual filesystem (no provider for scheme)
    return ((AbstractFileName) realBase).createName(resolvedPath, fileType);
}
 
Example 13
Source File: DefaultFileSystemManager.java    From commons-vfs with Apache License 2.0 3 votes vote down vote up
/**
 * Get the configuration builder for the given scheme.
 *
 * @param scheme The scheme to locate.
 * @return The FileSystemConfigBuilder for the scheme.
 * @throws FileSystemException if the given scheme is not konwn
 */
@Override
public FileSystemConfigBuilder getFileSystemConfigBuilder(final String scheme) throws FileSystemException {
    final FileProvider provider = providers.get(scheme);
    FileSystemException.requireNonNull(provider, "vfs.impl/unknown-scheme.error", scheme);
    return provider.getConfigBuilder();
}
 
Example 14
Source File: DefaultFileSystemManager.java    From commons-vfs with Apache License 2.0 3 votes vote down vote up
/**
 * Get the capabilities for a given scheme.
 *
 * @param scheme The scheme to located.
 * @return A Collection of capabilities.
 * @throws FileSystemException if the given scheme is not konwn
 */
@Override
public Collection<Capability> getProviderCapabilities(final String scheme) throws FileSystemException {
    final FileProvider provider = providers.get(scheme);
    FileSystemException.requireNonNull(provider, "vfs.impl/unknown-scheme.error", scheme);
    return provider.getCapabilities();
}
 
Example 15
Source File: DefaultFileSystemManager.java    From commons-vfs with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a layered file system.
 *
 * @param file The FileObject to use.
 * @return The layered FileObject.
 * @throws FileSystemException if an error occurs.
 */
@Override
public FileObject createFileSystem(final FileObject file) throws FileSystemException {
    final String scheme = typeMap.getScheme(file);
    FileSystemException.requireNonNull(scheme, "vfs.impl/no-provider-for-file.error", file);
    return createFileSystem(scheme, file);
}
 
Example 16
Source File: DefaultFileSystemManager.java    From commons-vfs with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a layered file system.
 *
 * @param scheme The scheme to use.
 * @param file The FileObject.
 * @return The layered FileObject.
 * @throws FileSystemException if an error occurs.
 */
@Override
public FileObject createFileSystem(final String scheme, final FileObject file) throws FileSystemException {
    final FileProvider provider = providers.get(scheme);
    FileSystemException.requireNonNull(provider, "vfs.impl/unknown-provider.error", scheme, file);
    return provider.createFileSystem(scheme, file, file.getFileSystem().getFileSystemOptions());
}
 
Example 17
Source File: Http4FileObject.java    From commons-vfs with Apache License 2.0 3 votes vote down vote up
@Override
protected long doGetLastModifiedTime() throws Exception {
    FileSystemException.requireNonNull(lastHeadResponse, "vfs.provider.http/last-modified.error", getName());

    final Header header = lastHeadResponse.getFirstHeader("Last-Modified");

    FileSystemException.requireNonNull(header, "vfs.provider.http/last-modified.error", getName());

    return DateUtils.parseDate(header.getValue()).getTime();
}
 
Example 18
Source File: Http5FileObject.java    From commons-vfs with Apache License 2.0 3 votes vote down vote up
@Override
protected long doGetLastModifiedTime() throws Exception {
    FileSystemException.requireNonNull(lastHeadResponse, "vfs.provider.http/last-modified.error", getName());

    final Header header = lastHeadResponse.getFirstHeader("Last-Modified");

    FileSystemException.requireNonNull(header, "vfs.provider.http/last-modified.error", getName());

    return DateUtils.parseDate(header.getValue()).getTime();
}
 
Example 19
Source File: DefaultFileSystemManager.java    From commons-vfs with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the temporary file store.
 *
 * @return The file store. Never returns null.
 * @throws FileSystemException if there is no TemporaryFileStore.
 */
public TemporaryFileStore getTemporaryFileStore() throws FileSystemException {
    return FileSystemException.requireNonNull(tempFileStore, "vfs.impl/no-temp-file-store.error");
}
 
Example 20
Source File: DefaultFileSystemManager.java    From commons-vfs with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the file replicator.
 *
 * @return The file replicator. Never returns null.
 * @throws FileSystemException if there is no FileReplicator.
 */
public FileReplicator getReplicator() throws FileSystemException {
    return FileSystemException.requireNonNull(fileReplicator, "vfs.impl/no-replicator.error");
}