Java Code Examples for org.apache.directory.api.ldap.model.entry.Attribute#add()

The following examples show how to use org.apache.directory.api.ldap.model.entry.Attribute#add() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ModificationTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateServerModification() throws LdapException
{
    Attribute attribute = new DefaultAttribute( "cn" );
    attribute.add( "test1", "test2" );

    Modification mod = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, attribute );
    Modification clone = mod.clone();

    attribute.remove( "test2" );

    Attribute clonedAttribute = clone.getAttribute();

    assertEquals( 1, mod.getAttribute().size() );
    assertTrue( mod.getAttribute().contains( "test1" ) );

    assertEquals( 2, clonedAttribute.size() );
    assertTrue( clone.getAttribute().contains( "test1" ) );
    assertTrue( clone.getAttribute().contains( "test2" ) );
}
 
Example 2
Source File: SchemaElementImpl.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Return the extensions formated as Ldif lines
 *
 * @param id The attributeId : can be m-objectClassExtension or
 * m-attributeTypeExtension
 * @return The extensions formated as ldif lines
 * @throws org.apache.directory.api.ldap.model.exception.LdapException If the conversion goes wrong
 */
protected String extensionsToLdif( String id ) throws LdapException
{
    StringBuilder sb = new StringBuilder();

    Entry entry = new DefaultEntry();
    Attribute attribute = new DefaultAttribute( id );

    for ( String extension : extensions.keySet() )
    {
        attribute.add( extension );
    }

    sb.append( LdifUtils.convertAttributesToLdif( entry ) );

    return sb.toString();
}
 
Example 3
Source File: SchemaAwareAttributeTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Test method iterator()
 */
@Test
public void testIterator() throws LdapException
{
    Attribute attr1 = new DefaultAttribute( atCN );
    attr1.add( "a", "b", "c" );

    Iterator<Value> iter = attr1.iterator();

    assertTrue( iter.hasNext() );

    String[] values = new String[]
        { "a", "b", "c" };
    int pos = 0;

    for ( Value val : attr1 )
    {
        assertTrue( val instanceof Value );
        assertEquals( values[pos++], val.getString() );
    }
}
 
Example 4
Source File: LdapDataProvider.java    From directory-fortress-core with Apache License 2.0 6 votes vote down vote up
/**
 * Given a collection of {@link java.util.Properties}, convert to raw data name-value format and load into ldap
 * modification set in preparation for ldap add.
 *
 * @param props    contains {@link java.util.Properties} targeted for adding to ldap.
 * @param entry    contains ldap entry to pull attrs from.
 * @param attrName contains the name of the ldap attribute to be added.
 * @throws LdapException If we weren't able to add the properies into the entry
 */
protected void loadProperties( Properties props, Entry entry, String attrName ) throws LdapException
{
    if ( ( props != null ) && ( props.size() > 0 ) )
    {
        Attribute attr = new DefaultAttribute( attrName );

        for ( Enumeration<?> e = props.propertyNames(); e.hasMoreElements(); )
        {
            // This LDAP attr is stored as a name-value pair separated by a ':'.
            String key = ( String ) e.nextElement();
            String val = props.getProperty( key );
            String prop = key + GlobalIds.PROP_SEP + val;

            attr.add( prop );
        }

        if ( attr.size() != 0 )
        {
            entry.add( attr );
        }
    }
}
 
Example 5
Source File: SchemaInterceptor.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
/**
 * Check a String attribute to see if there is some byte[] value in it.
 *
 * If this is the case, try to change it to a String value.
 */
private boolean checkHumanReadable( Attribute attribute ) throws LdapException
{
    boolean isModified = false;

    // Loop on each values
    for ( Value<?> value : attribute )
    {
        if ( value instanceof StringValue )
        {
            continue;
        }
        else if ( value instanceof BinaryValue )
        {
            // we have a byte[] value. It should be a String UTF-8 encoded
            // Let's transform it
            try
            {
                String valStr = new String( value.getBytes(), "UTF-8" );
                attribute.remove( value );
                attribute.add( valStr );
                isModified = true;
            }
            catch ( UnsupportedEncodingException uee )
            {
                throw new LdapException( I18n.err( I18n.ERR_281 ) );
            }
        }
        else
        {
            throw new LdapException( I18n.err( I18n.ERR_282 ) );
        }
    }

    return isModified;
}
 
