From de53ea1cb792e9afda057607adf4825166adc583 Mon Sep 17 00:00:00 2001
From: Jean-Noël Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Fri, 19 Aug 2016 16:37:53 +0000
Subject: [PATCH] code cleanup

---
 opendj-server-legacy/src/main/java/org/opends/server/util/cli/LDAPConnectionConsoleInteraction.java |    6 +-
 opendj-server-legacy/src/main/java/org/opends/admin/ads/util/ApplicationTrustManager.java           |   15 +++----
 opendj-server-legacy/src/main/java/org/opends/server/admin/client/cli/SecureConnectionCliArgs.java  |   77 ++++++++++++++------------------------
 3 files changed, 39 insertions(+), 59 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/admin/ads/util/ApplicationTrustManager.java b/opendj-server-legacy/src/main/java/org/opends/admin/ads/util/ApplicationTrustManager.java
index 67112d5..29271c9 100644
--- a/opendj-server-legacy/src/main/java/org/opends/admin/ads/util/ApplicationTrustManager.java
+++ b/opendj-server-legacy/src/main/java/org/opends/admin/ads/util/ApplicationTrustManager.java
@@ -29,6 +29,7 @@
 import javax.net.ssl.TrustManager;
 import javax.net.ssl.TrustManagerFactory;
 import javax.net.ssl.X509TrustManager;
+import javax.security.auth.x500.X500Principal;
 
 import org.forgerock.i18n.LocalizableMessage;
 import org.forgerock.i18n.slf4j.LocalizedLogger;
@@ -312,9 +313,7 @@
     copy.acceptedChains.addAll(acceptedChains);
     copy.acceptedAuthTypes.addAll(acceptedAuthTypes);
     copy.acceptedHosts.addAll(acceptedHosts);
-
     copy.host = host;
-
     return copy;
   }
 
@@ -360,10 +359,11 @@
   {
     if (host != null)
     {
+      final X500Principal subjectX500Principal = chain[0].getSubjectX500Principal();
       boolean matches = false;
       try
       {
-        DN dn = DN.valueOf(chain[0].getSubjectX500Principal().getName());
+        DN dn = DN.valueOf(subjectX500Principal.getName());
         String value = dn.rdn(dn.size() - 1).getFirstAVA().getAttributeValue().toString();
         matches = hostMatch(value, host);
         if (!matches)
@@ -388,14 +388,14 @@
       catch (Throwable t)
       {
         logger.warn(LocalizableMessage.raw("Error parsing subject dn: "+
-            chain[0].getSubjectX500Principal(), t));
+            subjectX500Principal, t));
       }
 
       if (!matches)
       {
         throw new OpendsCertificateException(
             "Hostname mismatch between host name " + host
-                + " and subject DN: " + chain[0].getSubjectX500Principal(),
+                + " and subject DN: " + subjectX500Principal,
             chain);
       }
     }
@@ -433,8 +433,7 @@
    * host name.
    * @param host1 the first host name.
    * @param host2 the second host name.
