EMMA Coverage Report (generated Fri Oct 21 16:16:13 KST 2005)
[all classes][org.apache.mina.io.filter]

COVERAGE SUMMARY FOR SOURCE FILE [BlacklistFilter.java]

nameclass, %method, %block, %line, %
BlacklistFilter.java0%   (0/1)0%   (0/6)0%   (0/59)0%   (0/19)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class BlacklistFilter0%   (0/1)0%   (0/6)0%   (0/59)0%   (0/19)
BlacklistFilter (): void 0%   (0/1)0%   (0/8)0%   (0/2)
block (InetAddress): void 0%   (0/1)0%   (0/6)0%   (0/2)
dataRead (IoFilter$NextFilter, IoSession, ByteBuffer): void 0%   (0/1)0%   (0/10)0%   (0/3)
isBlocked (IoSession): boolean 0%   (0/1)0%   (0/17)0%   (0/5)
sessionOpened (IoFilter$NextFilter, IoSession): void 0%   (0/1)0%   (0/12)0%   (0/5)
unblock (InetAddress): void 0%   (0/1)0%   (0/6)0%   (0/2)

1/*
2 *   @(#) $Id: BlacklistFilter.java 327113 2005-10-21 06:59:15Z 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 */
19package org.apache.mina.io.filter;
20 
21import java.net.InetAddress;
22import java.net.InetSocketAddress;
23import java.net.SocketAddress;
24import java.util.HashSet;
25import java.util.Set;
26 
27import org.apache.mina.common.ByteBuffer;
28import org.apache.mina.io.IoFilter;
29import org.apache.mina.io.IoFilterAdapter;
30import 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: 327113 $, $Date: 2005-10-21 15:59:15 +0900 $
38 */
39public 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}

[all classes][org.apache.mina.io.filter]
EMMA 2.0.4217 (C) Vladimir Roubtsov