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 java.io.IOException;
20
21 import org.forgerock.opendj.ldap.Connection;
22 import org.forgerock.opendj.ldap.Entries;
23 import org.forgerock.opendj.ldap.Entry;
24 import org.forgerock.opendj.ldap.LdapException;
25 import org.forgerock.opendj.ldap.LDAPConnectionFactory;
26 import org.forgerock.opendj.ldap.LinkedHashMapEntry;
27 import org.forgerock.opendj.ldap.ResultCode;
28 import org.forgerock.opendj.ldap.TreeMapEntry;
29 import org.forgerock.opendj.ldap.requests.ModifyRequest;
30 import org.forgerock.opendj.ldif.LDIFEntryWriter;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public final class ShortLife {
47
48
49
50
51
52
53
54
55 public static void main(final String[] args) {
56 if (args.length != 2) {
57 System.err.println("Usage: host port");
58 System.err.println("For example: localhost 1389");
59 System.exit(1);
60 }
61 String host = args[0];
62 int port = Integer.parseInt(args[1]);
63
64
65
66
67
68
69
70
71 String adminDN = "uid=kvaughan,ou=people,dc=example,dc=com";
72 char[] adminPwd = "bribery".toCharArray();
73
74
75
76 String entryDN = "cn=Bob,ou=People,dc=example,dc=com";
77 Entry entry = new LinkedHashMapEntry(entryDN)
78 .addAttribute("cn", "Bob")
79 .addAttribute("objectclass", "top")
80 .addAttribute("objectclass", "person")
81 .addAttribute("objectclass", "organizationalPerson")
82 .addAttribute("objectclass", "inetOrgPerson")
83 .addAttribute("mail", "subgenius@example.com")
84 .addAttribute("sn", "Dobbs");
85
86 LDIFEntryWriter writer = new LDIFEntryWriter(System.out);
87
88 final LDAPConnectionFactory factory = new LDAPConnectionFactory(host, port);
89 Connection connection = null;
90 try {
91 connection = factory.getConnection();
92 connection.bind(adminDN, adminPwd);
93
94 System.out.println("Creating an entry...");
95 writeToConsole(writer, entry);
96 connection.add(entry);
97 System.out.println("...done.");
98
99
100
101 System.out.println("Updating mail address, adding description...");
102 Entry old = TreeMapEntry.deepCopyOfEntry(entry);
103 entry = entry.replaceAttribute("mail", "spammer@example.com")
104 .addAttribute("description", "Good user gone bad");
105 writeToConsole(writer, entry);
106 ModifyRequest request = Entries.diffEntries(old, entry);
107 connection.modify(request);
108 System.out.println("...done.");
109
110
111
112 System.out.println("Renaming the entry...");
113 String newDN = "cn=Ted,ou=People,dc=example,dc=com";
114 entry = entry.setName(newDN);
115 writeToConsole(writer, entry);
116 connection.modifyDN(entryDN, "cn=Ted");
117 System.out.println("...done.");
118
119
120
121 System.out.println("Deleting the entry...");
122 writeToConsole(writer, entry);
123 connection.delete(newDN);
124 System.out.println("...done.");
125
126 } catch (final LdapException e) {
127 System.err.println(e.getMessage());
128 System.exit(e.getResult().getResultCode().intValue());
129 return;
130 } catch (final IOException e) {
131 System.err.println(e.getMessage());
132 System.exit(ResultCode.CLIENT_SIDE_LOCAL_ERROR.intValue());
133 return;
134 } finally {
135 if (connection != null) {
136 connection.close();
137 }
138 try {
139 writer.close();
140 } catch (final IOException ignored) {
141
142 }
143 }
144 }
145
146
147
148
149
150
151
152 private static void writeToConsole(LDIFEntryWriter writer, Entry entry) throws IOException {
153 writer.writeEntry(entry);
154 writer.flush();
155 }
156
157
158
159
160 private ShortLife() {
161
162 }
163 }