View Javadoc
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  import java.util.Arrays;
22  
23  import org.forgerock.opendj.ldap.Connection;
24  import org.forgerock.opendj.ldap.LdapException;
25  import org.forgerock.opendj.ldap.LDAPConnectionFactory;
26  import org.forgerock.opendj.ldap.ResultCode;
27  import org.forgerock.opendj.ldap.SearchScope;
28  import org.forgerock.opendj.ldap.responses.SearchResultEntry;
29  import org.forgerock.opendj.ldap.responses.SearchResultReference;
30  import org.forgerock.opendj.ldif.ConnectionEntryReader;
31  import org.forgerock.opendj.ldif.LDIFEntryWriter;
32  
33  /**
34   * An example client application which searches a Directory Server. This example
35   * takes the following command line parameters:
36   *
37   * <pre>
38   *  {@code <host> <port> <username> <password>
39   *      <baseDN> <scope> <filter> [<attribute> <attribute> ...]}
40   * </pre>
41   */
42  public final class Search {
43      /**
44       * Main method.
45       *
46       * @param args
47       *            The command line arguments: host, port, username, password,
48       *            base DN, scope, filter, and zero or more attributes to be
49       *            retrieved.
50       */
51      public static void main(final String[] args) {
52          if (args.length < 7) {
53              System.err.println("Usage: host port username password baseDN scope "
54                      + "filter [attribute ...]");
55              System.exit(1);
56          }
57  
58          // Parse command line arguments.
59          final String hostName = args[0];
60          final int port = Integer.parseInt(args[1]);
61          final String userName = args[2];
62          final String password = args[3];
63          final String baseDN = args[4];
64          final String scopeString = args[5];
65          final String filter = args[6];
66          String[] attributes;
67          if (args.length > 7) {
68              attributes = Arrays.copyOfRange(args, 7, args.length);
69          } else {
70              attributes = new String[0];
71          }
72  
73          final SearchScope scope = SearchScope.valueOf(scopeString);
74          if (scope == null) {
75              System.err.println("Unknown scope: " + scopeString);
76              System.exit(ResultCode.CLIENT_SIDE_PARAM_ERROR.intValue());
77              return;
78          }
79  
80          // --- JCite ---
81          // Create an LDIF writer which will write the search results to stdout.
82          final LDIFEntryWriter writer = new LDIFEntryWriter(System.out);
83  
84          // Connect and bind to the server.
85          final LDAPConnectionFactory factory = new LDAPConnectionFactory(hostName, port);
86          Connection connection = null;
87  
88          try {
89              connection = factory.getConnection();
90              connection.bind(userName, password.toCharArray());
91  
92              // Read the entries and output them as LDIF.
93              final ConnectionEntryReader reader =
94                      connection.search(baseDN, scope, filter, attributes);
95              while (reader.hasNext()) {
96                  if (!reader.isReference()) {
97                      final SearchResultEntry entry = reader.readEntry();
98                      writer.writeComment("Search result entry: " + entry.getName());
99                      writer.writeEntry(entry);
100                 } else {
101                     final SearchResultReference ref = reader.readReference();
102 
103                     // Got a continuation reference.
104                     writer.writeComment("Search result reference: " + ref.getURIs());
105                 }
106             }
107             writer.flush();
108         } catch (final LdapException e) {
109             System.err.println(e.getMessage());
110             System.exit(e.getResult().getResultCode().intValue());
111         } catch (final IOException e) {
112             System.err.println(e.getMessage());
113             System.exit(ResultCode.CLIENT_SIDE_LOCAL_ERROR.intValue());
114         } finally {
115             if (connection != null) {
116                 connection.close();
117             }
118         }
119         // --- JCite ---
120     }
121 
122     private Search() {
123         // Not used.
124     }
125 }