Example 6
Source File: SchemaAwareEntryTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test method for add( EntryAttribute... )
 */
@Test
public void testAddEntryAttributeArray() throws LdapException
{
    Entry entry = createEntry();

    assertEquals( 4, entry.size() );
    assertTrue( entry.containsAttribute( "ObjectClass" ) );
    assertTrue( entry.containsAttribute( "CN" ) );
    assertTrue( entry.containsAttribute( "  sn  " ) );
    assertTrue( entry.containsAttribute( "userPassword" ) );

    Attribute attr = entry.get( "objectclass" );
    assertEquals( 2, attr.size() );

    Attribute attrCN2 = new DefaultAttribute( "cn", "test1", "test3" );
    entry.add( attrCN2 );
    assertEquals( 4, entry.size() );
    attr = entry.get( "cn" );
    assertEquals( 3, attr.size() );
    assertTrue( attr.contains( "test1", "test2", "test3" ) );

    // Check adding some byte[] values (they will not be transformed to Strings)
    attrCN2.clear();
    attrCN2.add( BYTES1, BYTES2 );
    entry.add( attrCN2 );
    assertEquals( 4, entry.size() );
    attr = entry.get( "cn" );
    assertEquals( 3, attr.size() );
    assertTrue( attr.contains( "test1", "test2", "test3" ) );
    assertFalse( attr.contains( "ab", "b" ) );
}
 
Example 7
Source File: ModificationTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
@Test
public void testSerializationModificationREMOVE() throws ClassNotFoundException, IOException, LdapException
{
    Attribute attribute = new DefaultAttribute( "cn" );
    attribute.add( "test1", "test2" );

    DefaultModification mod = new DefaultModification( ModificationOperation.REMOVE_ATTRIBUTE, attribute );

    Modification modSer = deserializeValue( serializeValue( mod ) );

    assertEquals( mod, modSer );
}
 
Example 8
Source File: SchemaInterceptor.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
/**
 * Check a String attribute to see if there is some byte[] value in it.
 *
 * If this is the case, try to change it to a String value.
 */
private boolean checkHumanReadable( Attribute attribute ) throws LdapException
{
    boolean isModified = false;

    // Loop on each values
    for ( Value<?> value : attribute )
    {
        if ( value instanceof StringValue )
        {
            continue;
        }
        else if ( value instanceof BinaryValue )
        {
            // we have a byte[] value. It should be a String UTF-8 encoded
            // Let's transform it
            try
            {
                String valStr = new String( value.getBytes(), "UTF-8" );
                attribute.remove( value );
                attribute.add( valStr );
                isModified = true;
            }
            catch ( UnsupportedEncodingException uee )
            {
                throw new LdapException( I18n.err( I18n.ERR_281 ) );
            }
        }
        else
        {
            throw new LdapException( I18n.err( I18n.ERR_282 ) );
        }
    }

    return isModified;
}
 
Example 9
Source File: ModificationTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
@Test
public void testSerializationModificationADD() throws ClassNotFoundException, IOException, LdapException
{
    Attribute attribute = new DefaultAttribute( "cn" );
    attribute.add( "test1", "test2" );

    DefaultModification mod = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, attribute );

    Modification modSer = deserializeValue( serializeValue( mod ) );

    assertEquals( mod, modSer );
}
 
Example 10
Source File: SchemaAwareModificationSerializationTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
@Test
public void testSerializationModificationREPLACE() throws ClassNotFoundException, IOException,
    LdapInvalidAttributeValueException
{
    Attribute attribute = new DefaultAttribute( "cn", cnAT );
    attribute.add( "test1", "test2" );

    DefaultModification mod = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, attribute );

    Modification modSer = deserializeValue( serializeValue( mod ) );

    assertEquals( mod, modSer );
}
 
