+package mircoders.abuse;\r
+\r
+import mir.entity.Entity;\r
+import mir.session.Request;\r
+\r
+import java.util.Arrays;\r
+import java.util.Iterator;\r
+import java.util.List;\r
+\r
+/**\r
+ * Instances of this filter type will match if the filter expression is a substringo\r
+ * of the value(s) tested agains\r
+ */\r
+ public class SubStringFilterType extends AbstractFilterType {\r
+ private boolean exactMatch;\r
+ private boolean caseSensitive;\r
+ private int fieldKind;\r
+ private List selectedFields;\r
+\r
+ public static final int ENTITY_FIELDS = 0;\r
+ public static final int REQUEST_HEADERS = 2;\r
+\r
+ public SubStringFilterType(String aName) {\r
+ this(aName, false, false, null);\r
+ }\r
+\r
+ public SubStringFilterType(String aName, boolean aCaseSensitive, boolean anExactMatch, String[] aSelectedFields) {\r
+ this (aName, aCaseSensitive, anExactMatch, ENTITY_FIELDS, aSelectedFields);\r
+ }\r
+\r
+ public SubStringFilterType(String aName, boolean aCaseSensitive, boolean anExactMatch, int aFieldKind, String[] aSelectedFields) {\r
+ super(aName);\r
+\r
+ fieldKind = aFieldKind;\r
+\r
+ caseSensitive = aCaseSensitive;\r
+ exactMatch = anExactMatch;\r
+ if (aSelectedFields==null) {\r
+ selectedFields = null;\r
+ }\r
+ else {\r
+ selectedFields = Arrays.asList(aSelectedFields);\r
+ }\r
+ }\r
+\r
+\r
+ public FilterInstance constructFilterInstance(final String anExpression) throws AbuseExc {\r
+ return new FilterInstance() {\r
+ public boolean test(Entity anEntity, Request aRequest) {\r
+ Iterator j;\r
+ String expression = anExpression;\r
+ if (!caseSensitive) {\r
+ expression = anExpression.toUpperCase();\r
+ }\r
+\r
+ switch (fieldKind) {\r
+ case REQUEST_HEADERS:\r
+ if (selectedFields != null) {\r
+ j = selectedFields.iterator();\r
+\r
+ while (j.hasNext()) {\r
+ String fieldName = (String) j.next();\r
+ String field = aRequest.getHeader(fieldName);\r
+ if (test(field, expression)) {\r
+ return true;\r
+ }\r
+ }\r
+ }\r
+ break;\r
+\r
+ case ENTITY_FIELDS:\r
+ if (selectedFields != null) {\r
+ j = selectedFields.iterator();\r
+ }\r
+ else {\r
+ j = anEntity.getFieldNames().iterator();\r
+ }\r
+\r
+ while (j.hasNext()) {\r
+ String field = anEntity.getFieldValue( (String) j.next());\r
+\r
+ if (test(field, expression)) {\r
+ return true;\r
+ }\r
+ }\r
+ }\r
+ return false;\r
+ }\r
+\r
+ private boolean test(String aValue, String anExpression) {\r
+ if (!caseSensitive) {\r
+ aValue=aValue.toUpperCase();\r
+ }\r
+\r
+ if (exactMatch) {\r
+ if (aValue!= null && aValue.equals(anExpression)) {\r
+ return true;\r
+ }\r
+ }\r
+ else {\r
+ if (aValue!= null && aValue.indexOf(anExpression)>=0) {\r
+ return true;\r
+ }\r
+ }\r
+\r
+ return false;\r
+ }\r
+\r
+ public String status() {\r
+ return null;\r
+ }\r
+ };\r
+ }\r
+}\r