directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r371248 - /directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/filter/BlacklistFilter.java
Date Sun, 22 Jan 2006 09:23:24 GMT
Author: trustin
Date: Sun Jan 22 01:23:20 2006
New Revision: 371248

URL: http://svn.apache.org/viewcvs?rev=371248&view=rev
Log:
Changed BlacklistFilter to block sessions using regular expressions

Modified:
    directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/filter/BlacklistFilter.java

Modified: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/filter/BlacklistFilter.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/filter/BlacklistFilter.java?rev=371248&r1=371247&r2=371248&view=diff
==============================================================================
--- directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/filter/BlacklistFilter.java
(original)
+++ directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/filter/BlacklistFilter.java
Sun Jan 22 01:23:20 2006
@@ -18,10 +18,11 @@
  */
 package org.apache.mina.filter;
 
-import java.net.InetAddress;
 import java.util.Collection;
 import java.util.HashSet;
+import java.util.Iterator;
 import java.util.Set;
+import java.util.regex.Pattern;
 
 import org.apache.mina.common.IdleStatus;
 import org.apache.mina.common.IoAddress;
@@ -39,29 +40,32 @@
  */
 public class BlacklistFilter extends IoFilterAdapter
 {
-    private final Set blacklist = new HashSet();
+    private 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.
+     * @param addressPatterns an array of {@link IoAddress} {@link Pattern}s
+     *                        to be blacklisted.
      */
-    public void setBlacklist( InetAddress[] addresses )
+    public void setBlacklist( Pattern[] addressPatterns )
     {
-        if( addresses == null )
-            throw new NullPointerException( "addresses" );
-        blacklist.clear();
-        for( int i = 0; i < addresses.length; i++ )
+        if( addressPatterns == null )
+            throw new NullPointerException( "addressPatterns" );
+        
+        Set newBlacklist = new HashSet();
+        for( int i = 0; i < addressPatterns.length; i++ )
         {
-            InetAddress addr = addresses[ i ];
-            if( addr == null )
+            Pattern pattern = addressPatterns[ i ];
+            if( pattern == null )
             {
-                throw new NullPointerException( "addresses[" + i + ']' );
+                throw new NullPointerException( "addressPatterns[" + i + ']' );
             }
-            blacklist.add( addr );
+            newBlacklist.add( pattern );
         }
+        blacklist = newBlacklist;
     }
     
     /**
@@ -69,25 +73,25 @@
      * 
      * NOTE: this call will remove any previously blacklisted addresses.
      * 
-     * @param addresses a collection of InetAddress objects representing the 
-     *        addresses to be blacklisted.
+     * @param addressPatterns a collection of {@link IoAddress} {@link Pattern}s
+     *                  representing the addresses to be blacklisted.
      * @throws IllegalArgumentException if the specified collections contains 
-     *         non-{@link InetAddress} objects.
+     *                                  non-{@link Pattern} objects.
      */
-    public void setBlacklist( Collection addresses )
+    public void setBlacklist( Collection addressPatterns )
     {
-        if( addresses == null )
+        if( addressPatterns == null )
             throw new NullPointerException( "addresses" );
 
-        InetAddress[] inetAddresses = new InetAddress[ addresses.size() ];
+        Pattern[] patterns = new Pattern[ addressPatterns.size() ];
         try
         {
-            setBlacklist( ( InetAddress[] ) addresses.toArray( inetAddresses ) );
+            setBlacklist( ( Pattern[] ) addressPatterns.toArray( patterns ) );
         }
         catch ( ArrayStoreException ase )
         {
             IllegalArgumentException iae = new IllegalArgumentException(
-                    "Collection of addresses must contain only InetAddress instances." );
+                    "Collection of addressPatterns must contain only Pattern instances."
);
             iae.initCause( ase );
             throw iae;
         }
@@ -96,21 +100,25 @@
     /**
      * Blocks the specified endpoint.
      */
-    public synchronized void block( InetAddress address )
+    public synchronized void block( Pattern addressPattern )
     {
-        if( address == null )
-            throw new NullPointerException( "address" );
-        blacklist.add( address );
+        if( addressPattern == null )
+            throw new NullPointerException( "addressPattern" );
+        Set newBlacklist = new HashSet( blacklist );
+        newBlacklist.add( addressPattern );
+        blacklist = newBlacklist;
     }
 
     /**
      * Unblocks the specified endpoint.
      */
-    public synchronized void unblock( InetAddress address )
+    public synchronized void unblock( Pattern addressPattern )
     {
-        if( address == null )
-            throw new NullPointerException( "address" );
-        blacklist.remove( address );
+        if( addressPattern == null )
+            throw new NullPointerException( "addressPattern" );
+        Set newBlacklist = new HashSet( blacklist );
+        newBlacklist.remove( addressPattern );
+        blacklist = newBlacklist;
     }
     
     public void sessionCreated( NextFilter nextFilter, IoSession session )
@@ -200,11 +208,14 @@
     private boolean isBlocked( IoSession session )
     {
         IoAddress remoteAddress = session.getRemoteAddress();
-        if( blacklist.contains( remoteAddress ) )
+        for( Iterator i = blacklist.iterator(); i.hasNext(); )
         {
-            return true;
+            if( ( ( Pattern ) i.next() ).matcher( remoteAddress.toString() ).find() )
+            {
+                return true;
+            }
         }
-
+        
         return false;
     }
 }



Mime
View raw message