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 org.forgerock.i18n.LocalizableMessage;
20 import org.forgerock.opendj.ldap.Connection;
21 import org.forgerock.opendj.ldap.DN;
22 import org.forgerock.opendj.ldap.DecodeException;
23 import org.forgerock.opendj.ldap.Entry;
24 import org.forgerock.opendj.ldap.EntryNotFoundException;
25 import org.forgerock.opendj.ldap.LDAPConnectionFactory;
26 import org.forgerock.opendj.ldap.LdapException;
27 import org.forgerock.opendj.ldap.ResultCode;
28 import org.forgerock.opendj.ldap.responses.Result;
29 import org.forgerock.opendj.ldap.schema.Schema;
30 import org.forgerock.opendj.ldap.schema.SchemaValidationPolicy;
31 import org.forgerock.opendj.ldif.LDIFEntryReader;
32
33 import java.io.IOException;
34 import java.util.LinkedList;
35 import java.util.List;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public final class UseSchema {
54
55
56
57
58
59
60 public static void main(final String[] args) {
61 if (args.length != 4) {
62 System.err.println("Usage: host port bindDN bindPassword");
63 System.exit(1);
64 }
65
66
67 final String host = args[0];
68 final int port = Integer.parseInt(args[1]);
69 final String bindDn = args[2];
70 final String bindPassword = args[3];
71
72
73
74 final LDAPConnectionFactory factory = new LDAPConnectionFactory(host, port);
75 Connection connection = null;
76
77 try {
78 connection = factory.getConnection();
79 connection.bind(bindDn, bindPassword.toCharArray());
80
81
82
83 Schema schema = null;
84 try {
85 schema = Schema.readSchema(connection, DN.valueOf("cn=schema"));
86 } catch (EntryNotFoundException e) {
87 System.err.println(e.getMessage());
88 schema = Schema.getDefaultSchema();
89 } finally {
90 if (schema == null) {
91 System.err.println("Failed to get schema.");
92 System.exit(ResultCode.CLIENT_SIDE_LOCAL_ERROR.intValue());
93 }
94 }
95
96
97 final LDIFEntryReader reader = new LDIFEntryReader(System.in);
98 final Entry entry = reader.readEntry();
99
100
101 final List<LocalizableMessage> schemaErrors = new LinkedList<>();
102 boolean conformsToSchema = schema.validateEntry(
103 entry, SchemaValidationPolicy.defaultPolicy(), schemaErrors);
104 final String entryDn = entry.getName().toString();
105 Result result = null;
106 if (conformsToSchema) {
107 System.out.println("Processing ADD request for " + entryDn);
108 result = connection.add(entry);
109 } else {
110 for (LocalizableMessage error : schemaErrors) {
111 System.err.println(error);
112 }
113 System.exit(ResultCode.CLIENT_SIDE_LOCAL_ERROR.intValue());
114 }
115
116
117 if (result != null) {
118 System.out.println("ADD operation successful for DN " + entryDn);
119 }
120 } catch (final LdapException e) {
121 System.err.println(e.getMessage());
122 System.exit(e.getResult().getResultCode().intValue());
123 } catch (DecodeException e) {
124 System.err.println(e.getMessage());
125 System.exit(ResultCode.CLIENT_SIDE_DECODING_ERROR.intValue());
126 } catch (IOException e) {
127 System.err.println(e.getMessage());
128 System.exit(ResultCode.CLIENT_SIDE_LOCAL_ERROR.intValue());
129 } finally {
130 if (connection != null) {
131 connection.close();
132 }
133 }
134
135 }
136
137 private UseSchema() {
138
139 }
140 }