Java Code Examples for org.apache.directory.api.ldap.model.entry.ModificationOperation#REPLACE_ATTRIBUTE

The following examples show how to use org.apache.directory.api.ldap.model.entry.ModificationOperation#REPLACE_ATTRIBUTE . 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: AttributeUtilsTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Test the removing by modification of an existing attribute in an .
 * 
 * @throws LdapException
 */
@Test
public void testApplyModifyModificationRemoveAttribute() throws LdapException
{
    Entry entry = new DefaultEntry();
    entry.put( "cn", "test" );
    entry.put( "ou", "apache", "acme corp" );

    Attribute newOu = new DefaultAttribute( "ou" );

    Modification modification = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, newOu );

    AttributeUtils.applyModification( entry, modification );

    assertEquals( 1, entry.size() );

    assertNotNull( entry.get( "cn" ) );
    assertNull( entry.get( "ou" ) );
}
 
Example 2
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 3
Source File: ModificationTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
@Test
public void testSerializationModificationREPLACE() throws ClassNotFoundException, IOException, LdapException
{
    Attribute attribute = new DefaultAttribute( "cn" );
    attribute.add( "test1", "test2" );

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

    Modification modSer = deserializeValue( serializeValue( mod ) );

    assertEquals( mod, modSer );
}
 
Example 4
Source File: AttributeUtilsTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * test the addition by modification of an attribute in an empty entry.
 * 
 * As we are replacing a non existing attribute, it should be added.
 *
 * @throws LdapException
 */
@Test
public void testApplyModifyModificationFromEmptyEntry() throws LdapException
{
    Entry entry = new DefaultEntry();

    Attribute attr = new DefaultAttribute( "cn", "test" );

    Modification modification = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, attr );
    AttributeUtils.applyModification( entry, modification );
    assertNotNull( entry.get( "cn" ) );
    assertEquals( 1, entry.size() );
}
 
Example 5
Source File: AttributeUtilsTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test the replacement by modification of an attribute in an empty entry.
 * 
 * As we are replacing a non existing attribute, it should not change the entry.
 *
 * @throws LdapException
 */
@Test
public void testApplyModifyEmptyModificationFromEmptyEntry() throws LdapException
{
    Entry entry = new DefaultEntry();

    Attribute attr = new DefaultAttribute( "cn" );

    Modification modification = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, attr );
    AttributeUtils.applyModification( entry, modification );
    assertNull( entry.get( "cn" ) );
    assertEquals( 0, entry.size() );
}
 
Example 6
Source File: AttributeUtilsTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test the replacement by modification of an attribute in an empty entry.
 * 
 * As we are replacing a non existing attribute, it should not change the entry.
 *
 * @throws LdapException
 */
@Test
public void testApplyModifyAttributeModification() throws LdapException
{
    Entry entry = new DefaultEntry();
    entry.put( "cn", "test" );
    entry.put( "ou", "apache", "acme corp" );

    Attribute newOu = new DefaultAttribute( "ou", "Big Company", "directory" );

    Modification modification = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, newOu );

    AttributeUtils.applyModification( entry, modification );

    assertEquals( 2, entry.size() );

    assertNotNull( entry.get( "cn" ) );
    assertNotNull( entry.get( "ou" ) );

    Attribute modifiedAttr = entry.get( "ou" );

    assertTrue( modifiedAttr.size() != 0 );

    Set<String> expectedValues = new HashSet<String>();
    expectedValues.add( "Big Company" );
    expectedValues.add( "directory" );

    for ( Value value : modifiedAttr )
    {
        String valueStr = value.getString();

        assertTrue( expectedValues.contains( valueStr ) );

        expectedValues.remove( valueStr );
    }

    assertEquals( 0, expectedValues.size() );
}
 
Example 7
Source File: ServerEntryUtils.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
/**
 * Convert a ModificationItem to an instance of a ServerModification object
 *
 * @param modificationImpl the modification instance to convert
 * @param attributeType the associated attributeType
 * @return a instance of a ServerModification object
 */
private static Modification toServerModification( ModificationItem modificationImpl, AttributeType attributeType )
    throws LdapException
{
    ModificationOperation operation;

    switch ( modificationImpl.getModificationOp() )
    {
        case DirContext.REMOVE_ATTRIBUTE:
            operation = ModificationOperation.REMOVE_ATTRIBUTE;
            break;

        case DirContext.REPLACE_ATTRIBUTE:
            operation = ModificationOperation.REPLACE_ATTRIBUTE;
            break;

        case DirContext.ADD_ATTRIBUTE:
        default:
            operation = ModificationOperation.ADD_ATTRIBUTE;
            break;

    }

    Modification modification = new DefaultModification(
        operation,
        ServerEntryUtils.toServerAttribute( modificationImpl.getAttribute(), attributeType ) );

    return modification;

}
 
Example 8
Source File: ServerEntryUtils.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
/**
 * Convert a ModificationItem to an instance of a ServerModification object
 *
 * @param modificationImpl the modification instance to convert
 * @param attributeType the associated attributeType
 * @return a instance of a ServerModification object
 */
private static Modification toServerModification( ModificationItem modificationImpl, AttributeType attributeType )
    throws LdapException
{
    ModificationOperation operation;

    switch ( modificationImpl.getModificationOp() )
    {
        case DirContext.REMOVE_ATTRIBUTE:
            operation = ModificationOperation.REMOVE_ATTRIBUTE;
            break;

        case DirContext.REPLACE_ATTRIBUTE:
            operation = ModificationOperation.REPLACE_ATTRIBUTE;
            break;

        case DirContext.ADD_ATTRIBUTE:
        default:
            operation = ModificationOperation.ADD_ATTRIBUTE;
            break;

    }

    Modification modification = new DefaultModification(
        operation,
        ServerEntryUtils.toServerAttribute( modificationImpl.getAttribute(), attributeType ) );

    return modification;

}
 
