}
public static RepositoryInformation validateRepository(String repoURL, String repoUsername, String repoPassword,
boolean privateRepo, boolean testConnection) throws RepositoryRequiredException, ADCException,
RepositoryCredentialsRequiredException, InvalidRepositoryException, RepositoryTransportException {
RepositoryInformation repositoryInformation = new RepositoryInformation();
repositoryInformation.setRepoURL(repoURL);
if (log.isDebugEnabled()) {
log.debug("Validating Git Repository");
}
if (repoURL != null && repoURL.trim().length() > 0 && privateRepo) {
if (log.isDebugEnabled()) {
log.debug("External repo validation is a private repo: " + repoURL);
}
if (repoUsername == null || repoUsername.trim().length() == 0 || repoPassword == null
|| repoPassword.trim().length() == 0) {
throw new RepositoryCredentialsRequiredException(
"Username and Password are required for private repository");
}
}
if (!testConnection) {
if (log.isDebugEnabled()) {
log.debug("External repo validation is not enabled");
}
return repositoryInformation;
}
if (repoURL == null || repoURL.trim().length() == 0) {
// This means, no repo to validate.
return repositoryInformation;
}
if (log.isDebugEnabled()) {
log.debug("External repo validation enabled");
}
// This assumes running on Linux.
String parentDirName = "/tmp/" + UUID.randomUUID().toString();
CredentialsProvider credentialsProvider = null;
if (repoUsername != null && repoUsername.trim().length() > 0 && repoPassword != null
&& repoPassword.trim().length() > 0) {
if (log.isDebugEnabled()) {
log.debug("External repo credentails are passed: " + repoUsername);
}
credentialsProvider = new UsernamePasswordCredentialsProvider(repoUsername, repoPassword.toCharArray());
}
// Initialize temp local file repo
FileRepository localRepo = null;
try {
File f = new File(parentDirName + "/.git");
localRepo = new FileRepository(f);
if (log.isDebugEnabled()) {
log.debug("Local File Repo: " + f.getAbsoluteFile());
}
} catch (IOException e) {
throw new ADCException("Error creating local file repo", e);
}
Git git = new Git(localRepo);
LsRemoteCommand cmd = git.lsRemote().setRemote(repoURL);
if (credentialsProvider != null) {
cmd.setCredentialsProvider(credentialsProvider);
}
try {
Collection<Ref> collection = cmd.call();
List<String> refNames = new ArrayList<String>();
if (collection != null) {
for (Ref ref : collection) {
if (log.isDebugEnabled()) {
log.debug(repoURL + ": " + ref.getName());
}
refNames.add(ref.getName());
}
}
repositoryInformation.setRefName(refNames.toArray(new String[refNames.size()]));
} catch (InvalidRemoteException e) {
throw new InvalidRepositoryException("Provided repository url is not valid", e);
} catch (TransportException e) {
throw new RepositoryTransportException("Transport error when checking remote repository", e);
} catch (GitAPIException e) {