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