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