Example 11
Source File: SchemaAwareAttributeTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test method isValid()
 */
@Test
public void testIsValid() throws Exception
{
    Attribute attr = new DefaultAttribute( atCN );

    // No value, this should not be valid
    assertFalse( attr.isValid( atCN ) );

    attr.add( "test", "test2", "A123\\;" );
    assertTrue( attr.isValid( atCN ) );

    // If we try to add a wrong value, it will not be added. The
    // attribute remains valid.
    assertEquals( 0, attr.add( new byte[]
        { 0x01 } ) );
    assertTrue( attr.isValid( atCN ) );

    // test a SINGLE-VALUE attribute. CountryName is SINGLE-VALUE
    attr.clear();
    attr.apply( atC );
    attr.add( "FR" );
    assertTrue( attr.isValid( atC ) );
    assertEquals( 0, attr.add( "US" ) );
    assertFalse( attr.contains( "US" ) );
    assertTrue( attr.isValid( atC ) );
}
 
Example 12
Source File: SchemaAwareAttributeTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test method isValid( SyntaxChecker )
 */
@Test
public void testIsValidSyntaxChecker() throws LdapException
{
    Attribute attr = new DefaultAttribute( "test" );

    attr.add( "test", "another test" );

    assertTrue( attr.isValid( atCN ) );

    attr.add( "test an invalid '\uFFFD' char" );
    assertFalse( attr.isValid( atCN ) );
}
 
Example 13
Source File: SchemaAwareAttributeTest.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Test method contains( Value... ) throws LdapException
 */
@Test
public void testContainsValueArray() throws LdapException
{
    Attribute attr1 = new DefaultAttribute( atEMail );

    assertEquals( 0, attr1.size() );
    assertFalse( attr1.contains( stringValue1 ) );
    assertFalse( attr1.contains( nullStringValue ) );

    attr1.add( ( String ) null );
    assertEquals( 1, attr1.size() );
    assertTrue( attr1.contains( nullStringValue ) );

    attr1.remove( ( String ) null );
    assertFalse( attr1.contains( nullStringValue ) );
    assertEquals( 0, attr1.size() );

    attr1.add( "a", "b", "c" );
    assertEquals( 3, attr1.size() );
    assertTrue( attr1.contains( stringValue1 ) );
    assertTrue( attr1.contains( stringValue2 ) );
    assertTrue( attr1.contains( stringValue3 ) );
    assertTrue( attr1.contains( stringValue1, stringValue3 ) );
    assertFalse( attr1.contains( stringValue4 ) );
    assertFalse( attr1.contains( nullStringValue ) );

    Attribute attr2 = new DefaultAttribute( atPwd );
    assertEquals( 0, attr2.size() );
    assertFalse( attr2.contains( BYTES1 ) );
    assertFalse( attr2.contains( nullBinaryValue ) );

    attr2.add( ( byte[] ) null );
    assertEquals( 1, attr2.size() );
    assertTrue( attr2.contains( nullBinaryValue ) );

    attr2.remove( ( byte[] ) null );
    assertFalse( attr2.contains( nullBinaryValue ) );
    assertEquals( 0, attr2.size() );

    attr2.add( BYTES1, BYTES2, BYTES3 );
    assertEquals( 3, attr2.size() );
    assertTrue( attr2.contains( binaryValue1 ) );
    assertTrue( attr2.contains( binaryValue2 ) );
    assertTrue( attr2.contains( binaryValue3 ) );
    assertFalse( attr2.contains( nullBinaryValue ) );
}
 
Example 14
Source File: ServerEntryUtils.java    From MyVirtualDirectory with Apache License 2.0 4 votes vote down vote up
/**
 * Convert a BasicAttribute or a AttributeImpl to a ServerAtribute
 *
 * @param attribute the BasicAttributes or AttributesImpl instance to convert
 * @param attributeType
 * @return An instance of a ServerEntry object
 * 
 * @throws InvalidAttributeIdentifierException If we had an incorrect attribute
 */
