{
host = getHostNameForLdapUrl(host);
String ldapUrl = "ldaps://"+host+":"+port;
InitialLdapContext ctx = null;
ApplicationTrustManager trustManager = getTrustManager();
trustManager.setHost(host);
trustManager.resetLastRefusedItems();
try
{
effectiveDn[0] = dn;
try
{
ctx = createLdapsContext(ldapUrl, dn, pwd,
getConnectTimeout(), null, trustManager);
}
catch (Throwable t)
{
if (!isCertificateException(t))
{
// Try using a global administrator
dn = ADSContext.getAdministratorDN(dn);
effectiveDn[0] = dn;
ctx = createLdapsContext(ldapUrl, dn, pwd,
getConnectTimeout(), null, trustManager);
}
else
{
throw t;
}
}
ADSContext adsContext = new ADSContext(ctx);
if (adsContext.hasAdminData())
{
/* Check if there are already global administrators */
Set<?> administrators = adsContext.readAdministratorRegistry();
if (administrators.size() > 0)
{
hasGlobalAdministrators[0] = true;
}
else
{
hasGlobalAdministrators[0] = false;
}
Set<TopologyCacheException> exceptions =
updateUserDataWithSuffixesInADS(adsContext, trustManager);
Set<Message> exceptionMsgs = new LinkedHashSet<Message>();
/* Check the exceptions and see if we throw them or not. */
for (TopologyCacheException e : exceptions)
{
switch (e.getType())
{
case NOT_GLOBAL_ADMINISTRATOR:
Message errorMsg = INFO_NOT_GLOBAL_ADMINISTRATOR_PROVIDED.get();
throw new UserDataException(Step.REPLICATION_OPTIONS, errorMsg);
case GENERIC_CREATING_CONNECTION:
if ((e.getCause() != null) &&
isCertificateException(e.getCause()))
{
UserDataCertificateException.Type excType;
ApplicationTrustManager.Cause cause = null;
if (e.getTrustManager() != null)
{
cause = e.getTrustManager().getLastRefusedCause();
}
LOG.log(Level.INFO, "Certificate exception cause: "+cause);
if (cause == ApplicationTrustManager.Cause.NOT_TRUSTED)
{
excType = UserDataCertificateException.Type.NOT_TRUSTED;
}
else if (cause ==
ApplicationTrustManager.Cause.HOST_NAME_MISMATCH)
{
excType = UserDataCertificateException.Type.HOST_NAME_MISMATCH;
}
else
{
excType = null;
}
if (excType != null)
{
String h;
int p;
try
{
URI uri = new URI(e.getLdapUrl());
h = uri.getHost();
p = uri.getPort();
}
catch (Throwable t)
{
LOG.log(Level.WARNING,
"Error parsing ldap url of TopologyCacheException.", t);
h = INFO_NOT_AVAILABLE_LABEL.get().toString();
p = -1;
}
throw new UserDataCertificateException(
Step.REPLICATION_OPTIONS,
INFO_CERTIFICATE_EXCEPTION.get(
h, String.valueOf(p)),
e.getCause(), h, p,
e.getTrustManager().getLastRefusedChain(),
e.getTrustManager().getLastRefusedAuthType(), excType);
}
}
}
exceptionMsgs.add(getMessage(e));
}
if (exceptionMsgs.size() > 0)
{
Message confirmationMsg =
INFO_ERROR_READING_REGISTERED_SERVERS_CONFIRM.get(
getMessageFromCollection(exceptionMsgs, "\n"));
throw new UserDataConfirmationException(Step.REPLICATION_OPTIONS,
confirmationMsg);
}
}
else
{
updateUserDataWithSuffixesInServer(ctx);
}
}
catch (UserDataException ude)
{
throw ude;
}
catch (Throwable t)
{
LOG.log(Level.INFO, "Error connecting to remote server.", t);
if (isCertificateException(t))
{
UserDataCertificateException.Type excType;
ApplicationTrustManager.Cause cause =
trustManager.getLastRefusedCause();
LOG.log(Level.INFO, "Certificate exception cause: "+cause);
if (cause == ApplicationTrustManager.Cause.NOT_TRUSTED)
{
excType = UserDataCertificateException.Type.NOT_TRUSTED;
}
else if (cause == ApplicationTrustManager.Cause.HOST_NAME_MISMATCH)
{
excType = UserDataCertificateException.Type.HOST_NAME_MISMATCH;
}
else
{
excType = null;
}
if (excType != null)
{
throw new UserDataCertificateException(Step.REPLICATION_OPTIONS,
INFO_CERTIFICATE_EXCEPTION.get(host, String.valueOf(port)), t,
host, port, trustManager.getLastRefusedChain(),
trustManager.getLastRefusedAuthType(), excType);
}
else
{
qs.displayFieldInvalid(FieldName.REMOTE_SERVER_HOST, true);
qs.displayFieldInvalid(FieldName.REMOTE_SERVER_PORT, true);