<commit_before>/*
 *   @(#) $Id$
 *
 *   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.mina.filter;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.apache.mina.common.IdleStatus;
import org.apache.mina.common.IoFilter;
import org.apache.mina.common.IoFilterAdapter;
import org.apache.mina.common.IoSession;
import org.apache.mina.util.SessionLog;

/**
 * A {@link IoFilter} which blocks connections from blacklisted remote
 * address.
 * 
 * @author The Apache Directory Project (<EMAIL>)
 * @version $Rev$, $Date$
 */
public class BlacklistFilter extends IoFilterAdapter
{
    private final Set blacklist = new HashSet();

    /**
     * Sets the addresses to be blacklisted.
     * 
     * NOTE: this call will remove any previously blacklisted addresses.
     * 
     * @param addresses an array of addresses to be blacklisted.
     */
    public void setBlacklist( InetAddress[] addresses )
    {
        if( addresses == null )
            throw new NullPointerException( "addresses" );
        blacklist.clear();
        for( int i = 0; i < addresses.length; i++ )
        {
            InetAddress addr = addresses[ i ];
            if( addr == null )
            {
                throw new NullPointerException( "addresses[" + i + ']' );
            }
            blacklist.add( addr );
        }
    }
    
    /**
     * Sets the addresses to be blacklisted.
     * 
     * NOTE: this call will remove any previously blacklisted addresses.
     * 
     * @param addresses a collection of InetAddress objects representing the 
     *        addresses to be blacklisted.
     * @throws IllegalArgumentException if the specified collections contains 
     *         non-{@link InetAddress} objects.
     */
    public void setBlacklist( Collection addresses )
    {
        if( addresses == null )
            throw new NullPointerException( "addresses" );

        InetAddress[] inetAddresses = new InetAddress[ addresses.size() ];
        try
        {
            setBlacklist( ( InetAddress[] ) addresses.toArray( inetAddresses ) );
        }
        catch ( ArrayStoreException ase )
        {
            IllegalArgumentException iae = new IllegalArgumentException(
                    "Collection of addresses must contain only InetAddress instances." );
            iae.initCause( ase );
            throw iae;
        }
    }
    
    /**
     * Blocks the specified endpoint.
     */
    public synchronized void block( InetAddress address )
    {
        if( address == null )
            throw new NullPointerException( "address" );
        blacklist.add( address );
    }

    /**
     * Unblocks the specified endpoint.
     */
    public synchronized void unblock( InetAddress address )
    {
        if( address == null )<commit_msg>Adding blocked endpoints should always be synchronized. So centralize where this is done.<commit_after>/*
 *   @(#) $Id$
 *
 *   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.mina.filter;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.apache.mina.common.IdleStatus;
import org.apache.mina.common.IoFilter;
import org.apache.mina.common.IoFilterAdapter;
import org.apache.mina.common.IoSession;
import org.apache.mina.util.SessionLog;

/**
 * A {@link IoFilter} which blocks connections from blacklisted remote
 * address.
 * 
 * @author The Apache Directory Project (<EMAIL>)
 * @version $Rev$, $Date$
 */
public class BlacklistFilter extends IoFilterAdapter
{
    private final Set blacklist = new HashSet();

    /**
     * Sets the addresses to be blacklisted.
     * 
     * NOTE: this call will remove any previously blacklisted addresses.
     * 
     * @param addresses an array of addresses to be blacklisted.
     */
    public void setBlacklist( InetAddress[] addresses )
    {
        if( addresses == null )
            throw new NullPointerException( "addresses" );
        blacklist.clear();
        for( int i = 0; i < addresses.length; i++ )
        {
            InetAddress addr = addresses[ i ];
            block (addr, "addresses[" + i + ']' );
        }
    }
    
    /**
     * Sets the addresses to be blacklisted.
     * 
     * NOTE: this call will remove any previously blacklisted addresses.
     * 
     * @param addresses a collection of InetAddress objects representing the 
     *        addresses to be blacklisted.
     * @throws IllegalArgumentException if the specified collections contains 
     *         non-{@link InetAddress} objects.
     */
    public void setBlacklist( Collection addresses )
    {
        if( addresses == null )
            throw new NullPointerException( "addresses" );

        InetAddress[] inetAddresses = new InetAddress[ addresses.size() ];
        try
        {
            setBlacklist( ( InetAddress[] ) addresses.toArray( inetAddresses ) );
        }
        catch ( ArrayStoreException ase )
        {
            IllegalArgumentException iae = new IllegalArgumentException(
                    "Collection of addresses must contain only InetAddress instances." );
            iae.initCause( ase );
            throw iae;
        }
    }
    
    /**
     * Blocks the specified endpoint.
     */
    public synchronized void block( InetAddress address , String error_string )
    {
        if( address == null )
            throw new NullPointerException( error_string );
        blacklist.add( address );
    }

    /**
     * Blocks the specified endpoint.
     */
    public synchronized void block( InetAddress address )
    {
        block( address, "address" );
    }

    /**
     * Unblocks the specified endpoint.
     */
    public synchronized void unblock( InetAddress address )
    {
        if( address == null )
{'fraction_non_alphanumeric': 0.08868030260577192, 'fraction_numerical': 0.0025217147660409078, 'mean_word_length': 2.3995238095238096, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3403142', 'n_tokens_mistral': 1890, 'n_tokens_neox': 1843, 'n_words': 639}