public void openConnectionInternal() throws AuthenticationException {
    if (authenticationInfo == null) {
      authenticationInfo = new AuthenticationInfo();
    }

    if (!interactive) {
      uIKeyboardInteractive = null;
      setInteractiveUserInfo(new NullInteractiveUserInfo());
    }

    JSch sch = new JSch();

    File privateKey;
    try {
      privateKey = ScpHelper.getPrivateKey(authenticationInfo);
    } catch (FileNotFoundException e) {
      throw new AuthenticationException(e.getMessage());
    }

    try {
      Connector connector = ConnectorFactory.getDefault().createConnector();
      if (connector != null) {
        IdentityRepository repo = new RemoteIdentityRepository(connector);
        sch.setIdentityRepository(repo);
      }
    } catch (AgentProxyException e) {
      fireSessionDebug("Unable to connect to agent: " + e.toString());
    }

    if (privateKey != null && privateKey.exists()) {
      fireSessionDebug("Using private key: " + privateKey);
      try {
        sch.addIdentity(privateKey.getAbsolutePath(), authenticationInfo.getPassphrase());
      } catch (JSchException e) {
        throw new AuthenticationException("Cannot connect. Reason: " + e.getMessage(), e);
      }
    }

    String host = getRepository().getHost();
    int port =
        repository.getPort() == WagonConstants.UNKNOWN_PORT
            ? ScpHelper.DEFAULT_SSH_PORT
            : repository.getPort();
    try {
      String userName = authenticationInfo.getUserName();
      if (userName == null) {
        userName = System.getProperty("user.name");
      }
      session = sch.getSession(userName, host, port);
      session.setTimeout(getTimeout());
    } catch (JSchException e) {
      throw new AuthenticationException("Cannot connect. Reason: " + e.getMessage(), e);
    }

    Proxy proxy = null;
    ProxyInfo proxyInfo = getProxyInfo(ProxyInfo.PROXY_SOCKS5, getRepository().getHost());
    if (proxyInfo != null && proxyInfo.getHost() != null) {
      proxy = new ProxySOCKS5(proxyInfo.getHost(), proxyInfo.getPort());
      ((ProxySOCKS5) proxy).setUserPasswd(proxyInfo.getUserName(), proxyInfo.getPassword());
    } else {
      proxyInfo = getProxyInfo(ProxyInfo.PROXY_HTTP, getRepository().getHost());
      if (proxyInfo != null && proxyInfo.getHost() != null) {
        proxy = new ProxyHTTP(proxyInfo.getHost(), proxyInfo.getPort());
        ((ProxyHTTP) proxy).setUserPasswd(proxyInfo.getUserName(), proxyInfo.getPassword());
      } else {
        // Backwards compatibility
        proxyInfo = getProxyInfo(getRepository().getProtocol(), getRepository().getHost());
        if (proxyInfo != null && proxyInfo.getHost() != null) {
          // if port == 1080 we will use SOCKS5 Proxy, otherwise will use HTTP Proxy
          if (proxyInfo.getPort() == SOCKS5_PROXY_PORT) {
            proxy = new ProxySOCKS5(proxyInfo.getHost(), proxyInfo.getPort());
            ((ProxySOCKS5) proxy).setUserPasswd(proxyInfo.getUserName(), proxyInfo.getPassword());
          } else {
            proxy = new ProxyHTTP(proxyInfo.getHost(), proxyInfo.getPort());
            ((ProxyHTTP) proxy).setUserPasswd(proxyInfo.getUserName(), proxyInfo.getPassword());
          }
        }
      }
    }
    session.setProxy(proxy);

    // username and password will be given via UserInfo interface.
    UserInfo ui = new WagonUserInfo(authenticationInfo, getInteractiveUserInfo());

    if (uIKeyboardInteractive != null) {
      ui = new UserInfoUIKeyboardInteractiveProxy(ui, uIKeyboardInteractive);
    }

    Properties config = new Properties();
    if (getKnownHostsProvider() != null) {
      try {
        String contents = getKnownHostsProvider().getContents();
        if (contents != null) {
          sch.setKnownHosts(new StringInputStream(contents));
        }
      } catch (JSchException e) {
        // continue without known_hosts
      }
      config.setProperty("StrictHostKeyChecking", getKnownHostsProvider().getHostKeyChecking());
    }

    if (authenticationInfo.getPassword() != null) {
      config.setProperty(
          "PreferredAuthentications", "gssapi-with-mic,publickey,password,keyboard-interactive");
    }

    config.setProperty("BatchMode", interactive ? "no" : "yes");

    session.setConfig(config);

    session.setUserInfo(ui);

    StringWriter stringWriter = new StringWriter();
    try {
      session.connect();

      if (getKnownHostsProvider() != null) {
        PrintWriter w = new PrintWriter(stringWriter);

        HostKeyRepository hkr = sch.getHostKeyRepository();
        HostKey[] keys = hkr.getHostKey();

        for (int i = 0; keys != null && i < keys.length; i++) {
          HostKey key = keys[i];
          w.println(key.getHost() + " " + key.getType() + " " + key.getKey());
        }
      }
    } catch (JSchException e) {
      if (e.getMessage().startsWith("UnknownHostKey:")
          || e.getMessage().startsWith("reject HostKey:")) {
        throw new UnknownHostException(host, e);
      } else if (e.getMessage().contains("HostKey has been changed")) {
        throw new KnownHostChangedException(host, e);
      } else {
        throw new AuthenticationException("Cannot connect. Reason: " + e.getMessage(), e);
      }
    }

    try {
      getKnownHostsProvider().storeKnownHosts(stringWriter.toString());
    } catch (IOException e) {
      closeConnection();

      throw new AuthenticationException(
          "Connection aborted - failed to write to known_hosts. Reason: " + e.getMessage(), e);
    }
  }
 public void putDirectory(File sourceDirectory, String destinationDirectory)
     throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException {
   sshTool.putDirectory(this, sourceDirectory, destinationDirectory);
 }
 public boolean resourceExists(String resourceName)
     throws TransferFailedException, AuthorizationException {
   return sshTool.resourceExists(resourceName, repository);
 }
 public List<String> getFileList(String destinationDirectory)
     throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException {
   return sshTool.getFileList(destinationDirectory, repository);
 }