core/src/main/java/org/apache/commons/vfs2/provider/sftp/SftpClientFactory.java
/**
     * 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.
     * @throws FileSystemException if an error occurs.
     */
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.
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 .
public static Session createConnection(String hostname, int port, char[] username, char[] password,
                                           FileSystemOptions fileSystemOptions) throws FileSystemException
    {
        JSch.setLogger(new JSchLogger());
        JSch jsch = new JSch();
        File sshDir = null;
        File knownHostsFile = SftpFileSystemConfigBuilder.getInstance().getKnownHosts(fileSystemOptions);
        File[] identities = SftpFileSystemConfigBuilder.getInstance().getIdentities(fileSystemOptions);
        String passPhrase = SftpFileSystemConfigBuilder.getInstance().getIdentityPassPhrase(fileSystemOptions);
        if (knownHostsFile != null)
        {
            try
            {
                jsch.setKnownHosts(knownHostsFile.getAbsolutePath());
            }
            catch (JSchException e)
            {
                throw new FileSystemException("vfs.provider.sftp/known-hosts.error",
                    knownHostsFile.getAbsolutePath(), e);
            }
        }
        else
        {
            sshDir = findSshDir();
            knownHostsFile = new File(sshDir, "known_hosts");
            if (knownHostsFile.isFile() && knownHostsFile.canRead())
            {
                try
                {
                    jsch.setKnownHosts(knownHostsFile.getAbsolutePath());
                }
                catch (JSchException e)
                {
                    throw new FileSystemException("vfs.provider.sftp/known-hosts.error",
                        knownHostsFile.getAbsolutePath(), e);
                }
            }
        }
        if (identities != null)
        {
            for (int iterIdentities = 0; iterIdentities < identities.length; iterIdentities++)
            {
                final File privateKeyFile = identities[iterIdentities];
                try
                {
                    if (passPhrase != null) {
                        jsch.addIdentity(privateKeyFile.getAbsolutePath(), passPhrase);
                    } else {
                        jsch.addIdentity(privateKeyFile.getAbsolutePath());
                    }
                }
                catch (final JSchException e)
                {
                    throw new FileSystemException("vfs.provider.sftp/load-private-key.error", privateKeyFile, e);
                }
            }
        }
        else
        {
            if (sshDir == null)
            {
                sshDir = findSshDir();
            }
            final File privateKeyFile = new File(sshDir, "id_rsa");
            if (privateKeyFile.isFile() && privateKeyFile.canRead())
            {
                try
                {
                    if (passPhrase != null) {
                        jsch.addIdentity(privateKeyFile.getAbsolutePath(), passPhrase);
                    } else {
                        jsch.addIdentity(privateKeyFile.getAbsolutePath());
                    }
                }
                catch (final JSchException e)
                {
                    throw new FileSystemException("vfs.provider.sftp/load-private-key.error", privateKeyFile, e);
                }
            }
        }
        UserAuthenticationData proxyAuthData = null;
        Session session;
        try
        {
            session = jsch.getSession(new String(username),
                    hostname,
                    port);
            if (password != null)
            {
                session.setPassword(new String(password));
            }
            Integer timeout = SftpFileSystemConfigBuilder.getInstance().getTimeout(fileSystemOptions);
            if (timeout != null)
            {
                session.setTimeout(timeout.intValue());
            }
            UserInfo userInfo = SftpFileSystemConfigBuilder.getInstance().getUserInfo(fileSystemOptions);
            if (userInfo != null)
            {
                session.setUserInfo(userInfo);
            }
            Properties config = new Properties();
            String strictHostKeyChecking =
                SftpFileSystemConfigBuilder.getInstance().getStrictHostKeyChecking(fileSystemOptions);
            if (strictHostKeyChecking != null)
            {
                config.setProperty("StrictHostKeyChecking", strictHostKeyChecking);
            }
            String preferredAuthentications = SftpFileSystemConfigBuilder.getInstance().
            getPreferredAuthentications(fileSystemOptions);
            if (preferredAuthentications != null)
            {
                config.setProperty("PreferredAuthentications", preferredAuthentications);
            }
            String compression = SftpFileSystemConfigBuilder.getInstance().getCompression(fileSystemOptions);
            if (compression != null)
            {
                config.setProperty("compression.s2c", compression);
                config.setProperty("compression.c2s", compression);
            }
            String proxyHost = SftpFileSystemConfigBuilder.getInstance().getProxyHost(fileSystemOptions);
            if (proxyHost != null)
            {
                int proxyPort = SftpFileSystemConfigBuilder.getInstance().getProxyPort(fileSystemOptions);
                SftpFileSystemConfigBuilder.ProxyType proxyType =
                    SftpFileSystemConfigBuilder.getInstance().getProxyType(fileSystemOptions);
                Proxy proxy = null;
				UserAuthenticator proxyAuth =
				                              SftpFileSystemConfigBuilder.getInstance()
				                                                         .getProxyUserAuthenticator(fileSystemOptions);
				char[] proxyUsername = null, proxyPassword = null;
				if (proxyAuth != null) {
					proxyAuthData =
					                UserAuthenticatorUtils.authenticate(proxyAuth,
					                                                    SftpFileProvider.AUTHENTICATOR_TYPES);
					proxyUsername =
					                UserAuthenticatorUtils.getData(proxyAuthData,
					                                               UserAuthenticationData.USERNAME,
					                                               null);
					proxyPassword =
					                UserAuthenticatorUtils.getData(proxyAuthData,
					                                               UserAuthenticationData.PASSWORD,
					                                               null);
				}               
                if (SftpFileSystemConfigBuilder.PROXY_HTTP.equals(proxyType))
                {
                    if (proxyPort != 0)
                    {
                        proxy = new ProxyHTTP(proxyHost, proxyPort);
                    }
                    else
                    {
                        proxy = new ProxyHTTP(proxyHost);
                    }                    
					if (proxyUsername != null && proxyPassword != null) {
						((ProxyHTTP) proxy).setUserPasswd(new String(proxyUsername),
						                                  new String(proxyPassword));
					}
				}
                else if (SftpFileSystemConfigBuilder.PROXY_SOCKS5.equals(proxyType))
                {
                    if (proxyPort != 0)
                    {
                        proxy = new ProxySOCKS5(proxyHost, proxyPort);
                    }
                    else
                    {
                        proxy = new ProxySOCKS5(proxyHost);
                    }
					if (proxyUsername != null && proxyPassword != null) {
						((ProxySOCKS5) proxy).setUserPasswd(new String(proxyUsername),
						                                    new String(proxyPassword));
					}                    
                }
                if (proxy != null)
                {
                    session.setProxy(proxy);
                }
            }
            if (config.size() > 0)
            {
                session.setConfig(config);
            }
            session.setDaemonThread(true);
            session.connect();
        }
        catch (final Exception exc)
        {
            throw new FileSystemException("vfs.provider.sftp/connect.error", new Object[]{hostname}, exc);
        } finally {
			UserAuthenticatorUtils.cleanup(proxyAuthData);
		}
        return session;
    }