Example 9
Source File: ModifyRequestDsml.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Element toDsml( Element root )
{
    Element element = super.toDsml( root );

    ModifyRequest request = getDecorated();

    // Dn
    if ( request.getName() != null )
    {
        element.addAttribute( "dn", request.getName().getName() );
    }

    // Modifications
    Collection<Modification> modifications = request.getModifications();

    for ( Modification modification : modifications )
    {
        Element modElement = element.addElement( "modification" );

        if ( modification.getAttribute() != null )
        {
            modElement.addAttribute( "name", modification.getAttribute().getId() );

            for ( Value value : modification.getAttribute() )
            {
                if ( value.getString() != null )
                {
                    if ( ParserUtils.needsBase64Encoding( value.getString() ) )
                    {
                        Namespace xsdNamespace = new Namespace( "xsd", ParserUtils.XML_SCHEMA_URI );
                        Namespace xsiNamespace = new Namespace( "xsi", ParserUtils.XML_SCHEMA_INSTANCE_URI );
                        element.getDocument().getRootElement().add( xsdNamespace );
                        element.getDocument().getRootElement().add( xsiNamespace );

                        Element valueElement = modElement.addElement( "value" ).addText(
                            ParserUtils.base64Encode( value.getString() ) );
                        valueElement.addAttribute( new QName( "type", xsiNamespace ), "xsd:"
                            + ParserUtils.BASE64BINARY );
                    }
                    else
                    {
                        modElement.addElement( "value" ).setText( value.getString() );
                    }
                }
            }
        }

        ModificationOperation operation = modification.getOperation();

        if ( operation == ModificationOperation.ADD_ATTRIBUTE )
        {
            modElement.addAttribute( "operation", "add" );
        }
        else if ( operation == ModificationOperation.REPLACE_ATTRIBUTE )
        {
            modElement.addAttribute( "operation", "replace" );
        }
        else if ( operation == ModificationOperation.REMOVE_ATTRIBUTE )
        {
            modElement.addAttribute( "operation", "delete" );
        }
        else if ( operation == ModificationOperation.INCREMENT_ATTRIBUTE )
        {
            modElement.addAttribute( "operation", "increment" );
        }
    }

    return element;
}
 
Example 10
Source File: ServerEntryUtils.java    From MyVirtualDirectory with Apache License 2.0 4 votes vote down vote up
public static List<Modification> toServerModification( Modification[] modifications,
    SchemaManager schemaManager ) throws LdapException
{
    if ( modifications != null )
    {
        List<Modification> modificationsList = new ArrayList<Modification>();

        for ( Modification modification : modifications )
        {
            String attributeId = modification.getAttribute().getUpId();
            String id = stripOptions( attributeId );
            modification.getAttribute().setUpId( id );
            Set<String> options = getOptions( attributeId );

            // -------------------------------------------------------------------
            // DIRSERVER-646 Fix: Replacing an unknown attribute with no values 
            // (deletion) causes an error
            // -------------------------------------------------------------------
            if ( !schemaManager.getAttributeTypeRegistry().contains( id )
                && modification.getAttribute().size() == 0
                && modification.getOperation() == ModificationOperation.REPLACE_ATTRIBUTE )
            {
                // The attributeType does not exist in the schema.
                // It's an error
                String message = I18n.err( I18n.ERR_467, id );
                throw new LdapInvalidAttributeTypeException( message );
            }
            else
            {
                // TODO : handle options
                AttributeType attributeType = null;
                try {
                	attributeType = schemaManager.lookupAttributeTypeRegistry( id );
                } catch (LdapNoSuchAttributeException e) {
                	attributeType = ApacheDSUtil.addAttributeToSchema(modification.getAttribute(), schemaManager);
                }
                modificationsList.add( toServerModification( modification, attributeType ) );
            }
        }

        return modificationsList;
    }
    else
    {
        return null;
    }
}
 
Example 11
Source File: ServerEntryUtils.java    From MyVirtualDirectory with Apache License 2.0 4 votes vote down vote up
public static List<Modification> toServerModification( Modification[] modifications,
    SchemaManager schemaManager ) throws LdapException
{
    if ( modifications != null )
    {
        List<Modification> modificationsList = new ArrayList<Modification>();

        for ( Modification modification : modifications )
        {
            String attributeId = modification.getAttribute().getUpId();
            String id = stripOptions( attributeId );
            modification.getAttribute().setUpId( id );
            Set<String> options = getOptions( attributeId );

            // -------------------------------------------------------------------
            // DIRSERVER-646 Fix: Replacing an unknown attribute with no values 
            // (deletion) causes an error
            // -------------------------------------------------------------------
            if ( !schemaManager.getAttributeTypeRegistry().contains( id )
                && modification.getAttribute().size() == 0
                && modification.getOperation() == ModificationOperation.REPLACE_ATTRIBUTE )
            {
                // The attributeType does not exist in the schema.
                // It's an error
                String message = I18n.err( I18n.ERR_467, id );
                throw new LdapInvalidAttributeTypeException( message );
            }
            else
            {
                // TODO : handle options
                AttributeType attributeType = null;
                try {
                	attributeType = schemaManager.lookupAttributeTypeRegistry( id );
                } catch (LdapNoSuchAttributeException e) {
                	attributeType = ApacheDSUtil.addAttributeToSchema(modification.getAttribute(), schemaManager);
                }
                modificationsList.add( toServerModification( modification, attributeType ) );
            }
        }

        return modificationsList;
    }
    else
    {
        return null;
    }
}