View Javadoc

1   /*
2    *   @(#) $Id: BlacklistFilter.java 332218 2005-11-10 03:52:42Z trustin $
3    *
4    *   Copyright 2004 The Apache Software Foundation
5    *
6    *   Licensed under the Apache License, Version 2.0 (the "License");
7    *   you may not use this file except in compliance with the License.
8    *   You may obtain a copy of the License at
9    *
10   *       http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing, software
13   *   distributed under the License is distributed on an "AS IS" BASIS,
14   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *   See the License for the specific language governing permissions and
16   *   limitations under the License.
17   *
18   */
19  package org.apache.mina.io.filter;
20  
21  import java.net.InetAddress;
22  import java.net.InetSocketAddress;
23  import java.net.SocketAddress;
24  import java.util.HashSet;
25  import java.util.Set;
26  
27  import org.apache.mina.common.ByteBuffer;
28  import org.apache.mina.io.IoFilter;
29  import org.apache.mina.io.IoFilterAdapter;
30  import org.apache.mina.io.IoSession;
31  
32  /***
33   * A {@link IoFilter} which blocks connections from blacklisted remote
34   * address.
35   * 
36   * @author The Apache Directory Project (dev@directory.apache.org)
37   * @version $Rev: 332218 $, $Date: 2005-11-10 12:52:42 +0900 $
38   */
39  public class BlacklistFilter extends IoFilterAdapter
40  {
41      private final Set blacklist = new HashSet();
42  
43      /***
44       * Blocks the specified endpoint.
45       */
46      public synchronized void block( InetAddress address )
47      {
48          blacklist.add( address );
49      }
50  
51      /***
52       * Unblocks the specified endpoint.
53       */
54      public synchronized void unblock( InetAddress address )
55      {
56          blacklist.remove( address );
57      }
58  
59      /***
60       * Forwards event if and if only the remote address of session is not
61       * blacklisted.
62       */
63      public void dataRead( NextFilter nextFilter, IoSession session,
64                           ByteBuffer buf ) throws Exception
65      {
66          if( !isBlocked( session ) )
67          {
68              // forward if not blocked
69              super.dataRead( nextFilter, session, buf );
70          }
71      }
72  
73      /***
74       * Close connection immediately if the remote address of session is
75       * blacklisted.
76       */
77      public void sessionOpened( NextFilter nextFilter, IoSession session ) throws Exception
78      {
79          if( isBlocked( session ) )
80          {
81              // Close immediately if blocked
82              session.close();
83          }
84          else
85          {
86              super.sessionOpened( nextFilter, session );
87          }
88      }
89  
90      private boolean isBlocked( IoSession session )
91      {
92          SocketAddress remoteAddress = session.getRemoteAddress();
93          if( remoteAddress instanceof InetSocketAddress )
94          {
95              if( blacklist.contains( ( ( InetSocketAddress ) remoteAddress )
96                      .getAddress() ) )
97              {
98                  return true;
99              }
100         }
101 
102         return false;
103     }
104 }