2 * Copyright (C) 2001, 2002 The Mir-coders group
\r
4 * This file is part of Mir.
\r
6 * Mir is free software; you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation; either version 2 of the License, or
\r
9 * (at your option) any later version.
\r
11 * Mir is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with Mir; if not, write to the Free Software
\r
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 * In addition, as a special exception, The Mir-coders gives permission to link
\r
21 * the code of this program with any library licensed under the Apache Software License,
\r
22 * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
\r
23 * (or with modified versions of the above that use the same license as the above),
\r
24 * and distribute linked combinations including the two. You must obey the
\r
25 * GNU General Public License in all respects for all of the code used other than
\r
26 * the above mentioned libraries. If you modify this file, you may extend this
\r
27 * exception to your version of the file, but you are not obligated to do so.
\r
28 * If you do not wish to do so, delete this exception statement from your version.
\r
31 package mircoders.localizer.basic;
\r
33 import gnu.regexp.RE;
\r
35 import java.util.Arrays;
\r
36 import java.util.Iterator;
\r
37 import java.util.List;
\r
39 import mir.entity.Entity;
\r
40 import mir.session.Request;
\r
41 import mir.util.InternetFunctions;
\r
42 import mircoders.localizer.MirAntiAbuseFilterType;
\r
47 * <p>Description: </p>
\r
48 * <p>Copyright: Copyright (c) 2003</p>
\r
50 * @author not attributable
\r
54 public class MirBasicAntiAbuseFilterTypes {
\r
55 private MirBasicAntiAbuseFilterTypes() {
\r
58 public static abstract class BasicFilterType implements MirAntiAbuseFilterType {
\r
59 private String name;
\r
61 public BasicFilterType(String aName) {
\r
65 public String getName() {
\r
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.
\r
74 * <p>Description: </p>
\r
75 * <p>Copyright: Copyright (c) 2003</p>
\r
77 * @author not attributable
\r
81 public static class IPFilter extends BasicFilterType {
\r
82 public IPFilter(String aName) {
\r
86 public boolean validate(String anExpression) {
\r
88 InternetFunctions.isIpAddressInNetwork("1.1.1.1", anExpression);
\r
91 catch (Throwable t) {
\r
96 public boolean test(String anExpression, Entity anEntity, Request aRequest) {
\r
98 return InternetFunctions.isIpAddressInNetwork(aRequest.getHeader("ip"), anExpression);
\r
100 catch (Throwable t) {
\r
107 * A regular expression filter.
\r
110 * <p>Description: </p>
\r
111 * <p>Copyright: Copyright (c) 2003</p>
\r
113 * @author not attributable
\r
117 public static class RegularExpressionFilter extends BasicFilterType {
\r
118 private boolean exactMatch;
\r
119 private boolean caseSensitive;
\r
120 private int fieldKind;
\r
121 private List selectedFields;
\r
123 public static final int ENTITY_FIELDS = 0;
\r
124 public static final int REQUEST_HEADERS = 2;
\r
126 public RegularExpressionFilter(String aName) {
\r
127 this(aName, false, false, null);
\r
130 public RegularExpressionFilter(String aName, boolean aCaseSensitive, boolean anExactMatch, String[] aSelectedFields) {
\r
131 this (aName, aCaseSensitive, anExactMatch, ENTITY_FIELDS, aSelectedFields);
\r
134 public RegularExpressionFilter(String aName, boolean aCaseSensitive, boolean anExactMatch, int aFieldKind, String[] aSelectedFields) {
\r
137 fieldKind = aFieldKind;
\r
139 caseSensitive = aCaseSensitive;
\r
140 exactMatch = anExactMatch;
\r
141 if (aSelectedFields==null)
\r
142 selectedFields = null;
\r
144 selectedFields = Arrays.asList(aSelectedFields);
\r
147 public boolean validate(String anExpression) {
\r
149 new RE(anExpression);
\r
152 catch (Throwable t) {
\r
157 public boolean test(String anExpression, Entity anEntity, Request aRequest) {
\r
163 flags |= RE.REG_ICASE;
\r
165 RE regularExpression = new RE(anExpression, RE.REG_ICASE);
\r
167 switch (fieldKind) {
\r
168 case REQUEST_HEADERS:
\r
169 if (selectedFields != null) {
\r
170 j = selectedFields.iterator();
\r
172 while (j.hasNext()) {
\r
173 String field = aRequest.getHeader( (String) j.next());
\r
176 if (field != null && regularExpression.isMatch(field)) {
\r
181 if (field != null && regularExpression.getMatch(field) != null) {
\r
188 case ENTITY_FIELDS:
\r
189 if (selectedFields != null)
\r
190 j = selectedFields.iterator();
\r
192 j = anEntity.getFields().iterator();
\r
194 while (j.hasNext()) {
\r
195 String field = anEntity.getValue( (String) j.next());
\r
198 if (field != null && regularExpression.isMatch(field)) {
\r
203 if (field != null && regularExpression.getMatch(field) != null) {
\r
210 catch (Throwable t) {
\r