public static Attribute toServerAttribute( javax.naming.directory.Attribute attribute, AttributeType attributeType )
    throws LdapException
{
    if ( attribute == null )
    {
        return null;
    }

    try
    {
        Attribute serverAttribute = new DefaultAttribute( attributeType );

        for ( NamingEnumeration<?> values = attribute.getAll(); values.hasMoreElements(); )
        {
            Object value = values.nextElement();
            int nbAdded = 0;

            if ( value == null )
            {
                continue;
            }

            if ( serverAttribute.isHumanReadable() )
            {
                if ( value instanceof String )
                {
                    nbAdded = serverAttribute.add( ( String ) value );
                }
                else if ( value instanceof byte[] )
                {
                    nbAdded = serverAttribute.add( Strings.utf8ToString( ( byte[] ) value ) );
                }
                else
                {
                    throw new LdapInvalidAttributeTypeException();
                }
            }
            else
            {
                if ( value instanceof String )
                {
                    nbAdded = serverAttribute.add( Strings.getBytesUtf8( ( String ) value ) );
                }
                else if ( value instanceof byte[] )
                {
                    nbAdded = serverAttribute.add( ( byte[] ) value );
                }
                else
                {
                    throw new LdapInvalidAttributeTypeException();
                }
            }

            if ( nbAdded == 0 )
            {
                throw new LdapInvalidAttributeTypeException();
            }
        }

        return serverAttribute;
    }
    catch ( NamingException ne )
    {
        throw new LdapInvalidAttributeTypeException();
    }
}
 
Example 15
Source File: SchemaAwareAttributeTest.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Test method add( String... )
 */
@Test
public void testAddStringArray() throws LdapInvalidAttributeValueException
{
    Attribute attr1 = new DefaultAttribute( atDC );

    int nbAdded = attr1.add( ( String ) null );
    assertEquals( 1, nbAdded );
    assertTrue( attr1.isHumanReadable() );
    assertEquals( nullStringValue, attr1.get() );

    Attribute attr2 = new DefaultAttribute( atDC );

    nbAdded = attr2.add( "" );
    assertEquals( 1, nbAdded );
    assertTrue( attr2.isHumanReadable() );
    assertEquals( "", attr2.getString() );

    Attribute attr3 = new DefaultAttribute( atCN );

    nbAdded = attr3.add( "t" );
    assertEquals( 1, nbAdded );
    assertTrue( attr3.isHumanReadable() );
    assertEquals( "t", attr3.getString() );

    Attribute attr4 = new DefaultAttribute( atCN );

    nbAdded = attr4.add( "a", "b", "c", "d" );
    assertEquals( 4, nbAdded );
    assertTrue( attr4.isHumanReadable() );
    assertEquals( "a", attr4.getString() );
    assertTrue( attr4.contains( "a" ) );
    assertTrue( attr4.contains( "b" ) );
    assertTrue( attr4.contains( "c" ) );
    assertTrue( attr4.contains( "d" ) );

    nbAdded = attr4.add( "e" );
    assertEquals( 1, nbAdded );
    assertTrue( attr4.isHumanReadable() );
    assertEquals( "a", attr4.getString() );
    assertTrue( attr4.contains( "a" ) );
    assertTrue( attr4.contains( "b" ) );
    assertTrue( attr4.contains( "c" ) );
    assertTrue( attr4.contains( "d" ) );
    assertTrue( attr4.contains( "e" ) );

    nbAdded = attr4.add( BYTES1 );
    assertEquals( 0, nbAdded );
    assertTrue( attr4.isHumanReadable() );
    assertEquals( "a", attr4.getString() );
    assertTrue( attr4.contains( "a" ) );
    assertTrue( attr4.contains( "b" ) );
    assertTrue( attr4.contains( "c" ) );
    assertTrue( attr4.contains( "d" ) );
    assertTrue( attr4.contains( "e" ) );
    assertFalse( attr4.contains( "ab" ) );

    Attribute attr5 = new DefaultAttribute( atEMail );

    nbAdded = attr5.add( "a", "b", ( String ) null, "d" );
    assertEquals( 4, nbAdded );
    assertTrue( attr5.isHumanReadable() );
    assertTrue( attr5.contains( "a" ) );
    assertTrue( attr5.contains( "b" ) );
    assertTrue( attr5.contains( nullStringValue ) );
    assertTrue( attr5.contains( "d" ) );

    Attribute attr6 = new DefaultAttribute( atPwd );

    nbAdded = attr6.add( "a", ( String ) null );
    assertEquals( 2, nbAdded );
    assertFalse( attr6.isHumanReadable() );
}
 
