1 /*
2 * The contents of this file are subject to the terms of the Common Development and
3 * Distribution License (the License). You may not use this file except in compliance with the
4 * License.
5 *
6 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7 * specific language governing permission and limitations under the License.
8 *
9 * When distributing Covered Software, include this CDDL Header Notice in each file and include
10 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
11 * Header, with the fields enclosed by brackets [] replaced by your own identifying
12 * information: "Portions Copyright [year] [name of copyright owner]".
13 *
14 * Copyright 2009-2010 Sun Microsystems, Inc.
15 * Portions Copyright 2011-2015 ForgeRock AS.
16 */
17
18 package org.forgerock.opendj.examples;
19
20 import java.io.IOException;
21
22 import org.forgerock.opendj.ldap.Connection;
23 import org.forgerock.opendj.ldap.LdapException;
24 import org.forgerock.opendj.ldap.LDAPConnectionFactory;
25 import org.forgerock.opendj.ldap.ResultCode;
26 import org.forgerock.opendj.ldap.SearchScope;
27 import org.forgerock.opendj.ldap.controls.GenericControl;
28 import org.forgerock.opendj.ldap.requests.Requests;
29 import org.forgerock.opendj.ldap.requests.SearchRequest;
30 import org.forgerock.opendj.ldap.responses.SearchResultEntry;
31 import org.forgerock.opendj.ldap.responses.SearchResultReference;
32 import org.forgerock.opendj.ldif.ConnectionEntryReader;
33 import org.forgerock.opendj.ldif.LDIFEntryWriter;
34
35 /**
36 * An example client application which searches Microsoft Active Directory
37 * synchronously, using {@link GenericControl} to pass the <a
38 * href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms676877(v=vs.85).aspx"
39 * >Microsoft LDAP Notification control</a>.
40 *
41 * <p>This example is a near copy of {@link Search}, but works only with
42 * directory servers like Active Directory that support the control with OID
43 * 1.2.840.113556.1.4.528.
44 *
45 * <p>This example takes the following command line parameters:
46 *
47 * <pre>
48 * {@code <host> <port> <username> <password> <baseDN>}
49 * </pre>
50 *
51 * <p>The {@code baseDN} must be the root of a naming context in this example.
52 */
53 public final class GetADChangeNotifications {
54 /**
55 * Main method.
56 *
57 * @param args
58 * The command line arguments: host, port, username, password,
59 * base DN, where the base DN is the root of a naming context.
60 */
61 public static void main(final String[] args) {
62 if (args.length < 5) {
63 System.err.println("Usage: host port username password baseDN");
64 System.exit(1);
65 }
66
67 // Parse command line arguments.
68 final String hostName = args[0];
69 final int port = Integer.parseInt(args[1]);
70 final String userName = args[2];
71 final String password = args[3];
72 final String baseDN = args[4];
73
74 // See http://msdn.microsoft.com/en-us/library/windows/desktop/aa772153(v=vs.85).aspx
75 // --- JCite ---
76 final SearchScope scope = SearchScope.WHOLE_SUBTREE;
77 final String filter = "(objectclass=*)";
78 final String[] attributes = {
79 "objectclass", "objectGUID", "isDeleted", "uSNChanged"
80 };
81
82 // Create an LDIF writer which will write the search results to stdout.
83 final LDIFEntryWriter writer = new LDIFEntryWriter(System.out);
84
85 // Connect and bind to the server.
86 final LDAPConnectionFactory factory =
87 new LDAPConnectionFactory(hostName, port);
88 Connection connection = null;
89
90 try {
91 connection = factory.getConnection();
92 connection.bind(userName, password.toCharArray());
93
94 // Read the entries and output them as LDIF.
95 final SearchRequest request =
96 Requests
97 .newSearchRequest(baseDN, scope, filter, attributes)
98 .addControl(
99 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 }