From 79456ab528b185983884c14443e0fe64425874da Mon Sep 17 00:00:00 2001
From: Jean-Noël Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Thu, 07 Jul 2016 13:03:32 +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/admin/ads/util/ConnectionUtils.java |   81 ++++++++++++++++++++++++++++++++++++++++
 1 files changed, 81 insertions(+), 0 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/admin/ads/util/ConnectionUtils.java b/opendj-server-legacy/src/main/java/org/opends/admin/ads/util/ConnectionUtils.java
index 95cc437..49c23f3 100644
--- a/opendj-server-legacy/src/main/java/org/opends/admin/ads/util/ConnectionUtils.java
+++ b/opendj-server-legacy/src/main/java/org/opends/admin/ads/util/ConnectionUtils.java
@@ -39,6 +39,8 @@
 
 import org.forgerock.i18n.LocalizableMessage;
 import org.forgerock.i18n.slf4j.LocalizedLogger;
+import org.forgerock.opendj.ldap.DN;
+import org.forgerock.opendj.ldap.responses.SearchResultEntry;
 import org.opends.server.replication.plugin.EntryHistorical;
 import org.opends.server.schema.SchemaConstants;
 import org.opends.server.types.HostPort;
@@ -604,4 +606,83 @@
     }
     return values;
   }
+
+  /**
+   * Returns the first attribute value in this attribute decoded as a UTF-8 string.
+   *
+   * @param sr
+   *          the search result entry
+   * @param attrDesc
+   *          the attribute description
+   * @return The first attribute value in this attribute decoded as a UTF-8 string.
+   */
+  public static String firstValueAsString(SearchResultEntry sr, String attrDesc)
+  {
+    org.forgerock.opendj.ldap.Attribute attr = sr.getAttribute(attrDesc);
+    return (attr != null && !attr.isEmpty()) ? attr.firstValueAsString() : null;
+  }
+
+  /**
+   * Returns the first value decoded as an Integer, or {@code null} if the attribute does not
+   * contain any values.
+   *
+   * @param sr
+   *          the search result entry
+   * @param attrDesc
+   *          the attribute description
+   * @return The first value decoded as an Integer.
+   */
+  public static Integer asInteger(SearchResultEntry sr, String attrDesc)
+  {
+    org.forgerock.opendj.ldap.Attribute attr = sr.getAttribute(attrDesc);
+    return attr != null ? attr.parse().asInteger() : null;
+  }
+
+  /**
+   * Returns the first value decoded as a Boolean, or {@code null} if the attribute does not contain
+   * any values.
+   *
+   * @param sr
+   *          the search result entry
+   * @param attrDesc
+   *          the attribute description
+   * @return The first value decoded as an Boolean.
+   */
+  public static Boolean asBoolean(SearchResultEntry sr, String attrDesc)
+  {
+    org.forgerock.opendj.ldap.Attribute attr = sr.getAttribute(attrDesc);
+    return attr != null ? attr.parse().asBoolean() : null;
+  }
+
+  /**
+   * Returns the values decoded as a set of Strings.
+   *
+   * @param sr
+   *          the search result entry
+   * @param attrDesc
+   *          the attribute description
+   * @return The values decoded as a set of Strings. Never {@code null} and never contains
+   *         {@code null} values.
+   */
+  public static Set<String> asSetOfString(SearchResultEntry sr, String attrDesc)
+  {
+    org.forgerock.opendj.ldap.Attribute attr = sr.getAttribute(attrDesc);
+    return attr != null ? attr.parse().asSetOfString() : null;
+  }
+
+  /**
+   * Returns the values decoded as a set of DNs.
+   *
+   * @param sr
+   *          the search result entry
+   * @param attrDesc
+   *          the attribute description
+   * @return The values decoded as a set of DNs. Never {@code null} and never contains {@code null}
+   *         values.
+   */
+  public static Set<DN> asSetOfDN(SearchResultEntry sr, String attrDesc)
+  {
+    org.forgerock.opendj.ldap.Attribute attr = sr.getAttribute(attrDesc);
+    return attr != null ? attr.parse().asSetOfDN() : null;
+  }
 }

--
Gitblit v1.10.0