Search in sources :

Example 41 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 42 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 43 with FileSystemOptions

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

the class HttpClientFactory method createConnection.

/**
 * Creates a new connection to the server.
 *
 * @param builder The HttpFileSystemConfigBuilder.
 * @param scheme The protocol.
 * @param hostname The hostname.
 * @param port The port number.
 * @param username The username.
 * @param password The password
 * @param fileSystemOptions The file system options.
 * @return a new HttpClient connection.
 * @throws FileSystemException if an error occurs.
 * @since 2.0
 */
public static HttpClient createConnection(final HttpFileSystemConfigBuilder builder, final String scheme, final String hostname, final int port, final String username, final String password, final FileSystemOptions fileSystemOptions) throws FileSystemException {
    final HttpClient client;
    try {
        final HttpConnectionManager mgr = new MultiThreadedHttpConnectionManager();
        final HttpConnectionManagerParams connectionMgrParams = mgr.getParams();
        client = new HttpClient(mgr);
        final HostConfiguration config = new HostConfiguration();
        config.setHost(hostname, port, scheme);
        if (fileSystemOptions != null) {
            final String proxyHost = builder.getProxyHost(fileSystemOptions);
            final int proxyPort = builder.getProxyPort(fileSystemOptions);
            if (!StringUtils.isEmpty(proxyHost) && proxyPort > 0) {
                config.setProxy(proxyHost, proxyPort);
            }
            final UserAuthenticator proxyAuth = builder.getProxyAuthenticator(fileSystemOptions);
            if (proxyAuth != null) {
                final UserAuthenticationData authData = UserAuthenticatorUtils.authenticate(proxyAuth, new UserAuthenticationData.Type[] { UserAuthenticationData.USERNAME, UserAuthenticationData.PASSWORD });
                if (authData != null) {
                    final UsernamePasswordCredentials proxyCreds = new UsernamePasswordCredentials(UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData, UserAuthenticationData.USERNAME, null)), UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData, UserAuthenticationData.PASSWORD, null)));
                    final AuthScope scope = new AuthScope(proxyHost, AuthScope.ANY_PORT);
                    client.getState().setProxyCredentials(scope, proxyCreds);
                }
                if (builder.isPreemptiveAuth(fileSystemOptions)) {
                    final HttpClientParams httpClientParams = new HttpClientParams();
                    httpClientParams.setAuthenticationPreemptive(true);
                    client.setParams(httpClientParams);
                }
            }
            final Cookie[] cookies = builder.getCookies(fileSystemOptions);
            if (cookies != null) {
                client.getState().addCookies(cookies);
            }
        }
        /*
             * ConnectionManager set methods must be called after the host & port and proxy host & port are set in the
             * HostConfiguration. They are all used as part of the key when HttpConnectionManagerParams tries to locate
             * the host configuration.
             */
        connectionMgrParams.setMaxConnectionsPerHost(config, builder.getMaxConnectionsPerHost(fileSystemOptions));
        connectionMgrParams.setMaxTotalConnections(builder.getMaxTotalConnections(fileSystemOptions));
        connectionMgrParams.setConnectionTimeout(DurationUtils.toMillisInt(builder.getConnectionTimeoutDuration(fileSystemOptions)));
        connectionMgrParams.setSoTimeout(DurationUtils.toMillisInt(builder.getSoTimeoutDuration(fileSystemOptions)));
        client.setHostConfiguration(config);
        if (username != null) {
            final UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password);
            final AuthScope scope = new AuthScope(hostname, AuthScope.ANY_PORT);
            client.getState().setCredentials(scope, creds);
        }
    } catch (final Exception exc) {
        throw new FileSystemException("vfs.provider.http/connect.error", exc, hostname);
    }
    return client;
}
Also used : Cookie(org.apache.commons.httpclient.Cookie) HostConfiguration(org.apache.commons.httpclient.HostConfiguration) UserAuthenticator(org.apache.commons.vfs2.UserAuthenticator) FileSystemException(org.apache.commons.vfs2.FileSystemException) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) UserAuthenticationData(org.apache.commons.vfs2.UserAuthenticationData) FileSystemException(org.apache.commons.vfs2.FileSystemException) HttpConnectionManagerParams(org.apache.commons.httpclient.params.HttpConnectionManagerParams) HttpClient(org.apache.commons.httpclient.HttpClient) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager) AuthScope(org.apache.commons.httpclient.auth.AuthScope) HttpClientParams(org.apache.commons.httpclient.params.HttpClientParams) HttpConnectionManager(org.apache.commons.httpclient.HttpConnectionManager) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager)

