From 0f88b78b378ac39f0c9ad5d2926404497d5bf4c9 Mon Sep 17 00:00:00 2001
From: Jean-Noël Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Fri, 05 Aug 2016 19:04:23 +0000
Subject: [PATCH] Partial OPENDJ-2625 Convert all code that uses JNDI to use the SDK instead

---
 opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/ui/LocalOrRemotePanel.java |    2 
 /dev/null                                                                                      |   41 -------------
 opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/browser/NodeRefresher.java |    5 +
 opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/ui/LoginPanel.java         |    9 +-
 opendj-core/src/test/java/org/forgerock/opendj/ldap/DNTestCase.java                            |    1 
 opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/ui/BrowseEntriesPanel.java |   14 ++--
 opendj-server-legacy/src/main/java/org/opends/quicksetup/ui/CertificateDialog.java             |    8 +-
 opendj-server-legacy/src/main/java/org/opends/admin/ads/util/ApplicationTrustManager.java      |    9 +--
 opendj-server-legacy/src/main/java/org/opends/quicksetup/util/Utils.java                       |   72 +++++++-----------------
 opendj-core/src/main/java/org/forgerock/opendj/ldap/DN.java                                    |    2 
 opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/task/DeleteEntryTask.java  |    5 -
 11 files changed, 48 insertions(+), 120 deletions(-)

diff --git a/opendj-core/src/main/java/org/forgerock/opendj/ldap/DN.java b/opendj-core/src/main/java/org/forgerock/opendj/ldap/DN.java
index f1cd46d..bda5e8c 100644
--- a/opendj-core/src/main/java/org/forgerock/opendj/ldap/DN.java
+++ b/opendj-core/src/main/java/org/forgerock/opendj/ldap/DN.java
@@ -808,6 +808,8 @@
 
     /**
      * Returns the RDN of this DN, or {@code null} if this DN is the Root DN.
+     * <p>
+     * This is the equivalent of calling {@code dn.rdn(0)}.
      *
      * @return The RDN of this DN, or {@code null} if this DN is the Root DN.
      */
diff --git a/opendj-core/src/test/java/org/forgerock/opendj/ldap/DNTestCase.java b/opendj-core/src/test/java/org/forgerock/opendj/ldap/DNTestCase.java
index d5a8489..51046cb 100644
--- a/opendj-core/src/test/java/org/forgerock/opendj/ldap/DNTestCase.java
+++ b/opendj-core/src/test/java/org/forgerock/opendj/ldap/DNTestCase.java
@@ -503,6 +503,7 @@
         assertEquals(c.toString(), e.toString());
 
         assertEquals(c.rdn(), RDN.valueOf("dc=foo"));
+        assertEquals(c.rdn(), c.rdn(0));
 
         assertEquals(c.parent(), DN.valueOf("dc=opendj,dc=org"));
         assertEquals(c.parent(), e.parent());
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 cb1950c..67112d5 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
@@ -26,14 +26,13 @@
 import java.util.ArrayList;
 import java.util.List;
 
-import javax.naming.ldap.LdapName;
-import javax.naming.ldap.Rdn;
 import javax.net.ssl.TrustManager;
 import javax.net.ssl.TrustManagerFactory;
 import javax.net.ssl.X509TrustManager;
 
 import org.forgerock.i18n.LocalizableMessage;
 import org.forgerock.i18n.slf4j.LocalizedLogger;
