From c3da163b8b5cccbb1f80fe1a880c742945ad04fb Mon Sep 17 00:00:00 2001
From: Mark Craig <mark.craig@forgerock.com>
Date: Tue, 25 Oct 2016 10:10:21 +0000
Subject: [PATCH] OPENDJ-3405 Add LDAP SDK example for affinity control

---
 opendj-ldap-sdk-examples/src/main/java/org/forgerock/opendj/examples/Controls.java |   48 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 47 insertions(+), 1 deletions(-)

diff --git a/opendj-ldap-sdk-examples/src/main/java/org/forgerock/opendj/examples/Controls.java b/opendj-ldap-sdk-examples/src/main/java/org/forgerock/opendj/examples/Controls.java
index a209b22..7f59a1d 100644
--- a/opendj-ldap-sdk-examples/src/main/java/org/forgerock/opendj/examples/Controls.java
+++ b/opendj-ldap-sdk-examples/src/main/java/org/forgerock/opendj/examples/Controls.java
@@ -11,7 +11,7 @@
  * Header, with the fields enclosed by brackets [] replaced by your own identifying
  * information: "Portions Copyright [year] [name of copyright owner]".
  *
- * Copyright 2012-2015 ForgeRock AS.
+ * Copyright 2012-2016 ForgeRock AS.
  */
 
 package org.forgerock.opendj.examples;
@@ -19,6 +19,7 @@
 import java.io.IOException;
 import java.util.Collection;
 
+import com.forgerock.opendj.ldap.controls.AffinityControl;
 import org.forgerock.opendj.ldap.ByteString;
 import org.forgerock.opendj.ldap.Connection;
 import org.forgerock.opendj.ldap.DecodeException;
@@ -111,6 +112,7 @@
 
             // Uncomment a method to run one of the examples.
 
+            //useAffinityControl(connection);
             //useADNotificationRequestControl(connection);
             //useAssertionControl(connection);
             useAuthorizationIdentityRequestControl(connection);
@@ -141,6 +143,50 @@
     }
 
     /**
+     * Use the OpenDJ affinity control to bypass load balancing.
+     * <br>
+     * In other words, each request with a control having the same value
+     * is sent to the same LDAP server.
+     *
+     * @param connection Active connection to the directory server.
+     * @throws LdapException Operation failed.
+     */
+    static void useAffinityControl(Connection connection) throws LdapException {
+        // --- JCite affinity ---
+        if (isSupported(AffinityControl.OID)) {
+            final String dn = "uid=bjensen,ou=People,dc=example,dc=com";
+
+            // Get an affinity control with a random value.
+            final AffinityControl control = AffinityControl.newControl(true);
+
+            final ModifyRequest modification =
+                    Requests.newModifyRequest(dn)
+                            .addControl(control)
+                            .addModification(ModificationType.ADD, "description",
+                                    "Added with an Affinity control");
+            connection.modify(modification);
+
+            final SearchRequest read =
+                    Requests.newSearchRequest(dn,
+                            SearchScope.BASE_OBJECT, "(&)", "description")
+                            .addControl(control);
+            final ConnectionEntryReader reader = connection.search(read);
+
+            try (final LDIFEntryWriter writer = new LDIFEntryWriter(System.out)) {
+                while (reader.hasNext()) {
+                    writer.writeEntry(reader.readEntry());
+                }
+            } catch (final IOException e) {
+                System.err.println(e.getMessage());
+                System.exit(ResultCode.CLIENT_SIDE_LOCAL_ERROR.intValue());
+            }
+        } else {
+            System.err.println("AffinityControl not supported.");
+        }
+        // --- JCite affinity ---
+    }
+
+    /**
      * Use the <a
      * href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms676877(v=vs.85).aspx"
      * >Microsoft LDAP Notification control</a>

--
Gitblit v1.10.0