2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
6 * Mir is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Mir is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Mir; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * In addition, as a special exception, The Mir-coders gives permission to link
21 * the code of this program with any library licensed under the Apache Software License,
22 * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23 * (or with modified versions of the above that use the same license as the above),
24 * and distribute linked combinations including the two. You must obey the
25 * GNU General Public License in all respects for all of the code used other than
26 * the above mentioned libraries. If you modify this file, you may extend this
27 * exception to your version of the file, but you are not obligated to do so.
28 * If you do not wish to do so, delete this exception statement from your version.
31 package mircoders.localizer.basic;
34 import mir.entity.Entity;
35 import mir.log.LoggerWrapper;
36 import mir.session.Request;
37 import mir.util.InternetFunctions;
38 import mircoders.localizer.MirAntiAbuseFilterType;
40 import java.util.Arrays;
41 import java.util.Iterator;
42 import java.util.List;
46 * Wrapper around a collection of common filter classes for Mir's
47 * anti-abuse filtering system.
49 public class MirBasicAntiAbuseFilterTypes {
50 protected static final LoggerWrapper logger = new LoggerWrapper("Localizer.ProducerAssistant");
52 private MirBasicAntiAbuseFilterTypes() {
55 public static abstract class BasicFilterType implements MirAntiAbuseFilterType {
58 public BasicFilterType(String aName) {
62 public String getName() {
68 * A basic ip filter. Supports x.x.x.x, x.x.x.x/x and x.x.x.x/x.x.x.x expressions.
70 public static class IPFilter extends BasicFilterType {
74 public IPFilter(String aName) {
81 public boolean validate(String anExpression) {
83 InternetFunctions.isIpAddressInNetwork("1.1.1.1", anExpression);
94 public boolean test(String anExpression, Entity anEntity, Request aRequest) {
96 return InternetFunctions.isIpAddressInNetwork(aRequest.getHeader("ip"), anExpression);
105 * A regular expression filter.
107 public static class RegularExpressionFilter extends BasicFilterType {
108 private boolean exactMatch;
109 private boolean caseSensitive;
110 private int fieldKind;
111 private List selectedFields;
113 public static final int ENTITY_FIELDS = 0;
114 public static final int REQUEST_HEADERS = 2;
116 public RegularExpressionFilter(String aName) {
117 this(aName, false, false, null);
120 public RegularExpressionFilter(String aName, boolean aCaseSensitive, boolean anExactMatch, String[] aSelectedFields) {
121 this (aName, aCaseSensitive, anExactMatch, ENTITY_FIELDS, aSelectedFields);
124 public RegularExpressionFilter(String aName, boolean aCaseSensitive, boolean anExactMatch, int aFieldKind, String[] aSelectedFields) {
127 fieldKind = aFieldKind;
129 caseSensitive = aCaseSensitive;
130 exactMatch = anExactMatch;
131 if (aSelectedFields==null)
132 selectedFields = null;
134 selectedFields = Arrays.asList(aSelectedFields);
137 public boolean validate(String anExpression) {
139 new RE(anExpression);
142 catch (Throwable t) {
147 public boolean test(String anExpression, Entity anEntity, Request aRequest) {
152 if (!caseSensitive) {
153 flags |= RE.REG_ICASE;
156 RE regularExpression = new RE(anExpression, flags);
159 case REQUEST_HEADERS:
160 if (selectedFields != null) {
161 j = selectedFields.iterator();
163 while (j.hasNext()) {
164 String fieldName = (String) j.next();
165 String field = aRequest.getHeader(fieldName);
168 if (field != null && regularExpression.isMatch(field)) {
173 if (field != null && regularExpression.getMatch(field) != null) {
181 if (selectedFields != null)
182 j = selectedFields.iterator();
184 j = anEntity.getFieldNames().iterator();
186 while (j.hasNext()) {
187 String field = anEntity.getFieldValue( (String) j.next());
190 if (field != null && regularExpression.isMatch(field)) {
195 if (field != null && regularExpression.getMatch(field) != null) {
202 catch (Throwable t) {