1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.forgerock.opendj.examples;
19
20 import org.forgerock.opendj.io.ASN1;
21 import org.forgerock.opendj.io.ASN1Writer;
22 import org.forgerock.opendj.ldap.ByteStringBuilder;
23 import org.forgerock.opendj.ldap.Connection;
24 import org.forgerock.opendj.ldap.DecodeOptions;
25 import org.forgerock.opendj.ldap.Entry;
26 import org.forgerock.opendj.ldap.LdapException;
27 import org.forgerock.opendj.ldap.LDAPConnectionFactory;
28 import org.forgerock.opendj.ldap.ModificationType;
29 import org.forgerock.opendj.ldap.ResultCode;
30 import org.forgerock.opendj.ldap.SearchScope;
31 import org.forgerock.opendj.ldap.controls.GenericControl;
32 import org.forgerock.opendj.ldap.controls.PreReadResponseControl;
33 import org.forgerock.opendj.ldap.requests.ModifyRequest;
34 import org.forgerock.opendj.ldap.requests.Requests;
35 import org.forgerock.opendj.ldap.responses.Result;
36 import org.forgerock.opendj.ldap.responses.SearchResultEntry;
37 import org.forgerock.opendj.ldif.LDIFEntryWriter;
38
39 import java.io.IOException;
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 public final class UseGenericControl {
57
58
59
60
61
62
63 public static void main(final String[] args) {
64 if (args.length < 5) {
65 System.err.println("Usage: host port username password userDN");
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 userDN = args[4];
75
76
77
78 final LDIFEntryWriter writer = new LDIFEntryWriter(System.out);
79
80
81 final LDAPConnectionFactory factory =
82 new LDAPConnectionFactory(hostName, port);
83 Connection connection = null;
84
85
86
87
88
89
90
91
92 ByteStringBuilder builder = new ByteStringBuilder();
93 ASN1Writer asn1Writer = ASN1.getWriter(builder);
94 try {
95 asn1Writer.writeStartSequence();
96 asn1Writer.writeOctetString("description");
97 asn1Writer.writeEndSequence();
98 asn1Writer.flush();
99 asn1Writer.close();
100 } catch (Exception e) {
101 System.out.println("Failed to prepare control value: "
102 + e.getCause());
103 System.exit(-1);
104 }
105
106 try {
107 connection = factory.getConnection();
108 connection.bind(userName, password.toCharArray());
109
110
111 final ModifyRequest request =
112 Requests
113 .newModifyRequest(userDN)
114 .addModification(ModificationType.REPLACE,
115 "description", "A new description")
116 .addControl(
117 GenericControl
118 .newControl(
119 "1.3.6.1.1.13.1",
120 true,
121 builder.toByteString()));
122 final Result result = connection.modify(request);
123
124
125 if (result.isSuccess()) {
126 final PreReadResponseControl control = result.getControl(
127 PreReadResponseControl.DECODER, new DecodeOptions()
128 );
129 final Entry unmodifiedEntry = control.getEntry();
130 writer.writeComment("Before modification");
131 writer.writeEntry(unmodifiedEntry);
132 writer.flush();
133
134 final SearchResultEntry modifiedEntry =
135 connection.searchSingleEntry(
136 userDN,
137 SearchScope.BASE_OBJECT,
138 "(objectclass=*)",
139 "description");
140 writer.writeComment("After modification");
141 writer.writeEntry(modifiedEntry);
142 writer.flush();
143 }
144
145 } catch (final LdapException e) {
146 System.err.println(e.getMessage());
147 System.exit(e.getResult().getResultCode().intValue());
148 } catch (final IOException e) {
149 System.err.println(e.getMessage());
150 System.exit(ResultCode.CLIENT_SIDE_LOCAL_ERROR.intValue());
151 } finally {
152 if (connection != null) {
153 connection.close();
154 }
155 }
156
157 }
158
159 private UseGenericControl() {
160
161 }
162 }