Class SftpConnector


  • public class SftpConnector
    extends Object
    A simple wrapper around a JSch instance that handles connecting and disconnecting using the configuration specified in an SftpConfig.
    Implementation Note:
    This requires JSch being available at runtime.
    • Constructor Detail

      • SftpConnector

        public SftpConnector​(SftpConfig config)
        Construct using the given SftpConfig.

        The instance is not connected; call connect() to open a connection.

        Parameters:
        config - the SFTP configuration
      • SftpConnector

        public SftpConnector​(com.jcraft.jsch.JSch jsch,
                             SftpConfig config)
        Construct using the given JSch and SftpConfig.

        The instance is not connected; call connect() to open a connection.

        Parameters:
        jsch - the JSch instance to use
        config - the SFTP configuration
    • Method Detail

      • setupAndOpenConnection

        public static SftpConnector setupAndOpenConnection​(SftpConfig config)
        Creates a new connector and immediately opens a new connection.
        Parameters:
        config - The configuration used for setting up the connection
        Returns:
        The initialized and opened sftp connection
        API Note:
        This is a convenience method to not have to call connect after initialization.
      • connect

        public void connect()
        Opens a connection to the remote SFTP server.

        Applies the following configurations:

        • Known hosts (JSch.setKnownHosts(String))
        • User, host, and port (to create a Session via JSch.getSession(String, String, int))
        • Session timeout (Session.setTimeout(int)
        • PreferredAuthentications (via Session.setConfig(String, String))
        • Key exchange type (detected from known hosts, see KiwiJSchHelpers
        • Private key or password (JSch.addIdentity(String) or Session.setPassword(String)
        • Enable/disable StrictHostKeyChecking (via Session.setConfig(String, String), default is enabled)
      • disconnect

        public void disconnect()
        Closes and cleans up the connection to the remote SFTP server.

        Once disconnected, calling this method again will have no effect unless connect() is called again.