020605dc074e0b4bef9a2bc49e2547886b91cc71
[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 gnu.regexp.RE;\r
34 \r
35 import java.util.Arrays;\r
36 import java.util.Iterator;\r
37 import java.util.List;\r
38 \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
43 \r
44 \r
45 /**\r
46  * <p>Title: </p>\r
47  * <p>Description: </p>\r
48  * <p>Copyright: Copyright (c) 2003</p>\r
49  * <p>Company: </p>\r
50  * @author not attributable\r
51  * @version 1.0\r
52  */\r
53 \r
54 public class MirBasicAntiAbuseFilterTypes {\r
55   private MirBasicAntiAbuseFilterTypes() {\r
56   }\r
57 \r
58   public static abstract class BasicFilterType implements MirAntiAbuseFilterType {\r
59     private String name;\r
60 \r
61     public BasicFilterType(String aName) {\r
62       name = aName;\r
63     }\r
64 \r
65     public String getName() {\r
66       return name;\r
67     }\r
68   }\r
69 \r
70   /**\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
72    *\r
73    * <p>Title: </p>\r
74    * <p>Description: </p>\r
75    * <p>Copyright: Copyright (c) 2003</p>\r
76    * <p>Company: </p>\r
77    * @author not attributable\r
78    * @version 1.0\r
79    */\r
80 \r
81   public static class IPFilter extends BasicFilterType {\r
82     public IPFilter(String aName) {\r
83       super(aName);\r
84     }\r
85 \r
86     public boolean validate(String anExpression) {\r
87       try {\r
88         InternetFunctions.isIpAddressInNetwork("1.1.1.1", anExpression);\r
89         return true;\r
90       }\r
91       catch (Throwable t) {\r
92         return false;\r
93       }\r
94     };\r
95 \r
96     public boolean test(String anExpression, Entity anEntity, Request aRequest) {\r
97       try {\r
98         return InternetFunctions.isIpAddressInNetwork(aRequest.getHeader("ip"), anExpression);\r
99       }\r
100       catch (Throwable t) {\r
101         return false;\r
102       }\r
103     };\r
104   }\r
105 \r
106   /**\r
107    * A regular expression filter.\r
108    *\r
109    * <p>Title: </p>\r
110    * <p>Description: </p>\r
111    * <p>Copyright: Copyright (c) 2003</p>\r
112    * <p>Company: </p>\r
113    * @author not attributable\r
114    * @version 1.0\r
115    */\r
116 \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
122 \r
123     public static final int ENTITY_FIELDS = 0;\r
124     public static final int REQUEST_HEADERS = 2;\r
125 \r
126     public RegularExpressionFilter(String aName) {\r
127       this(aName, false, false, null);\r
128     }\r
129 \r
130     public RegularExpressionFilter(String aName, boolean aCaseSensitive, boolean anExactMatch, String[] aSelectedFields) {\r
131       this (aName, aCaseSensitive, anExactMatch, ENTITY_FIELDS, aSelectedFields);\r
132     }\r
133 \r
134     public RegularExpressionFilter(String aName, boolean aCaseSensitive, boolean anExactMatch, int aFieldKind, String[] aSelectedFields) {\r
135       super(aName);\r
136 \r
137       fieldKind = aFieldKind;\r
138 \r
139       caseSensitive = aCaseSensitive;\r
140       exactMatch = anExactMatch;\r
141       if (aSelectedFields==null)\r
142         selectedFields = null;\r
143       else\r
144         selectedFields = Arrays.asList(aSelectedFields);\r
145     }\r
146 \r
147     public boolean validate(String anExpression) {\r
148       try {\r
149         new RE(anExpression);\r
150         return true;\r
151       }\r
152       catch (Throwable t) {\r
153         return false;\r
154       }\r
155     };\r
156 \r
157     public boolean test(String anExpression, Entity anEntity, Request aRequest) {\r
158       try {\r
159         Iterator j;\r
160         int flags = 0;\r
161 \r
162         if (caseSensitive)\r
163           flags |= RE.REG_ICASE;\r
164 \r
165         RE regularExpression = new RE(anExpression, RE.REG_ICASE);\r
166 \r
167         switch (fieldKind) {\r
168           case REQUEST_HEADERS:\r
169             if (selectedFields != null) {\r
170               j = selectedFields.iterator();\r
171 \r
172               while (j.hasNext()) {\r
173                 String field = aRequest.getHeader( (String) j.next());\r
174 \r
175                 if (exactMatch) {\r
176                   if (field != null && regularExpression.isMatch(field)) {\r
177                     return true;\r
178                   }\r
179                 }\r
180                 else {\r
181                   if (field != null && regularExpression.getMatch(field) != null) {\r
182                     return true;\r
183                   }\r
184                 }\r
185               }\r
186             }\r
187             break;\r
188           case ENTITY_FIELDS:\r
189             if (selectedFields != null)\r
190               j = selectedFields.iterator();\r
191             else\r
192               j = anEntity.getFields().iterator();\r
193 \r
194             while (j.hasNext()) {\r
195               String field = anEntity.getValue( (String) j.next());\r
196 \r
197               if (exactMatch) {\r
198                 if (field != null && regularExpression.isMatch(field)) {\r
199                   return true;\r
200                 }\r
201               }\r
202               else {\r
203                 if (field != null && regularExpression.getMatch(field) != null) {\r
204                   return true;\r
205                 }\r
206               }\r
207             }\r
208         }\r
209       }\r
210       catch (Throwable t) {\r
211       }\r
212       return false;\r
213     }\r
214   }\r
215 \r
216 }