View Javadoc
1   /*
2    * The contents of this file are subject to the terms of the Common Development and
3    * Distribution License (the License). You may not use this file except in compliance with the
4    * License.
5    *
6    * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7    * specific language governing permission and limitations under the License.
8    *
9    * When distributing Covered Software, include this CDDL Header Notice in each file and include
10   * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
11   * Header, with the fields enclosed by brackets [] replaced by your own identifying
12   * information: "Portions Copyright [year] [name of copyright owner]".
13   *
14   * Copyright 2012-2015 ForgeRock AS.
15   */
16  
17  package org.forgerock.opendj.examples;
18  
19  import java.io.IOException;
20  
21  import org.forgerock.opendj.ldap.Connection;
22  import org.forgerock.opendj.ldap.Entries;
23  import org.forgerock.opendj.ldap.Entry;
24  import org.forgerock.opendj.ldap.LdapException;
25  import org.forgerock.opendj.ldap.LDAPConnectionFactory;
26  import org.forgerock.opendj.ldap.LinkedHashMapEntry;
27  import org.forgerock.opendj.ldap.ResultCode;
28  import org.forgerock.opendj.ldap.TreeMapEntry;
29  import org.forgerock.opendj.ldap.requests.ModifyRequest;
30  import org.forgerock.opendj.ldif.LDIFEntryWriter;
31  
32  /**
33   * A command-line client that creates, updates, renames, and deletes a
34   * short-lived entry in order to demonstrate LDAP write operations using the
35   * synchronous API. The client takes as arguments the host and port for the
36   * directory server, and expects to find the entries and access control
37   * instructions as defined in <a
38   * href="http://opendj.forgerock.org/Example.ldif">Example.ldif</a>.
39   *
40   * <ul>
41   * <li>host - host name of the directory server</li>
42   * <li>port - port number of the directory server</li>
43   * </ul>
44   * All arguments are required.
45   */
46  public final class ShortLife {
47  
48      /**
49       * Connect to directory server as a user with rights to add, modify, and
50       * delete entries, and then proceed to carry out the operations.
51       *
52       * @param args
53       *            The command line arguments: host, port
54       */
55      public static void main(final String[] args) {
56          if (args.length != 2) {
57              System.err.println("Usage: host port");
58              System.err.println("For example: localhost 1389");
59              System.exit(1);
60          }
61          String host = args[0];
62          int port = Integer.parseInt(args[1]);
63  
64          // User credentials of a "Directory Administrators" group member.
65          // Kirsten Vaughan is authorized to create, update, and delete
66          // entries.
67          //
68          // You might prompt an administrator user for information needed to
69          // authenticate, or your application might have its own account with
70          // rights to perform all necessary operations.
71          String adminDN = "uid=kvaughan,ou=people,dc=example,dc=com";
72          char[] adminPwd = "bribery".toCharArray();
73  
74          // --- JCite add ---
75          // An entry to add to the directory
76          String entryDN = "cn=Bob,ou=People,dc=example,dc=com";
77          Entry entry = new LinkedHashMapEntry(entryDN)
78              .addAttribute("cn", "Bob")
79              .addAttribute("objectclass", "top")
80              .addAttribute("objectclass", "person")
81              .addAttribute("objectclass", "organizationalPerson")
82              .addAttribute("objectclass", "inetOrgPerson")
83              .addAttribute("mail", "subgenius@example.com")
84              .addAttribute("sn", "Dobbs");
85  
86          LDIFEntryWriter writer = new LDIFEntryWriter(System.out);
87  
88          final LDAPConnectionFactory factory = new LDAPConnectionFactory(host, port);
89          Connection connection = null;
90          try {
91              connection = factory.getConnection();
92              connection.bind(adminDN, adminPwd);
93  
94              System.out.println("Creating an entry...");
95              writeToConsole(writer, entry);
96              connection.add(entry);
97              System.out.println("...done.");
98              // --- JCite add ---
99  
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 }