1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.forgerock.opendj.examples;
18
19 import static org.forgerock.util.Utils.closeSilently;
20 import org.forgerock.opendj.ldap.Connection;
21 import org.forgerock.opendj.ldap.Entries;
22 import org.forgerock.opendj.ldap.Entry;
23 import org.forgerock.opendj.ldap.LDAPConnectionFactory;
24 import org.forgerock.opendj.ldap.LdapException;
25 import org.forgerock.opendj.ldap.LinkedHashMapEntry;
26 import org.forgerock.opendj.ldap.ResultCode;
27 import org.forgerock.opendj.ldap.TreeMapEntry;
28 import org.forgerock.opendj.ldap.requests.ModifyRequest;
29 import org.forgerock.opendj.ldap.requests.Requests;
30 import org.forgerock.opendj.ldap.responses.BindResult;
31 import org.forgerock.opendj.ldap.responses.Result;
32 import org.forgerock.opendj.ldif.LDIFEntryWriter;
33 import org.forgerock.util.AsyncFunction;
34 import org.forgerock.util.promise.ExceptionHandler;
35 import org.forgerock.util.promise.Promise;
36 import org.forgerock.util.promise.ResultHandler;
37
38 import java.io.IOException;
39 import java.util.concurrent.CountDownLatch;
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 public final class ShortLifeAsync {
58
59 private static Entry entry;
60
61 private static LDIFEntryWriter writer = new LDIFEntryWriter(System.out);
62
63 private static Connection connection;
64
65 private static int resultCode;
66
67 private static final CountDownLatch COMPLETION_LATCH = new CountDownLatch(1);
68
69
70
71
72
73
74
75 public static void main(final String[] args) {
76 if (args.length != 2) {
77 System.err.println("Usage: host port");
78 System.err.println("For example: localhost 1389");
79 System.exit(1);
80 }
81 final String host = args[0];
82 final int port = Integer.parseInt(args[1]);
83
84
85
86
87
88
89 final String adminDn = "uid=kvaughan,ou=people,dc=example,dc=com";
90 final char[] adminPwd = "bribery".toCharArray();
91
92
93 final String entryDn = "cn=Bob,ou=People,dc=example,dc=com";
94 entry = new LinkedHashMapEntry(entryDn)
95 .addAttribute("cn", "Bob")
96 .addAttribute("objectclass", "top")
97 .addAttribute("objectclass", "person")
98 .addAttribute("objectclass", "organizationalPerson")
99 .addAttribute("objectclass", "inetOrgPerson")
100 .addAttribute("mail", "subgenius@example.com")
101 .addAttribute("sn", "Dobbs");
102
103 new LDAPConnectionFactory(host, port)
104 .getConnectionAsync()
105 .thenAsync(new AsyncFunction<Connection, BindResult, LdapException>() {
106 @Override
107 public Promise<BindResult, LdapException> apply(Connection connection)
108 throws LdapException {
109 ShortLifeAsync.connection = connection;
110 return connection.bindAsync(
111 Requests.newSimpleBindRequest(adminDn, adminPwd));
112 }
113 })
114 .thenAsync(new AsyncFunction<BindResult, Result, LdapException>() {
115 @Override
116 public Promise<Result, LdapException> apply(BindResult bindResult)
117 throws LdapException {
118 log("Adding the entry...");
119 log(entry);
120 return connection.addAsync(Requests.newAddRequest(entry));
121 }
122 })
123 .thenAsync(new AsyncFunction<Result, Result, LdapException>() {
124 @Override
125 public Promise<Result, LdapException> apply(Result result)
126 throws LdapException {
127 Entry old = TreeMapEntry.deepCopyOfEntry(entry);
128 entry = entry
129 .replaceAttribute("mail", "spammer@example.com")
130 .addAttribute("description", "Good user gone bad");
131 log("Updating mail address, adding description...");
132 log(entry);
133 ModifyRequest request = Entries.diffEntries(old, entry);
134 return connection.modifyAsync(request);
135 }
136 })
137 .thenAsync(new AsyncFunction<Result, Result, LdapException>() {
138 @Override
139 public Promise<Result, LdapException> apply(Result result)
140 throws LdapException {
141 entry = entry.setName("cn=Renamed,ou=People,dc=example,dc=com");
142 log("Renaming the entry...");
143 log(entry);
144 return connection.modifyDNAsync(
145 Requests.newModifyDNRequest(entryDn, "cn=Renamed"));
146 }
147 })
148 .thenAsync(new AsyncFunction<Result, Result, LdapException>() {
149 @Override
150 public Promise<Result, LdapException> apply(Result result)
151 throws LdapException {
152 final String newDn = entryDn.replace("Bob", "Renamed");
153 log("Deleting " + newDn + "...");
154 return connection.deleteAsync(
155 Requests.newDeleteRequest(newDn));
156 }
157 })
158 .thenOnResult(new ResultHandler<Result>() {
159 @Override
160 public void handleResult(Result result) {
161 resultCode = result.getResultCode().intValue();
162 log("... done.");
163 COMPLETION_LATCH.countDown();
164 }
165 })
166 .thenOnException(new ExceptionHandler<LdapException>() {
167 @Override
168 public void handleException(LdapException e) {
169 System.err.println(e.getMessage());
170 resultCode = e.getResult().getResultCode().intValue();
171 COMPLETION_LATCH.countDown();
172 }
173 });
174
175 try {
176 COMPLETION_LATCH.await();
177 } catch (InterruptedException e) {
178 System.err.println(e.getMessage());
179 System.exit(ResultCode.CLIENT_SIDE_USER_CANCELLED.intValue());
180 return;
181 }
182
183 closeSilently(connection);
184 System.exit(resultCode);
185 }
186
187
188
189
190
191
192 private static void log(final String message) {
193 System.out.println(message);
194 }
195
196
197
198
199
200
201 private static void log(final Entry entry) {
202 try {
203 writer.writeEntry(entry);
204 writer.flush();
205 } catch (IOException e) {
206 System.err.println(e.getMessage());
207 System.exit(ResultCode.CLIENT_SIDE_LOCAL_ERROR.intValue());
208 }
209 }
210
211
212
213
214 private ShortLifeAsync() {
215
216 }
217 }