001: /*
002: * $Header: /cvsroot/mvnforum/myvietnam/src/net/myvietnam/mvncore/service/impl/IPFilterServiceImplDefault.java,v 1.8 2007/01/15 10:31:19 dungbtm Exp $
003: * $Author: dungbtm $
004: * $Revision: 1.8 $
005: * $Date: 2007/01/15 10:31:19 $
006: *
007: * ====================================================================
008: *
009: * Copyright (C) 2002-2007 by MyVietnam.net
010: *
011: * All copyright notices regarding MyVietnam and MyVietnam CoreLib
012: * MUST remain intact in the scripts and source code.
013: *
014: * This library is free software; you can redistribute it and/or
015: * modify it under the terms of the GNU Lesser General Public
016: * License as published by the Free Software Foundation; either
017: * version 2.1 of the License, or (at your option) any later version.
018: *
019: * This library is distributed in the hope that it will be useful,
020: * but WITHOUT ANY WARRANTY; without even the implied warranty of
021: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
022: * Lesser General Public License for more details.
023: *
024: * You should have received a copy of the GNU Lesser General Public
025: * License along with this library; if not, write to the Free Software
026: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
027: *
028: * Correspondence and Marketing Questions can be sent to:
029: * info at MyVietnam net
030: *
031: * @author: Minh Nguyen
032: * @author: Mai Nguyen
033: */
034: package net.myvietnam.mvncore.service.impl;
035:
036: import javax.servlet.http.HttpServletRequest;
037:
038: import net.myvietnam.mvncore.MVNCoreConfig;
039: import net.myvietnam.mvncore.service.IPFilterService;
040: import net.myvietnam.mvncore.util.AssertionUtil;
041: import net.myvietnam.mvncore.util.StringUtil;
042: import org.apache.commons.logging.Log;
043: import org.apache.commons.logging.LogFactory;
044: import org.apache.regexp.RE;
045:
046: public final class IPFilterServiceImplDefault implements
047: IPFilterService {
048:
049: private static Log log = LogFactory
050: .getLog(IPFilterServiceImplDefault.class);
051:
052: private static RE[] blockedIPs = null;
053:
054: private static int count;
055:
056: public IPFilterServiceImplDefault() {
057: count++;
058: AssertionUtil.doAssert(count == 1,
059: "Assertion: Must have only one instance.");
060: }
061:
062: static {
063: //IPOptions ipOptions = new IPOptions();
064: //String[] blockedIPArray = StringUtil.getStringArray(ipOptions.blockedIP, ";");
065:
066: String[] blockedIPArray = StringUtil.getStringArray(
067: MVNCoreConfig.getBlockedIPs(), ";");
068: blockedIPs = new RE[blockedIPArray.length];
069: for (int i = 0; i < blockedIPArray.length; i++) {
070: String currentIPRegExp = StringUtil.replace(
071: blockedIPArray[i], '*', "(\\d{1,3})");
072: currentIPRegExp = "^" + currentIPRegExp + "$";
073: try {
074: log.debug("currentIPRegExp = " + currentIPRegExp);
075: blockedIPs[i] = new RE(currentIPRegExp);
076: } catch (Exception ex) {
077: log.error("Cannot parse the regular expression = "
078: + currentIPRegExp, ex);
079: }
080: }
081: }
082:
083: /**
084: * Filter the IP
085: * @param request
086: * @return true if the IP in this request is ok
087: * false if the IP in this request is blocked
088: */
089: public boolean filter(HttpServletRequest request) {
090: if (request == null) {
091: throw new NullPointerException(
092: "Cannot accept a null request in IPFilter");
093: }
094: String checkIP = request.getRemoteAddr();
095:
096: for (int i = 0; i < blockedIPs.length; i++) {
097: RE currentBlockedIP = blockedIPs[i];
098: if (currentBlockedIP != null) {
099: synchronized (currentBlockedIP) {
100: if (currentBlockedIP.match(checkIP)) {
101: return false;
102: }
103: }
104: }
105: }
106: return true;
107: }
108: }
|