use of com.jcraft.jsch.ConfigRepository.Config in project ant-ivy by apache.
the class AbstractSshBasedRepository method getSession.
/**
* get a new session using the default attributes if the given String is a full uri, use the
* data from the uri instead
*
* @param pathOrUri
* might be just a path or a full ssh or sftp uri
* @return matching Session
* @throws IOException if something goes wrong
*/
protected Session getSession(String pathOrUri) throws IOException {
URI uri = parseURI(pathOrUri);
String host = getHost();
int port = getPort();
String user = getUser();
String userPassword = getUserPassword();
String sshConfig = getSshConfig();
File keyFile = getKeyFile();
if (uri != null && uri.getScheme() != null) {
if (uri.getHost() != null) {
host = uri.getHost();
}
if (uri.getPort() != -1) {
port = uri.getPort();
}
if (uri.getUserInfo() != null) {
String userInfo = uri.getUserInfo();
if (!userInfo.contains(":")) {
user = userInfo;
} else {
user = userInfo.substring(0, userInfo.indexOf(":"));
userPassword = userInfo.substring(userInfo.indexOf(":") + 1);
}
}
}
if (sshConfig != null) {
ConfigRepository configRepository = OpenSSHConfig.parseFile(sshConfig);
Config config = configRepository.getConfig(host);
host = config.getHostname();
if (user == null) {
user = config.getUser();
}
String keyFilePath = config.getValue("IdentityFile");
if (keyFilePath != null && keyFile == null) {
keyFile = new File(keyFilePath);
}
}
if (host == null) {
throw new IllegalArgumentException("missing host information. host should be provided either " + "directly on the repository or in the connection URI " + ", or in the openssh config file specified by sshConfig");
}
if (user == null) {
Credentials c = requestCredentials(host);
if (c != null) {
user = c.getUserName();
userPassword = c.getPasswd();
} else {
Message.error("username is not set");
}
}
return SshCache.getInstance().getSession(host, port, user, userPassword, keyFile, getKeyFilePassword(), getPassFile(), isAllowedAgentUse());
}
Aggregations