1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
46
47
48
49
50
51
52
53 public final class Search {
54
55
56
57
58
59
60
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
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
100 final LDIFEntryWriter writer = new LDIFEntryWriter(System.out);
101
102
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
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
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
147 }
148 }