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 2009-2010 Sun Microsystems, Inc.
025 *      Portions Copyright 2011-2015 ForgeRock AS.
026 */
027
028package org.forgerock.opendj.examples;
029
030import java.io.FileInputStream;
031import java.io.FileNotFoundException;
032import java.io.IOException;
033import java.io.InputStream;
034
035import org.forgerock.opendj.ldap.Connection;
036import org.forgerock.opendj.ldap.LdapException;
037import org.forgerock.opendj.ldap.LDAPConnectionFactory;
038import org.forgerock.opendj.ldap.ResultCode;
039import org.forgerock.opendj.ldif.ChangeRecord;
040import org.forgerock.opendj.ldif.ConnectionChangeRecordWriter;
041import org.forgerock.opendj.ldif.LDIFChangeRecordReader;
042
043/**
044 * An example client application which applies update operations to a Directory
045 * Server. The update operations will be read from an LDIF file, or stdin if no
046 * filename is provided. This example takes the following command line
047 * parameters (it will read from stdin if no LDIF file is provided):
048 *
049 * <pre>
050 *  {@code <host> <port> <username> <password> [<ldifFile>]}
051 * </pre>
052 */
053public final class Modify {
054    /**
055     * Main method.
056     *
057     * @param args
058     *            The command line arguments: host, port, username, password,
059     *            LDIF file name containing the update operations (will use
060     *            stdin if not provided).
061     */
062    public static void main(final String[] args) {
063        if (args.length < 4 || args.length > 5) {
064            System.err.println("Usage: host port username password [ldifFileName]");
065            System.exit(1);
066        }
067
068        // Parse command line arguments.
069        final String hostName = args[0];
070        final int port = Integer.parseInt(args[1]);
071        final String userName = args[2];
072        final String password = args[3];
073
074        // Create the LDIF reader which will either used the named file, if
075        // provided, or stdin.
076        InputStream ldif;
077        if (args.length > 4) {
078            try {
079                ldif = new FileInputStream(args[4]);
080            } catch (final FileNotFoundException e) {
081                System.err.println(e.getMessage());
082                System.exit(ResultCode.CLIENT_SIDE_PARAM_ERROR.intValue());
083                return;
084            }
085        } else {
086            ldif = System.in;
087        }
088        // --- JCite ---
089        final LDIFChangeRecordReader reader = new LDIFChangeRecordReader(ldif);
090
091        // Connect and bind to the server.
092        final LDAPConnectionFactory factory = new LDAPConnectionFactory(hostName, port);
093        Connection connection = null;
094
095        try {
096            connection = factory.getConnection();
097            connection.bind(userName, password.toCharArray());
098
099            // Write the changes.
100            final ConnectionChangeRecordWriter writer =
101                    new ConnectionChangeRecordWriter(connection);
102            while (reader.hasNext()) {
103                ChangeRecord changeRecord = reader.readChangeRecord();
104                writer.writeChangeRecord(changeRecord);
105                System.err.println("Successfully modified entry " + changeRecord.getName());
106            }
107        } catch (final LdapException e) {
108            System.err.println(e.getMessage());
109            System.exit(e.getResult().getResultCode().intValue());
110            return;
111        } catch (final IOException e) {
112            System.err.println(e.getMessage());
113            System.exit(ResultCode.CLIENT_SIDE_LOCAL_ERROR.intValue());
114            return;
115        } finally {
116            if (connection != null) {
117                connection.close();
118            }
119
120            try {
121                reader.close();
122            } catch (final IOException ignored) {
123                // Ignore.
124            }
125        }
126        // --- JCite ---
127    }
128
129    private Modify() {
130        // Not used.
131    }
132}