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-2014 ForgeRock AS. 015 */ 016 017package org.forgerock.opendj.examples; 018 019import java.io.IOException; 020import java.util.Calendar; 021import java.util.Set; 022 023import org.forgerock.opendj.ldap.AttributeDescription; 024import org.forgerock.opendj.ldap.Connection; 025import org.forgerock.opendj.ldap.DN; 026import org.forgerock.opendj.ldap.Entry; 027import org.forgerock.opendj.ldap.LdapException; 028import org.forgerock.opendj.ldap.LDAPConnectionFactory; 029import org.forgerock.opendj.ldap.LinkedHashMapEntry; 030import org.forgerock.opendj.ldap.ModificationType; 031import org.forgerock.opendj.ldap.ResultCode; 032import org.forgerock.opendj.ldap.requests.ModifyRequest; 033import org.forgerock.opendj.ldap.requests.Requests; 034import org.forgerock.opendj.ldap.responses.SearchResultEntry; 035import org.forgerock.opendj.ldap.schema.Schema; 036import org.forgerock.opendj.ldif.LDIFEntryWriter; 037 038/** 039 * This command-line client demonstrates parsing entry attribute values to 040 * objects. The client takes as arguments the host and port for the directory 041 * server, and expects to find the entries and access control instructions as 042 * defined in <a 043 * href="http://opendj.forgerock.org/Example.ldif">Example.ldif</a>. 044 */ 045public final class ParseAttributes { 046 047 /** 048 * Connect to the server, and then try to use some LDAP controls. 049 * 050 * @param args 051 * The command line arguments: host, port 052 */ 053 public static void main(final String[] args) { 054 if (args.length != 2) { 055 System.err.println("Usage: host port"); 056 System.err.println("For example: localhost 1389"); 057 System.exit(1); 058 } 059 final String host = args[0]; 060 final int port = Integer.parseInt(args[1]); 061 062 // --- JCite --- 063 final LDAPConnectionFactory factory = new LDAPConnectionFactory(host, port); 064 Connection connection = null; 065 try { 066 connection = factory.getConnection(); 067 068 // Use Kirsten Vaughan's credentials and her entry. 069 String name = "uid=kvaughan,ou=People,dc=example,dc=com"; 070 char[] password = "bribery".toCharArray(); 071 connection.bind(name, password); 072 073 // Make sure we have a timestamp to play with. 074 updateEntry(connection, name, "description"); 075 076 // Read Kirsten's entry. 077 final SearchResultEntry entry = connection.readEntry(name, 078 "cn", "objectClass", "hasSubordinates", "numSubordinates", 079 "isMemberOf", "modifyTimestamp"); 080 081 // Get the entry DN and some attribute values as objects. 082 DN dn = entry.getName(); 083 084 Set<String> cn = entry.parseAttribute("cn").asSetOfString(""); 085 Set<AttributeDescription> objectClasses = 086 entry.parseAttribute("objectClass").asSetOfAttributeDescription(); 087 boolean hasChildren = entry.parseAttribute("hasSubordinates").asBoolean(); 088 int numChildren = entry.parseAttribute("numSubordinates").asInteger(0); 089 Set<DN> groups = entry 090 .parseAttribute("isMemberOf") 091 .usingSchema(Schema.getDefaultSchema()).asSetOfDN(); 092 Calendar timestamp = entry 093 .parseAttribute("modifyTimestamp") 094 .asGeneralizedTime().toCalendar(); 095 096 // Do something with the objects. 097 // ... 098 099 // This example simply dumps what was obtained. 100 entry.setName(dn); 101 Entry newEntry = new LinkedHashMapEntry(name) 102 .addAttribute("cn", cn.toArray()) 103 .addAttribute("objectClass", objectClasses.toArray()) 104 .addAttribute("hasChildren", hasChildren) 105 .addAttribute("numChildren", numChildren) 106 .addAttribute("groups", groups.toArray()) 107 .addAttribute("timestamp", timestamp.getTimeInMillis()); 108 109 final LDIFEntryWriter writer = new LDIFEntryWriter(System.out); 110 writer.writeEntry(newEntry); 111 writer.close(); 112 } catch (final LdapException e) { 113 System.err.println(e.getMessage()); 114 System.exit(e.getResult().getResultCode().intValue()); 115 return; 116 } catch (IOException e) { 117 System.err.println(e.getMessage()); 118 System.exit(ResultCode.CLIENT_SIDE_LOCAL_ERROR.intValue()); 119 } finally { 120 if (connection != null) { 121 connection.close(); 122 } 123 } 124 // --- JCite --- 125 } 126 127 /** 128 * Update and entry to generate a time stamp. 129 * 130 * @param connection 131 * Connection to the directory server with rights to perform a 132 * modification on the entry. 133 * @param name 134 * DN of the entry to modify. 135 * @param attributeDescription 136 * Attribute to modify. Must take a String value. 137 * @throws LdapException 138 * Modify failed. 139 */ 140 private static void updateEntry(final Connection connection, final String name, 141 final String attributeDescription) throws LdapException { 142 ModifyRequest request = Requests.newModifyRequest(name) 143 .addModification(ModificationType.REPLACE, attributeDescription, "This is a String."); 144 connection.modify(request); 145 } 146 147 /** 148 * Constructor not used. 149 */ 150 private ParseAttributes() { 151 // Not used. 152 } 153}