Example 44 with FileSystemOptions

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

the class Http4FileProvider method doCreateFileSystem.

@Override
protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions) throws FileSystemException {
    final GenericFileName rootName = (GenericFileName) name;
    UserAuthenticationData authData = null;
    HttpClient httpClient;
    HttpClientContext httpClientContext;
    try {
        final Http4FileSystemConfigBuilder builder = Http4FileSystemConfigBuilder.getInstance();
        authData = UserAuthenticatorUtils.authenticate(fileSystemOptions, AUTHENTICATOR_TYPES);
        httpClientContext = createHttpClientContext(builder, rootName, fileSystemOptions, authData);
        httpClient = createHttpClient(builder, rootName, fileSystemOptions);
    } finally {
        UserAuthenticatorUtils.cleanup(authData);
    }
    return new Http4FileSystem(rootName, fileSystemOptions, httpClient, httpClientContext);
}
Also used : UserAuthenticationData(org.apache.commons.vfs2.UserAuthenticationData) GenericFileName(org.apache.commons.vfs2.provider.GenericFileName) HttpClient(org.apache.http.client.HttpClient) HttpClientContext(org.apache.http.client.protocol.HttpClientContext)

Example 45 with FileSystemOptions

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

the class DefaultURLStreamHandler method parseURL.

@Override
protected void parseURL(final URL u, final String spec, final int start, final int limit) {
    try {
        final FileObject old = context.resolveFile(u.toExternalForm(), fileSystemOptions);
        final FileObject newURL;
        if (start > 0 && spec.charAt(start - 1) == ':') {
            newURL = context.resolveFile(old, spec, fileSystemOptions);
        } else if (old.isFile() && old.getParent() != null) {
            // for files we have to resolve relative
            newURL = old.getParent().resolveFile(spec);
        } else {
            newURL = old.resolveFile(spec);
        }
        final String url = newURL.getName().getURI();
        final StringBuilder filePart = new StringBuilder();
        final String protocolPart = UriParser.extractScheme(context.getFileSystemManager().getSchemes(), url, filePart);
        setURL(u, protocolPart, "", -1, null, null, filePart.toString(), null, null);
    } catch (final FileSystemException fse) {
        // This is rethrown to MalformedURLException in URL anyway
        throw new RuntimeException(fse.getMessage());
    }
}
Also used : FileSystemException(org.apache.commons.vfs2.FileSystemException) FileObject(org.apache.commons.vfs2.FileObject)

Aggregations

FileSystemOptions (org.apache.commons.vfs2.FileSystemOptions)97 FileObject (org.apache.commons.vfs2.FileObject)46 FileSystemException (org.apache.commons.vfs2.FileSystemException)29 Test (org.junit.Test)25 IOException (java.io.IOException)16 FileName (org.apache.commons.vfs2.FileName)16 URL (java.net.URL)13 File (java.io.File)12 GenericFileName (org.apache.commons.vfs2.provider.GenericFileName)12 UserAuthenticationData (org.apache.commons.vfs2.UserAuthenticationData)11 StaticUserAuthenticator (org.apache.commons.vfs2.auth.StaticUserAuthenticator)9 FileSystem (org.apache.commons.vfs2.FileSystem)8 ArrayList (java.util.ArrayList)7 DefaultFileSystemManager (org.apache.commons.vfs2.impl.DefaultFileSystemManager)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