+import org.forgerock.opendj.ldap.DN;
 import org.opends.server.util.Platform;
 
 /**
@@ -364,10 +363,8 @@
       boolean matches = false;
       try
       {
-        LdapName dn =
-          new LdapName(chain[0].getSubjectX500Principal().getName());
-        Rdn rdn = dn.getRdn(dn.getRdns().size() - 1);
-        String value = rdn.getValue().toString();
+        DN dn = DN.valueOf(chain[0].getSubjectX500Principal().getName());
+        String value = dn.rdn(dn.size() - 1).getFirstAVA().getAttributeValue().toString();
         matches = hostMatch(value, host);
         if (!matches)
         {
diff --git a/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/browser/NodeRefresher.java b/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/browser/NodeRefresher.java
index 64b6d40..cd13044 100644
--- a/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/browser/NodeRefresher.java
+++ b/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/browser/NodeRefresher.java
@@ -424,7 +424,7 @@
     }
     if (referral != null)
     {
-      throwAbandonIfNeeded(new ReferralLimitExceededException(
+      throwAbandonIfNeeded(newLdapException(CLIENT_SIDE_REFERRAL_LIMIT_EXCEEDED,
           AdminToolMessages.ERR_REFERRAL_LIMIT_EXCEEDED.get(hopCount)));
     }
   }
@@ -924,7 +924,8 @@
             && hp.equals(controller.getUserDataConnection().getConnectionWrapper().getHostPort());
         if (!checkSucceeded)
         {
-          Exception cause = new ReferralLimitExceededException(ERR_CTRL_PANEL_REFERRAL_LOOP.get(url.getRawBaseDN()));
+          LdapException cause = newLdapException(CLIENT_SIDE_REFERRAL_LIMIT_EXCEEDED,
+              ERR_CTRL_PANEL_REFERRAL_LOOP.get(url.getRawBaseDN()));
           throw new SearchAbandonException(State.FAILED, cause, referral);
         }
       }
diff --git a/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/browser/ReferralLimitExceededException.java b/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/browser/ReferralLimitExceededException.java
deleted file mode 100644
index 0d0abe1..0000000
--- a/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/browser/ReferralLimitExceededException.java
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * The contents of this file are subject to the terms of the Common Development and
- * Distribution License (the License). You may not use this file except in compliance with the
- * License.
- *
- * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
- * specific language governing permission and limitations under the License.
- *
- * When distributing Covered Software, include this CDDL Header Notice in each file and include
- * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
- * Header, with the fields enclosed by brackets [] replaced by your own identifying
- * information: "Portions Copyright [year] [name of copyright owner]".
- *
- * Copyright 2008-2010 Sun Microsystems, Inc.
- * Portions Copyright 2014 ForgeRock AS.
- */
-
-package org.opends.guitools.controlpanel.browser;
-
-import javax.naming.NamingException;
-
-import org.forgerock.i18n.LocalizableMessage;
-
-/**
- * The exception that is launched when we exceed the maximum number of hops
- * following referrals.
- *
- */
-public class ReferralLimitExceededException extends NamingException
-{
-  private static final long serialVersionUID = -5640515839144837865L;
-
-  /**
-   * Constructor of the exception.
-   * @param message the message associated with the exception.
-   */
-  public ReferralLimitExceededException(LocalizableMessage message)
-  {
-    super(message.toString());
-  }
-}
diff --git a/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/task/DeleteEntryTask.java b/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/task/DeleteEntryTask.java
index 56b466c..365e954 100644
--- a/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/task/DeleteEntryTask.java
+++ b/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/task/DeleteEntryTask.java
@@ -30,7 +30,6 @@
 import java.util.SortedSet;
 import java.util.TreeSet;
 
-import javax.naming.NamingException;
 import javax.swing.SwingUtilities;
 import javax.swing.tree.TreePath;
 
