From 97aafc7c83ab91e2d3647699b15db7f3b6a29bef Mon Sep 17 00:00:00 2001
From: Matthew Swift <matthew.swift@forgerock.com>
Date: Fri, 22 Feb 2013 11:27:11 +0000
Subject: [PATCH] Fix OPENDJ-757: Add Rest2LDAP gateway Servlet

---
 opendj3/opendj-rest2ldap/src/main/java/org/forgerock/opendj/rest2ldap/Rest2LDAP.java |   46 ++++++++++++++++++++++++++++++----------------
 1 files changed, 30 insertions(+), 16 deletions(-)

diff --git a/opendj3/opendj-rest2ldap/src/main/java/org/forgerock/opendj/rest2ldap/Rest2LDAP.java b/opendj3/opendj-rest2ldap/src/main/java/org/forgerock/opendj/rest2ldap/Rest2LDAP.java
index 6ce5469..97826f5 100644
--- a/opendj3/opendj-rest2ldap/src/main/java/org/forgerock/opendj/rest2ldap/Rest2LDAP.java
+++ b/opendj3/opendj-rest2ldap/src/main/java/org/forgerock/opendj/rest2ldap/Rest2LDAP.java
@@ -47,6 +47,8 @@
 import org.forgerock.opendj.ldap.RDN;
 import org.forgerock.opendj.ldap.RoundRobinLoadBalancingAlgorithm;
 import org.forgerock.opendj.ldap.SearchScope;
+import org.forgerock.opendj.ldap.requests.BindRequest;
+import org.forgerock.opendj.ldap.requests.Requests;
 import org.forgerock.opendj.ldap.requests.SearchRequest;
 import org.forgerock.opendj.ldap.schema.AttributeType;
 import org.forgerock.opendj.ldap.schema.Schema;
@@ -117,8 +119,7 @@
          * @throws IllegalArgumentException
          *             If the configuration is invalid.
          */
-        public Builder configureConnectionFactory(final JsonValue configuration)
-                throws IllegalArgumentException {
+        public Builder configureConnectionFactory(final JsonValue configuration) {
             connectionFactory(Rest2LDAP.configureConnectionFactory(configuration));
             return this;
         }
@@ -192,8 +193,7 @@
          * @throws IllegalArgumentException
          *             If the configuration is invalid.
          */
-        public Builder configureMapping(final JsonValue configuration)
-                throws IllegalArgumentException {
+        public Builder configureMapping(final JsonValue configuration) {
             baseDN(configuration.get("baseDN").required().asString());
 
             final JsonValue readOnUpdatePolicy = configuration.get("readOnUpdatePolicy");
@@ -545,9 +545,10 @@
      *         ...
      *     },
      *
-     *     // Authentication configuration (mandatory and TBD).
+     *     // Authentication configuration (optional and TBD).
      *     "authentication" : {
-     *         ...
+     *         "bindDN"   : "cn=directory manager",
+     *         "password" : "password"
      *     },
      * }
      * </pre>
@@ -558,32 +559,41 @@
      * @throws IllegalArgumentException
      *             If the configuration is invalid.
      */
-    public static ConnectionFactory configureConnectionFactory(final JsonValue configuration)
-            throws IllegalArgumentException {
+    public static ConnectionFactory configureConnectionFactory(final JsonValue configuration) {
         // Parse pool parameters,
         final int connectionPoolSize =
                 Math.max(configuration.get("connectionPoolSize").defaultTo(10).asInteger(), 1);
         final int heartBeatIntervalSeconds =
                 Math.max(configuration.get("heartBeatIntervalSeconds").defaultTo(30).asInteger(), 1);
 
+        // Parse authentication parameters.
+        final BindRequest bindRequest;
+        if (configuration.isDefined("authentication")) {
+            final JsonValue authn = configuration.get("authentication");
+            bindRequest =
+                    Requests.newSimpleBindRequest(authn.get("bindDN").required().asString(), authn
+                            .get("password").required().asString().toCharArray());
+        } else {
+            bindRequest = null;
+        }
+
         // Parse primary data center.
         final JsonValue primaryLDAPServers = configuration.get("primaryLDAPServers");
-        if (primaryLDAPServers == null || !primaryLDAPServers.isList()
-                || primaryLDAPServers.size() == 0) {
+        if (!primaryLDAPServers.isList() || primaryLDAPServers.size() == 0) {
             throw new IllegalArgumentException("No primaryLDAPServers");
         }
         final ConnectionFactory primary =
-                parseLDAPServers(primaryLDAPServers, connectionPoolSize, heartBeatIntervalSeconds);
+                parseLDAPServers(primaryLDAPServers, bindRequest, connectionPoolSize,
+                        heartBeatIntervalSeconds);
 
         // Parse secondary data center(s).
         final JsonValue secondaryLDAPServers = configuration.get("secondaryLDAPServers");
         final ConnectionFactory secondary;
-        if (secondaryLDAPServers != null && secondaryLDAPServers.isList()
-                && secondaryLDAPServers.size() != 0) {
+        if (secondaryLDAPServers.isList() && secondaryLDAPServers.size() != 0) {
             secondary =
-                    parseLDAPServers(secondaryLDAPServers, connectionPoolSize,
+                    parseLDAPServers(secondaryLDAPServers, bindRequest, connectionPoolSize,
                             heartBeatIntervalSeconds);
-        } else if (secondaryLDAPServers != null && !secondaryLDAPServers.isList()) {
+        } else if (!secondaryLDAPServers.isNull()) {
             throw new IllegalArgumentException("Invalid secondaryLDAPServers configuration");
         } else {
             secondary = null;
@@ -615,12 +625,16 @@
     }
 
     private static ConnectionFactory parseLDAPServers(final JsonValue config,
-            final int connectionPoolSize, final int heartBeatIntervalSeconds) {
+            final BindRequest bindRequest, final int connectionPoolSize,
+            final int heartBeatIntervalSeconds) {
         final List<ConnectionFactory> servers = new ArrayList<ConnectionFactory>(config.size());
         for (final JsonValue server : config) {
             final String host = server.get("hostname").required().asString();
             final int port = server.get("port").required().asInteger();
             ConnectionFactory factory = new LDAPConnectionFactory(host, port);
+            if (bindRequest != null) {
+                factory = Connections.newAuthenticatedConnectionFactory(factory, bindRequest);
+            }
             if (connectionPoolSize > 1) {
                 factory =
                         Connections.newHeartBeatConnectionFactory(factory,

--
Gitblit v1.10.0