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 import java.util.Calendar;
21 import java.util.Set;
22
23 import org.forgerock.opendj.ldap.AttributeDescription;
24 import org.forgerock.opendj.ldap.Connection;
25 import org.forgerock.opendj.ldap.DN;
26 import org.forgerock.opendj.ldap.Entry;
27 import org.forgerock.opendj.ldap.LdapException;
28 import org.forgerock.opendj.ldap.LDAPConnectionFactory;
29 import org.forgerock.opendj.ldap.LinkedHashMapEntry;
30 import org.forgerock.opendj.ldap.ModificationType;
31 import org.forgerock.opendj.ldap.ResultCode;
32 import org.forgerock.opendj.ldap.requests.ModifyRequest;
33 import org.forgerock.opendj.ldap.requests.Requests;
34 import org.forgerock.opendj.ldap.responses.SearchResultEntry;
35 import org.forgerock.opendj.ldap.schema.Schema;
36 import org.forgerock.opendj.ldif.LDIFEntryWriter;
37
38
39
40
41
42
43
44
45 public final class ParseAttributes {
46
47
48
49
50
51
52
53 public static void main(final String[] args) {
54 if (args.length != 2) {
55 System.err.println("Usage: host port");
56 System.err.println("For example: localhost 1389");
57 System.exit(1);
58 }
59 final String host = args[0];
60 final int port = Integer.parseInt(args[1]);
61
62
63 final LDAPConnectionFactory factory = new LDAPConnectionFactory(host, port);
64 Connection connection = null;
65 try {
66 connection = factory.getConnection();
67
68
69 String name = "uid=kvaughan,ou=People,dc=example,dc=com";
70 char[] password = "bribery".toCharArray();
71 connection.bind(name, password);
72
73
74 updateEntry(connection, name, "description");
75
76
77 final SearchResultEntry entry = connection.readEntry(name,
78 "cn", "objectClass", "hasSubordinates", "numSubordinates",
79 "isMemberOf", "modifyTimestamp");
80
81
82 DN dn = entry.getName();
83
84 Set<String> cn = entry.parseAttribute("cn").asSetOfString("");
85 Set<AttributeDescription> objectClasses =
86 entry.parseAttribute("objectClass").asSetOfAttributeDescription();
87 boolean hasChildren = entry.parseAttribute("hasSubordinates").asBoolean();
88 int numChildren = entry.parseAttribute("numSubordinates").asInteger(0);
89 Set<DN> groups = entry
90 .parseAttribute("isMemberOf")
91 .usingSchema(Schema.getDefaultSchema()).asSetOfDN();
92 Calendar timestamp = entry
93 .parseAttribute("modifyTimestamp")
94 .asGeneralizedTime().toCalendar();
95
96
97
98
99
100 entry.setName(dn);
101 Entry newEntry = new LinkedHashMapEntry(name)
102 .addAttribute("cn", cn.toArray())
103 .addAttribute("objectClass", objectClasses.toArray())
104 .addAttribute("hasChildren", hasChildren)
105 .addAttribute("numChildren", numChildren)
106 .addAttribute("groups", groups.toArray())
107 .addAttribute("timestamp", timestamp.getTimeInMillis());
108
109 final LDIFEntryWriter writer = new LDIFEntryWriter(System.out);
110 writer.writeEntry(newEntry);
111 writer.close();
112 } catch (final LdapException e) {
113 System.err.println(e.getMessage());
114 System.exit(e.getResult().getResultCode().intValue());
115 return;
116 } catch (IOException e) {
117 System.err.println(e.getMessage());
118 System.exit(ResultCode.CLIENT_SIDE_LOCAL_ERROR.intValue());
119 } finally {
120 if (connection != null) {
121 connection.close();
122 }
123 }
124
125 }
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140 private static void updateEntry(final Connection connection, final String name,
141 final String attributeDescription) throws LdapException {
142 ModifyRequest request = Requests.newModifyRequest(name)
143 .addModification(ModificationType.REPLACE, attributeDescription, "This is a String.");
144 connection.modify(request);
145 }
146
147
148
149
150 private ParseAttributes() {
151
152 }
153 }