Example 16
Source File: LdifAttributesReader.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Parse an AttributeType/AttributeValue
 *
 * @param schemaManager The SchemaManager
 * @param entry The entry where to store the value
 * @param line The line to parse
 * @param lowerLine The same line, lowercased
 * @throws LdapLdifException If anything goes wrong
 */
private void parseEntryAttribute( SchemaManager schemaManager, Entry entry, String line, String lowerLine )
    throws LdapLdifException
{
    int colonIndex = line.indexOf( ':' );

    String attributeName = lowerLine.substring( 0, colonIndex );
    AttributeType attributeType = null;

    // We should *not* have a Dn twice
    if ( "dn".equals( attributeName ) )
    {
        LOG.error( I18n.err( I18n.ERR_13400_ENTRY_WITH_TWO_DNS ) );
        throw new LdapLdifException( I18n.err( I18n.ERR_13439_LDIF_ENTRY_WITH_TWO_DNS ) );
    }

    if ( schemaManager != null )
    {
        attributeType = schemaManager.getAttributeType( attributeName );

        if ( attributeType == null )
        {
            String msg = I18n.err( I18n.ERR_13475_UNKNOWN_ATTRIBUTETYPE,  attributeName );
            LOG.error( msg );
            throw new LdapLdifException( msg );
        }
    }

    Object attributeValue = parseValue( attributeName, line, colonIndex );

    // Update the entry
    Attribute attribute;

    if ( schemaManager == null )
    {
        attribute = entry.get( attributeName );
    }
    else
    {
        attribute = entry.get( attributeType );
    }

    if ( attribute == null )
    {
        if ( schemaManager == null )
        {
            if ( attributeValue instanceof String )
            {
                entry.put( attributeName, ( String ) attributeValue );
            }
            else
            {
                entry.put( attributeName, ( byte[] ) attributeValue );
            }
        }
        else
        {
            try
            {
                if ( attributeValue instanceof String )
                {
                    entry.put( attributeName, attributeType, ( String ) attributeValue );
                }
                else
                {
                    entry.put( attributeName, attributeType, ( byte[] ) attributeValue );
                }
            }
            catch ( LdapException le )
            {
                throw new LdapLdifException( I18n.err( I18n.ERR_13460_BAD_ATTRIBUTE ), le );
            }
        }
    }
    else
    {
        try
        {
            if ( attributeValue instanceof String )
            {
                attribute.add( ( String ) attributeValue );
            }
            else
            {
                attribute.add( ( byte[] ) attributeValue );
            }
        }
        catch ( LdapInvalidAttributeValueException liave )
        {
            throw new LdapLdifException( liave.getMessage(), liave );
        }
    }
}
 
Example 17
Source File: SchemaInterceptor.java    From MyVirtualDirectory with Apache License 2.0 4 votes vote down vote up
/**
 * Given the objectClasses for an entry, this method adds missing ancestors
 * in the hierarchy except for top which it removes.  This is used for this
 * solution to DIREVE-276.  More information about this solution can be found
 * <a href="http://docs.safehaus.org:8080/x/kBE">here</a>.
 *
 * @param objectClassAttr the objectClass attribute to modify
 * @throws Exception if there are problems
 */
