View Javadoc
1   /*
2    * The contents of this file are subject to the terms of the Common Development and
3    * Distribution License (the License). You may not use this file except in compliance with the
4    * License.
5    *
6    * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7    * specific language governing permission and limitations under the License.
8    *
9    * When distributing Covered Software, include this CDDL Header Notice in each file and include
10   * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
11   * Header, with the fields enclosed by brackets [] replaced by your own identifying
12   * information: "Portions Copyright [year] [name of copyright owner]".
13   *
14   * Copyright 2015 ForgeRock AS.
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   * This example command-line client application validates an entry
39   * against the directory server schema before adding it.
40   *
41   * <br>
42   *
43   * This example takes the following command line parameters:
44   *
45   * <pre>
46   *  {@code <host> <port> <bindDN> <bindPassword>}
47   * </pre>
48   *
49   * Then it reads an entry to add from System.in.
50   * If the entry is valid according to the directory schema,
51   * it tries to add the entry to the directory.
52   */
53  public final class UseSchema {
54      /**
55       * Main method.
56       *
57       * @param args
58       *            The command line arguments: host, port, bindDN, bindPassword.
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          // Parse command line arguments.
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          // --- JCite ---
73          // Connect and bind to the server.
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              // Read the schema from the directory server.
82              // If that fails, use the default schema from the LDAP SDK.
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              // Read an entry from System.in.
97              final LDIFEntryReader reader = new LDIFEntryReader(System.in);
98              final Entry entry = reader.readEntry();
99  
100             // If the entry is valid, try to add it. Otherwise display errors.
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             // Display the result. (A failed add results in an LdapException.)
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         // --- JCite ---
135     }
136 
137     private UseSchema() {
138         // Not used.
139     }
140 }