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 2012-2015 ForgeRock AS.
015 */
016
017package org.forgerock.opendj.examples;
018
019import java.io.IOException;
020
021import org.forgerock.opendj.ldap.Connection;
022import org.forgerock.opendj.ldap.Entries;
023import org.forgerock.opendj.ldap.Entry;
024import org.forgerock.opendj.ldap.LdapException;
025import org.forgerock.opendj.ldap.LDAPConnectionFactory;
026import org.forgerock.opendj.ldap.LinkedHashMapEntry;
027import org.forgerock.opendj.ldap.ResultCode;
028import org.forgerock.opendj.ldap.TreeMapEntry;
029import org.forgerock.opendj.ldap.requests.ModifyRequest;
030import org.forgerock.opendj.ldif.LDIFEntryWriter;
031
032/**
033 * A command-line client that creates, updates, renames, and deletes a
034 * short-lived entry in order to demonstrate LDAP write operations using the
035 * synchronous API. The client takes as arguments the host and port for the
036 * directory server, and expects to find the entries and access control
037 * instructions as defined in <a
038 * href="http://opendj.forgerock.org/Example.ldif">Example.ldif</a>.
039 *
040 * <ul>
041 * <li>host - host name of the directory server</li>
042 * <li>port - port number of the directory server</li>
043 * </ul>
044 * All arguments are required.
045 */
046public final class ShortLife {
047
048    /**
049     * Connect to directory server as a user with rights to add, modify, and
050     * delete entries, and then proceed to carry out the operations.
051     *
052     * @param args
053     *            The command line arguments: host, port
054     */
055    public static void main(final String[] args) {
056        if (args.length != 2) {
057            System.err.println("Usage: host port");
058            System.err.println("For example: localhost 1389");
059            System.exit(1);
060        }
061        String host = args[0];
062        int port = Integer.parseInt(args[1]);
063
064        // User credentials of a "Directory Administrators" group member.
065        // Kirsten Vaughan is authorized to create, update, and delete
066        // entries.
067        //
068        // You might prompt an administrator user for information needed to
069        // authenticate, or your application might have its own account with
070        // rights to perform all necessary operations.
071        String adminDN = "uid=kvaughan,ou=people,dc=example,dc=com";
072        char[] adminPwd = "bribery".toCharArray();
073
074        // --- JCite add ---
075        // An entry to add to the directory
076        String entryDN = "cn=Bob,ou=People,dc=example,dc=com";
077        Entry entry = new LinkedHashMapEntry(entryDN)
078            .addAttribute("cn", "Bob")
079            .addAttribute("objectclass", "top")
080            .addAttribute("objectclass", "person")
081            .addAttribute("objectclass", "organizationalPerson")
082            .addAttribute("objectclass", "inetOrgPerson")
083            .addAttribute("mail", "subgenius@example.com")
084            .addAttribute("sn", "Dobbs");
085
086        LDIFEntryWriter writer = new LDIFEntryWriter(System.out);
087
088        final LDAPConnectionFactory factory = new LDAPConnectionFactory(host, port);
089        Connection connection = null;
090        try {
091            connection = factory.getConnection();
092            connection.bind(adminDN, adminPwd);
093
094            System.out.println("Creating an entry...");
095            writeToConsole(writer, entry);
096            connection.add(entry);
097            System.out.println("...done.");
098            // --- JCite add ---
099
100            // --- JCite modify ---
101            System.out.println("Updating mail address, adding description...");
102            Entry old = TreeMapEntry.deepCopyOfEntry(entry);
103            entry = entry.replaceAttribute("mail", "spammer@example.com")
104                    .addAttribute("description", "Good user gone bad");
105            writeToConsole(writer, entry);
106            ModifyRequest request = Entries.diffEntries(old, entry);
107            connection.modify(request);
108            System.out.println("...done.");
109            // --- JCite modify ---
110
111            // --- JCite rename ---
112            System.out.println("Renaming the entry...");
113            String newDN = "cn=Ted,ou=People,dc=example,dc=com";
114            entry = entry.setName(newDN);
115            writeToConsole(writer, entry);
116            connection.modifyDN(entryDN, "cn=Ted");
117            System.out.println("...done.");
118            // --- JCite rename ---
119
120            // --- JCite delete ---
121            System.out.println("Deleting the entry...");
122            writeToConsole(writer, entry);
123            connection.delete(newDN);
124            System.out.println("...done.");
125            // --- JCite delete ---
126        } catch (final LdapException e) {
127            System.err.println(e.getMessage());
128            System.exit(e.getResult().getResultCode().intValue());
129            return;
130        } catch (final IOException e) {
131            System.err.println(e.getMessage());
132            System.exit(ResultCode.CLIENT_SIDE_LOCAL_ERROR.intValue());
133            return;
134        } finally {
135            if (connection != null) {
136                connection.close();
137            }
138            try {
139                writer.close();
140            } catch (final IOException ignored) {
141                // Ignore.
142            }
143        }
144    }
145
146    /**
147     * Write the entry in LDIF form to System.out.
148     *
149     * @param entry
150     *            The entry to write to the console.
151     */
152    private static void writeToConsole(LDIFEntryWriter writer, Entry entry) throws IOException {
153        writer.writeEntry(entry);
154        writer.flush();
155    }
156
157    /**
158     * Constructor not used.
159     */
160    private ShortLife() {
161        // Not used.
162    }
163}