001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2009-2010 Sun Microsystems, Inc.
015 * Portions Copyright 2011-2015 ForgeRock AS.
016 */
017
018package org.forgerock.opendj.examples;
019
020import java.io.IOException;
021
022import org.forgerock.opendj.ldap.Connection;
023import org.forgerock.opendj.ldap.LdapException;
024import org.forgerock.opendj.ldap.LDAPConnectionFactory;
025import org.forgerock.opendj.ldap.ResultCode;
026import org.forgerock.opendj.ldap.SearchScope;
027import org.forgerock.opendj.ldap.controls.GenericControl;
028import org.forgerock.opendj.ldap.requests.Requests;
029import org.forgerock.opendj.ldap.requests.SearchRequest;
030import org.forgerock.opendj.ldap.responses.SearchResultEntry;
031import org.forgerock.opendj.ldap.responses.SearchResultReference;
032import org.forgerock.opendj.ldif.ConnectionEntryReader;
033import org.forgerock.opendj.ldif.LDIFEntryWriter;
034
035/**
036 * An example client application which searches Microsoft Active Directory
037 * synchronously, using {@link GenericControl} to pass the <a
038 * href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms676877(v=vs.85).aspx"
039 * >Microsoft LDAP Notification control</a>.
040 *
041 * <p>This example is a near copy of {@link Search}, but works only with
042 * directory servers like Active Directory that support the control with OID
043 * 1.2.840.113556.1.4.528.
044 *
045 * <p>This example takes the following command line parameters:
046 *
047 * <pre>
048 *  {@code <host> <port> <username> <password> <baseDN>}
049 * </pre>
050 *
051 * <p>The {@code baseDN} must be the root of a naming context in this example.
052 */
053public final class GetADChangeNotifications {
054    /**
055     * Main method.
056     *
057     * @param args
058     *            The command line arguments: host, port, username, password,
059     *            base DN, where the base DN is the root of a naming context.
060     */
061    public static void main(final String[] args) {
062        if (args.length < 5) {
063            System.err.println("Usage: host port username password baseDN");
064            System.exit(1);
065        }
066
067        // Parse command line arguments.
068        final String hostName = args[0];
069        final int port = Integer.parseInt(args[1]);
070        final String userName = args[2];
071        final String password = args[3];
072        final String baseDN = args[4];
073
074        // See http://msdn.microsoft.com/en-us/library/windows/desktop/aa772153(v=vs.85).aspx
075        // --- JCite ---
076        final SearchScope scope = SearchScope.WHOLE_SUBTREE;
077        final String filter = "(objectclass=*)";
078        final String[] attributes = {
079            "objectclass", "objectGUID", "isDeleted", "uSNChanged"
080        };
081
082        // Create an LDIF writer which will write the search results to stdout.
083        final LDIFEntryWriter writer = new LDIFEntryWriter(System.out);
084
085        // Connect and bind to the server.
086        final LDAPConnectionFactory factory =
087                new LDAPConnectionFactory(hostName, port);
088        Connection connection = null;
089
090        try {
091            connection = factory.getConnection();
092            connection.bind(userName, password.toCharArray());
093
094            // Read the entries and output them as LDIF.
095            final SearchRequest request =
096                    Requests
097                            .newSearchRequest(baseDN, scope, filter, attributes)
098                            .addControl(
099                                    GenericControl
100                                            .newControl(
101                                                    "1.2.840.113556.1.4.528",
102                                                    true));
103            final ConnectionEntryReader reader = connection.search(request);
104            while (reader.hasNext()) {
105                if (!reader.isReference()) {
106                    final SearchResultEntry entry = reader.readEntry();
107                    writer.writeComment("Search result entry: " + entry.getName());
108                    writer.writeEntry(entry);
109                    writer.flush();
110                } else {
111                    final SearchResultReference ref = reader.readReference();
112
113                    // Got a continuation reference.
114                    writer.writeComment("Search result reference: " + ref.getURIs());
115                }
116            }
117        } catch (final LdapException e) {
118            System.err.println(e.getMessage());
119            System.exit(e.getResult().getResultCode().intValue());
120        } catch (final IOException e) {
121            System.err.println(e.getMessage());
122            System.exit(ResultCode.CLIENT_SIDE_LOCAL_ERROR.intValue());
123        } finally {
124            if (connection != null) {
125                connection.close();
126            }
127        }
128        // --- JCite ---
129    }
130
131    private GetADChangeNotifications() {
132        // Not used.
133    }
134}