Search in sources :

Example 96 with FileSystemOptions

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

the class SftpClientFactory method createConnection.

/**
 * Creates a new connection to the server.
 *
 * @param hostname The name of the host to connect to.
 * @param port The port to use.
 * @param username The user's id.
 * @param password The user's password.
 * @param fileSystemOptions The FileSystem options.
 * @return A Session, never null.
 * @throws FileSystemException if an error occurs.
 */
public static Session createConnection(final String hostname, final int port, final char[] username, final char[] password, final FileSystemOptions fileSystemOptions) throws FileSystemException {
    Objects.requireNonNull(username, "username");
    final JSch jsch = new JSch();
    // new style - user passed
    final SftpFileSystemConfigBuilder builder = SftpFileSystemConfigBuilder.getInstance();
    final File knownHostsFile = builder.getKnownHosts(fileSystemOptions);
    final IdentityProvider[] identities = builder.getIdentityProvider(fileSystemOptions);
    final IdentityRepositoryFactory repositoryFactory = builder.getIdentityRepositoryFactory(fileSystemOptions);
    final ConfigRepository configRepository = builder.getConfigRepository(fileSystemOptions);
    final boolean loadOpenSSHConfig = builder.isLoadOpenSSHConfig(fileSystemOptions);
    final File sshDir = findSshDir();
    setKnownHosts(jsch, sshDir, knownHostsFile);
    if (repositoryFactory != null) {
        jsch.setIdentityRepository(repositoryFactory.create(jsch));
    }
    addIdentities(jsch, sshDir, identities);
    setConfigRepository(jsch, sshDir, configRepository, loadOpenSSHConfig);
    final Session session;
    try {
        session = jsch.getSession(new String(username), hostname, port);
        if (password != null) {
            session.setPassword(new String(password));
        }
        final Duration sessionTimeout = builder.getSessionTimeout(fileSystemOptions);
        if (sessionTimeout != null) {
            session.setTimeout(DurationUtils.toMillisInt(sessionTimeout));
        }
        final UserInfo userInfo = builder.getUserInfo(fileSystemOptions);
        if (userInfo != null) {
            session.setUserInfo(userInfo);
        }
        final Properties config = new Properties();
        // set StrictHostKeyChecking property
        final String strictHostKeyChecking = builder.getStrictHostKeyChecking(fileSystemOptions);
        if (strictHostKeyChecking != null) {
            config.setProperty(KEY_STRICT_HOST_KEY_CHECKING, strictHostKeyChecking);
        }
        // set PreferredAuthentications property
        final String preferredAuthentications = builder.getPreferredAuthentications(fileSystemOptions);
        if (preferredAuthentications != null) {
            config.setProperty(KEY_PREFERRED_AUTHENTICATIONS, preferredAuthentications);
        }
        // set compression property
        final String compression = builder.getCompression(fileSystemOptions);
        if (compression != null) {
            config.setProperty(KEY_COMPRESSION_S2C, compression);
            config.setProperty(KEY_COMPRESSION_C2S, compression);
        }
        final String keyExchangeAlgorithm = builder.getKeyExchangeAlgorithm(fileSystemOptions);
        if (keyExchangeAlgorithm != null) {
            config.setProperty("kex", keyExchangeAlgorithm);
        }
        final String proxyHost = builder.getProxyHost(fileSystemOptions);
        if (proxyHost != null) {
            final int proxyPort = builder.getProxyPort(fileSystemOptions);
            final SftpFileSystemConfigBuilder.ProxyType proxyType = builder.getProxyType(fileSystemOptions);
            final String proxyUser = builder.getProxyUser(fileSystemOptions);
            final String proxyPassword = builder.getProxyPassword(fileSystemOptions);
            Proxy proxy = null;
            if (SftpFileSystemConfigBuilder.PROXY_HTTP.equals(proxyType)) {
                proxy = createProxyHTTP(proxyHost, proxyPort);
                ((ProxyHTTP) proxy).setUserPasswd(proxyUser, proxyPassword);
            } else if (SftpFileSystemConfigBuilder.PROXY_SOCKS5.equals(proxyType)) {
                proxy = createProxySOCKS5(proxyHost, proxyPort);
                ((ProxySOCKS5) proxy).setUserPasswd(proxyUser, proxyPassword);
            } else if (SftpFileSystemConfigBuilder.PROXY_STREAM.equals(proxyType)) {
                proxy = createStreamProxy(proxyHost, proxyPort, fileSystemOptions, builder);
            }
            if (proxy != null) {
                session.setProxy(proxy);
            }
        }
        // set properties for the session
        if (!config.isEmpty()) {
            session.setConfig(config);
        }
        session.setDaemonThread(true);
        session.connect();
    } catch (final Exception exc) {
        throw new FileSystemException("vfs.provider.sftp/connect.error", exc, hostname);
    }
    return session;
}
Also used : ConfigRepository(com.jcraft.jsch.ConfigRepository) Duration(java.time.Duration) UserInfo(com.jcraft.jsch.UserInfo) JSch(com.jcraft.jsch.JSch) Properties(java.util.Properties) FileSystemException(org.apache.commons.vfs2.FileSystemException) IOException(java.io.IOException) JSchException(com.jcraft.jsch.JSchException) Proxy(com.jcraft.jsch.Proxy) FileSystemException(org.apache.commons.vfs2.FileSystemException) ProxyHTTP(com.jcraft.jsch.ProxyHTTP) File(java.io.File) Session(com.jcraft.jsch.Session)

