From 506ac793e154de7241da16611bb9ba1b1cb6667c Mon Sep 17 00:00:00 2001
From: Mark Craig <mark.craig@forgerock.com>
Date: Mon, 27 Jul 2015 07:48:40 +0000
Subject: [PATCH] CR-7576 OPENDJ-1767 Show an example schema check

---
 opendj-ldap-sdk-examples/src/main/java/org/forgerock/opendj/examples/UseSchema.java |  150 ++++++++++++++++++++++++++++++++++++++++++++++++++
 opendj-ldap-sdk-examples/src/site/xdoc/index.xml.vm                                 |    8 ++
 2 files changed, 156 insertions(+), 2 deletions(-)

diff --git a/opendj-ldap-sdk-examples/src/main/java/org/forgerock/opendj/examples/UseSchema.java b/opendj-ldap-sdk-examples/src/main/java/org/forgerock/opendj/examples/UseSchema.java
new file mode 100644
index 0000000..b25a370
--- /dev/null
+++ b/opendj-ldap-sdk-examples/src/main/java/org/forgerock/opendj/examples/UseSchema.java
@@ -0,0 +1,150 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License, Version 1.0 only
+ * (the "License").  You may not use this file except in compliance
+ * with the License.
+ *
+ * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
+ * or http://forgerock.org/license/CDDLv1.0.html.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at legal-notices/CDDLv1_0.txt.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information:
+ *      Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ *
+ *
+ *      Copyright 2015 ForgeRock AS.
+ */
+
+package org.forgerock.opendj.examples;
+
+import org.forgerock.i18n.LocalizableMessage;
+import org.forgerock.opendj.ldap.Connection;
+import org.forgerock.opendj.ldap.DN;
+import org.forgerock.opendj.ldap.DecodeException;
+import org.forgerock.opendj.ldap.Entry;
+import org.forgerock.opendj.ldap.EntryNotFoundException;
+import org.forgerock.opendj.ldap.LDAPConnectionFactory;
+import org.forgerock.opendj.ldap.LdapException;
+import org.forgerock.opendj.ldap.ResultCode;
+import org.forgerock.opendj.ldap.responses.Result;
+import org.forgerock.opendj.ldap.schema.Schema;
+import org.forgerock.opendj.ldap.schema.SchemaValidationPolicy;
+import org.forgerock.opendj.ldif.LDIFEntryReader;
+
+import java.io.IOException;
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * This example command-line client application validates an entry
+ * against the directory server schema before adding it.
+ *
+ * <br>
+ *
+ * This example takes the following command line parameters:
+ *
+ * <pre>
+ *  &lt;host> &lt;port> &lt;bindDN> &lt;bindPassword>
+ * </pre>
+ *
+ * Then it reads an entry to add from System.in.
+ * If the entry is valid according to the directory schema,
+ * it tries to add the entry to the directory.
+ */
+public final class UseSchema {
+    /**
+     * Main method.
+     *
+     * @param args
+     *            The command line arguments: host, port, bindDN, bindPassword.
+     */
+    public static void main(final String[] args) {
+        if (args.length != 4) {
+            System.err.println("Usage: host port bindDN bindPassword");
+            System.exit(1);
+        }
+
+        // Parse command line arguments.
+        final String host         = args[0];
+        final int    port         = Integer.parseInt(args[1]);
+        final String bindDn       = args[2];
+        final String bindPassword = args[3];
+
+        // --- JCite ---
+        // Connect and bind to the server.
+        final LDAPConnectionFactory factory = new LDAPConnectionFactory(host, port);
+        Connection connection = null;
+
+        try {
+            connection = factory.getConnection();
+            connection.bind(bindDn, bindPassword.toCharArray());
+
+            // Read the schema from the directory server.
+            // If that fails, use the default schema from the LDAP SDK.
+            Schema schema = null;
+            try {
+                schema = Schema.readSchema(connection, DN.valueOf("cn=schema"));
+            } catch (EntryNotFoundException e) {
+                System.err.println(e.getMessage());
+                schema = Schema.getDefaultSchema();
+            } finally {
+                if (schema == null) {
+                    System.err.println("Failed to get schema.");
+                    System.exit(ResultCode.CLIENT_SIDE_LOCAL_ERROR.intValue());
+                }
+            }
+
+            // Read an entry from System.in.
+            final LDIFEntryReader reader = new LDIFEntryReader(System.in);
+            final Entry entry = reader.readEntry();
+
+            // If the entry is valid, try to add it. Otherwise display errors.
+            final List<LocalizableMessage> schemaErrors = new LinkedList<>();
+            boolean conformsToSchema = schema.validateEntry(
+                    entry, SchemaValidationPolicy.defaultPolicy(), schemaErrors);
+            final String entryDn = entry.getName().toString();
+            Result result = null;
+            if (conformsToSchema) {
+                System.out.println("Processing ADD request for " + entryDn);
+                result = connection.add(entry);
+            } else {
+                for (LocalizableMessage error : schemaErrors) {
+                    System.err.println(error);
+                }
+                System.exit(ResultCode.CLIENT_SIDE_LOCAL_ERROR.intValue());
+            }
+
+            // Display the result. (A failed add results in an LdapException.)
+            if (result != null) {
+                System.out.println("ADD operation successful for DN " + entryDn);
+            }
+        } catch (final LdapException e) {
+            System.err.println(e.getMessage());
+            System.exit(e.getResult().getResultCode().intValue());
+        } catch (DecodeException e) {
+            System.err.println(e.getMessage());
+            System.exit(ResultCode.CLIENT_SIDE_DECODING_ERROR.intValue());
+        } catch (IOException e) {
+            System.err.println(e.getMessage());
+            System.exit(ResultCode.CLIENT_SIDE_LOCAL_ERROR.intValue());
+        } finally {
+            if (connection != null) {
+                connection.close();
+            }
+        }
+        // --- JCite ---
+    }
+
+    private UseSchema() {
+        // Not used.
+    }
+}
diff --git a/opendj-ldap-sdk-examples/src/site/xdoc/index.xml.vm b/opendj-ldap-sdk-examples/src/site/xdoc/index.xml.vm
index 6ae533b..70b7509 100644
--- a/opendj-ldap-sdk-examples/src/site/xdoc/index.xml.vm
+++ b/opendj-ldap-sdk-examples/src/site/xdoc/index.xml.vm
@@ -85,6 +85,10 @@
           - illustrates how to create, update, rename, and delete an entry
         </li>
         <li>
+          <a href="xref/org/forgerock/opendj/examples/UseSchema.html">Use LDAP Schema</a>
+          - illustrates how to validate an entry using the directory server LDAP schema
+        </li>
+        <li>
           <a href="xref/org/forgerock/opendj/examples/Controls.html">Use LDAP Controls</a>
           - illustrates how to use supported LDAP controls
         </li>
@@ -106,8 +110,8 @@
           - illustrates how to use <code>GenericControl</code> to add a pre-read request control
         </li>
         <li>
-          <a href="xref/org/forgerock/opendj/examples/GetADChangeNotifications.html">Use <code>GenericControl</code></a>
-          - illustrates how to use <code>GenericControl</code> to get change notifications from Active Directory
+          <a href="xref/org/forgerock/opendj/examples/GetADChangeNotifications.html">Get AD Change Notifications</a>
+          - illustrates how to use <code>GetADChangeNotifications</code> to get change notifications from Active Directory
         </li>
         <li>
           <a href="xref/org/forgerock/opendj/examples/PasswordResetForAD.html">Reset AD user password</a>

--
Gitblit v1.10.0