org.apache.commons.vfs2.provider.UriParser Java Examples
The following examples show how to use
org.apache.commons.vfs2.provider.UriParser.
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: S3FileNameParser.java From hop with Apache License 2.0 | 6 votes |
public FileName parseUri( VfsComponentContext context, FileName base, String uri ) throws FileSystemException { StringBuilder name = new StringBuilder(); String scheme = UriParser.extractScheme( uri, name ); UriParser.canonicalizePath( name, 0, name.length(), this ); // Normalize separators in the path UriParser.fixSeparators( name ); // Normalise the path FileType fileType = UriParser.normalisePath( name ); String fullPath = name.toString(); // Extract bucket name final String bucketName = UriParser.extractFirstElement( name ); return new S3FileName( scheme, bucketName, fullPath, fileType ); }
Example #2
Source File: UrlTests.java From commons-vfs with Apache License 2.0 | 6 votes |
/** * 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 #3
Source File: VFSFileSystem.java From commons-configuration with Apache License 2.0 | 6 votes |
@Override public String getBasePath(final String path) { if (UriParser.extractScheme(path) == null) { return super.getBasePath(path); } try { final FileSystemManager fsManager = VFS.getManager(); final FileName name = fsManager.resolveURI(path); return name.getParent().getURI(); } catch (final FileSystemException fse) { fse.printStackTrace(); return null; } }
Example #4
Source File: VFSFileSystem.java From commons-configuration with Apache License 2.0 | 6 votes |
@Override public String getFileName(final String path) { if (UriParser.extractScheme(path) == null) { return super.getFileName(path); } try { final FileSystemManager fsManager = VFS.getManager(); final FileName name = fsManager.resolveURI(path); return name.getBaseName(); } catch (final FileSystemException fse) { fse.printStackTrace(); return null; } }
Example #5
Source File: FtpFileObject.java From commons-vfs with Apache License 2.0 | 6 votes |
/** * Lists the children of the file. */ @Override protected String[] doListChildren() throws Exception { // List the children of this file doGetChildren(); // VFS-210 if (children == null) { return null; } // TODO - get rid of this children stuff final String[] childNames = new String[children.size()]; int childNum = -1; final Iterator<FTPFile> iterChildren = children.values().iterator(); while (iterChildren.hasNext()) { childNum++; final FTPFile child = iterChildren.next(); childNames[childNum] = child.getName(); } return UriParser.encode(childNames); }
Example #6
Source File: S3NFileNameParser.java From hop with Apache License 2.0 | 6 votes |
public FileName parseUri( VfsComponentContext context, FileName base, String uri ) throws FileSystemException { StringBuilder name = new StringBuilder(); String scheme = UriParser.extractScheme( uri, name ); UriParser.canonicalizePath( name, 0, name.length(), this ); // Normalize separators in the path UriParser.fixSeparators( name ); // Normalise the path FileType fileType = UriParser.normalisePath( name ); // Extract bucket name final String bucketName = UriParser.extractFirstElement( name ); return new S3NFileName( scheme, bucketName, name.toString(), fileType ); }
Example #7
Source File: S3AFileNameParser.java From hop with Apache License 2.0 | 6 votes |
public FileName parseUri( VfsComponentContext context, FileName base, String uri ) throws FileSystemException { StringBuilder name = new StringBuilder(); String scheme = UriParser.extractScheme( uri, name ); UriParser.canonicalizePath( name, 0, name.length(), this ); // Normalize separators in the path UriParser.fixSeparators( name ); // Normalise the path FileType fileType = UriParser.normalisePath( name ); // Extract bucket name final String bucketName = UriParser.extractFirstElement( name ); return new S3AFileName( scheme, bucketName, name.toString(), fileType ); }
Example #8
Source File: VfsFileChooserControls.java From pentaho-kettle with Apache License 2.0 | 5 votes |
private void updateLocation() { String pathText = wPath.getText(); String scheme = pathText.isEmpty() ? HDFS_SCHEME : UriParser.extractScheme( pathText ); if ( scheme != null ) { List<VFSScheme> availableVFSSchemes = getAvailableVFSSchemes(); for ( int i = 0; i < availableVFSSchemes.size(); i++ ) { VFSScheme s = availableVFSSchemes.get( i ); if ( scheme.equals( s.scheme ) ) { wLocation.select( i ); selectedVFSScheme = s; } } } }
Example #9
Source File: DefaultFileSystemManager.java From commons-vfs with Apache License 2.0 | 5 votes |
/** * 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 #10
Source File: CustomRamProviderTest.java From commons-vfs with Apache License 2.0 | 5 votes |
/** Create directory structure for {@link #testSpecialName()} and {@link #testSchemePrefix()} */ private FileObject prepareSpecialFile(final String dirname, final String testFileName) throws FileSystemException { // set up a folder containing an filename with special characters: final FileObject dir = manager.resolveFile("ram:" + dirname); dir.createFolder(); // construct the absolute name to make sure the relative name is not miss-interpreted // ("./" + UriParser.encode(testFileName, ENC) would also work) final String filePath = dir.getName().getPath() + "/" + UriParser.encode(testFileName, ENC); final FileObject specialFile = dir.resolveFile(filePath); specialFile.createFile(); return dir; }
Example #11
Source File: DefaultFileReplicator.java From commons-vfs with Apache License 2.0 | 5 votes |
/** * Create the temporary file name. * * @param baseName The base to prepend to the file name being created. * @return the name of the File. */ protected String createFilename(final String baseName) { // BUG29007 // return baseName + "_" + getFilecount() + ".tmp"; // [email protected]: BUG34976 get rid of maybe reserved and dangerous characters // e.g. to allow replication of http://hostname.org/fileservlet?file=abc.txt final String safeBasename = UriParser.encode(baseName, TMP_RESERVED_CHARS).replace('%', '_'); return "tmp_" + getFilecount() + "_" + safeBasename; }
Example #12
Source File: CustomRamProviderTest.java From commons-vfs with Apache License 2.0 | 5 votes |
/** * Test some special file name symbols. * <p> * Use the RamProvider since it has no character limitations like * the (Windows) LocalFileProvider. */ @Test public void testSpecialName() throws FileSystemException { // we test with this file name // does not work with '!' final String testDir = "/spacialtest/"; final String testFileName = "test:+-_ \"()<>%#.txt"; final String expectedName = testDir + testFileName; final FileObject dir = prepareSpecialFile(testDir, testFileName); // DO: verify you can list it: final FileObject[] findFilesResult = dir.findFiles(new AllFileSelector()); // includes dir final FileObject[] getChildrenResult = dir.getChildren(); final FileObject getChildResult = dir.getChild(UriParser.encode(testFileName, ENC)); // validate findFiles returns expected result assertEquals("Unexpected result findFiles: " + Arrays.toString(findFilesResult), 2, findFilesResult.length); String resultName = findFilesResult[0].getName().getPathDecoded(); assertEquals("findFiles Child name does not match", expectedName, resultName); assertEquals("Did findFiles but child was no file", FileType.FILE, findFilesResult[0].getType()); // validate getChildren returns expected result assertEquals("Unexpected result getChildren: " + Arrays.toString(getChildrenResult), 1, getChildrenResult.length); resultName = getChildrenResult[0].getName().getPathDecoded(); assertEquals("getChildren Child name does not match", expectedName, resultName); assertEquals("Did getChildren but child was no file", FileType.FILE, getChildrenResult[0].getType()); // validate getChild returns expected child assertNotNull("Did not find direct child", getChildResult); resultName = getChildResult.getName().getPathDecoded(); assertEquals("getChild name does not match", expectedName, resultName); assertEquals("getChild was no file", FileType.FILE, getChildResult.getType()); }
Example #13
Source File: ResourceFileName.java From commons-vfs with Apache License 2.0 | 5 votes |
/** * Returns the decoded URI of the file. * * @return the FileName as a URI. */ @Override public String toString() { try { return UriParser.decode(super.getURI()); } catch (final FileSystemException e) { return super.getURI(); } }
Example #14
Source File: SmbFileNameParser.java From commons-vfs with Apache License 2.0 | 5 votes |
@Override public FileName parseUri(final VfsComponentContext context, final FileName base, final String filename) throws FileSystemException { final StringBuilder name = new StringBuilder(); // Extract the scheme and authority parts final Authority auth = extractToPath(context, filename, name); // extract domain String username = auth.getUserName(); final String domain = extractDomain(username); if (domain != null) { username = username.substring(domain.length() + 1); } // Decode and adjust separators UriParser.canonicalizePath(name, 0, name.length(), this); UriParser.fixSeparators(name); // Extract the share final String share = UriParser.extractFirstElement(name); if (share == null || share.length() == 0) { throw new FileSystemException("vfs.provider.smb/missing-share-name.error", filename); } // Normalise the path. Do this after extracting the share name, // to deal with things like smb://hostname/share/.. final FileType fileType = UriParser.normalisePath(name); final String path = name.toString(); return new SmbFileName(auth.getScheme(), auth.getHostName(), auth.getPort(), username, auth.getPassword(), domain, share, path, fileType); }
Example #15
Source File: SmbFileObject.java From commons-vfs with Apache License 2.0 | 5 votes |
/** * Lists the children of the file. Is only called if {@link #doGetType} returns {@link FileType#FOLDER}. */ @Override protected String[] doListChildren() throws Exception { // VFS-210: do not try to get listing for anything else than directories if (!file.isDirectory()) { return null; } return UriParser.encode(file.list()); }
Example #16
Source File: FtpFileObject.java From commons-vfs with Apache License 2.0 | 5 votes |
/** * Called when the children of this file change. */ @Override protected void onChildrenChanged(final FileName child, final FileType newType) { if (children != null && newType.equals(FileType.IMAGINARY)) { try { children.remove(UriParser.decode(child.getBaseName())); } catch (final FileSystemException e) { throw new RuntimeException(e.getMessage()); } } else { // if child was added we have to rescan the children // TODO - get rid of this children = null; } }
Example #17
Source File: FtpFileObject.java From commons-vfs with Apache License 2.0 | 5 votes |
protected FtpFileObject(final AbstractFileName name, final FtpFileSystem fileSystem, final FileName rootName) throws FileSystemException { super(name, fileSystem); final String relPath = UriParser.decode(rootName.getRelativeName(name)); if (".".equals(relPath)) { // do not use the "." as path against the ftp-server // e.g. the uu.net ftp-server do a recursive listing then // this.relPath = UriParser.decode(rootName.getPath()); // this.relPath = "."; this.relPath = null; } else { this.relPath = relPath; } }
Example #18
Source File: TemporaryFileProvider.java From commons-vfs with Apache License 2.0 | 5 votes |
/** * Locates a file object, by absolute URI. * * @param baseFile The base FileObject. * @param uri The URI of the file to be located. * @param properties FileSystemOptions to use to locate or create the file. * @return The FileObject. * @throws FileSystemException if an error occurs. */ @Override public synchronized FileObject findFile(final FileObject baseFile, final String uri, final FileSystemOptions properties) throws FileSystemException { // Parse the name final StringBuilder buffer = new StringBuilder(uri); final String scheme = UriParser.extractScheme(getContext().getFileSystemManager().getSchemes(), uri, buffer); UriParser.fixSeparators(buffer); UriParser.normalisePath(buffer); final String path = buffer.toString(); // Create the temp file system if it does not exist // FileSystem filesystem = findFileSystem( this, (Properties) null); FileSystem filesystem = findFileSystem(this, properties); if (filesystem == null) { if (rootFile == null) { rootFile = getContext().getTemporaryFileStore().allocateFile("tempfs"); } final FileName rootName = getContext().parseURI(scheme + ":" + FileName.ROOT_PATH); // final FileName rootName = // new LocalFileName(scheme, scheme + ":", FileName.ROOT_PATH); filesystem = new LocalFileSystem(rootName, rootFile.getAbsolutePath(), properties); addFileSystem(this, filesystem); } // Find the file return filesystem.resolveFile(path); }
Example #19
Source File: VFSFileSystem.java From commons-configuration with Apache License 2.0 | 5 votes |
@Override public URL getURL(final String basePath, final String file) throws MalformedURLException { if ((basePath != null && UriParser.extractScheme(basePath) == null) || (basePath == null && UriParser.extractScheme(file) == null)) { return super.getURL(basePath, file); } try { final FileSystemManager fsManager = VFS.getManager(); FileName path; if (basePath != null && UriParser.extractScheme(file) == null) { final FileName base = fsManager.resolveURI(basePath); path = fsManager.resolveName(base, file); } else { path = fsManager.resolveURI(file); } final URLStreamHandler handler = new VFSURLStreamHandler(path); return new URL(null, path.getURI(), handler); } catch (final FileSystemException fse) { throw new ConfigurationRuntimeException("Could not parse basePath: " + basePath + " and fileName: " + file, fse); } }
Example #20
Source File: LocalFileNameParser.java From commons-vfs with Apache License 2.0 | 5 votes |
@Override public FileName parseUri(final VfsComponentContext context, final FileName base, final String uri) throws FileSystemException { final StringBuilder name = new StringBuilder(); // Extract the scheme String scheme = UriParser.extractScheme(getSchemes(context, base, uri), uri, name); if (scheme == null && base != null) { scheme = base.getScheme(); } if (scheme == null) { scheme = "file"; } // Remove encoding, and adjust the separators UriParser.canonicalizePath(name, 0, name.length(), this); UriParser.fixSeparators(name); // Extract the root prefix final String rootFile = extractRootPrefix(uri, name); // Normalise the path final FileType fileType = UriParser.normalisePath(name); final String path = name.toString(); return createFileName(scheme, rootFile, path, fileType); }
Example #21
Source File: LocalFileNameParser.java From commons-vfs with Apache License 2.0 | 5 votes |
/** * Determines if a name is an absolute file name. * * @param name The file name. * @return true if the name is absolute, false otherwise. */ public boolean isAbsoluteName(final String name) { // TODO - this is yucky final StringBuilder b = new StringBuilder(name); try { UriParser.fixSeparators(b); extractRootPrefix(name, b); return true; } catch (final FileSystemException e) { return false; } }
Example #22
Source File: LocalFileName.java From commons-vfs with Apache License 2.0 | 5 votes |
/** * Returns the decoded URI of the file. * * @return the FileName as a URI. */ @Override public String toString() { try { return UriParser.decode(super.getURI()); } catch (final FileSystemException e) { return super.getURI(); } }
Example #23
Source File: LocalFile.java From commons-vfs with Apache License 2.0 | 5 votes |
/** * Returns the URI of the file. * * @return The URI of the file. */ @Override public String toString() { try { // VFS-325: URI may contain percent-encoded values as part of file name, so decode // those characters before returning return UriParser.decode(getName().getURI()); } catch (final FileSystemException e) { return getName().getURI(); } }
Example #24
Source File: UrlTests.java From commons-vfs with Apache License 2.0 | 4 votes |
/** * Tests resolution of an absolute file name. */ public void testHashURL() throws Exception { final FileObject file = getReadFolder().resolveFile("test-hash-#test.txt"); assertEquals(file.toString(), UriParser.decode(file.getURL().toString())); }
Example #25
Source File: ConnectionFileNameParser.java From pentaho-kettle with Apache License 2.0 | 4 votes |
public static AbstractFileName parseUri( String uri, FileNameParser fileNameParser ) throws FileSystemException { StringBuilder name = new StringBuilder(); String scheme = UriParser.extractScheme( uri, name ); UriParser.canonicalizePath( name, 0, name.length(), fileNameParser ); UriParser.fixSeparators( name ); FileType fileType = UriParser.normalisePath( name ); // Extract the named connection name final String connection = UriParser.extractFirstElement( name ); String path = name.toString(); return new ConnectionFileName( scheme, connection, path, fileType ); }
Example #26
Source File: DefaultFileSystemManager.java From commons-vfs with Apache License 2.0 | 4 votes |
/** * 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 #27
Source File: DefaultFileSystemManager.java From commons-vfs with Apache License 2.0 | 4 votes |
/** * 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 #28
Source File: TarFileSystem.java From commons-vfs with Apache License 2.0 | 4 votes |
@Override public void init() throws FileSystemException { super.init(); // Build the index try { final List<TarFileObject> strongRef = new ArrayList<>(DEFAULT_INDEX_SIZE); TarArchiveEntry entry; while ((entry = getTarFile().getNextTarEntry()) != null) { final AbstractFileName name = (AbstractFileName) getFileSystemManager().resolveName(getRootName(), UriParser.encode(entry.getName())); // Create the file TarFileObject fileObj; if (entry.isDirectory() && getFileFromCache(name) != null) { fileObj = (TarFileObject) getFileFromCache(name); fileObj.setTarEntry(entry); continue; } fileObj = createTarFileObject(name, entry); putFileToCache(fileObj); strongRef.add(fileObj); fileObj.holdObject(strongRef); // Make sure all ancestors exist // TODO - create these on demand TarFileObject parent = null; for (AbstractFileName parentName = (AbstractFileName) name .getParent(); parentName != null; fileObj = parent, parentName = (AbstractFileName) parentName .getParent()) { // Locate the parent parent = (TarFileObject) getFileFromCache(parentName); if (parent == null) { parent = createTarFileObject(parentName, null); putFileToCache(parent); strongRef.add(parent); parent.holdObject(strongRef); } // Attach child to parent parent.attachChild(fileObj.getName()); } } } catch (final IOException e) { throw new FileSystemException(e); } finally { closeCommunicationLink(); } }
Example #29
Source File: SftpFileObject.java From commons-vfs with Apache License 2.0 | 4 votes |
protected SftpFileObject(final AbstractFileName name, final SftpFileSystem fileSystem) throws FileSystemException { super(name, fileSystem); relPath = UriParser.decode(fileSystem.getRootName().getRelativeName(name)); }
Example #30
Source File: LocalFile.java From commons-vfs with Apache License 2.0 | 4 votes |
/** * Returns the children of the file. */ @Override protected String[] doListChildren() throws Exception { return UriParser.encode(file.list()); }