Search in sources :

Example 21 with FileSystemOptions

use of org.apache.commons.vfs2.FileSystemOptions in project hop by apache.

the class S3FileProviderTest method testDoCreateFileSystem.

@Test
public void testDoCreateFileSystem() throws Exception {
    FileName fileName = mock(FileName.class);
    FileSystemOptions options = new FileSystemOptions();
    assertNotNull(provider.doCreateFileSystem(fileName, options));
}
Also used : FileName(org.apache.commons.vfs2.FileName) FileSystemOptions(org.apache.commons.vfs2.FileSystemOptions) Test(org.junit.Test)

Example 22 with FileSystemOptions

use of org.apache.commons.vfs2.FileSystemOptions in project agileway by fangjinuo.

the class SftpFileProvider method doCreateFileSystem.

@Override
protected FileSystem doCreateFileSystem(FileName rootName, FileSystemOptions fileSystemOptions) throws FileSystemException {
    // Create the file system
    final GenericFileName root = (GenericFileName) rootName;
    SshConnectionFactory sshConnectionFactory = new SshConnectionFactoryRegistry().getDefault();
    SshConnectionConfig connectionConfig = sshConnectionFactory.newConfig();
    connectionConfig.setHost(root.getHostName());
    connectionConfig.setPort(root.getPort());
    connectionConfig.setUser(root.getUserName());
    connectionConfig.setPassword(root.getPassword());
    SftpFileSystemConfigBuilder configBuilder = (SftpFileSystemConfigBuilder) getConfigBuilder();
    final File knownHostsFile = configBuilder.getKnownHosts(fileSystemOptions);
    if (knownHostsFile != null) {
        connectionConfig.setKnownHostsPath(knownHostsFile.getAbsolutePath());
    }
    // JSCH 特有属性:
    Integer timout = configBuilder.getTimeout(fileSystemOptions);
    if (timout != null) {
        connectionConfig.setProperty("ConnectTimeout", timout);
    }
    String strictHostKeyChecking = configBuilder.getStrictHostKeyChecking(fileSystemOptions);
    if (Strings.isNotEmpty(strictHostKeyChecking)) {
        connectionConfig.setProperty("StrictHostKeyChecking", strictHostKeyChecking);
    }
    final String preferredAuthentications = configBuilder.getPreferredAuthentications(fileSystemOptions);
    if (preferredAuthentications != null) {
        connectionConfig.setProperty("PreferredAuthentications", preferredAuthentications);
    }
    // set compression property
    final String compression = configBuilder.getCompression(fileSystemOptions);
    if (compression != null) {
        connectionConfig.setProperty("compression.s2c", compression);
        connectionConfig.setProperty("compression.c2s", compression);
    }
    SshConnection sshConnection = sshConnectionFactory.get(connectionConfig);
    SftpSession session = sshConnection.openSftpSession();
    return new SftpFileSystem(root, session, null, fileSystemOptions);
}
Also used : SshConnectionFactory(com.jn.agileway.ssh.client.SshConnectionFactory) SshConnection(com.jn.agileway.ssh.client.SshConnection) GenericFileName(org.apache.commons.vfs2.provider.GenericFileName) File(java.io.File) SshConnectionConfig(com.jn.agileway.ssh.client.SshConnectionConfig) SshConnectionFactoryRegistry(com.jn.agileway.ssh.client.SshConnectionFactoryRegistry) SftpSession(com.jn.agileway.ssh.client.sftp.SftpSession)

Example 23 with FileSystemOptions

use of org.apache.commons.vfs2.FileSystemOptions in project agileway by fangjinuo.

the class AgilewaySftpProviderTests method testListChildren.

