directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r122739 - /incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/filter/BlacklistFilter.java
Date Sun, 19 Dec 2004 03:07:02 GMT
Author: trustin
Date: Sat Dec 18 19:07:02 2004
New Revision: 122739

URL: http://svn.apache.org/viewcvs?view=rev&rev=122739
Log:
Added: BlacklistFilter as an example
Added:
   incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/filter/BlacklistFilter.java
  (contents, props changed)

Added: incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/filter/BlacklistFilter.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/filter/BlacklistFilter.java?view=auto&rev=122739
==============================================================================
--- (empty file)
+++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/filter/BlacklistFilter.java
Sat Dec 18 19:07:02 2004
@@ -0,0 +1,80 @@
+/*
+ * @(#) $Id$
+ */
+package org.apache.mina.io.filter;
+
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.SocketAddress;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.mina.io.IoHandler;
+import org.apache.mina.io.IoHandlerFilterAdapter;
+import org.apache.mina.io.IoSession;
+import org.apache.mina.io.ReadBuffer;
+
+/**
+ * TODO Document me.
+ * 
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$
+ */
+public class BlacklistFilter extends IoHandlerFilterAdapter
+{
+    private final Set blacklist = new HashSet();
+
+    public void block( InetAddress address )
+    {
+        blacklist.add( address );
+    }
+
+    public void unblock( InetAddress address )
+    {
+        blacklist.remove( address );
+    }
+
+    public void dataRead( IoHandler nextHandler, IoSession session,
+                         int readBytes )
+    {
+        if( isBlocked( session ) )
+        {
+            // just ignore all
+            ReadBuffer buf = session.getReadBuffer();
+            buf.skipAll();
+            buf.signal();
+        }
+        else
+        {
+            // or forward
+            super.dataRead( nextHandler, session, readBytes );
+        }
+    }
+
+    public void sessionOpened( IoHandler nextHandler, IoSession session )
+    {
+        if( isBlocked( session ) )
+        {
+            session.close();
+        }
+        else
+        {
+            super.sessionOpened( nextHandler, session );
+        }
+    }
+
+    private boolean isBlocked( IoSession session )
+    {
+        SocketAddress remoteAddress = session.getRemoteAddress();
+        if( remoteAddress instanceof InetSocketAddress )
+        {
+            if( blacklist.contains( ( ( InetSocketAddress ) remoteAddress )
+                    .getAddress() ) )
+            {
+                return true;
+            }
+        }
+
+        return false;
+    }
+}
\ No newline at end of file

Mime
View raw message