| CONTENTS | PREV | NEXT | JNDI API |
Appendix B:
| B.5.1 Modify Attributes Using LDAP C API | B.5.2 Modify Attributes Using JNDI |
/** Copyright (c) 1996. Netscape Communications Corporation. All* rights reserved.** Modify an entry:* - replace any existing values in the mail attribute with babs@ace.com* - add a new value to the description attribute*/#include examples.hintmain( int argc, char **argv ){LDAP *ld;LDAPMod mod0;LDAPMod mod1;LDAPMod *mods[ 3 ];char *vals0[ 2 ];char *vals1[ 2 ];time_t now;char buf[ 128 ];/* get a handle to an LDAP connection */if ( (ld = ldap_init( MY_HOST, MY_PORT )) == NULL ) {perror( ldap_init );return( 1 );}/* authenticate */if ( ldap_simple_bind_s( ld, ENTRYDN, ENTRYPW ) != LDAP_SUCCESS ) {ldap_perror( ld, ldap_simple_bind_s );return( 1 );}/* construct the list of modifications to make */mod0.mod_op = LDAP_MOD_REPLACE;mod0.mod_type = mail;vals0[0] = babs@ace.com;vals0[1] = NULL;mod0.mod_values = vals0;mod1.mod_op = LDAP_MOD_ADD;mod1.mod_type = description;time( &now );sprintf( buf, This entry was modified with the modattrs program on %s,ctime( &now ));/* Get rid of \n which ctime put on the end of the time string */if ( buf[ strlen( buf ) - 1 ] == \n ) {buf[ strlen( buf ) - 1 ] = \0;}vals1[ 0 ] = buf;vals1[ 1 ] = NULL;mod1.mod_values = vals1;mods[ 0 ] = &mod0;mods[ 1 ] = &mod1;mods[ 2 ] = NULL;/* make the change */if ( ldap_modify_s( ld, ENTRYDN, mods )!= LDAP_SUCCESS ) {ldap_perror( ld, ldap_modify_s );return( 1 );}ldap_unbind( ld );printf( modification was successful\n );return( 0 );}
/** @(#)Modattrs.java 1.2 99/07/26** Copyright 1997, 1998, 1999 Sun Microsystems, Inc. All Rights* Reserved.** Sun grants you ("Licensee") a non-exclusive, royalty free,* license to use, modify and redistribute this software in source and* binary code form, provided that i) this copyright notice and license* appear on all copies of the software; and ii) Licensee does not utilize* the software in a manner which is disparaging to Sun.** This software is provided "AS IS," without a warranty of any* kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND* WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS* FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN* AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY* LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THE SOFTWARE* OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR* ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,* CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND* REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF* OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE* POSSIBILITY OF SUCH DAMAGES.** This software is not designed or intended for use in on-line* control of aircraft, air traffic, aircraft navigation or aircraft* communications; or in the design, construction, operation or* maintenance of any nuclear facility. Licensee represents and warrants* that it will not use or redistribute the Software for such purposes.*/import java.util.Hashtable;import java.util.Date;import javax.naming.*;import javax.naming.directory.*;/** Modify an entry:* - replace any existing values in the "mail" attribute with "babs@ace.com"* - add a new value to the "description" attribute** [equivalent to modattrs.c in Netscape SDK]*/class Modattrs {public static void main(String[] args) {Hashtable env = new Hashtable(5, 0.75f);/** Specify the initial context implementation to use.* This could also be set by using the -D option to the java program.* For example,* java -Djava.naming.factory.initial=com.sun.jndi.ldap.LdapCtxFactory \* Modattrs*/env.put(Context.INITIAL_CONTEXT_FACTORY, Env.INITCTX);/* Specify host and port to use for directory service */env.put(Context.PROVIDER_URL, Env.MY_SERVICE);/* specify authentication information */env.put(Context.SECURITY_AUTHENTICATION, "simple");env.put(Context.SECURITY_PRINCIPAL, Env.MGR_DN);env.put(Context.SECURITY_CREDENTIALS, Env.MGR_PW);try {/* get a handle to an Initial DirContext */DirContext ctx = new InitialDirContext(env);/* construct the list of modifications to make */ModificationItem[] mods = new ModificationItem[2];Attribute mod0 = new BasicAttribute("mail", "babs@eng");// Update mail attributemods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, mod0);// Add another value to description attributeAttribute mod1 =new BasicAttribute("description","This entry was modified with the Modattrs program on " +(new Date()).toString());mods[1] = new ModificationItem(DirContext.ADD_ATTRIBUTE, mod1);/* Delete the description attribute altogether *//*Attribute mod1 = new BasicAttribute("description");mods[2] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, mod1);*//* make the change */ctx.modifyAttributes(Env.ENTRYDN, mods);System.out.println( "modification was successful." );} catch (NamingException e) {System.err.println("modification failed. " + e);}}}