in commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/sftp/SftpClientFactory.java [140:239]
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;
}