user agent filter now works with substrings
[mir.git] / source / mircoders / abuse / SubStringFilterType.java
1 package mircoders.abuse;\r
2 \r
3 import mir.entity.Entity;\r
4 import mir.session.Request;\r
5 \r
6 import java.util.Arrays;\r
7 import java.util.Iterator;\r
8 import java.util.List;\r
9 \r
10 /**\r
11   * Instances of this filter type will match if the filter expression is a substringo\r
12  *  of the value(s) tested agains\r
13   */\r
14  public class SubStringFilterType extends AbstractFilterType {\r
15     private boolean exactMatch;\r
16     private boolean caseSensitive;\r
17     private int fieldKind;\r
18     private List selectedFields;\r
19 \r
20     public static final int ENTITY_FIELDS = 0;\r
21     public static final int REQUEST_HEADERS = 2;\r
22 \r
23     public SubStringFilterType(String aName) {\r
24       this(aName, false, false, null);\r
25     }\r
26 \r
27     public SubStringFilterType(String aName, boolean aCaseSensitive, boolean anExactMatch, String[] aSelectedFields) {\r
28       this (aName, aCaseSensitive, anExactMatch, ENTITY_FIELDS, aSelectedFields);\r
29     }\r
30 \r
31     public SubStringFilterType(String aName, boolean aCaseSensitive, boolean anExactMatch, int aFieldKind, String[] aSelectedFields) {\r
32       super(aName);\r
33 \r
34       fieldKind = aFieldKind;\r
35 \r
36       caseSensitive = aCaseSensitive;\r
37       exactMatch = anExactMatch;\r
38       if (aSelectedFields==null) {\r
39         selectedFields = null;\r
40       }\r
41       else {\r
42         selectedFields = Arrays.asList(aSelectedFields);\r
43       }\r
44     }\r
45 \r
46 \r
47   public FilterInstance constructFilterInstance(final String anExpression) throws AbuseExc {\r
48     return new FilterInstance() {\r
49       public boolean test(Entity anEntity, Request aRequest) {\r
50         Iterator j;\r
51         String expression = anExpression;\r
52         if (!caseSensitive) {\r
53           expression = anExpression.toUpperCase();\r
54         }\r
55 \r
56         switch (fieldKind) {\r
57           case REQUEST_HEADERS:\r
58             if (selectedFields != null) {\r
59               j = selectedFields.iterator();\r
60 \r
61               while (j.hasNext()) {\r
62                 String fieldName = (String) j.next();\r
63                 String field = aRequest.getHeader(fieldName);\r
64                 if (test(field, expression)) {\r
65                   return true;\r
66                 }\r
67               }\r
68             }\r
69             break;\r
70 \r
71           case ENTITY_FIELDS:\r
72             if (selectedFields != null) {\r
73               j = selectedFields.iterator();\r
74             }\r
75             else {\r
76               j = anEntity.getFieldNames().iterator();\r
77             }\r
78 \r
79             while (j.hasNext()) {\r
80               String field = anEntity.getFieldValue( (String) j.next());\r
81 \r
82               if (test(field, expression)) {\r
83                 return true;\r
84               }\r
85             }\r
86         }\r
87         return false;\r
88       }\r
89 \r
90       private boolean test(String aValue, String anExpression) {\r
91         if (!caseSensitive) {\r
92           aValue=aValue.toUpperCase();\r
93         }\r
94 \r
95         if (exactMatch) {\r
96           if (aValue!= null && aValue.equals(anExpression)) {\r
97             return true;\r
98           }\r
99         }\r
100         else {\r
101           if (aValue!= null && aValue.indexOf(anExpression)>=0) {\r
102             return true;\r
103           }\r
104         }\r
105 \r
106         return false;\r
107       }\r
108 \r
109       public String status() {\r
110         return null;\r
111       }\r
112     };\r
113   }\r
114 }\r