Example 97 with FileSystemOptions

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

the class SftpClientFactory method createStreamProxy.

private static Proxy createStreamProxy(final String proxyHost, final int proxyPort, final FileSystemOptions fileSystemOptions, final SftpFileSystemConfigBuilder builder) {
    // Use a stream proxy, i.e. it will use a remote host as a proxy
    // and run a command (e.g. netcat) that forwards input/output
    // to the target host.
    // Here we get the settings for connecting to the proxy:
    // user, password, options and a command
    final String proxyUser = builder.getProxyUser(fileSystemOptions);
    final String proxyPassword = builder.getProxyPassword(fileSystemOptions);
    final FileSystemOptions proxyOptions = builder.getProxyOptions(fileSystemOptions);
    final String proxyCommand = builder.getProxyCommand(fileSystemOptions);
    // Create the stream proxy
    return new SftpStreamProxy(proxyCommand, proxyUser, proxyHost, proxyPort, proxyPassword, proxyOptions);
}
Also used : FileSystemOptions(org.apache.commons.vfs2.FileSystemOptions)

Example 98 with FileSystemOptions

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

the class HdfsFileSystem method resolveFile.

/**
 * Resolve FileName into FileObject.
 *
 * @param name The name of a file on the HdfsFileSystem.
 * @return resolved FileObject.
 * @throws FileSystemException if an error occurred.
 */
