001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2015 ForgeRock AS.
025 */
026
027package org.forgerock.opendj.examples;
028
029import org.forgerock.i18n.LocalizableMessage;
030import org.forgerock.opendj.ldap.Connection;
031import org.forgerock.opendj.ldap.DN;
032import org.forgerock.opendj.ldap.DecodeException;
033import org.forgerock.opendj.ldap.Entry;
034import org.forgerock.opendj.ldap.EntryNotFoundException;
035import org.forgerock.opendj.ldap.LDAPConnectionFactory;
036import org.forgerock.opendj.ldap.LdapException;
037import org.forgerock.opendj.ldap.ResultCode;
038import org.forgerock.opendj.ldap.responses.Result;
039import org.forgerock.opendj.ldap.schema.Schema;
040import org.forgerock.opendj.ldap.schema.SchemaValidationPolicy;
041import org.forgerock.opendj.ldif.LDIFEntryReader;
042
043import java.io.IOException;
044import java.util.LinkedList;
045import java.util.List;
046
047/**
048 * This example command-line client application validates an entry
049 * against the directory server schema before adding it.
050 *
051 * <br>
052 *
053 * This example takes the following command line parameters:
054 *
055 * <pre>
056 *  {@code <host> <port> <bindDN> <bindPassword>}
057 * </pre>
058 *
059 * Then it reads an entry to add from System.in.
060 * If the entry is valid according to the directory schema,
061 * it tries to add the entry to the directory.
062 */
063public final class UseSchema {
064    /**
065     * Main method.
066     *
067     * @param args
068     *            The command line arguments: host, port, bindDN, bindPassword.
069     */
070    public static void main(final String[] args) {
071        if (args.length != 4) {
072            System.err.println("Usage: host port bindDN bindPassword");
073            System.exit(1);
074        }
075
076        // Parse command line arguments.
077        final String host         = args[0];
078        final int    port         = Integer.parseInt(args[1]);
079        final String bindDn       = args[2];
080        final String bindPassword = args[3];
081
082        // --- JCite ---
083        // Connect and bind to the server.
084        final LDAPConnectionFactory factory = new LDAPConnectionFactory(host, port);
085        Connection connection = null;
086
087        try {
088            connection = factory.getConnection();
089            connection.bind(bindDn, bindPassword.toCharArray());
090
091            // Read the schema from the directory server.
092            // If that fails, use the default schema from the LDAP SDK.
093            Schema schema = null;
094            try {
095                schema = Schema.readSchema(connection, DN.valueOf("cn=schema"));
096            } catch (EntryNotFoundException e) {
097                System.err.println(e.getMessage());
098                schema = Schema.getDefaultSchema();
099            } finally {
100                if (schema == null) {
101                    System.err.println("Failed to get schema.");
102                    System.exit(ResultCode.CLIENT_SIDE_LOCAL_ERROR.intValue());
103                }
104            }
105
106            // Read an entry from System.in.
107            final LDIFEntryReader reader = new LDIFEntryReader(System.in);
108            final Entry entry = reader.readEntry();
109
110            // If the entry is valid, try to add it. Otherwise display errors.
111            final List<LocalizableMessage> schemaErrors = new LinkedList<>();
112            boolean conformsToSchema = schema.validateEntry(
113                    entry, SchemaValidationPolicy.defaultPolicy(), schemaErrors);
114            final String entryDn = entry.getName().toString();
115            Result result = null;
116            if (conformsToSchema) {
117                System.out.println("Processing ADD request for " + entryDn);
118                result = connection.add(entry);
119            } else {
120                for (LocalizableMessage error : schemaErrors) {
121                    System.err.println(error);
122                }
123                System.exit(ResultCode.CLIENT_SIDE_LOCAL_ERROR.intValue());
124            }
125
126            // Display the result. (A failed add results in an LdapException.)
127            if (result != null) {
128                System.out.println("ADD operation successful for DN " + entryDn);
129            }
130        } catch (final LdapException e) {
131            System.err.println(e.getMessage());
132            System.exit(e.getResult().getResultCode().intValue());
133        } catch (DecodeException e) {
134            System.err.println(e.getMessage());
135            System.exit(ResultCode.CLIENT_SIDE_DECODING_ERROR.intValue());
136        } catch (IOException e) {
137            System.err.println(e.getMessage());
138            System.exit(ResultCode.CLIENT_SIDE_LOCAL_ERROR.intValue());
139        } finally {
140            if (connection != null) {
141                connection.close();
142            }
143        }
144        // --- JCite ---
145    }
146
147    private UseSchema() {
148        // Not used.
149    }
150}