Search in sources :

Example 11 with VfsComponentContext

use of org.apache.commons.vfs2.provider.VfsComponentContext in project commons-vfs by apache.

the class HostFileNameParser method extractToPath.

/**
 * Extracts the scheme, userinfo, hostname and port components of a generic URI.
 *
 * @param context component context.
 * @param uri The absolute URI to parse.
 * @param name Used to return the remainder of the URI.
 * @return Authority extracted host authority, never null.
 * @throws FileSystemException if authority cannot be extracted.
 */
protected Authority extractToPath(final VfsComponentContext context, final String uri, final StringBuilder name) throws FileSystemException {
    final Authority auth = new Authority();
    final FileSystemManager fsm;
    if (context != null) {
        fsm = context.getFileSystemManager();
    } else {
        fsm = VFS.getManager();
    }
    // Extract the scheme
    auth.scheme = UriParser.extractScheme(fsm.getSchemes(), uri, name);
    // Expecting "//"
    if (name.length() < 2 || name.charAt(0) != '/' || name.charAt(1) != '/') {
        throw new FileSystemException("vfs.provider/missing-double-slashes.error", uri);
    }
    name.delete(0, 2);
    // Extract userinfo, and split into username and password
    final String userInfo = extractUserInfo(name);
    final String userName;
    final String password;
    if (userInfo != null) {
        final int idx = userInfo.indexOf(':');
        if (idx == -1) {
            userName = userInfo;
            password = null;
        } else {
            userName = userInfo.substring(0, idx);
            password = userInfo.substring(idx + 1);
        }
    } else {
        userName = null;
        password = null;
    }
    auth.userName = UriParser.decode(userName);
    auth.password = UriParser.decode(password);
    if (auth.password != null && auth.password.startsWith("{") && auth.password.endsWith("}")) {
        try {
            final Cryptor cryptor = CryptorFactory.getCryptor();
            auth.password = cryptor.decrypt(auth.password.substring(1, auth.password.length() - 1));
        } catch (final Exception ex) {
            throw new FileSystemException("Unable to decrypt password", ex);
        }
    }
    // Extract hostname, and normalize (lowercase)
    final String hostName = extractHostName(name);
    if (hostName == null) {
        throw new FileSystemException("vfs.provider/missing-hostname.error", uri);
    }
    auth.hostName = hostName.toLowerCase();
    // Extract port
    auth.port = extractPort(name, uri);
    // Expecting '/' or empty name
    if (name.length() > 0 && name.charAt(0) != '/') {
        throw new FileSystemException("vfs.provider/missing-hostname-path-sep.error", uri);
    }
    return auth;
}
Also used : FileSystemException(org.apache.commons.vfs2.FileSystemException) Cryptor(org.apache.commons.vfs2.util.Cryptor) FileSystemManager(org.apache.commons.vfs2.FileSystemManager) FileSystemException(org.apache.commons.vfs2.FileSystemException)

Example 12 with VfsComponentContext

use of org.apache.commons.vfs2.provider.VfsComponentContext in project commons-vfs by apache.

the class HostFileNameParser method parseUri.

@Override
public FileName parseUri(final VfsComponentContext context, final FileName base, final String fileName) throws FileSystemException {
    // FTP URI are generic URI (as per RFC 2396)
    final StringBuilder name = new StringBuilder();
    // Extract the scheme and authority parts
    final Authority auth = extractToPath(context, fileName, name);
    // Decode and normalize the file name
    UriParser.canonicalizePath(name, 0, name.length(), this);
    UriParser.fixSeparators(name);
    final FileType fileType = UriParser.normalisePath(name);
    final String path = name.toString();
    return new GenericFileName(auth.scheme, auth.hostName, auth.port, defaultPort, auth.userName, auth.password, path, fileType);
}
Also used : FileType(org.apache.commons.vfs2.FileType)

Example 13 with VfsComponentContext

use of org.apache.commons.vfs2.provider.VfsComponentContext in project commons-vfs by apache.

the class GenericURLFileNameParser method parseUri.