@@ -278,7 +277,7 @@
   }
 
   private void deleteSubtreeRecursively(ConnectionWithControls conn, DN dnToRemove, TreePath path,
-      List<BrowserNodeInfo> toNotify) throws NamingException, IOException, DirectoryException
+      List<BrowserNodeInfo> toNotify) throws IOException, DirectoryException
   {
     lastDn = dnToRemove;
 
@@ -371,7 +370,7 @@
   }
 
   private void deleteSubtreeWithControl(ConnectionWithControls conn, DN dn, TreePath path,
-      List<BrowserNodeInfo> toNotify) throws LdapException, NamingException
+      List<BrowserNodeInfo> toNotify) throws LdapException
   {
     lastDn = dn;
     long t = System.currentTimeMillis();
diff --git a/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/ui/BrowseEntriesPanel.java b/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/ui/BrowseEntriesPanel.java
index 12ba993..97db78a 100644
--- a/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/ui/BrowseEntriesPanel.java
+++ b/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/ui/BrowseEntriesPanel.java
@@ -39,10 +39,10 @@
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.KeyEvent;
+import java.io.InterruptedIOException;
 import java.util.ArrayList;
 import java.util.LinkedHashSet;
 
-import javax.naming.InterruptedNamingException;
 import javax.swing.ButtonGroup;
 import javax.swing.JCheckBoxMenuItem;
 import javax.swing.JComponent;
@@ -1463,18 +1463,20 @@
 
   private boolean isInterruptedException(Throwable t)
   {
-    boolean isInterruptedException = false;
-    isInterruptedException = t instanceof java.io.InterruptedIOException ||
-    t instanceof InterruptedNamingException;
+    boolean isInterruptedException = isInterrupted(t);
     while (t != null && !isInterruptedException)
     {
       t = t.getCause();
-      isInterruptedException = t instanceof java.io.InterruptedIOException ||
-      t instanceof InterruptedNamingException;
+      isInterruptedException = isInterrupted(t);
     }
     return isInterruptedException;
   }
 
+  private boolean isInterrupted(Throwable t)
+  {
+    return t instanceof InterruptedIOException || t instanceof InterruptedException;
+  }
+
   private void refreshClicked()
   {
     // Refresh the contents of the selected entry.
diff --git a/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/ui/LocalOrRemotePanel.java b/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/ui/LocalOrRemotePanel.java
index 08a75f7..03a336a 100644
--- a/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/ui/LocalOrRemotePanel.java
+++ b/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/ui/LocalOrRemotePanel.java
@@ -639,7 +639,7 @@
                 Iterator<DN> it = info.getServerDescriptor().getAdministrativeUsers().iterator();
                 while (it.hasNext() && !found)
                 {
-                  found = Utils.areDnsEqual(providedDn, it.next().toString());
+                  found = DN.valueOf(providedDn).equals(it.next());
                 }
                 if (!found)
                 {
diff --git a/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/ui/LoginPanel.java b/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/ui/LoginPanel.java
index a47b467..fc488d2 100644
--- a/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/ui/LoginPanel.java
+++ b/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/ui/LoginPanel.java
@@ -22,7 +22,6 @@
 import java.util.Iterator;
 import java.util.LinkedHashSet;
 
-import javax.naming.NamingException;
 import javax.swing.JLabel;
 import javax.swing.JPasswordField;
 import javax.swing.JTextField;
@@ -31,6 +30,7 @@
 import org.forgerock.i18n.LocalizableMessage;
 import org.forgerock.i18n.slf4j.LocalizedLogger;
 import org.forgerock.opendj.ldap.DN;
+import org.forgerock.opendj.ldap.LdapException;
 import org.opends.admin.ads.util.ApplicationTrustManager;
 import org.opends.admin.ads.util.ConnectionWrapper;
 import org.opends.guitools.controlpanel.datamodel.ConfigReadException;
@@ -265,7 +265,7 @@
                 handleCertificateException = true;
               }
             }
-            else if (throwable instanceof NamingException)
+            else if (throwable instanceof LdapException)
             {
               boolean found = false;
               String providedDn = dn.getText();
@@ -273,7 +273,7 @@
               getAdministrativeUsers().iterator();
               while (it.hasNext() && !found)
               {
-                found = Utils.areDnsEqual(providedDn, it.next().toString());
+                found = DN.valueOf(providedDn).equals(it.next());
               }
               if (!found)
               {
@@ -281,8 +281,7 @@
               }
               else
               {
-                errors.add(Utils.getMessageForException(
-                    (NamingException)throwable));
+                errors.add(Utils.getMessageForException((LdapException) throwable));
               }
 
               setPrimaryInvalid(dnLabel);
diff --git a/opendj-server-legacy/src/main/java/org/opends/quicksetup/ui/CertificateDialog.java b/opendj-server-legacy/src/main/java/org/opends/quicksetup/ui/CertificateDialog.java
index 9704d8c..6396293 100644
--- a/opendj-server-legacy/src/main/java/org/opends/quicksetup/ui/CertificateDialog.java
+++ b/opendj-server-legacy/src/main/java/org/opends/quicksetup/ui/CertificateDialog.java
@@ -36,8 +36,6 @@
 import java.util.HashMap;
 import java.util.Map;
 
-import javax.naming.ldap.LdapName;
-import javax.naming.ldap.Rdn;
 import javax.swing.Box;
 import javax.swing.JButton;
 import javax.swing.JComboBox;
@@ -55,6 +53,7 @@
 import org.forgerock.i18n.LocalizableMessage;
 import org.forgerock.i18n.LocalizableMessageBuilder;
 import org.forgerock.i18n.slf4j.LocalizedLogger;
+import org.forgerock.opendj.ldap.DN;
 import org.opends.quicksetup.UserDataCertificateException;
 import org.opends.quicksetup.event.MinimumSizeComponentListener;
 
@@ -663,9 +662,8 @@
     String name = cert.getSubjectX500Principal().getName();
     try
     {
-      LdapName dn = new LdapName(name);
-      Rdn rdn = dn.getRdn(0);
-      return rdn.getValue().toString();
+      DN dn = DN.valueOf(name);
+      return dn.rdn().getFirstAVA().getAttributeValue().toString();
     }
     catch (Throwable t)
     {
diff --git a/opendj-server-legacy/src/main/java/org/opends/quicksetup/util/Utils.java b/opendj-server-legacy/src/main/java/org/opends/quicksetup/util/Utils.java
index c7d6657..ad34cf4 100644
--- a/opendj-server-legacy/src/main/java/org/opends/quicksetup/util/Utils.java
+++ b/opendj-server-legacy/src/main/java/org/opends/quicksetup/util/Utils.java
@@ -52,20 +52,17 @@
 import java.util.Set;
 import java.util.TimeZone;
 
-import javax.naming.AuthenticationException;
-import javax.naming.CommunicationException;
-import javax.naming.NamingException;
-import javax.naming.NamingSecurityException;
-import javax.naming.NoPermissionException;
 import javax.naming.ldap.LdapName;
 
 import org.forgerock.i18n.LocalizableMessage;
 import org.forgerock.i18n.LocalizableMessageBuilder;
+import org.forgerock.i18n.LocalizedIllegalArgumentException;
 import org.forgerock.i18n.slf4j.LocalizedLogger;
 import org.forgerock.opendj.config.ManagedObjectDefinition;
 import org.forgerock.opendj.ldap.AuthorizationException;
 import org.forgerock.opendj.ldap.ConnectionException;
 import org.forgerock.opendj.ldap.DN;
+import org.forgerock.opendj.ldap.LdapException;
 import org.forgerock.opendj.ldap.requests.SearchRequest;
 import org.forgerock.opendj.ldap.responses.SearchResultEntry;
 import org.forgerock.opendj.server.config.client.BackendCfgClient;
@@ -87,6 +84,7 @@
 import org.opends.quicksetup.installer.SuffixesToReplicateOptions;
 import org.opends.quicksetup.ui.UIFactory;
 import org.opends.server.tools.BackendTypeHelper;
+import org.opends.server.types.HostPort;
 import org.opends.server.util.SetupUtils;
 
 import com.forgerock.opendj.cli.ArgumentConstants;
@@ -370,17 +368,27 @@
   /**
    * Returns whether the provided string is a configuration DN.
    *
-   * @param dn
+   * @param dnStr
    *          the String we are analyzing.
    * @return {@code true} if the provided string is a configuration DN and {@code false} otherwise.
    */
-  public static boolean isConfigurationDn(String dn)
+  public static boolean isConfigurationDn(String dnStr)
   {
+    DN dn;
+    try
+    {
+      dn = DN.valueOf(dnStr);
+    }
+    catch (LocalizedIllegalArgumentException e)
+    {
+      return false;
+    }
+
     boolean isConfigurationDn = false;
-    String[] configDns = { "cn=config", Constants.SCHEMA_DN.toString() };
+    DN[] configDns = { DN.valueOf("cn=config"), Constants.SCHEMA_DN };
     for (int i = 0; i < configDns.length && !isConfigurationDn; i++)
     {
-      isConfigurationDn = areDnsEqual(dn, configDns[i]);
+      isConfigurationDn = dn.equals(configDns[i]);
     }
     return isConfigurationDn;
   }
@@ -553,18 +561,17 @@
   {
     LocalizableMessageBuilder buf = new LocalizableMessageBuilder();
 
-    String ldapUrl = te.getLdapUrl();
-    if (ldapUrl != null)
+    HostPort hp = te.getHostPort();
+    if (hp != null)
     {
-      String hostName = ldapUrl.substring(ldapUrl.indexOf("://") + 3);
-      buf.append(INFO_SERVER_ERROR.get(hostName));
+      buf.append(INFO_SERVER_ERROR.get(hp.getHost()));
       buf.append(" ");
     }
     if (te.getType() == TopologyCacheException.Type.TIMEOUT)
     {
       buf.append(INFO_ERROR_CONNECTING_TIMEOUT.get());
     }
-    else if (te.getCause() instanceof NamingException)
+    else if (te.getCause() instanceof LdapException)
     {
       buf.append(getThrowableMsg(INFO_ERROR_CONNECTING_TO_LOCAL.get(), te.getCause()));
     }
@@ -646,43 +653,6 @@
   }
 
   /**
-   * Returns a message object for the given NamingException. The code assume
-   * that we are trying to connect to the local server.
-   *
-   * @param ne
-   *          the NamingException.
-   * @return a message object for the given NamingException.
-   */
-  public static LocalizableMessage getMessageForException(NamingException ne)
-  {
-    final String detailedException = ne.toString(true);
-    if (isCertificateException(ne))
-    {
-      return INFO_ERROR_READING_CONFIG_LDAP_CERTIFICATE.get(detailedException);
-    }
-    else if (ne instanceof AuthenticationException)
-    {
-      return ERR_CANNOT_CONNECT_TO_LOCAL_AUTHENTICATION.get(detailedException);
-    }
-    else if (ne instanceof NoPermissionException)
-    {
-      return ERR_CANNOT_CONNECT_TO_LOCAL_PERMISSIONS.get(detailedException);
-    }
-    else if (ne instanceof NamingSecurityException)
-    {
-      return ERR_CANNOT_CONNECT_TO_LOCAL_PERMISSIONS.get(detailedException);
-    }
-    else if (ne instanceof CommunicationException)
-    {
-      return ERR_CANNOT_CONNECT_TO_LOCAL_COMMUNICATION.get(detailedException);
-    }
-    else
-    {
-      return ERR_CANNOT_CONNECT_TO_LOCAL_GENERIC.get(detailedException);
-    }
-  }
-
-  /**
    * Returns a message object for the given IOException. The code assume that we are trying to
    * connect to the local server.
    *

--
Gitblit v1.10.0