public static Session createConnection ( String hostname , int port , char [ ] username , char [ ] password , FileSystemOptions fileSystemOptions ) throws FileSystemException { JSch . setLogger ( new JSchLogger ( ) ) ; JSch jsch = new JSch ( ) ; File sshDir = null ; File knownHostsFile = SftpFileSystemConfigBuilder . getInstance ( ) . getKnownHosts ( fileSystemOptions ) ; File [ ] identities = SftpFileSystemConfigBuilder . getInstance ( ) . getIdentities ( fileSystemOptions ) ; String passPhrase = SftpFileSystemConfigBuilder . getInstance ( ) . getIdentityPassPhrase ( fileSystemOptions ) ; if ( knownHostsFile != null ) { try { jsch . setKnownHosts ( knownHostsFile . getAbsolutePath ( ) ) ; } catch ( JSchException e ) { throw new FileSystemException ( "vfs.provider.sftp/known-hosts.error" , knownHostsFile . getAbsolutePath ( ) , e ) ; } } else { sshDir = findSshDir ( ) ; knownHostsFile = new File ( sshDir , "known_hosts" ) ; if ( knownHostsFile . isFile ( ) && knownHostsFile . canRead ( ) ) { try { jsch . setKnownHosts ( knownHostsFile . getAbsolutePath ( ) ) ; } catch ( JSchException e ) { throw new FileSystemException ( "vfs.provider.sftp/known-hosts.error" , knownHostsFile . getAbsolutePath ( ) , e ) ; } } } if ( identities != null ) { for ( int iterIdentities = 0 ; iterIdentities < identities . length ; iterIdentities ++ ) { final File privateKeyFile = identities [ iterIdentities ] ; try { if ( passPhrase != null ) { jsch . addIdentity ( privateKeyFile . getAbsolutePath ( ) , passPhrase ) ; } else { jsch . addIdentity ( privateKeyFile . getAbsolutePath ( ) ) ; } } catch ( final JSchException e ) { throw new FileSystemException ( "vfs.provider.sftp/load-private-key.error" , privateKeyFile , e ) ; } } } else { if ( sshDir == null ) { sshDir = findSshDir ( ) ; } final File privateKeyFile = new File ( sshDir , "id_rsa" ) ; if ( privateKeyFile . isFile ( ) && privateKeyFile . canRead ( ) ) { try { if ( passPhrase != null ) { jsch . addIdentity ( privateKeyFile . getAbsolutePath ( ) , passPhrase ) ; } else { jsch . addIdentity ( privateKeyFile . getAbsolutePath ( ) ) ; } } catch ( final JSchException e ) { throw new FileSystemException ( "vfs.provider.sftp/load-private-key.error" , privateKeyFile , e ) ; } } } UserAuthenticationData proxyAuthData = null ; Session session ; try { session = jsch . getSession ( new String ( username ) , hostname , port ) ; if ( password != null ) { session . setPassword ( new String ( password ) ) ; } Integer timeout = SftpFileSystemConfigBuilder . getInstance ( ) . getTimeout ( fileSystemOptions ) ; if ( timeout != null ) { session . setTimeout ( timeout . intValue ( ) ) ; } UserInfo userInfo = SftpFileSystemConfigBuilder . getInstance ( ) . getUserInfo ( fileSystemOptions ) ; if ( userInfo != null ) { session . setUserInfo ( userInfo ) ; } Properties config = new Properties ( ) ; String strictHostKeyChecking = SftpFileSystemConfigBuilder . getInstance ( ) . getStrictHostKeyChecking ( fileSystemOptions ) ; if ( strictHostKeyChecking != null ) { config . setProperty ( "StrictHostKeyChecking" , strictHostKeyChecking ) ; } String preferredAuthentications = SftpFileSystemConfigBuilder . getInstance ( ) . getPreferredAuthentications ( fileSystemOptions ) ; if ( preferredAuthentications != null ) { config . setProperty ( "PreferredAuthentications" , preferredAuthentications ) ; } String compression = SftpFileSystemConfigBuilder . getInstance ( ) . getCompression ( fileSystemOptions ) ; if ( compression != null ) { config . setProperty ( "compression.s2c" , compression ) ; config . setProperty ( "compression.c2s" , compression ) ; } String proxyHost = SftpFileSystemConfigBuilder . getInstance ( ) . getProxyHost ( fileSystemOptions ) ; if ( proxyHost != null ) { int proxyPort = SftpFileSystemConfigBuilder . getInstance ( ) . getProxyPort ( fileSystemOptions ) ; SftpFileSystemConfigBuilder . ProxyType proxyType = SftpFileSystemConfigBuilder . getInstance ( ) . getProxyType ( fileSystemOptions ) ; Proxy proxy = null ; UserAuthenticator proxyAuth = SftpFileSystemConfigBuilder . getInstance ( ) . getProxyUserAuthenticator ( fileSystemOptions ) ; char [ ] proxyUsername = null , proxyPassword = null ; if ( proxyAuth != null ) { proxyAuthData = UserAuthenticatorUtils . authenticate ( proxyAuth , SftpFileProvider . AUTHENTICATOR_TYPES ) ; proxyUsername = UserAuthenticatorUtils . getData ( proxyAuthData , UserAuthenticationData . USERNAME , null ) ; proxyPassword = UserAuthenticatorUtils . getData ( proxyAuthData , UserAuthenticationData . PASSWORD , null ) ; } if ( SftpFileSystemConfigBuilder . PROXY_HTTP . equals ( proxyType ) ) { if ( proxyPort != 0 ) { proxy = new ProxyHTTP ( proxyHost , proxyPort ) ; } else { proxy = new ProxyHTTP ( proxyHost ) ; } if ( proxyUsername != null && proxyPassword != null ) { ( ( ProxyHTTP ) proxy ) . setUserPasswd ( new String ( proxyUsername ) , new String ( proxyPassword ) ) ; } } else if ( SftpFileSystemConfigBuilder . PROXY_SOCKS5 . equals ( proxyType ) ) { if ( proxyPort != 0 ) { proxy = new ProxySOCKS5 ( proxyHost , proxyPort ) ; } else { proxy = new ProxySOCKS5 ( proxyHost ) ; } if ( proxyUsername != null && proxyPassword != null ) { ( ( ProxySOCKS5 ) proxy ) . setUserPasswd ( new String ( proxyUsername ) , new String ( proxyPassword ) ) ; } } if ( proxy != null ) { session . setProxy ( proxy ) ; } } if ( config . size ( ) > 0 ) { session . setConfig ( config ) ; } session . setDaemonThread ( true ) ; session . connect ( ) ; } catch ( final Exception exc ) { throw new FileSystemException ( "vfs.provider.sftp/connect.error" , new Object [ ] { hostname } , exc ) ; } finally { UserAuthenticatorUtils . cleanup ( proxyAuthData ) ; } return session ; }
Creates a new connection to the server.
Creates a new connection to the server .
// new style - user passed // Load the known hosts file // Load the private key (rsa-key only) //set StrictHostKeyChecking property //set PreferredAuthentications property //set compression property //set properties for the session