Java Code Examples for javax.naming.directory.ModificationItem#getModificationOp()

The following examples show how to use javax.naming.directory.ModificationItem#getModificationOp() . 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: 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 2
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 3
Source File: ServerEntryUtils.java    From MyVirtualDirectory with Apache License 2.0 4 votes vote down vote up
public static List<Modification> toServerModification( ModificationItem[] modifications,
    SchemaManager schemaManager ) throws LdapException
{
    if ( modifications != null )
    {
        List<Modification> modificationsList = new ArrayList<Modification>();

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

            // -------------------------------------------------------------------
            // DIRSERVER-646 Fix: Replacing an unknown attribute with no values 
            // (deletion) causes an error
            // -------------------------------------------------------------------

            // TODO - after removing JNDI we need to make the server handle 
            // this in the codec

            if ( !schemaManager.getAttributeTypeRegistry().contains( id )
                && modification.getAttribute().size() == 0
                && modification.getModificationOp() == DirContext.REPLACE_ATTRIBUTE )
            {
                continue;
            }

            // -------------------------------------------------------------------
            // END DIRSERVER-646 Fix
            // -------------------------------------------------------------------

            // TODO : handle options
            AttributeType attributeType = schemaManager.lookupAttributeTypeRegistry( id );
            modificationsList.add( toServerModification( modification, attributeType ) );
        }

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

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

            // -------------------------------------------------------------------
            // DIRSERVER-646 Fix: Replacing an unknown attribute with no values 
            // (deletion) causes an error
            // -------------------------------------------------------------------

            // TODO - after removing JNDI we need to make the server handle 
            // this in the codec

            if ( !schemaManager.getAttributeTypeRegistry().contains( id )
                && modification.getAttribute().size() == 0
                && modification.getModificationOp() == DirContext.REPLACE_ATTRIBUTE )
            {
                continue;
            }

            // -------------------------------------------------------------------
            // END DIRSERVER-646 Fix
            // -------------------------------------------------------------------

            // TODO : handle options
            AttributeType attributeType = schemaManager.lookupAttributeTypeRegistry( id );
            modificationsList.add( toServerModification( modification, attributeType ) );
        }

        return modificationsList;
    }
    else
    {
        return null;
    }
}
 
Example 5
Source File: Entry.java    From scriptella-etl with Apache License 2.0 4 votes vote down vote up
/**
 * Dumps the modifications
 */
private String dumpModificationItems()
{
    StringBuffer sb = new StringBuffer();

    for (ModificationItem modif : modificationList) {
        sb.append("            Operation: ");

        switch (modif.getModificationOp()) {
            case DirContext.ADD_ATTRIBUTE :
                sb.append("ADD\n");
                break;

            case DirContext.REMOVE_ATTRIBUTE :
                sb.append("REMOVE\n");
                break;

            case DirContext.REPLACE_ATTRIBUTE :
                sb.append("REPLACE \n");
                break;
        }

        Attribute attribute = modif.getAttribute();

        sb.append("                Attribute: ").append(attribute.getID()).append('\n');

        if (attribute.size() != 0) {
            try {
                for (NamingEnumeration values = attribute.getAll(); values.hasMoreElements();) {
                    Object value = values.nextElement();

                    if (value instanceof String) {
                        sb.append("                ").append((String) value).append('\n');
                    } else {
                        sb.append("                ").append(Utils.dumpBytes((byte[]) value)).append('\n');
                    }
                }
            }
            catch (NamingException ne) {
                return "";
            }
        }
    }

    return sb.toString();
}
 
Example 6
Source File: ModifyAttributesOperationRecorder.java    From spring-ldap with Apache License 2.0 4 votes vote down vote up
/**
 * Get a ModificationItem to use for rollback of the supplied modification.
 * 
 * @param originalAttributes
 *            All Attributes of the target DN that are affected of any of
 *            the ModificationItems.
 * @param modificationItem
 *            the ModificationItem to create a rollback item for.
 * @return A ModificationItem to use for rollback of the supplied
 *         ModificationItem.
 */
protected ModificationItem getCompensatingModificationItem(
        Attributes originalAttributes, ModificationItem modificationItem) {
    Attribute modificationAttribute = modificationItem.getAttribute();
    Attribute originalAttribute = originalAttributes
            .get(modificationAttribute.getID());

    if (modificationItem.getModificationOp() == DirContext.REMOVE_ATTRIBUTE) {
        if (modificationAttribute.size() == 0) {
            // If the modification attribute size it means that the
            // Attribute should be removed entirely - we should store a
            // ModificationItem to restore all present values for rollback.
            return new ModificationItem(DirContext.ADD_ATTRIBUTE,
                    (Attribute) originalAttribute.clone());
        } else {
            // The rollback modification will be to re-add the removed
            // attribute values.
            return new ModificationItem(DirContext.ADD_ATTRIBUTE,
                    (Attribute) modificationAttribute.clone());
        }
    } else if (modificationItem.getModificationOp() == DirContext.REPLACE_ATTRIBUTE) {
        if (originalAttribute != null) {
            return new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                    (Attribute) originalAttribute.clone());
        } else {
            // The attribute doesn't previously exist - the rollback
            // operation will be to remove the attribute.
            return new ModificationItem(DirContext.REMOVE_ATTRIBUTE,
                    new BasicAttribute(modificationAttribute.getID()));
        }
    } else {
        // An ADD_ATTRIBUTE operation
        if (originalAttribute == null) {
            // The attribute doesn't previously exist - the rollback
            // operation will be to remove the attribute.
            return new ModificationItem(DirContext.REMOVE_ATTRIBUTE,
                    new BasicAttribute(modificationAttribute.getID()));
        } else {
            // The attribute does exist before - we should store the
            // previous value and it should be used for replacing in
            // rollback.
            return new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                    (Attribute) originalAttribute.clone());
        }
    }
}