Java Code Examples for org.apache.directory.api.ldap.model.ldif.LdifEntry#addAttribute()

The following examples show how to use org.apache.directory.api.ldap.model.ldif.LdifEntry#addAttribute() . 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: LdifUtilsTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test a conversion of an entry from a LDIF file
 */
@Test
public void testConvertToLdif() throws LdapException
{
    LdifEntry entry = new LdifEntry();
    entry.setDn( "cn=Saarbr\u00FCcken, dc=example, dc=com" );
    entry.setChangeType( ChangeType.Add );

    entry.addAttribute( "objectClass", "top", "person", "inetorgPerson" );
    entry.addAttribute( "cn", "Saarbr\u00FCcken" );
    entry.addAttribute( "sn", "test" );

    LdifUtils.convertToLdif( entry, 15 );
}
 
Example 2
Source File: LdifUtilsTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
@Test
public void testConvertEntryNoControls() throws Exception
{
    LdifReader reader = new LdifReader();

    String expected =
        "dn: ou=test\n" +
            "ObjectClass: top\n" +
            "ObjectClass: metaTop\n" +
            "ObjectClass: metaSyntax\n" +
            "m-oid: 1.2.3.4\n" +
            "m-description: description\n\n";

    List<LdifEntry> entries = reader.parseLdif( expected );
    LdifEntry expectedEntry = entries.get( 0 );

    LdifEntry entry = new LdifEntry();

    entry.setDn( "ou=test" );
    entry.addAttribute( "ObjectClass", "top", "metaTop", "metaSyntax" );
    entry.addAttribute( "m-oid", "1.2.3.4" );
    entry.addAttribute( "m-description", "description" );

    String converted = LdifUtils.convertToLdif( entry );

    assertNotNull( converted );

    entries = reader.parseLdif( converted );
    LdifEntry convertedEntry = entries.get( 0 );

    assertEquals( expectedEntry, convertedEntry );
    
    reader.close();
}
 
Example 3
Source File: LdifUtilsTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
@Test
public void testConvertEntryOneControl() throws Exception
{
    LdifReader reader = new LdifReader();

    String expected =
        "dn: ou=test\n" +
            "control: 2.16.840.1.113730.3.4.2 false\n" +
            "changetype: add\n" +
            "ObjectClass: top\n" +
            "ObjectClass: metaTop\n" +
            "ObjectClass: metaSyntax\n" +
            "m-oid: 1.2.3.4\n" +
            "m-description: description\n\n";

    List<LdifEntry> entries = reader.parseLdif( expected );
    LdifEntry expectedEntry = entries.get( 0 );

    LdifEntry entry = new LdifEntry();

    entry.setDn( "ou=test" );
    entry.addAttribute( "ObjectClass", "top", "metaTop", "metaSyntax" );
    entry.addAttribute( "m-oid", "1.2.3.4" );
    entry.addAttribute( "m-description", "description" );

    ManageDsaITImpl control = new ManageDsaITImpl();

    entry.addControl( control );

    String converted = LdifUtils.convertToLdif( entry );

    assertNotNull( converted );

    entries = reader.parseLdif( converted );
    LdifEntry convertedEntry = entries.get( 0 );

    assertEquals( expectedEntry, convertedEntry );
    reader.close();
}
 
Example 4
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;
}