@Override
public FileName parseUri(final VfsComponentContext context, final FileName base, final String fileName) throws FileSystemException {
    // FTP URI are generic URI (as per RFC 2396)
    final StringBuilder name = new StringBuilder();
    // Extract the scheme and authority parts
    final Authority auth = extractToPath(context, fileName, name);
    // Extract the queryString
    final String queryString = UriParser.extractQueryString(name);
    // Decode and normalise the file name
    UriParser.canonicalizePath(name, 0, name.length(), this);
    UriParser.fixSeparators(name);
    final FileType fileType = UriParser.normalisePath(name);
    final String path = name.toString();
    return new GenericURLFileName(auth.getScheme(), auth.getHostName(), auth.getPort(), getDefaultPort(), auth.getUserName(), auth.getPassword(), path, fileType, queryString);
}
Also used : FileType(org.apache.commons.vfs2.FileType)

Example 14 with VfsComponentContext

use of org.apache.commons.vfs2.provider.VfsComponentContext in project commons-vfs by apache.

the class URLFileNameParser method parseUri.

@Override
public FileName parseUri(final VfsComponentContext context, final FileName base, final String fileName) throws FileSystemException {
    // FTP URI are generic URI (as per RFC 2396)
    final StringBuilder name = new StringBuilder();
    // Extract the scheme and authority parts
    final Authority auth = extractToPath(context, fileName, name);
    // Extract the queryString
    final String queryString = UriParser.extractQueryString(name);
    // Decode and normalise the file name
    UriParser.canonicalizePath(name, 0, name.length(), this);
    UriParser.fixSeparators(name);
    final FileType fileType = UriParser.normalisePath(name);
    final String path = name.toString();
    return new URLFileName(auth.getScheme(), auth.getHostName(), auth.getPort(), getDefaultPort(), auth.getUserName(), auth.getPassword(), path, fileType, queryString);
}
Also used : FileType(org.apache.commons.vfs2.FileType)

Example 15 with VfsComponentContext

use of org.apache.commons.vfs2.provider.VfsComponentContext in project commons-vfs by apache.

the class LocalFileNameParser method parseUri.

@Override
public FileName parseUri(final VfsComponentContext context, final FileName base, final String uri) throws FileSystemException {
    final StringBuilder nameBuilder = new StringBuilder();
    // Extract the scheme
    String scheme = UriParser.extractScheme(getSchemes(context, base, uri), uri, nameBuilder);
    if (scheme == null && base != null) {
        scheme = base.getScheme();
    }
    if (scheme == null) {
        scheme = "file";
    }
    // Remove encoding, and adjust the separators
    UriParser.canonicalizePath(nameBuilder, 0, nameBuilder.length(), this);
    UriParser.fixSeparators(nameBuilder);
    // Extract the root prefix
    final String rootFile = extractRootPrefix(uri, nameBuilder);
    // Normalise the path
    final FileType fileType = UriParser.normalisePath(nameBuilder);
    final String path = nameBuilder.toString();
    return createFileName(scheme, rootFile, path, fileType);
}
Also used : FileType(org.apache.commons.vfs2.FileType)

Aggregations

FileType (org.apache.commons.vfs2.FileType)15 VfsComponentContext (org.apache.commons.vfs2.provider.VfsComponentContext)9 FileName (org.apache.commons.vfs2.FileName)6 Test (org.junit.Test)5 AmazonS3 (com.amazonaws.services.s3.AmazonS3)4 ArrayList (java.util.ArrayList)4 DefaultFileSystemManager (org.apache.commons.vfs2.impl.DefaultFileSystemManager)4 Before (org.junit.Before)4 FileSystemException (org.apache.commons.vfs2.FileSystemException)3 ListObjectsRequest (com.amazonaws.services.s3.model.ListObjectsRequest)2 ObjectListing (com.amazonaws.services.s3.model.ObjectListing)2 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)2 S3Object (com.amazonaws.services.s3.model.S3Object)2 S3ObjectInputStream (com.amazonaws.services.s3.model.S3ObjectInputStream)2 FileSystemOptions (org.apache.commons.vfs2.FileSystemOptions)2 FilesCache (org.apache.commons.vfs2.FilesCache)2 S3FileName (org.apache.hop.vfs.s3.s3.vfs.S3FileName)2 S3NFileName (org.apache.hop.vfs.s3.s3n.vfs.S3NFileName)2 FileSystemManager (org.apache.commons.vfs2.FileSystemManager)1 Cryptor (org.apache.commons.vfs2.util.Cryptor)1