KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > myvietnam > mvncore > filter > IPFilter


1 /*
2  * $Header: /cvsroot/mvnforum/myvietnam/src/net/myvietnam/mvncore/filter/IPFilter.java,v 1.11 2006/04/15 02:59:19 minhnn Exp $
3  * $Author: minhnn $
4  * $Revision: 1.11 $
5  * $Date: 2006/04/15 02:59:19 $
6  *
7  * ====================================================================
8  *
9  * Copyright (C) 2002-2006 by MyVietnam.net
10  *
11  * All copyright notices regarding MyVietnam and MyVietnam CoreLib
12  * MUST remain intact in the scripts and source code.
13  *
14  * This library is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU Lesser General Public
16  * License as published by the Free Software Foundation; either
17  * version 2.1 of the License, or (at your option) any later version.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  *
28  * Correspondence and Marketing Questions can be sent to:
29  * info at MyVietnam net
30  *
31  * @author: Minh Nguyen
32  * @author: Mai Nguyen
33  */

34 package net.myvietnam.mvncore.filter;
35
36 import javax.servlet.http.HttpServletRequest JavaDoc;
37
38 import net.myvietnam.mvncore.MVNCoreConfig;
39 import net.myvietnam.mvncore.util.StringUtil;
40 import org.apache.commons.logging.Log;
41 import org.apache.commons.logging.LogFactory;
42 import org.apache.regexp.RE;
43
44 public final class IPFilter {
45
46     private static Log log = LogFactory.getLog(IPFilter.class);
47
48     private static RE[] blockedIPs = null;
49
50     private IPFilter() { //prevent instantiation
51
}
52
53     static {
54         //IPOptions ipOptions = new IPOptions();
55
//String[] blockedIPArray = StringUtil.getStringArray(ipOptions.blockedIP, ";");
56

57         String JavaDoc[] blockedIPArray = StringUtil.getStringArray(MVNCoreConfig.getBlockedIPs(), ";");
58         blockedIPs = new RE[blockedIPArray.length];
59         for (int i = 0; i < blockedIPArray.length; i++) {
60             String JavaDoc currentIPRegExp = StringUtil.replace(blockedIPArray[i], '*', "(\\d{1,3})");
61             currentIPRegExp = "^" + currentIPRegExp + "$";
62             try {
63                 log.debug("currentIPRegExp = " + currentIPRegExp);
64                 blockedIPs[i] = new RE(currentIPRegExp);
65             } catch (Exception JavaDoc ex) {
66                 log.error("Cannot parse the regular expression = " + currentIPRegExp, ex);
67             }
68         }
69     }
70
71     /**
72      * Filter the IP
73      * @param request
74      * @return true if the IP in this request is ok
75      * false if the IP in this request is blocked
76      */

77     public static boolean filter(HttpServletRequest JavaDoc request) {
78         if (request == null) {
79             throw new NullPointerException JavaDoc("Cannot accept a null request in IPFilter");
80         }
81         String JavaDoc checkIP = request.getRemoteAddr();
82
83         for (int i = 0; i < blockedIPs.length; i++) {
84             RE currentBlockedIP = blockedIPs[i];
85             if (currentBlockedIP != null) {
86                 synchronized (currentBlockedIP) {
87                     if (currentBlockedIP.match(checkIP)) {
88                         return false;
89                     }
90                 }
91             }
92         }
93         return true;
94     }
95 }
96
Popular Tags