1.1 restoration
[mir.git] / source / mircoders / localizer / basic / MirBasicAntiAbuseFilterTypes.java
1 /*
2  * Copyright (C) 2001, 2002 The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
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.
10  *
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.
15  *
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
19  *
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.
29  */
30
31 package mircoders.localizer.basic;
32
33 import gnu.regexp.RE;
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;
39
40 import java.util.Arrays;
41 import java.util.Iterator;
42 import java.util.List;
43
44
45 /**
46  * Wrapper around a collection of common filter classes for Mir's
47  * anti-abuse filtering system.
48  */
49 public class MirBasicAntiAbuseFilterTypes {
50   protected static final LoggerWrapper logger = new LoggerWrapper("Localizer.ProducerAssistant");
51
52   private MirBasicAntiAbuseFilterTypes() {
53   }
54
55   public static abstract class BasicFilterType implements MirAntiAbuseFilterType {
56     private String name;
57
58     public BasicFilterType(String aName) {
59       name = aName;
60     }
61
62     public String getName() {
63       return name;
64     }
65   }
66
67   /**
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.
69    */
70   public static class IPFilter extends BasicFilterType {
71     /**
72      *
73      */
74     public IPFilter(String aName) {
75       super(aName);
76     }
77
78     /**
79      *
80      */
81     public boolean validate(String anExpression) {
82       try {
83         InternetFunctions.isIpAddressInNetwork("1.1.1.1", anExpression);
84         return true;
85       }
86       catch (Throwable t) {
87         return false;
88       }
89     };
90
91     /**
92      *
93      */
94     public boolean test(String anExpression, Entity anEntity, Request aRequest) {
95       try {
96         return InternetFunctions.isIpAddressInNetwork(aRequest.getHeader("ip"), anExpression);
97       }
98       catch (Throwable t) {
99         return false;
100       }
101     };
102   }
103
104   /**
105    * A regular expression filter.
106    */
107   public static class RegularExpressionFilter extends BasicFilterType {
108     private boolean exactMatch;
109     private boolean caseSensitive;
110     private int fieldKind;
111     private List selectedFields;
112
113     public static final int ENTITY_FIELDS = 0;
114     public static final int REQUEST_HEADERS = 2;
115
116     public RegularExpressionFilter(String aName) {
117       this(aName, false, false, null);
118     }
119
120     public RegularExpressionFilter(String aName, boolean aCaseSensitive, boolean anExactMatch, String[] aSelectedFields) {
121       this (aName, aCaseSensitive, anExactMatch, ENTITY_FIELDS, aSelectedFields);
122     }
123
124     public RegularExpressionFilter(String aName, boolean aCaseSensitive, boolean anExactMatch, int aFieldKind, String[] aSelectedFields) {
125       super(aName);
126
127       fieldKind = aFieldKind;
128
129       caseSensitive = aCaseSensitive;
130       exactMatch = anExactMatch;
131       if (aSelectedFields==null)
132         selectedFields = null;
133       else
134         selectedFields = Arrays.asList(aSelectedFields);
135     }
136
137     public boolean validate(String anExpression) {
138       try {
139         new RE(anExpression);
140         return true;
141       }
142       catch (Throwable t) {
143         return false;
144       }
145     };
146
147     public boolean test(String anExpression, Entity anEntity, Request aRequest) {
148       try {
149         Iterator j;
150         int flags = 0;
151
152         if (!caseSensitive) {
153           flags |= RE.REG_ICASE;
154         }
155
156         RE regularExpression = new RE(anExpression, flags);
157
158         switch (fieldKind) {
159           case REQUEST_HEADERS:
160             if (selectedFields != null) {
161               j = selectedFields.iterator();
162
163               while (j.hasNext()) {
164                 String fieldName = (String) j.next();
165                 String field = aRequest.getHeader(fieldName);
166
167                 if (exactMatch) {
168                   if (field != null && regularExpression.isMatch(field)) {
169                     return true;
170                   }
171                 }
172                 else {
173                   if (field != null && regularExpression.getMatch(field) != null) {
174                     return true;
175                   }
176                 }
177               }
178             }
179             break;
180           case ENTITY_FIELDS:
181             if (selectedFields != null)
182               j = selectedFields.iterator();
183             else
184               j = anEntity.getFieldNames().iterator();
185
186             while (j.hasNext()) {
187               String field = anEntity.getFieldValue( (String) j.next());
188
189               if (exactMatch) {
190                 if (field != null && regularExpression.isMatch(field)) {
191                   return true;
192                 }
193               }
194               else {
195                 if (field != null && regularExpression.getMatch(field) != null) {
196                   return true;
197                 }
198               }
199             }
200         }
201       }
202       catch (Throwable t) {
203       }
204       return false;
205     }
206   }
207
208 }