-   * @return <CODE>true</CODE> if the host match and <CODE>false</CODE>
-   * otherwise.
+   * @return {@code true} if the host match, {@code false} otherwise.
    */
   private boolean hostMatch(String host1, String host2)
   {
@@ -446,9 +445,9 @@
     {
       throw new IllegalArgumentException("The host2 parameter cannot be null");
     }
+
     String[] h1 = host1.split("\\.");
     String[] h2 = host2.split("\\.");
-
     boolean hostMatch = h1.length == h2.length;
     for (int i=0; i<h1.length && hostMatch; i++)
     {
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/admin/client/cli/SecureConnectionCliArgs.java b/opendj-server-legacy/src/main/java/org/opends/server/admin/client/cli/SecureConnectionCliArgs.java
index 0e1d35a..640f524 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/admin/client/cli/SecureConnectionCliArgs.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/admin/client/cli/SecureConnectionCliArgs.java
@@ -96,18 +96,18 @@
   private boolean configurationInitialized;
 
   /** Defines if the CLI always use the SSL connection type. */
-  private final boolean alwaysSSL;
+  private final boolean alwaysUseSSL;
 
   /**
    * Creates a new instance of secure arguments.
    *
-   * @param alwaysSSL
-   *          If true, always use the SSL connection type. In this case, the
+   * @param alwaysUseSSL
+   *          Whether to always use the SSL connection type. In this case, the
    *          arguments useSSL and startTLS are not present.
    */
-  public SecureConnectionCliArgs(boolean alwaysSSL)
+  public SecureConnectionCliArgs(boolean alwaysUseSSL)
   {
-    this.alwaysSSL = alwaysSSL;
+    this.alwaysUseSSL = alwaysUseSSL;
   }
 
   /**
@@ -139,11 +139,7 @@
    */
   public String getAdministratorUID()
   {
-    if (adminUidArg.isPresent())
-    {
-      return adminUidArg.getValue();
-    }
-    return adminUidArg.getDefaultValue();
+    return getValueOrDefault(adminUidArg);
   }
 
   /**
@@ -154,11 +150,7 @@
    */
   public String getBindDN()
   {
-    if (bindDnArg.isPresent())
-    {
-      return bindDnArg.getValue();
-    }
-    return bindDnArg.getDefaultValue();
+    return getValueOrDefault(bindDnArg);
   }
 
   /**
@@ -174,7 +166,7 @@
     argList = new LinkedHashSet<>();
 
     useSSLArg = useSSLArgument();
-    if (!alwaysSSL)
+    if (!alwaysUseSSL)
     {
       argList.add(useSSLArg);
     }
@@ -185,7 +177,7 @@
     }
 
     useStartTLSArg = startTLSArgument();
-    if (!alwaysSSL)
+    if (!alwaysUseSSL)
     {
       argList.add(useStartTLSArg);
     }
@@ -249,11 +241,7 @@
    */
   public String getHostName()
   {
-    if (hostNameArg.isPresent())
-    {
-      return hostNameArg.getValue();
-    }
-    return hostNameArg.getDefaultValue();
+    return getValueOrDefault(hostNameArg);
   }
 
   /**
@@ -281,11 +269,16 @@
    */
   public String getPort()
   {
-    if (portArg.isPresent())
+    return getValueOrDefault(portArg);
+  }
+
+  private String getValueOrDefault(Argument arg)
+  {
+    if (arg.isPresent())
     {
-      return portArg.getValue();
+      return arg.getValue();
     }
-    return portArg.getDefaultValue();
+    return arg.getDefaultValue();
   }
 
   /**
@@ -337,9 +330,9 @@
    *
    * @return True if SSL mode is always used.
    */
-  public boolean alwaysSSL()
+  public boolean alwaysUseSsl()
   {
-    return alwaysSSL;
+    return alwaysUseSSL;
   }
 
   /**
@@ -430,13 +423,8 @@
     TrustManagerProviderCfg trustManagerCfg = null;
     AdministrationConnectorCfg administrationConnectorCfg = null;
 
-    boolean couldInitializeConfig = configurationInitialized;
     // Initialization for admin framework
-    if (!configurationInitialized)
-    {
-      couldInitializeConfig = initializeConfiguration();
-    }
-    if (couldInitializeConfig)
+    if (configurationInitialized || initializeConfiguration())
     {
       RootCfg root = DirectoryServer.getInstance().getServerContext().getRootConfig();
       administrationConnectorCfg = root.getAdministrationConnector();
@@ -456,6 +444,7 @@
         {
           truststoreFileAbsolute = DirectoryServer.getInstanceRoot() + File.separator + truststoreFile;
         }
+
         File f = new File(truststoreFileAbsolute);
         if (!f.exists() || !f.canRead() || f.isDirectory())
         {
@@ -488,12 +477,7 @@
   public int getAdminPortFromConfig() throws ConfigException
   {
     // Initialization for admin framework
-    boolean couldInitializeConfiguration = configurationInitialized;
-    if (!configurationInitialized)
-    {
-      couldInitializeConfiguration = initializeConfiguration();
-    }
-    if (couldInitializeConfiguration)
+    if (configurationInitialized || initializeConfiguration())
     {
       RootCfg root = DirectoryServer.getInstance().getServerContext().getRootConfig();
       return root.getAdministrationConnector().getListenPort();
@@ -539,25 +523,22 @@
    */
   public int getPortFromConfig()
   {
-    int portNumber;
-    if (alwaysSSL())
+    if (alwaysUseSsl())
     {
-      portNumber = AdministrationConnector.DEFAULT_ADMINISTRATION_CONNECTOR_PORT;
-      // Try to get the port from the config file
       try
       {
-        portNumber = getAdminPortFromConfig();
+        // Try to get the port from the config file
+        return getAdminPortFromConfig();
       }
       catch (ConfigException ex)
       {
-        // Nothing to do
+        return AdministrationConnector.DEFAULT_ADMINISTRATION_CONNECTOR_PORT;
       }
     }
     else
     {
-      portNumber = CliConstants.DEFAULT_SSL_PORT;
+      return CliConstants.DEFAULT_SSL_PORT;
     }
-    return portNumber;
   }
 
   /**
@@ -603,7 +584,7 @@
   private IntegerArgument createPortArgument(final int defaultValue) throws ArgumentException
   {
     return portArgument(
-            defaultValue, alwaysSSL ? INFO_DESCRIPTION_ADMIN_PORT.get() : INFO_DESCRIPTION_PORT.get());
+            defaultValue, alwaysUseSSL ? INFO_DESCRIPTION_ADMIN_PORT.get() : INFO_DESCRIPTION_PORT.get());
   }
 
   /**
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/util/cli/LDAPConnectionConsoleInteraction.java b/opendj-server-legacy/src/main/java/org/opends/server/util/cli/LDAPConnectionConsoleInteraction.java
index c609010..8282adf 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/util/cli/LDAPConnectionConsoleInteraction.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/util/cli/LDAPConnectionConsoleInteraction.java
@@ -161,7 +161,7 @@
 
     private void setSsl(final SecureConnectionCliArgs secureArgs)
     {
-      this.useSSL = secureArgs.alwaysSSL() || secureArgs.getUseSSLArg().isPresent();
+      this.useSSL = secureArgs.alwaysUseSsl() || secureArgs.getUseSSLArg().isPresent();
       this.useStartTLS = secureArgs.getUseStartTLSArg().isPresent();
     }
   }
@@ -312,7 +312,7 @@
     this.commandBuilder = new CommandBuilder();
     this.allowAnonymousIfNonInteractive = allowAnonymousIfNonInteractive;
     state = new State(secureArgs);
-    copySecureArgsList = new SecureConnectionCliArgs(secureArgs.alwaysSSL());
+    copySecureArgsList = new SecureConnectionCliArgs(secureArgs.alwaysUseSsl());
     try
     {
       copySecureArgsList.createGlobalArguments();
@@ -583,7 +583,7 @@
     try
     {
       app.println();
-      final LocalizableMessage askPortNumberMsg = secureArgsList.alwaysSSL() ?
+      final LocalizableMessage askPortNumberMsg = secureArgsList.alwaysUseSsl() ?
           INFO_ADMIN_CONN_PROMPT_PORT_NUMBER.get(portNumber) :
           INFO_LDAP_CONN_PROMPT_PORT_NUMBER.get(portNumber);
       portNumber = app.readValidatedInput(askPortNumberMsg, portValidationCallback(portNumber));

--
Gitblit v1.10.0