private void alterObjectClasses( Attribute objectClassAttr ) throws LdapException
{
    Set<String> objectClasses = new HashSet<String>();
    Set<String> objectClassesUP = new HashSet<String>();

    // Init the objectClass list with 'top'
    objectClasses.add( SchemaConstants.TOP_OC );
    objectClassesUP.add( SchemaConstants.TOP_OC );

    // Construct the new list of ObjectClasses
    for ( Value<?> ocValue : objectClassAttr )
    {
        String ocName = ocValue.getString();

        if ( !ocName.equalsIgnoreCase( SchemaConstants.TOP_OC ) )
        {
            String ocLowerName = Strings.toLowerCase( ocName );

            ObjectClass objectClass = schemaManager.lookupObjectClassRegistry( ocLowerName );

            if ( !objectClasses.contains( ocLowerName ) )
            {
                objectClasses.add( ocLowerName );
                objectClassesUP.add( ocName );
            }

            List<ObjectClass> ocSuperiors = superiors.get( objectClass.getOid() );

            if ( ocSuperiors != null )
            {
                for ( ObjectClass oc : ocSuperiors )
                {
                    if ( !objectClasses.contains( Strings.toLowerCase( oc.getName() ) ) )
                    {
                        objectClasses.add( oc.getName() );
                        objectClassesUP.add( oc.getName() );
                    }
                }
            }
        }
    }

    // Now, reset the ObjectClass attribute and put the new list into it
    objectClassAttr.clear();

    for ( String attribute : objectClassesUP )
    {
        objectClassAttr.add( attribute );
    }
}
 
Example 18
Source File: SchemaAwareAttributeTest.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Test method hashCode()
 */
@Test
public void testHashCode() throws LdapException
{
    Attribute attr1 = new DefaultAttribute( atDC );
    Attribute attr2 = new DefaultAttribute( atSN );
    assertNotSame( attr1.hashCode(), attr2.hashCode() );

    attr2.apply( atDC );
    assertEquals( attr1.hashCode(), attr2.hashCode() );

    attr1.add( ( String ) null );
    assertNotSame( attr1.hashCode(), attr2.hashCode() );

    attr1.clear();
    assertEquals( attr1.hashCode(), attr2.hashCode() );

    attr1.add( "a", "b" );
    assertNotSame( attr1.hashCode(), attr2.hashCode() );

    attr2.add( "a", "b" );
    assertEquals( attr1.hashCode(), attr2.hashCode() );

    // Order matters
    attr2.clear();
    attr2.add( "b", "a" );
    assertNotSame( attr1.hashCode(), attr2.hashCode() );

    Attribute attr3 = new DefaultAttribute( atPwd );
    Attribute attr4 = new DefaultAttribute( atPwd );
    assertNotSame( attr3.hashCode(), attr4.hashCode() );

    attr3.add( ( byte[] ) null );
    assertNotSame( attr3.hashCode(), attr4.hashCode() );

    attr3.clear();
    assertEquals( attr3.hashCode(), attr4.hashCode() );

    attr3.add( new byte[]
        { 0x01, 0x02 }, new byte[]
        { 0x03, 0x04 } );
    assertNotSame( attr1.hashCode(), attr2.hashCode() );

    attr4.add( new byte[]
        { 0x01, 0x02 }, new byte[]
        { 0x03, 0x04 } );
    assertNotSame( attr1.hashCode(), attr2.hashCode() );

    // Order matters
    attr4.clear();
    attr4.add( new byte[]
        { 0x03, 0x04 }, new byte[]
        { 0x01, 0x02 } );
    assertNotSame( attr1.hashCode(), attr2.hashCode() );
}
 
Example 19
Source File: LdifAnonymizer.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Anonymize a Add change
 * 
 * @param ldifEntry The entry to anonymize
 * @return The anonymized entry
 * @throws LdapException If the anonymization failed
 */
