public static void filterContents()

in core-api/src/main/java/org/apache/directory/server/core/api/entry/ServerEntryUtils.java [760:927]


    public static void filterContents( SchemaManager schemaManager, FilteringOperationContext operationContext,
        Entry entry ) throws LdapException
    {
        boolean typesOnly = operationContext.isTypesOnly();

        boolean returnAll = ( operationContext.isAllOperationalAttributes() && operationContext.isAllUserAttributes() )
            && ( !typesOnly );

        if ( returnAll )
        {
            return;
        }

        // for special handling of entryDN attribute, see DIRSERVER-1902
        Entry originalEntry = ( ( ClonedServerEntry ) entry ).getOriginalEntry();

        AttributeType entryDnType = schemaManager.lookupAttributeTypeRegistry( SchemaConstants.ENTRY_DN_AT_OID );
        AttributeType refType = schemaManager.lookupAttributeTypeRegistry( SchemaConstants.REF_AT_OID );

        // First, remove all the attributes if we have the NoAttribute flag set to true
        if ( operationContext.isNoAttributes() )
        {
            for ( Attribute attribute : originalEntry )
            {
                AttributeType attributeType = attribute.getAttributeType();

                // Bypass the ref attribute, unless the ManageDSAIT control is present
                if ( operationContext.isReferralThrown() && attributeType.equals( refType ) )
                {
                    continue;
                }

                entry.remove( entry.get( attributeType ) );
            }

            entry.removeAttributes( entryDnType );

            return;
        }

        // If the user has requested all the User attributes ('*') only, we filter the entry's attribute to keep only
        // the USER attributes, plus the Operational attributes in the returning list 
        if ( operationContext.isAllUserAttributes() )
        {
            for ( Attribute attribute : originalEntry )
            {
                AttributeType attributeType = attribute.getAttributeType();

                // Bypass the ref attribute, unless the ManageDSAIT control is present
                if ( operationContext.isReferralThrown() && attributeType.equals( refType ) )
                {
                    continue;
                }

                if ( attributeType.isOperational() )
                {
                    if ( !operationContext.contains( schemaManager, attributeType ) )
                    {
                        entry.removeAttributes( attributeType );
                    }
                    else if ( typesOnly )
                    {
                        entry.get( attributeType ).clear();
                    }
                }
                else if ( typesOnly )
                {
                    entry.get( attributeType ).clear();
                }
            }

            // DIRSERVER-1953
            if ( !operationContext.contains( schemaManager, entryDnType ) )
            {
                entry.removeAttributes( entryDnType );
            }

            return;
        }

        // If the user has requested all the Operational attributes ('+') only, we filter the entry's attribute to keep only
        // the OPERATIONAL attributes, plus the User attributes in the returning list 
        if ( operationContext.isAllOperationalAttributes() )
        {
            for ( Attribute attribute : originalEntry )
            {
                AttributeType attributeType = attribute.getAttributeType();

                if ( attributeType.isUser() )
                {
                    if ( !operationContext.contains( schemaManager, attributeType ) )
                    {
                        entry.removeAttributes( attributeType );
                    }
                    else if ( typesOnly )
                    {
                        entry.get( attributeType ).clear();
                    }
                }
                else if ( typesOnly )
                {
                    entry.get( attributeType ).clear();
                }
            }

            if ( !operationContext.contains( schemaManager, entryDnType ) )
            {
                entry.removeAttributes( entryDnType );
            }
            else if ( typesOnly )
            {
                entry.get( entryDnType ).clear();
            }

            return;
        }

        // Last, not least, check if the attributes are in the returning list
        if ( operationContext.getReturningAttributes() != null )
        {
            for ( Attribute attribute : originalEntry )
            {
                AttributeType attributeType = attribute.getAttributeType();

                // Bypass the ref attribute, unless the ManageDSAIT control is present
                if ( operationContext.isReferralThrown() && attributeType.equals( refType ) )
                {
                    continue;
                }

                if ( !operationContext.contains( schemaManager, attributeType ) )
                {
                    entry.removeAttributes( attributeType );
                    continue;
                }

                boolean isNotRequested = true;

                for ( AttributeTypeOptions attrOptions : operationContext.getReturningAttributes() )
                {
                    if ( attrOptions.getAttributeType().equals( attributeType )
                        || attrOptions.getAttributeType().isAncestorOf( attributeType ) )
                    {
                        isNotRequested = false;
                        break;
                    }
                }

                if ( isNotRequested )
                {
                    entry.removeAttributes( attributeType );
                }
                else if ( typesOnly )
                {
                    entry.get( attributeType ).clear();
                }
            }

            if ( !operationContext.contains( schemaManager, entryDnType ) )
            {
                entry.removeAttributes( entryDnType );
            }
            else if ( typesOnly )
            {
                entry.get( entryDnType ).clear();
            }
        }
    }