@Test
public void testListChildren() throws Throwable {
    DefaultFileSystemManager fileSystemManager = (DefaultFileSystemManager) VFS.getManager();
    String url = "sftp://fangjinuo:fjn13570@192.168.1.70:22/test2/vfs_sftp_test";
    FileSystemOptions fileSystemOptions = new FileSystemOptions();
    SftpFileSystemConfigBuilder configBuilder = SftpFileSystemConfigBuilder.getInstance();
    configBuilder.setUserDirIsRoot(fileSystemOptions, true);
    FileObject fileObject = fileSystemManager.resolveFile(url, fileSystemOptions);
    System.out.println("===============Show remote directory==============");
    showFile(0, fileObject);
    System.out.println("===============Copy remote files to local==============");
    url = "file://d:/tmp002";
    FileObject localFileObject = fileSystemManager.resolveFile(url);
    if (fileObject.isFolder()) {
        if (!localFileObject.exists()) {
            localFileObject.createFolder();
        } else {
            localFileObject.delete(Selectors.EXCLUDE_SELF);
        }
        localFileObject.copyFrom(fileObject, Selectors.EXCLUDE_SELF);
    } else {
        // 单独测试 文件时,将上面的 url 改成一个 文件的url即可
        long writeSize = fileObject.getContent().write(localFileObject);
        long expectedSize = fileObject.getContent().getSize();
        Preconditions.checkTrue(writeSize == expectedSize);
        localFileObject.copyFrom(fileObject, Selectors.SELECT_SELF);
    }
    System.out.println("================Copy local files to remote=============");
    url = "sftp://fangjinuo:fjn13570@192.168.1.70:22/test2/vfs_sftp_test2";
    fileObject = fileSystemManager.resolveFile(url, fileSystemOptions);
    if (!fileObject.exists()) {
        fileObject.createFolder();
    } else {
        fileObject.delete(Selectors.EXCLUDE_SELF);
    }
    fileObject.copyFrom(localFileObject, Selectors.EXCLUDE_SELF);
}
Also used : DefaultFileSystemManager(org.apache.commons.vfs2.impl.DefaultFileSystemManager) SftpFileSystemConfigBuilder(com.jn.agileway.vfs.provider.sftp.SftpFileSystemConfigBuilder) FileObject(org.apache.commons.vfs2.FileObject) FileSystemOptions(org.apache.commons.vfs2.FileSystemOptions) Test(org.junit.Test)

Example 24 with FileSystemOptions

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

the class TemporaryFileProvider method findFile.

/**
 * Locates a file object, by absolute URI.
 *
 * @param baseFile The base FileObject.
 * @param uri The URI of the file to be located.
 * @param fileSystemOptions 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 fileSystemOptions) 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, fileSystemOptions);
    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(), fileSystemOptions);
        addFileSystem(this, filesystem);
    }
    // Find the file
    return filesystem.resolveFile(path);
}
Also used : LocalFileSystem(org.apache.commons.vfs2.provider.local.LocalFileSystem) FileSystem(org.apache.commons.vfs2.FileSystem) LocalFileSystem(org.apache.commons.vfs2.provider.local.LocalFileSystem) FileName(org.apache.commons.vfs2.FileName)

Example 25 with FileSystemOptions

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

the class UrlFileProvider method findFile.

/**
 * Locates a file object, by absolute URI.
 *
 * @param baseFile The base FileObject.
 * @param fileUri The uri of the file to locate.
 * @param fileSystemOptions The FileSystemOptions
 * @return The FileObject
 * @throws FileSystemException if an error occurs.
 */
@Override
public synchronized FileObject findFile(final FileObject baseFile, final String fileUri, final FileSystemOptions fileSystemOptions) throws FileSystemException {
    try {
        final URI uri = URI.create(fileUri);
        final URI rootUri = uri.resolve("/");
        final String key = this.getClass().getName() + rootUri.toString();
        FileSystem fs = findFileSystem(key, fileSystemOptions);
        if (fs == null) {
            final String extForm = rootUri.toString();
            final FileName rootName = getContext().parseURI(extForm);
            // final FileName rootName =
            // new BasicFileName(rootUrl, FileName.ROOT_PATH);
            fs = new UrlFileSystem(rootName, fileSystemOptions);
            addFileSystem(key, fs);
        }
        return fs.resolveFile(uri.getPath());
    } catch (final Exception e) {
        throw new FileSystemException("vfs.provider.url/badly-formed-uri.error", fileUri, e);
    }
}
Also used : FileSystemException(org.apache.commons.vfs2.FileSystemException) FileSystem(org.apache.commons.vfs2.FileSystem) FileName(org.apache.commons.vfs2.FileName) URI(java.net.URI) FileSystemException(org.apache.commons.vfs2.FileSystemException)

Aggregations

FileSystemOptions (org.apache.commons.vfs2.FileSystemOptions)98 FileObject (org.apache.commons.vfs2.FileObject)47 FileSystemException (org.apache.commons.vfs2.FileSystemException)29 Test (org.junit.Test)25 IOException (java.io.IOException)17 FileName (org.apache.commons.vfs2.FileName)17 URL (java.net.URL)13 GenericFileName (org.apache.commons.vfs2.provider.GenericFileName)13 File (java.io.File)12 UserAuthenticationData (org.apache.commons.vfs2.UserAuthenticationData)12 StaticUserAuthenticator (org.apache.commons.vfs2.auth.StaticUserAuthenticator)9 FileSystem (org.apache.commons.vfs2.FileSystem)8 DefaultFileSystemManager (org.apache.commons.vfs2.impl.DefaultFileSystemManager)8 ArrayList (java.util.ArrayList)7 FileNotFolderException (org.apache.commons.vfs2.FileNotFolderException)6 Test (org.junit.jupiter.api.Test)6 OutputStream (java.io.OutputStream)5 UserAuthenticator (org.apache.commons.vfs2.UserAuthenticator)5 Before (org.junit.Before)5 AmazonS3 (com.amazonaws.services.s3.AmazonS3)4