001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2015 ForgeRock AS.
025 */
026
027package org.forgerock.opendj.examples;
028
029import static org.forgerock.util.Utils.closeSilently;
030import org.forgerock.i18n.LocalizableMessage;
031import org.forgerock.opendj.ldap.Connection;
032import org.forgerock.opendj.ldap.DN;
033import org.forgerock.opendj.ldap.Entry;
034import org.forgerock.opendj.ldap.LDAPConnectionFactory;
035import org.forgerock.opendj.ldap.LdapException;
036import org.forgerock.opendj.ldap.ResultCode;
037import org.forgerock.opendj.ldap.requests.Requests;
038import org.forgerock.opendj.ldap.responses.BindResult;
039import org.forgerock.opendj.ldap.responses.Result;
040import org.forgerock.opendj.ldap.schema.Schema;
041import org.forgerock.opendj.ldap.schema.SchemaValidationPolicy;
042import org.forgerock.opendj.ldif.LDIFEntryReader;
043import org.forgerock.util.AsyncFunction;
044import org.forgerock.util.promise.ExceptionHandler;
045import org.forgerock.util.promise.Promise;
046import org.forgerock.util.promise.Promises;
047import org.forgerock.util.promise.ResultHandler;
048
049import java.io.IOException;
050import java.util.LinkedList;
051import java.util.List;
052import java.util.concurrent.CountDownLatch;
053
054/**
055 * This example command-line client application validates an entry
056 * against the directory server schema before adding it
057 * using the asynchronous APIs.
058 *
059 * <br>
060 *
061 * This example takes the following command line parameters:
062 *
063 * <pre>
064 *  {@code <host> <port> <bindDN> <bindPassword>}
065 * </pre>
066 *
067 * Then it reads an entry to add from System.in.
068 * If the entry is valid according to the directory schema,
069 * it tries to add the entry to the directory.
070 */
071public final class UseSchemaAsync {
072    /** Connection to the LDAP server. */
073    private static Connection connection;
074    /** Result for the operation. */
075    private static int resultCode;
076    /** Count down latch to wait for modify operation to complete. */
077    private static final CountDownLatch COMPLETION_LATCH = new CountDownLatch(1);
078
079    /**
080     * Main method.
081     *
082     * @param args
083     *            The command line arguments: host, port, bindDN, bindPassword.
084     */
085    public static void main(final String[] args) {
086        if (args.length != 4) {
087            System.err.println("Usage: host port bindDN bindPassword");
088            System.exit(1);
089        }
090
091        // Parse command line arguments.
092        final String host         = args[0];
093        final int    port         = Integer.parseInt(args[1]);
094        final String bindDn       = args[2];
095        final char[] bindPassword = args[3].toCharArray();
096
097        // Read an entry from System.in.
098        final Entry entry;
099        try {
100            System.out.println("Enter entry to add in LDIF format:");
101            entry = new LDIFEntryReader(System.in).readEntry();
102        } catch (IOException e) {
103            System.err.println(e.getMessage());
104            System.exit(ResultCode.CLIENT_SIDE_LOCAL_ERROR.intValue());
105            return;
106        }
107        final String entryDn = entry.getName().toString();
108
109        // Connect, bind, read schema, and add entry if valid according to schema.
110        new LDAPConnectionFactory(host, port)
111                .getConnectionAsync()
112                .thenAsync(new AsyncFunction<Connection, BindResult, LdapException>() {
113                    @Override
114                    public Promise<BindResult, LdapException> apply(Connection connection)
115                            throws LdapException {
116                        UseSchemaAsync.connection = connection;
117                        return connection.bindAsync(
118                                Requests.newSimpleBindRequest(bindDn, bindPassword));
119                    }
120                })
121                .thenAsync(new AsyncFunction<BindResult, Schema, LdapException>() {
122                    @Override
123                    public Promise<Schema, LdapException> apply(BindResult bindResult)
124                            throws LdapException {
125                        return Schema.readSchemaForEntryAsync(connection, DN.rootDN());
126                    }
127                })
128                .thenAsync(new AsyncFunction<Schema, Result, LdapException>() {
129                    @Override
130                    public Promise<Result, LdapException> apply(Schema schema)
131                            throws LdapException {
132                        final List<LocalizableMessage> schemaErrors = new LinkedList<>();
133                        boolean isValid = schema.validateEntry(
134                                entry,
135                                SchemaValidationPolicy.defaultPolicy(),
136                                schemaErrors);
137                        if (isValid) {
138                            System.out.println("Processing ADD request for " + entryDn);
139                            return connection.addAsync(Requests.newAddRequest(entry));
140                        } else {
141                            for (LocalizableMessage error : schemaErrors) {
142                                System.err.println(error);
143                            }
144                            return Promises.newExceptionPromise(
145                                    LdapException.newLdapException(
146                                            ResultCode.CLIENT_SIDE_PARAM_ERROR,
147                                            "Entry does not conform to schema."));
148                        }
149                    }
150                })
151                .thenOnResult(new ResultHandler<Result>() {
152                    @Override
153                    public void handleResult(Result result) {
154                        System.out.println("ADD operation successful for DN " + entryDn);
155                        resultCode = result.getResultCode().intValue();
156                        COMPLETION_LATCH.countDown();
157                    }
158                })
159                .thenOnException(new ExceptionHandler<LdapException>() {
160                    @Override
161                    public void handleException(LdapException e) {
162                        System.err.println(e.getMessage());
163                        resultCode = e.getResult().getResultCode().intValue();
164                        COMPLETION_LATCH.countDown();
165                    }
166                });
167
168        try {
169            COMPLETION_LATCH.await();
170        }  catch (InterruptedException e) {
171            System.err.println(e.getMessage());
172            System.exit(ResultCode.CLIENT_SIDE_USER_CANCELLED.intValue());
173            return;
174        }
175
176        closeSilently(connection);
177        System.exit(resultCode);
178    }
179
180    private UseSchemaAsync() {
181        // Not used.
182    }
183}