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;
35 import java.util.Arrays;
36 import java.util.Iterator;
37 import java.util.List;
39 import mir.entity.Entity;
40 import mir.session.Request;
41 import mir.util.InternetFunctions;
42 import mircoders.localizer.MirAntiAbuseFilterType;
47 * <p>Description: </p>
48 * <p>Copyright: Copyright (c) 2003</p>
50 * @author not attributable
54 public class MirBasicAntiAbuseFilterTypes {
55 private MirBasicAntiAbuseFilterTypes() {
58 public static abstract class BasicFilterType implements MirAntiAbuseFilterType {
61 public BasicFilterType(String aName) {
65 public String getName() {
71 * A basic ip filter. Supports x.x.x.x, x.x.x.x/x and x.x.x.x/x.x.x.x expressions.
74 * <p>Description: </p>
75 * <p>Copyright: Copyright (c) 2003</p>
77 * @author not attributable
81 public static class IPFilter extends BasicFilterType {
82 public IPFilter(String aName) {
86 public boolean validate(String anExpression) {
88 InternetFunctions.isIpAddressInNetwork("1.1.1.1", anExpression);
96 public boolean test(String anExpression, Entity anEntity, Request aRequest) {
98 return InternetFunctions.isIpAddressInNetwork(aRequest.getHeader("ip"), anExpression);
100 catch (Throwable t) {
107 * A regular expression filter.
110 * <p>Description: </p>
111 * <p>Copyright: Copyright (c) 2003</p>
113 * @author not attributable
117 public static class RegularExpressionFilter extends BasicFilterType {
118 private boolean exactMatch;
119 private boolean caseSensitive;
120 private int fieldKind;
121 private List selectedFields;
123 public static final int ENTITY_FIELDS = 0;
124 public static final int REQUEST_HEADERS = 2;
126 public RegularExpressionFilter(String aName) {
127 this(aName, false, false, null);
130 public RegularExpressionFilter(String aName, boolean aCaseSensitive, boolean anExactMatch, String[] aSelectedFields) {
131 this (aName, aCaseSensitive, anExactMatch, ENTITY_FIELDS, aSelectedFields);
134 public RegularExpressionFilter(String aName, boolean aCaseSensitive, boolean anExactMatch, int aFieldKind, String[] aSelectedFields) {
137 fieldKind = aFieldKind;
139 caseSensitive = aCaseSensitive;
140 exactMatch = anExactMatch;
141 if (aSelectedFields==null)
142 selectedFields = null;
144 selectedFields = Arrays.asList(aSelectedFields);
147 public boolean validate(String anExpression) {
149 new RE(anExpression);
152 catch (Throwable t) {
157 public boolean test(String anExpression, Entity anEntity, Request aRequest) {
163 // TODO jlint says this operator is useless
164 flags |= RE.REG_ICASE;
166 RE regularExpression = new RE(anExpression, RE.REG_ICASE);
169 case REQUEST_HEADERS:
170 if (selectedFields != null) {
171 j = selectedFields.iterator();
173 while (j.hasNext()) {
174 String field = aRequest.getHeader( (String) j.next());
177 if (field != null && regularExpression.isMatch(field)) {
182 if (field != null && regularExpression.getMatch(field) != null) {
190 if (selectedFields != null)
191 j = selectedFields.iterator();
193 j = anEntity.getFields().iterator();
195 while (j.hasNext()) {
196 String field = anEntity.getValue( (String) j.next());
199 if (field != null && regularExpression.isMatch(field)) {
204 if (field != null && regularExpression.getMatch(field) != null) {
211 catch (Throwable t) {