@Override
public FileObject resolveFile(final FileName name) throws FileSystemException {
    synchronized (this) {
        if (this.fs == null) {
            final String hdfsUri = name.getRootURI();
            final HdfsFileSystemConfigBuilder builder = HdfsFileSystemConfigBuilder.getInstance();
            final FileSystemOptions options = getFileSystemOptions();
            final String[] configNames = builder.getConfigNames(options);
            final Path[] configPaths = builder.getConfigPaths(options);
            final URL[] configURLs = builder.getConfigURLs(options);
            final InputStream configStream = builder.getConfigInputStream(options);
            final Configuration configConfiguration = builder.getConfigConfiguration(options);
            final Configuration conf = new Configuration(true);
            conf.set(FileSystem.FS_DEFAULT_NAME_KEY, hdfsUri);
            // no matter where they might come from
            if (configNames != null) {
                for (final String configName : configNames) {
                    log.debug("Adding HDFS configuration resource: " + configName);
                    conf.addResource(configName);
                }
            }
            if (configPaths != null) {
                for (final Path path : configPaths) {
                    log.debug("Adding HDFS configuration path: " + path);
                    conf.addResource(path);
                }
            }
            if (configURLs != null) {
                for (final URL url : configURLs) {
                    log.debug("Adding HDFS configuration URL: " + url);
                    conf.addResource(url);
                }
            }
            if (configStream != null) {
                log.debug("Adding HDFS configuration stream");
                conf.addResource(configStream);
            }
            if (configConfiguration != null) {
                log.debug("Adding HDFS configuration object");
                conf.addResource(configConfiguration);
            }
            try {
                fs = FileSystem.get(conf);
            } catch (final IOException e) {
                log.error("Error connecting to filesystem " + hdfsUri, e);
                throw new FileSystemException("Error connecting to filesystem " + hdfsUri, e);
            }
        }
    }
    final boolean useCache = null != getFileSystemManager().getFilesCache();
    FileObject fileObject = useCache ? getFileFromCache(name) : null;
    if (null == fileObject) {
        String path;
        try {
            path = URLDecoder.decode(name.getPath(), "UTF-8");
        } catch (final UnsupportedEncodingException e) {
            path = name.getPath();
        }
        final Path filePath = new Path(path);
        fileObject = new HdfsFileObject((AbstractFileName) name, this, fs, filePath);
        fileObject = decorateFileObject(fileObject);
        if (useCache) {
            this.putFileToCache(fileObject);
        }
    }
    /*
          resync the file information if requested
         */
    if (getFileSystemManager().getCacheStrategy().equals(CacheStrategy.ON_RESOLVE)) {
        fileObject.refresh();
    }
    return fileObject;
}
Also used : Path(org.apache.hadoop.fs.Path) Configuration(org.apache.hadoop.conf.Configuration) InputStream(java.io.InputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) URL(java.net.URL) FileSystemException(org.apache.commons.vfs2.FileSystemException) FileObject(org.apache.commons.vfs2.FileObject) AbstractFileName(org.apache.commons.vfs2.provider.AbstractFileName) FileSystemOptions(org.apache.commons.vfs2.FileSystemOptions)

Example 99 with FileSystemOptions

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

the class RamFileObject method resize.

/**
 * @param newSize The new buffer size.
 * @throws IOException if the new size exceeds the limit
 */
synchronized void resize(final long newSize) throws IOException {
    final RamFileSystem afs = getAbstractFileSystem();
    final FileSystemOptions afsOptions = afs.getFileSystemOptions();
    if (afsOptions != null) {
        final long maxSize = RamFileSystemConfigBuilder.getInstance().getLongMaxSize(afsOptions);
        if (afs.size() + newSize - this.size() > maxSize) {
            throw new IOException("FileSystem capacity (" + maxSize + ") exceeded.");
        }
    }
    this.data.resize(newSize);
}
Also used : IOException(java.io.IOException) FileSystemOptions(org.apache.commons.vfs2.FileSystemOptions)

Example 100 with FileSystemOptions

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

the class AbstractLayeredFileProvider method findFile.

/**
 * Locates a file object, by absolute URI.
 *
 * @param baseFile The base FileObject.
 * @param uri The name of the file to locate.
 * @param fileSystemOptions The FileSystemOptions.
 * @return The FileObject if it is located, null otherwise.
 * @throws FileSystemException if an error occurs.
 */
@Override
public FileObject findFile(final FileObject baseFile, final String uri, final FileSystemOptions fileSystemOptions) throws FileSystemException {
    // Split the URI up into its parts
    final LayeredFileName name = (LayeredFileName) parseUri(baseFile != null ? baseFile.getName() : null, uri);
    // Make the URI canonical
    // Resolve the outer file name
    final FileName fileName = name.getOuterName();
    final FileObject file = getContext().resolveFile(baseFile, fileName.getURI(), fileSystemOptions);
    // Create the file system
    final FileObject rootFile = createFileSystem(name.getScheme(), file, fileSystemOptions);
    // Resolve the file
    return rootFile.resolveFile(name.getPath());
}
Also used : FileName(org.apache.commons.vfs2.FileName) 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