ServerEntry tempEntry = (ServerEntry)currentEntry.clone();
// Now, apply each mod one by one
for ( Modification mod:mods )
{
ServerAttribute attribute = (ServerAttribute)mod.getAttribute();
AttributeType attributeType = attribute.getAttributeType();
// We don't allow modification of operational attributes
if ( !attributeType.isUserModifiable() )
{
if ( !attributeType.equals( MODIFIERS_NAME_ATTRIBUTE_TYPE ) &&
!attributeType.equals( MODIFY_TIMESTAMP_ATTRIBUTE_TYPE ) )
{
String msg = I18n.err( I18n.ERR_52, attributeType );
LOG.error( msg );
throw new NoPermissionException( msg );
}
}
switch ( mod.getOperation() )
{
case ADD_ATTRIBUTE :
// Check the syntax here
if ( !attribute.isValid() )
{
// The value syntax is incorrect : this is an error
String msg = I18n.err( I18n.ERR_53, attributeType );
LOG.error( msg );
throw new LdapInvalidAttributeValueException( msg,
ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX );
}
EntryAttribute currentAttribute = tempEntry.get( attributeType );
// First check if the added Attribute is already present in the entry
// If not, we have to create the entry
if ( currentAttribute != null )
{
for ( Value<?> value : attribute )
{
// At this point, we know that the attribute's syntax is correct
// We just have to check that the current attribute does not
// contains the value already
if ( currentAttribute.contains( value ))
{
// This is an error.
String msg = I18n.err( I18n.ERR_54, value );
LOG.error( msg );
throw new LdapAttributeInUseException( msg );
}
currentAttribute.add( value );
}
}
else
{
// We don't check if the attribute is not in the MUST or MAY at this
// point, as one of the following modification can change the
// ObjectClasses.
EntryAttribute newAttribute = createNewAttribute( attribute );
tempEntry.put( newAttribute );
}
break;
case REMOVE_ATTRIBUTE :
// First check that the removed attribute exists
if ( !tempEntry.containsAttribute( attributeType ) )
{
String msg = I18n.err( I18n.ERR_55, attributeType );
LOG.error( msg );
throw new LdapNoSuchAttributeException( msg );
}
// We may have to remove the attribute or only some values
if ( attribute.size() == 0 )
{
// No value : we have to remove the entire attribute
tempEntry.removeAttributes( attributeType );
}
else
{
currentAttribute = tempEntry.get( attributeType );
// Now remove all the values
for ( Value<?> value:attribute )
{
// We can only remove existing values.
if ( currentAttribute.contains( value ) )
{
currentAttribute.remove( value );
}
else
{
String msg = I18n.err( I18n.ERR_56, attributeType );
LOG.error( msg );
throw new LdapNoSuchAttributeException( msg );
}
}
// If the current attribute is empty, we have to remove
// it from the entry
if ( currentAttribute.size() == 0 )
{
tempEntry.removeAttributes( attributeType );
}
}
break;
case REPLACE_ATTRIBUTE :
// The replaced attribute might not exist, it will then be a Add
// If there is no value, then the attribute will be removed
if ( !tempEntry.containsAttribute( attributeType ) )
{
if ( attribute.size() == 0 )
{
// Ignore the modification, as the attributeType does not
// exists in the entry
break;
}
else
{
// Create the new Attribute
EntryAttribute newAttribute = createNewAttribute( attribute );
tempEntry.put( newAttribute );
}
}
else
{
if ( attribute.size() == 0 )
{
// Remove the attribute from the entry
tempEntry.removeAttributes( attributeType );
}
else