<commit_before>/*
 *   Copyright 2004 The Apache Software Foundation
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 */
package org.apache.eve.jndi.ibs;


import java.util.Map;
import java.util.List;
import java.util.ArrayList;

import javax.naming.Name;
import javax.naming.NamingException;
import javax.naming.ldap.LdapContext;
import javax.naming.directory.*;

import org.apache.eve.db.SearchResultFilter;
import org.apache.eve.db.DbSearchResult;
import org.apache.eve.db.SearchResultEnumeration;
import org.apache.eve.db.ResultFilteringEnumeration;
import org.apache.eve.jndi.Invocation;
import org.apache.eve.jndi.BaseInterceptor;
import org.apache.eve.jndi.InvocationStateEnum;

import org.apache.ldap.common.filter.ExprNode;


/**
 * An interceptor based service which manages the filtering of result responses
 * back to callers.  This service is strictly post invocation based and
 * operates upon Attributes and SearchResults obtained through
 * {@link org.apache.eve.BackingStore#lookup(Name)},
 * {@link org.apache.eve.BackingStore#lookup(Name,String[])} and
 * {@link org.apache.eve.BackingStore#search(Name, Map, ExprNode, SearchControls)}
 * operations.
 * <p>
 * We try to limit the amount of filtering decorators used on search results and
 * lookup values to prevent inefficiencies such as the unecessary cloning of
 * Attributes.  Several other services may depend upon this service to modify
 * search and lookup results as callers access them.
 * </p><p>
 * This service should be registered last within after chain of the interceptor
 * framework.
 *
 * @author <a href="mailto:<EMAIL>">Apache Directory Project</a>
 * @version $Rev$
 */
