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