From 3c0147cd8c3af7e6d70392ec8897aaf39c9912f6 Mon Sep 17 00:00:00 2001
From: Jean-Noel Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Wed, 26 Mar 2014 10:52:44 +0000
Subject: [PATCH] LDAPManagementContextFactory.java: In getManagementContext(), added back null check to avoid NPE + added another null check. Extracted method couldNotConnect(). More code cleanup.
---
opendj-sdk/opendj3-server-dev/src/server/org/opends/server/tools/dsconfig/LDAPManagementContextFactory.java | 102 ++++++++++++++++++--------------------------------
1 files changed, 37 insertions(+), 65 deletions(-)
diff --git a/opendj-sdk/opendj3-server-dev/src/server/org/opends/server/tools/dsconfig/LDAPManagementContextFactory.java b/opendj-sdk/opendj3-server-dev/src/server/org/opends/server/tools/dsconfig/LDAPManagementContextFactory.java
index 57bafa9..8cf5c25 100644
--- a/opendj-sdk/opendj3-server-dev/src/server/org/opends/server/tools/dsconfig/LDAPManagementContextFactory.java
+++ b/opendj-sdk/opendj3-server-dev/src/server/org/opends/server/tools/dsconfig/LDAPManagementContextFactory.java
@@ -26,8 +26,6 @@
*/
package org.opends.server.tools.dsconfig;
-
-
import static com.forgerock.opendj.cli.ArgumentConstants.OPTION_LONG_HELP;
import static com.forgerock.opendj.cli.ArgumentConstants.OPTION_SHORT_HELP;
import static com.forgerock.opendj.dsconfig.DsconfigMessages.*;
@@ -45,13 +43,12 @@
import javax.net.ssl.SSLException;
import javax.net.ssl.TrustManager;
-import org.forgerock.i18n.LocalizableMessage;
import org.forgerock.i18n.LocalizableMessageBuilder;
+import org.forgerock.opendj.config.LDAPProfile;
import org.forgerock.opendj.config.client.ManagementContext;
+import org.forgerock.opendj.config.client.ldap.LDAPManagementContext;
import org.forgerock.opendj.config.server.ConfigException;
import org.forgerock.opendj.ldap.ErrorResultException;
-import org.forgerock.opendj.config.client.ldap.LDAPManagementContext;
-import org.forgerock.opendj.config.LDAPProfile;
import org.forgerock.opendj.ldap.AuthorizationException;
import org.forgerock.opendj.ldap.Connection;
import org.forgerock.opendj.ldap.LDAPConnectionFactory;
@@ -71,7 +68,6 @@
import com.forgerock.opendj.cli.ReturnCode;
import com.forgerock.opendj.cli.SubCommandArgumentParser;
-
/**
* An LDAP management context factory.
*/
@@ -79,10 +75,10 @@
ManagementContextFactory {
/** The SecureConnectionCliArgsList object. */
- private SecureConnectionCliArgs secureArgsList = null;
+ private SecureConnectionCliArgs secureArgsList;
/** The management context. */
- private ManagementContext context = null;
+ private ManagementContext context;
/** The connection parameters command builder. */
private CommandBuilder contextCommandBuilder;
@@ -91,7 +87,7 @@
private boolean alwaysSSL = false;
/** Raw arguments. */
- private String[] rawArgs = null;
+ private String[] rawArgs;
/**
* Creates a new LDAP management context factory.
@@ -168,10 +164,10 @@
KeyManager keyManager = ci.getKeyManager();
// Do we have a secure connection ?
- Connection connection;
final LDAPOptions options = new LDAPOptions();
options.setConnectTimeout(ci.getConnectTimeout(), TimeUnit.MILLISECONDS);
LDAPConnectionFactory factory = null;
+ Connection connection;
if (ci.useSSL())
{
while (true)
@@ -182,14 +178,7 @@
sslBuilder.setTrustManager((trustManager == null ? TrustManagers
.trustAll() : trustManager));
sslBuilder.setKeyManager(keyManager);
- if (ci.useStartTLS())
- {
- options.setUseStartTLS(true);
- }
- else
- {
- options.setUseStartTLS(false);
- }
+ options.setUseStartTLS(ci.useStartTLS());
options.setSSLContext(sslBuilder.getSSLContext());
factory = new LDAPConnectionFactory(hostName, portNumber, options);
@@ -199,10 +188,12 @@
}
catch (ErrorResultException e)
{
+ final Throwable cause = e.getCause();
if (app.isInteractive()
&& ci.isTrustStoreInMemory()
- && e.getCause() instanceof SSLException
- && e.getCause().getCause() instanceof CertificateException)
+ && cause != null
+ && cause instanceof SSLException
+ && cause.getCause() instanceof CertificateException)
{
String authType = null;
if (trustManager instanceof ApplicationTrustManager)
@@ -221,40 +212,17 @@
}
}
}
- if (e.getCause() instanceof SSLException)
+ if (cause instanceof SSLException)
{
- LocalizableMessage message =
- ERR_FAILED_TO_CONNECT_NOT_TRUSTED.get(
- hostName, portNumber);
throw new ClientException(ReturnCode.CLIENT_SIDE_CONNECT_ERROR,
- message);
+ ERR_FAILED_TO_CONNECT_NOT_TRUSTED.get(hostName, portNumber));
}
- if (e.getCause() instanceof AuthorizationException)
- {
- LocalizableMessage message =
- ERR_DSCFG_ERROR_LDAP_SIMPLE_BIND_NOT_SUPPORTED.get();
- throw new ClientException(ReturnCode.AUTH_METHOD_NOT_SUPPORTED,
- message);
- }
- else if (e.getCause() instanceof AuthenticationException)
- {
- LocalizableMessage message =
- ERR_DSCFG_ERROR_LDAP_SIMPLE_BIND_FAILED.get(bindDN);
- throw new ClientException(ReturnCode.INVALID_CREDENTIALS, message);
- }
- LocalizableMessage message =
- ERR_DSCFG_ERROR_LDAP_FAILED_TO_CONNECT
- .get(hostName, portNumber);
- throw new ClientException(ReturnCode.CLIENT_SIDE_CONNECT_ERROR,
- message);
+ throw couldNotConnect(cause, hostName, portNumber, bindDN);
}
catch (GeneralSecurityException e)
{
- LocalizableMessage message =
- ERR_DSCFG_ERROR_LDAP_FAILED_TO_CONNECT
- .get(hostName, portNumber);
throw new ClientException(ReturnCode.CLIENT_SIDE_CONNECT_ERROR,
- message);
+ ERR_DSCFG_ERROR_LDAP_FAILED_TO_CONNECT.get(hostName, portNumber));
}
}
}
@@ -270,27 +238,14 @@
}
catch (ErrorResultException e)
{
- if (e.getCause() instanceof AuthorizationException)
- {
- LocalizableMessage message =
- ERR_DSCFG_ERROR_LDAP_SIMPLE_BIND_NOT_SUPPORTED.get();
- throw new ClientException(ReturnCode.AUTH_METHOD_NOT_SUPPORTED,
- message);
- }
- else if (e.getCause() instanceof AuthenticationException)
- {
- LocalizableMessage message =
- ERR_DSCFG_ERROR_LDAP_SIMPLE_BIND_FAILED.get(bindDN);
- throw new ClientException(ReturnCode.INVALID_CREDENTIALS, message);
- }
- LocalizableMessage message =
- ERR_DSCFG_ERROR_LDAP_FAILED_TO_CONNECT.get(hostName, portNumber);
- throw new ClientException(ReturnCode.CLIENT_SIDE_CONNECT_ERROR,
- message);
+ throw couldNotConnect(e.getCause(), hostName, portNumber, bindDN);
}
finally
{
- factory.close();
+ if (factory != null)
+ {
+ factory.close();
+ }
}
}
context =
@@ -299,6 +254,23 @@
return context;
}
+ private ClientException couldNotConnect(Throwable cause, String hostName,
+ Integer portNumber, String bindDN)
+ {
+ if (cause instanceof AuthorizationException)
+ {
+ return new ClientException(ReturnCode.AUTH_METHOD_NOT_SUPPORTED,
+ ERR_DSCFG_ERROR_LDAP_SIMPLE_BIND_NOT_SUPPORTED.get());
+ }
+ else if (cause instanceof AuthenticationException)
+ {
+ return new ClientException(ReturnCode.INVALID_CREDENTIALS,
+ ERR_DSCFG_ERROR_LDAP_SIMPLE_BIND_FAILED.get(bindDN));
+ }
+ return new ClientException(ReturnCode.CLIENT_SIDE_CONNECT_ERROR,
+ ERR_DSCFG_ERROR_LDAP_FAILED_TO_CONNECT.get(hostName, portNumber));
+ }
+
/** {@inheritDoc} */
@Override
public void setRawArguments(String[] args) {
--
Gitblit v1.10.0