Package javax.naming.directory

Examples of javax.naming.directory.DirContext.modifyAttributes()


        DirContext ctx = new InitialDirContext( env );

        // replace cn attribute with "cn=test", must remove the previous "cn=aaa"
        Attribute attr = new BasicAttribute( "cn", "test" );
        ModificationItem modification = new ModificationItem( DirContext.REPLACE_ATTRIBUTE, attr );
        ctx.modifyAttributes( "cn=test", new ModificationItem[]
            { modification } );

        Attributes attrs = ctx.getAttributes( "cn=test", new String[]
            { "cn" } );
        Attribute cn = attrs.get( "cn" );


        // try to add an non-existing objectClass "test", must be rejected
        Attribute attr = new BasicAttribute( "objectclass", "test" );
        ModificationItem modification = new ModificationItem( DirContext.ADD_ATTRIBUTE, attr );
        try
        {
            ctx.modifyAttributes( "cn=test", new ModificationItem[]
                { modification } );
            fail( "Exception expected" );
        }
        catch ( SchemaViolationException sve )
        {

        // try to add an unallowed attribute, must be rejected
        Attribute attr = new BasicAttribute( "javaDoc", "test" );
        ModificationItem modification = new ModificationItem( DirContext.ADD_ATTRIBUTE, attr );
        try
        {
            ctx.modifyAttributes( "cn=test", new ModificationItem[]
                { modification } );
            fail( "Exception expected" );
        }
        catch ( SchemaViolationException sve )
        {

        Attribute objectClass = new BasicAttribute( "objectClass" );
        objectClass.add( "top" );
        objectClass.add( "extensibleObject" );
        objectClass.add( "organizationalUnit" );
        newattribs.put( objectClass );
        ctx.modifyAttributes( "", DirContext.REPLACE_ATTRIBUTE, newattribs );

        attributes = ctx.getAttributes( "" );
        assertNotNull( attributes );
        assertEquals( "testing00", attributes.get( "ou" ).get() );
        attribute = attributes.get( "objectClass" );

        DirContext ctx = ( DirContext ) initCtx.lookup( "" );
        LdapNoPermissionException notNull = null;

        try
        {
            ctx.modifyAttributes( "", DirContext.ADD_ATTRIBUTE, null );
            fail( "we should never get here" );
        }
        catch ( LdapNoPermissionException e )
        {
            notNull = e;

        LdapNoPermissionException notNull = null;

        try
        {
            ctx.modifyAttributes( "", new ModificationItem[]
                {} );

            fail( "we should never get here" );
        }
        catch ( LdapNoPermissionException e )

                try
                {
                    DirContext dc = openDirContext();

                    dc.modifyAttributes( "", mods );
                    dc.close();
                }
                catch ( NamingException e )
                {
                    String msg = "Failed modifying directory password attribute: " + e;

        if ( isKrb5KdcDisabled )
        {
            Attribute disabled = new BasicAttribute( "m-disabled" );
            ModificationItem[] mods = new ModificationItem[]
                { new ModificationItem( DirContext.REMOVE_ATTRIBUTE, disabled ) };
            schemaRoot.modifyAttributes( "cn=Krb5kdc", mods );
        }

        DirContext ctx = ( DirContext ) getWiredContext( ldapServer ).lookup( "dc=example,dc=com" );
        Attributes attrs = getOrgUnitAttributes( "users" );
        DirContext users = ctx.createSubcontext( "ou=users", attrs );

        attributes.put( attr );
        attr = new BasicAttribute( KerberosAttribute.KRB5_PRINCIPAL_NAME_AT, newPrincipalName );
        attributes.put( attr );

        DirContext person = ( DirContext ) ctx.lookup( RDN );
        person.modifyAttributes( "", DirContext.REPLACE_ATTRIBUTE, attributes );

        // Read again from directory.
        person = ( DirContext ) ctx.lookup( RDN );

        attributes = person.getAttributes( "" );

        attributes.put( attr );
        attr = new BasicAttribute( KerberosAttribute.KRB5_PRINCIPAL_NAME_AT, newPrincipalName );
        attributes.put( attr );

        person = ( DirContext ) ctx.lookup( RDN );
        person.modifyAttributes( "", DirContext.REPLACE_ATTRIBUTE, attributes );

        // Read again from directory.
        person = ( DirContext ) ctx.lookup( RDN );

        attributes = person.getAttributes( "" );

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.