e7ae73d3e8a5dbbe2a052fcb59fad2ac50f998fe
[mir.git] / source / mircoders / localizer / basic / MirBasicAntiAbuseFilterTypes.java
1 /*\r
2  * Copyright (C) 2001, 2002 The Mir-coders group\r
3  *\r
4  * This file is part of Mir.\r
5  *\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
10  *\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
15  *\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
19  *\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
29  */\r
30 \r
31 package mircoders.localizer.basic;\r
32 \r
33 import java.util.*;\r
34 \r
35 import gnu.regexp.*;\r
36 \r
37 import mir.entity.Entity;\r
38 import mir.session.Request;\r
39 import mir.util.InternetFunctions;\r
40 import mircoders.localizer.MirAntiAbuseFilterType;\r
41 \r
42 \r
43 /**\r
44  * <p>Title: </p>\r
45  * <p>Description: </p>\r
46  * <p>Copyright: Copyright (c) 2003</p>\r
47  * <p>Company: </p>\r
48  * @author not attributable\r
49  * @version 1.0\r
50  */\r
51 \r
52 public class MirBasicAntiAbuseFilterTypes {\r
53   private MirBasicAntiAbuseFilterTypes() {\r
54   }\r
55 \r
56   public static abstract class BasicFilterType implements MirAntiAbuseFilterType {\r
57     private String name;\r
58 \r
59     public BasicFilterType(String aName) {\r
60       name = aName;\r
61     }\r
62 \r
63     public String getName() {\r
64       return name;\r
65     }\r
66   }\r
67 \r
68   /**\r
69    * 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
70    *\r
71    * <p>Title: </p>\r
72    * <p>Description: </p>\r
73    * <p>Copyright: Copyright (c) 2003</p>\r
74    * <p>Company: </p>\r
75    * @author not attributable\r
76    * @version 1.0\r
77    */\r
78 \r
79   public static class IPFilter extends BasicFilterType {\r
80     public IPFilter(String aName) {\r
81       super(aName);\r
82     }\r
83 \r
84     public boolean validate(String anExpression) {\r
85       try {\r
86         InternetFunctions.isIpAddressInNetwork("1.1.1.1", anExpression);\r
87         return true;\r
88       }\r
89       catch (Throwable t) {\r
90         return false;\r
91       }\r
92     };\r
93 \r
94     public boolean test(String anExpression, Entity anEntity, Request aRequest) {\r
95       try {\r
96         return InternetFunctions.isIpAddressInNetwork(aRequest.getHeader("ip"), anExpression);\r
97       }\r
98       catch (Throwable t) {\r
99         return false;\r
100       }\r
101     };\r
102   }\r
103 \r
104   /**\r
105    * A regular expression filter.\r
106    *\r
107    * <p>Title: </p>\r
108    * <p>Description: </p>\r
109    * <p>Copyright: Copyright (c) 2003</p>\r
110    * <p>Company: </p>\r
111    * @author not attributable\r
112    * @version 1.0\r
113    */\r
114 \r
115   public static class RegularExpressionFilter extends BasicFilterType {\r
116     private boolean exactMatch;\r
117     private boolean caseSensitive;\r
118     private List selectedFields;\r
119 \r
120     public RegularExpressionFilter(String aName) {\r
121       this(aName, false, false, null);\r
122     }\r
123 \r
124     public RegularExpressionFilter(String aName, boolean aCaseSensitive, boolean anExactMatch, String[] aSelectedFields) {\r
125       super(aName);\r
126 \r
127       caseSensitive = aCaseSensitive;\r
128       exactMatch = anExactMatch;\r
129       if (aSelectedFields==null)\r
130         selectedFields = null;\r
131       else\r
132         selectedFields = Arrays.asList(aSelectedFields);\r
133     }\r
134 \r
135     public boolean validate(String anExpression) {\r
136       try {\r
137         new RE(anExpression);\r
138         return true;\r
139       }\r
140       catch (Throwable t) {\r
141         return false;\r
142       }\r
143     };\r
144 \r
145     public boolean test(String anExpression, Entity anEntity, Request aRequest) {\r
146       try {\r
147         Iterator j;\r
148         int flags = 0;\r
149 \r
150         if (caseSensitive)\r
151           flags |= RE.REG_ICASE;\r
152 \r
153         RE regularExpression = new RE(anExpression, RE.REG_ICASE);\r
154 \r
155         if (selectedFields!=null)\r
156           j = selectedFields.iterator();\r
157         else\r
158           j = anEntity.getFields().iterator();\r
159 \r
160         while (j.hasNext()) {\r
161           String field = anEntity.getValue( (String) j.next());\r
162 \r
163           if (exactMatch) {\r
164             if (field != null && regularExpression.isMatch(field)) {\r
165               return true;\r
166             }\r
167           }\r
168           else {\r
169             if (field != null && regularExpression.getMatch(field) != null) {\r
170               return true;\r
171             }\r
172           }\r
173         }\r
174       }\r
175       catch (Throwable t) {\r
176       }\r
177       return false;\r
178     }\r
179   }\r
180 }