001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2009-2010 Sun Microsystems, Inc.
015 * Portions Copyright 2011-2015 ForgeRock AS.
016 */
017
018package org.forgerock.opendj.examples;
019
020import java.io.FileInputStream;
021import java.io.FileNotFoundException;
022import java.io.IOException;
023import java.io.InputStream;
024
025import org.forgerock.opendj.ldap.Connection;
026import org.forgerock.opendj.ldap.LdapException;
027import org.forgerock.opendj.ldap.LDAPConnectionFactory;
028import org.forgerock.opendj.ldap.ResultCode;
029import org.forgerock.opendj.ldif.ChangeRecord;
030import org.forgerock.opendj.ldif.ConnectionChangeRecordWriter;
031import org.forgerock.opendj.ldif.LDIFChangeRecordReader;
032
033/**
034 * An example client application which applies update operations to a Directory
035 * Server. The update operations will be read from an LDIF file, or stdin if no
036 * filename is provided. This example takes the following command line
037 * parameters (it will read from stdin if no LDIF file is provided):
038 *
039 * <pre>
040 *  {@code <host> <port> <username> <password> [<ldifFile>]}
041 * </pre>
042 */
043public final class Modify {
044    /**
045     * Main method.
046     *
047     * @param args
048     *            The command line arguments: host, port, username, password,
049     *            LDIF file name containing the update operations (will use
050     *            stdin if not provided).
051     */
052    public static void main(final String[] args) {
053        if (args.length < 4 || args.length > 5) {
054            System.err.println("Usage: host port username password [ldifFileName]");
055            System.exit(1);
056        }
057
058        // Parse command line arguments.
059        final String hostName = args[0];
060        final int port = Integer.parseInt(args[1]);
061        final String userName = args[2];
062        final String password = args[3];
063
064        // Create the LDIF reader which will either used the named file, if
065        // provided, or stdin.
066        InputStream ldif;
067        if (args.length > 4) {
068            try {
069                ldif = new FileInputStream(args[4]);
070            } catch (final FileNotFoundException e) {
071                System.err.println(e.getMessage());
072                System.exit(ResultCode.CLIENT_SIDE_PARAM_ERROR.intValue());
073                return;
074            }
075        } else {
076            ldif = System.in;
077        }
078        // --- JCite ---
079        final LDIFChangeRecordReader reader = new LDIFChangeRecordReader(ldif);
080
081        // Connect and bind to the server.
082        final LDAPConnectionFactory factory = new LDAPConnectionFactory(hostName, port);
083        Connection connection = null;
084
085        try {
086            connection = factory.getConnection();
087            connection.bind(userName, password.toCharArray());
088
089            // Write the changes.
090            final ConnectionChangeRecordWriter writer =
091                    new ConnectionChangeRecordWriter(connection);
092            while (reader.hasNext()) {
093                ChangeRecord changeRecord = reader.readChangeRecord();
094                writer.writeChangeRecord(changeRecord);
095                System.err.println("Successfully modified entry " + changeRecord.getName());
096            }
097        } catch (final LdapException e) {
098            System.err.println(e.getMessage());
099            System.exit(e.getResult().getResultCode().intValue());
100            return;
101        } catch (final IOException e) {
102            System.err.println(e.getMessage());
103            System.exit(ResultCode.CLIENT_SIDE_LOCAL_ERROR.intValue());
104            return;
105        } finally {
106            if (connection != null) {
107                connection.close();
108            }
109
110            try {
111                reader.close();
112            } catch (final IOException ignored) {
113                // Ignore.
114            }
115        }
116        // --- JCite ---
117    }
118
119    private Modify() {
120        // Not used.
121    }
122}