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;
}
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);
}
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);
}
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);
}
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);
}
Aggregations