/*
* 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 2016 ForgeRock AS.
*/
package org.forgerock.opendj.examples;
import static org.forgerock.util.Utils.closeSilently;
import org.forgerock.opendj.ldap.Connection;
import org.forgerock.opendj.ldap.LDAPConnectionFactory;
import org.forgerock.opendj.ldap.LdapException;
import org.forgerock.opendj.ldap.ModificationType;
import org.forgerock.opendj.ldap.ResultCode;
import org.forgerock.opendj.ldap.RootDSE;
import org.forgerock.opendj.ldap.controls.PermissiveModifyRequestControl;
import org.forgerock.opendj.ldap.requests.Requests;
import org.forgerock.opendj.ldap.responses.BindResult;
import org.forgerock.opendj.ldap.responses.CompareResult;
import org.forgerock.opendj.ldap.responses.Responses;
import org.forgerock.opendj.ldap.responses.Result;
import org.forgerock.util.AsyncFunction;
import org.forgerock.util.promise.ExceptionHandler;
import org.forgerock.util.promise.Promise;
import org.forgerock.util.promise.Promises;
import org.forgerock.util.promise.ResultHandler;
import java.util.concurrent.CountDownLatch;
/**
* This command-line client demonstrates adding and removing a member from a
* (potentially large) static group using the asynchronous APIs.
*
* The client takes as arguments the host and port of the directory server, the
* group DN, the member DN, and whether to "add" or "del" the specified member
* from the group. The client uses the Permissive Modify control if it is
* available to avoid having to check whether the member belongs to the group or
* not.
*
* This client expects a group that is a groupOfNames such as:
*
*
* dn: cn=My Static Group,ou=Groups,dc=example,dc=com * cn: My Static Group * objectClass: groupOfNames * objectClass: top * ou: Groups * member: uid=ahunter,ou=People,dc=example,dc=com * member: uid=bjensen,ou=People,dc=example,dc=com * member: uid=tmorris,ou=People,dc=example,dc=com ** * This client connects as
cn=Directory Manager with password
* password. Not a best practice; in real code use application
* specific credentials to connect, and ensure that your application has access
* to use the Permissive Modify control if your directory server supports it.
*/
public final class UpdateGroupAsync {
/** Connection to the LDAP server. */
private static Connection connection;
/** Result for the operation. */
private static int resultCode;
/** Count down latch to wait for modify operation to complete. */
private static final CountDownLatch COMPLETION_LATCH = new CountDownLatch(1);
/**
* Updates the group as necessary.
*
* @param args
* The command line arguments: host, port, group-dn, member-dn, (add|del)
*/
public static void main(String[] args) {
if (args.length != 5) {
printUsage();
}
final String host = args[0];
final int port = Integer.parseInt(args[1]);
final String groupDn = args[2];
final String memberDn = args[3];
final ModificationType modType = getModificationType(args[4]);
// Connect, bind, update group.
new LDAPConnectionFactory(host, port)
.getConnectionAsync()
.thenAsync(new AsyncFunction