private LdifEntry anonymizeChangeAdd( LdifEntry ldifEntry ) throws LdapException
{
    Dn entryDn = ldifEntry.getDn();
    LdifEntry newLdifEntry = new LdifEntry( schemaManager );
    newLdifEntry.setChangeType( ChangeType.Add );

    // Process the DN first
    Dn anonymizedDn = anonymizeDn( entryDn );
    
    newLdifEntry.setDn( anonymizedDn );
    
    // Now, process the entry's attributes
    for ( Attribute attribute : ldifEntry )
    {
        AttributeType attributeType = attribute.getAttributeType();
        Attribute anonymizedAttribute = new DefaultAttribute( attributeType );
        
        // Deal with the special case of a DN syntax
        
        if ( attributeType.getSyntax().getSyntaxChecker() instanceof DnSyntaxChecker )
        {
            for ( Value dnValue : attribute )
            {
                Dn dn = new Dn( schemaManager, dnValue.getString() );
                Dn newdDn = anonymizeDn( dn );
                anonymizedAttribute.add( newdDn.toString() );
            }
            
            newLdifEntry.addAttribute( attribute );
        }
        else
        {
            Anonymizer anonymizer = attributeAnonymizers.get( attribute.getAttributeType().getOid() );

            if ( anonymizer == null )
            {
                newLdifEntry.addAttribute( attribute );
            }
            else
            {
                anonymizedAttribute = anonymizer.anonymize( valueMap, valueSet, attribute );
                
                if ( anonymizedAttribute != null )
                {
                    newLdifEntry.addAttribute( anonymizedAttribute );
                }
            }
        }
    }

    return newLdifEntry;
}
 
Example 20
Source File: SchemaAwareAttributeTest.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Test method remove( Value... )
 */
@Test
public void testRemoveValueArray() throws Exception
{
    Attribute attr1 = new DefaultAttribute( atEMail );

    assertFalse( attr1.remove( stringValue1 ) );

    attr1.add( "a", "b", "c" );
    assertTrue( attr1.remove( stringValue1 ) );
    assertEquals( 2, attr1.size() );

    assertTrue( attr1.remove( stringValue2, stringValue3 ) );
    assertEquals( 0, attr1.size() );

    assertFalse( attr1.remove( stringValue4 ) );

    attr1.clear();
    attr1.add( "a", "b", "c" );
    assertFalse( attr1.remove( stringValue2, stringValue4 ) );
    assertEquals( 2, attr1.size() );

    attr1.clear();
    attr1.add( "a", ( String ) null, "b" );
    assertTrue( attr1.remove( nullStringValue, stringValue1 ) );
    assertEquals( 1, attr1.size() );

    attr1.clear();
    attr1.add( "a", ( String ) null, "b" );
    attr1.add( BYTES3 );
    assertFalse( attr1.remove( nullStringValue, stringValue1, binaryValue3 ) );
    assertEquals( 1, attr1.size() );

    Attribute attr2 = new DefaultAttribute( atPwd );

    assertFalse( attr2.remove( binaryValue1 ) );

    attr2.add( BYTES1, BYTES2, BYTES3 );
    assertTrue( attr2.remove( binaryValue1 ) );
    assertEquals( 2, attr2.size() );

    assertTrue( attr2.remove( binaryValue2, binaryValue3 ) );
    assertEquals( 0, attr2.size() );

    assertFalse( attr2.remove( binaryValue4 ) );

    attr2.clear();
    attr2.add( BYTES1, BYTES2, BYTES3 );
    assertFalse( attr2.remove( binaryValue2, stringValue4 ) );
    assertEquals( 2, attr2.size() );

    attr2.clear();
    attr2.add( BYTES1, ( byte[] ) null, BYTES3 );
    assertFalse( attr2.remove( nullStringValue, binaryValue1 ) );
    assertEquals( 2, attr2.size() );

    attr2.clear();
    attr2.add( BYTES1, ( byte[] ) null, BYTES2 );
    attr2.add( "c" );
    assertEquals( 4, attr2.size() );
    assertFalse( attr2.remove( nullStringValue, binaryValue1, stringValue3 ) );
    assertEquals( 3, attr2.size() );
}