public class FilterServiceImpl extends BaseInterceptor
        implements FilterService
{
    /** the set of registered result filters for search */
    private final List resultFilters = new ArrayList();
    /** the set of registered entry filters for lookup operations */
    private final List lookupFilters = new ArrayList();
    private final static SearchControls LIST_CONTROLS = new SearchControls();


    // ------------------------------------------------------------------------
    // BaseInterceptor Overrides
    // ------------------------------------------------------------------------


    /**
     * @see FilterService#addLookupFilter(LookupFilter)
     */
    public boolean addLookupFilter( LookupFilter filter )
    {
        return lookupFilters.add( filter );
    }


    /**
     * @see FilterService#addSearchResultFilter(SearchResultFilter)
     */
    public boolean addSearchResultFilter( SearchResultFilter filter )
    {
        return resultFilters.add( filter );
    }


    // ------------------------------------------------------------------------
    // BaseInterceptor Overrides
    // ------------------------------------------------------------------------


    protected void list( final Name base ) throws NamingException
    {
        Invocation invocation = getInvocation();

        if ( invocation.getState() == InvocationStateEnum.POSTINVOCATION )
        {
            SearchResultEnumeration enum ;
            ResultFilteringEnumeration retval;
            LdapContext ctx = ( LdapContext ) invocation.getContextStack().peek();
            enum = ( SearchResultEnumeration ) invocation.getReturnValue();
            retval = new ResultFilteringEnumeration( enum, LIST_CONTROLS, ctx,
                new SearchResultFilter()
                {
                    public boolean accept( LdapContext ctx, DbSearchResult result,
                                           SearchControls controls )
                            throws NamingException
                    {
                        result.setName( result.getName() );
                        return FilterServiceImpl.this.accept( ctx, result, controls );
                    }
                } );
            invocation.setReturnValue( retval );
        }
    }


    /**
     * @see BaseInterceptor#lookup(javax.naming.Name)
     */
    protected void lookup( Name dn ) throws NamingException
    {
        Invocation invocation = getInvocation();

        if ( invocation.getState() == InvocationStateEnum.POSTINVOCATION )
        {
            Attributes attributes = ( Attributes ) invocation.getReturnValue();
            Attributes retval = ( Attributes ) attributes.clone();
            LdapContext ctx = ( LdapContext ) invocation.getContextStack().peek();
            filter( ctx, dn, retval );
            invocation.setReturnValue( retval );
        }
    }


    /**
     * @see BaseInterceptor#lookup(javax.naming.Name, String[])
     */
    protected void lookup( Name dn, String[] ids ) throws NamingException
    {
        Invocation invocation = getInvocation();

        if ( invocation.getState() == InvocationStateEnum.POSTINVOCATION )
        {
            LdapContext ctx = ( LdapContext ) invocation.getContextStack().peek();
            Attributes attributes = ( Attributes ) invocation.getReturnValue();

            if ( attributes == null )
            {
                return;
            }

            Attributes retval = ( Attributes ) attributes.clone();
            filter( ctx, dn, retval, ids );
            invocation.setReturnValue( retval );
        }
    }


    /**
     * @see BaseInterceptor#search(Name, Map, ExprNode, SearchControls)
     */
    protected void search( Name base, Map env, ExprNode filter,
                           SearchControls searchControls )
            throws NamingException
    {
        Invocation invocation = getInvocation();

        if ( invocation.getState() == InvocationStateEnum.POSTINVOCATION )
        {
            if ( searchControls.getReturningAttributes() != null )
            {
                return;
            }

            SearchResultEnumeration enum ;
            ResultFilteringEnumeration retval;
            LdapContext ctx = ( LdapContext ) invocation.getContextStack().peek();
            enum = ( SearchResultEnumeration ) invocation.getReturnValue();
            retval = new ResultFilteringEnumeration( enum, searchControls, ctx,
                new SearchResultFilter()
                {
                    public boolean accept( LdapContext ctx, DbSearchResult result,
                                           SearchControls controls )
                            throws NamingException
                    {<commit_msg>handle generic NamingEnumeration to allow for variance instead of just using a SearchResultEnumeration<commit_after>/*
 *   Copyright 2004 The Apache Software Foundation
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 */
package org.apache.eve.jndi.ibs;


import java.util.Map;
import java.util.List;
import java.util.ArrayList;

import javax.naming.Name;
import javax.naming.NamingException;
import javax.naming.NamingEnumeration;
import javax.naming.ldap.LdapContext;
import javax.naming.directory.*;

import org.apache.eve.db.SearchResultFilter;
import org.apache.eve.db.DbSearchResult;
import org.apache.eve.db.ResultFilteringEnumeration;
import org.apache.eve.jndi.Invocation;
import org.apache.eve.jndi.BaseInterceptor;
import org.apache.eve.jndi.InvocationStateEnum;

import org.apache.ldap.common.filter.ExprNode;


/**
 * An interceptor based service which manages the filtering of result responses
 * back to callers.  This service is strictly post invocation based and
 * operates upon Attributes and SearchResults obtained through
 * {@link org.apache.eve.BackingStore#lookup(Name)},
 * {@link org.apache.eve.BackingStore#lookup(Name,String[])} and
 * {@link org.apache.eve.BackingStore#search(Name, Map, ExprNode, SearchControls)}
 * operations.
 * <p>
 * We try to limit the amount of filtering decorators used on search results and
 * lookup values to prevent inefficiencies such as the unecessary cloning of
 * Attributes.  Several other services may depend upon this service to modify
 * search and lookup results as callers access them.
 * </p><p>
 * This service should be registered last within after chain of the interceptor
 * framework.
 *
 * @author <a href="mailto:<EMAIL>">Apache Directory Project</a>
 * @version $Rev$
 */
public class FilterServiceImpl extends BaseInterceptor
        implements FilterService
{
    /** the set of registered result filters for search */
    private final List resultFilters = new ArrayList();
    /** the set of registered entry filters for lookup operations */
    private final List lookupFilters = new ArrayList();
    private final static SearchControls LIST_CONTROLS = new SearchControls();


    // ------------------------------------------------------------------------
    // BaseInterceptor Overrides
    // ------------------------------------------------------------------------


    /**
     * @see FilterService#addLookupFilter(LookupFilter)
     */
    public boolean addLookupFilter( LookupFilter filter )
    {
        return lookupFilters.add( filter );
    }


    /**
     * @see FilterService#addSearchResultFilter(SearchResultFilter)
     */
    public boolean addSearchResultFilter( SearchResultFilter filter )
    {
        return resultFilters.add( filter );
    }


    // ------------------------------------------------------------------------
    // BaseInterceptor Overrides
    // ------------------------------------------------------------------------


    protected void list( final Name base ) throws NamingException
    {
        Invocation invocation = getInvocation();

        if ( invocation.getState() == InvocationStateEnum.POSTINVOCATION )
        {
            NamingEnumeration enum ;
            ResultFilteringEnumeration retval;
            LdapContext ctx = ( LdapContext ) invocation.getContextStack().peek();
            enum = ( NamingEnumeration ) invocation.getReturnValue();
            retval = new ResultFilteringEnumeration( enum, LIST_CONTROLS, ctx,
                new SearchResultFilter()
                {
                    public boolean accept( LdapContext ctx, DbSearchResult result,
                                           SearchControls controls )
                            throws NamingException
                    {
                        result.setName( result.getName() );
                        return FilterServiceImpl.this.accept( ctx, result, controls );
                    }
                } );
            invocation.setReturnValue( retval );
        }
    }


    /**
     * @see BaseInterceptor#lookup(javax.naming.Name)
     */
    protected void lookup( Name dn ) throws NamingException
    {
        Invocation invocation = getInvocation();

        if ( invocation.getState() == InvocationStateEnum.POSTINVOCATION )
        {
            Attributes attributes = ( Attributes ) invocation.getReturnValue();
            Attributes retval = ( Attributes ) attributes.clone();
            LdapContext ctx = ( LdapContext ) invocation.getContextStack().peek();
            filter( ctx, dn, retval );
            invocation.setReturnValue( retval );
        }
    }


    /**
     * @see BaseInterceptor#lookup(javax.naming.Name, String[])
     */
    protected void lookup( Name dn, String[] ids ) throws NamingException
    {
        Invocation invocation = getInvocation();

        if ( invocation.getState() == InvocationStateEnum.POSTINVOCATION )
        {
            LdapContext ctx = ( LdapContext ) invocation.getContextStack().peek();
            Attributes attributes = ( Attributes ) invocation.getReturnValue();

            if ( attributes == null )
            {
                return;
            }

            Attributes retval = ( Attributes ) attributes.clone();
            filter( ctx, dn, retval, ids );
            invocation.setReturnValue( retval );
        }
    }


    /**
     * @see BaseInterceptor#search(Name, Map, ExprNode, SearchControls)
     */
    protected void search( Name base, Map env, ExprNode filter,
                           SearchControls searchControls )
            throws NamingException
    {
        Invocation invocation = getInvocation();

        if ( invocation.getState() == InvocationStateEnum.POSTINVOCATION )
        {
            if ( searchControls.getReturningAttributes() != null )
            {
                return;
            }

            NamingEnumeration enum ;
            ResultFilteringEnumeration retval;
            LdapContext ctx = ( LdapContext ) invocation.getContextStack().peek();
            enum = ( NamingEnumeration ) invocation.getReturnValue();
            retval = new ResultFilteringEnumeration( enum, searchControls, ctx,
                new SearchResultFilter()
                {
                    public boolean accept( LdapContext ctx, DbSearchResult result,
                                           SearchControls controls )
                            throws NamingException
                    {
{'fraction_non_alphanumeric': 0.12110328975721879, 'fraction_numerical': 0.0011492601637695732, 'mean_word_length': 2.0728316045023174, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6961873', 'n_tokens_mistral': 3382, 'n_tokens_neox': 3305, 'n_words': 962}