View Javadoc

1   /*
2    * CDDL HEADER START
3    *
4    * The contents of this file are subject to the terms of the
5    * Common Development and Distribution License, Version 1.0 only
6    * (the "License").  You may not use this file except in compliance
7    * with the License.
8    *
9    * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
10   * or http://forgerock.org/license/CDDLv1.0.html.
11   * See the License for the specific language governing permissions
12   * and limitations under the License.
13   *
14   * When distributing Covered Code, include this CDDL HEADER in each
15   * file and include the License file at legal-notices/CDDLv1_0.txt.
16   * If applicable, add the following below this CDDL HEADER, with the
17   * fields enclosed by brackets "[]" replaced with your own identifying
18   * information:
19   *      Portions Copyright [yyyy] [name of copyright owner]
20   *
21   * CDDL HEADER END
22   *
23   *
24   *      Copyright 2009-2010 Sun Microsystems, Inc.
25   *      Portions copyright 2011-2012 ForgeRock AS
26   */
27  
28  package org.forgerock.opendj.examples;
29  
30  import java.io.IOException;
31  import java.util.Arrays;
32  
33  import org.forgerock.opendj.ldap.Connection;
34  import org.forgerock.opendj.ldap.ErrorResultException;
35  import org.forgerock.opendj.ldap.ErrorResultIOException;
36  import org.forgerock.opendj.ldap.LDAPConnectionFactory;
37  import org.forgerock.opendj.ldap.ResultCode;
38  import org.forgerock.opendj.ldap.SearchScope;
39  import org.forgerock.opendj.ldap.responses.SearchResultEntry;
40  import org.forgerock.opendj.ldap.responses.SearchResultReference;
41  import org.forgerock.opendj.ldif.ConnectionEntryReader;
42  import org.forgerock.opendj.ldif.LDIFEntryWriter;
43  
44  /**
45   * An example client application which searches a Directory Server. This example
46   * takes the following command line parameters:
47   *
48   * <pre>
49   *  &lt;host> &lt;port> &lt;username> &lt;password>
50   *      &lt;baseDN> &lt;scope> &lt;filter> [&lt;attibute> &lt;attribute> ...]
51   * </pre>
52   */
53  public final class Search {
54      /**
55       * Main method.
56       *
57       * @param args
58       *            The command line arguments: host, port, username, password,
59       *            base DN, scope, filter, and zero or more attributes to be
60       *            retrieved.
61       */
62      public static void main(final String[] args) {
63          if (args.length < 7) {
64              System.err.println("Usage: host port username password baseDN scope "
65                      + "filter [attribute ...]");
66              System.exit(1);
67          }
68  
69          // Parse command line arguments.
70          final String hostName = args[0];
71          final int port = Integer.parseInt(args[1]);
72          final String userName = args[2];
73          final String password = args[3];
74          final String baseDN = args[4];
75          final String scopeString = args[5];
76          final String filter = args[6];
77          String[] attributes;
78          if (args.length > 7) {
79              attributes = Arrays.copyOfRange(args, 7, args.length);
80          } else {
81              attributes = new String[0];
82          }
83  
84          SearchScope scope;
85          if (scopeString.equalsIgnoreCase("base")) {
86              scope = SearchScope.BASE_OBJECT;
87          } else if (scopeString.equalsIgnoreCase("one")) {
88              scope = SearchScope.SINGLE_LEVEL;
89          } else if (scopeString.equalsIgnoreCase("sub")) {
90              scope = SearchScope.WHOLE_SUBTREE;
91          } else if (scopeString.equalsIgnoreCase("subordinates")) {
92              scope = SearchScope.SUBORDINATES;
93          } else {
94              System.err.println("Unknown scope: " + scopeString);
95              System.exit(ResultCode.CLIENT_SIDE_PARAM_ERROR.intValue());
96              return;
97          }
98  
99          // Create an LDIF writer which will write the search results to stdout.
100         final LDIFEntryWriter writer = new LDIFEntryWriter(System.out);
101 
102         // Connect and bind to the server.
103         final LDAPConnectionFactory factory = new LDAPConnectionFactory(hostName, port);
104         Connection connection = null;
105 
106         try {
107             connection = factory.getConnection();
108             connection.bind(userName, password.toCharArray());
109 
110             // Read the entries and output them as LDIF.
111             final ConnectionEntryReader reader =
112                     connection.search(baseDN, scope, filter, attributes);
113             while (reader.hasNext()) {
114                 if (!reader.isReference()) {
115                     final SearchResultEntry entry = reader.readEntry();
116                     writer.writeComment("Search result entry: " + entry.getName().toString());
117                     writer.writeEntry(entry);
118                 } else {
119                     final SearchResultReference ref = reader.readReference();
120 
121                     // Got a continuation reference.
122                     writer.writeComment("Search result reference: " + ref.getURIs().toString());
123                 }
124             }
125             writer.flush();
126         } catch (final ErrorResultException e) {
127             System.err.println(e.getMessage());
128             System.exit(e.getResult().getResultCode().intValue());
129             return;
130         } catch (final ErrorResultIOException e) {
131             System.err.println(e.getMessage());
132             System.exit(e.getCause().getResult().getResultCode().intValue());
133             return;
134         } catch (final IOException e) {
135             System.err.println(e.getMessage());
136             System.exit(ResultCode.CLIENT_SIDE_LOCAL_ERROR.intValue());
137             return;
138         } finally {
139             if (connection != null) {
140                 connection.close();
141             }
142         }
143     }
144 
145     private Search() {
146         // Not used.
147     }
148 }