2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
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.
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.
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
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.
31 package mircoders.abuse;
33 import mir.entity.Entity;
34 import mir.entity.adapter.EntityAdapter;
35 import mir.entity.adapter.EntityAdapterModel;
36 import mir.entity.adapter.EntityIteratorAdapter;
37 import mir.log.LoggerWrapper;
38 import mir.session.Request;
39 import mir.storage.DatabaseExc;
40 import mir.storage.DatabaseHelper;
41 import mircoders.global.MirGlobal;
42 import mircoders.storage.DatabaseFilter;
43 import mircoders.storage.DatabaseFilterGroup;
45 import java.util.ArrayList;
46 import java.util.Collections;
47 import java.util.Date;
48 import java.util.HashMap;
49 import java.util.Iterator;
50 import java.util.List;
52 /** The FilterEngine manages a list of all filters and filter groups.
53 * Use the testPosting() method to apply all filters on an
54 * Entity (for ex. an article or a comment)
56 public class FilterEngine {
57 private final Map filterTypes = new HashMap();
58 private final List filterTypeIds = new ArrayList();
60 private final List filterGroups = new ArrayList();
61 private final Map idToFilterGroup = new HashMap();
62 private final LoggerWrapper logger = new LoggerWrapper("Global.Abuse.FilterEngine");
63 private EntityAdapterModel model;
65 public FilterEngine(EntityAdapterModel aModel) {
67 Iterator i = MirGlobal.localizer().openPostings().getAntiAbuseFilterTypes().iterator();
69 FilterType filterType = (FilterType) i.next();
71 filterTypes.put(filterType.getName(), filterType);
72 filterTypeIds.add(filterType.getName());
76 throw new RuntimeException(t.getMessage());
84 /** applies all filters from all filter groups to an Entity.
85 * The entity may be, for example, an article, or a comment.
86 * It returns a filter that matches if it finds one, null otherwise
88 public synchronized Filter testPosting(Entity anEntity, Request aRequest) {
89 Iterator i = filterGroups.iterator();
91 FilterGroup group = (FilterGroup) i.next();
93 Iterator j = group.getFilters().iterator();
95 Filter filter = (Filter) j.next();
97 if (filter.test(anEntity, aRequest)) {
101 catch (Throwable t) {
102 logger.warn("Exception thrown while testing filter " + filter.getType() + " ( " + filter.getExpression() + ") " + t.toString());
110 public List getFilterTypes() {
112 List result = new ArrayList();
114 Iterator i = filterTypeIds.iterator();
115 while (i.hasNext()) {
116 String id = (String) i.next();
118 Map action = new HashMap();
119 action.put("resource", id);
120 action.put("identifier", id);
127 catch (Throwable t) {
128 throw new RuntimeException("can't get article actions: " + t.getMessage());
131 /** This class reflects a row of the filter_group
132 * database table. Filters groups allow you to organize
133 * your filters. For example: group1=spammer ,
136 public class FilterGroup {
137 private List filters;
138 private Entity entity;
139 private Map idToFilter;
141 public FilterGroup(Entity anEntity) {
142 this (anEntity, Collections.EMPTY_LIST);
145 public FilterGroup(Entity anEntity, List aFilters) {
147 filters = new ArrayList();
148 idToFilter = new HashMap();
149 Iterator i = aFilters.iterator();
151 while (i.hasNext()) {
152 Entity entity = (Entity) i.next();
154 Filter filter = new Filter(entity);
155 introduceFilter(filter);
157 catch (Throwable e) {
158 logger.debug("Misbehaving filer: " + entity.toString() + ": " + e);
163 public Entity getEntity() {
167 public EntityAdapter getEntityAdapter() {
168 return model.makeEntityAdapter("filterGroup", entity);
171 public List getFilterEntityAdapterList() {
172 List result = new ArrayList();
174 Iterator i = filters.iterator();
175 while (i.hasNext()) {
176 Filter filter = (Filter) i.next();
177 result.add(filter.getEntityAdapter());
183 public List getFilters() {
187 public Filter getFilterForId(String anId) {
188 Filter result = (Filter) idToFilter.get(anId);
190 throw new NullPointerException("No such filter");
196 private void introduceFilter(Filter aFilter) {
197 filters.add(aFilter);
198 idToFilter.put(aFilter.getEntity().getId(), aFilter);
201 private void removeFilter(Filter aFilter) {
202 filters.remove(aFilter);
203 idToFilter.remove(aFilter.getEntity().getId());
206 private void deleteFilter(String anId) {
207 Filter filter = getFilterForId(anId);
208 removeFilter(filter);
209 DatabaseFilter.getInstance().delete(anId);
212 public void populateFilterEntity(Entity anEntity, String aType, String anExpression,
213 String aComments, String aTag, String anArticleAction,
214 String aCommentAction) {
216 anEntity.setFieldValue("type", aType);
217 anEntity.setFieldValue("expression", anExpression);
218 anEntity.setFieldValue("comment", aComments);
219 anEntity.setFieldValue("tag", aTag);
220 anEntity.setFieldValue("articleaction", anArticleAction);
221 anEntity.setFieldValue("commentaction", aCommentAction);
224 public String updateFilter(String anId, String aType, String anExpression,
225 String aComments, String aTag, String anArticleAction,
226 String aCommentAction) {
229 getFilterForId(anId).update(aType, anExpression, aComments, aTag,
230 anArticleAction, aCommentAction);
233 return e.getMessage();
239 public String createFilter(String aType, String anExpression,
240 String aComments, String aTag, String anArticleAction,
241 String aCommentAction) throws DatabaseExc {
242 FilterInstance instance;
245 instance = getFilterTypeForId(aType).constructFilterInstance(anExpression);
248 return e.getMessage();
251 Entity entity = DatabaseFilter.getInstance().createNewEntity();
252 populateFilterEntity(entity, aType, anExpression, aComments, aTag,
253 anArticleAction, aCommentAction);
256 String priority = "1";
258 if (filters.size() > 0) {
260 String lastPriorityString = ((Filter) filters.get(filters.size()-1)).getEntity().getFieldValue("priority");
261 int lastPriority = Integer.parseInt(lastPriorityString);
262 priority = Integer.toString(lastPriority + 1);
264 catch (Throwable e) {
268 entity.setFieldValue("priority", priority);
269 entity.setFieldValue("filter_group_id", getEntity().getId());
272 Filter filter = new Filter(entity, instance);
273 introduceFilter(filter);
278 public String moveFilterUp(String anId) {
279 Filter filter = getFilterForId(anId);
280 String priority = filter.getEntity().getFieldValue("priority");
281 int index = filters.indexOf(filter);
283 Filter filterBefore= (Filter) filters.remove(index-1);
284 filters.add(index, filterBefore);
285 filter.getEntity().setFieldValue("priority", filterBefore.getEntity().getFieldValue("priority"));
286 filterBefore.getEntity().setFieldValue("priority", priority);
287 filter.getEntity().update();
288 filterBefore.getEntity().update();
294 public String moveFilterToTop(String anId) {
295 Filter filter = getFilterForId(anId);
296 String priority = filter.getEntity().getFieldValue("priority");
297 int index = filters.indexOf(filter);
299 filters.remove(index);
300 Filter filterBefore= (Filter) filters.get(0);
301 filters.add(0, filter);
302 filter.getEntity().setFieldValue("priority", filterBefore.getEntity().getFieldValue("priority"));
303 filterBefore.getEntity().setFieldValue("priority", priority);
304 filter.getEntity().update();
305 filterBefore.getEntity().update();
311 public String moveFilterDown(String anId) {
312 Filter filter = getFilterForId(anId);
313 String priority = filter.getEntity().getFieldValue("priority");
314 int index = filters.indexOf(filter);
315 if (index<filters.size()-1) {
316 Filter filterAfter = (Filter) filters.remove(index+1);
317 filters.add(index, filterAfter);
318 filter.getEntity().setFieldValue("priority", filterAfter.getEntity().getFieldValue("priority"));
319 filterAfter.getEntity().setFieldValue("priority", priority);
320 filter.getEntity().update();
321 filterAfter.getEntity().update();
327 public String moveFilterToBottom(String anId) {
328 Filter filter = getFilterForId(anId);
329 String priority = filter.getEntity().getFieldValue("priority");
330 int index = filters.indexOf(filter);
331 if (index>=0 && index<filters.size()-1) {
332 filters.remove(index);
333 Filter filterBefore= (Filter) filters.get(filters.size()-1);
334 filters.add(filters.size(), filter);
335 filter.getEntity().setFieldValue("priority", filterBefore.getEntity().getFieldValue("priority"));
336 filterBefore.getEntity().setFieldValue("priority", priority);
337 filter.getEntity().update();
338 filterBefore.getEntity().update();
345 public String getName() {
346 return entity.getFieldValue("name");
350 /** This class reflects a row of the filter database table.
351 * To actually run a filter on data, use the test() method.
352 * This class will automatically retreive and use the correct
355 public class Filter {
356 private Entity entity;
357 private FilterInstance instance;
359 public Filter(Entity anEntity) throws AbuseExc {
360 this(anEntity, getFilterTypeForId(anEntity.getFieldValue("type")).constructFilterInstance(anEntity.getFieldValue("expression")));
363 public Filter(Entity anEntity, FilterInstance anInstance) {
365 instance = anInstance;
368 public Entity getEntity() {
372 public EntityAdapter getEntityAdapter() {
373 return model.makeEntityAdapter("filter", entity);
376 public void update(String aType, String anExpression, String aComments, String aTag,
377 String anArticleAction, String aCommentAction) throws AbuseExc {
379 instance = getFilterTypeForId(aType).constructFilterInstance(anExpression);
381 entity.setFieldValue("type", aType);
382 entity.setFieldValue("expression", anExpression);
383 entity.setFieldValue("tag", aType);
384 entity.setFieldValue("comment", aComments);
385 entity.setFieldValue("articleaction", anArticleAction);
386 entity.setFieldValue("commentaction", aCommentAction);
387 entity.setFieldValue("last_hit", null);
391 public void updateLastHit(Date aDate) {
392 entity.setFieldValue("last_hit",
393 DatabaseHelper.convertDateToInternalRepresenation(
394 new Date(System.currentTimeMillis())));
398 public String getType() {
399 return entity.getFieldValue("type");
402 public String getExpression() {
403 return entity.getFieldValue("expression");
406 public String getTag() {
407 return entity.getFieldValue("tag");
410 public String getComment() {
411 return entity.getFieldValue("comment");
414 public String getArticleAction() {
415 return entity.getFieldValue("articleaction");
418 public String getCommentAction() {
419 return entity.getFieldValue("commentaction");
422 public FilterInstance getInstance() {
426 public boolean test(Entity anEntity, Request aRequest) {
427 return instance.test(anEntity, aRequest);
431 public synchronized void reload() {
432 filterGroups.clear();
433 idToFilterGroup.clear();
436 Iterator i = new EntityIteratorAdapter("", "priority asc", 100, model, "filterGroup");
437 while (i.hasNext()) {
438 EntityAdapter entityAdapter = (EntityAdapter) i.next();
439 List filters = new ArrayList();
440 Iterator j = (Iterator) entityAdapter.getIterator("to_filters");
441 while (j.hasNext()) {
442 filters.add(((EntityAdapter) j.next()).getEntity());
445 FilterGroup filterGroup = new FilterGroup(entityAdapter.getEntity(), filters);
446 introduceFilterGroup(filterGroup);
449 catch (Throwable e) {
450 logger.error("Can't load filters: " + e.getMessage(), e);
454 public synchronized List getFilterGroups() {
455 List result = new ArrayList();
456 Iterator i = filterGroups.iterator();
457 while (i.hasNext()) {
458 result.add(((FilterGroup) i.next()).getEntityAdapter());
464 public synchronized void updateFilterGroup(String anId, String aName) {
465 FilterGroup filterGroup = getFilterGroupForId(anId);
466 filterGroup.getEntity().setFieldValue("name", aName);
467 filterGroup.getEntity().update();
470 public synchronized void addFilterGroup(String aName) throws DatabaseExc {
471 Entity entity = DatabaseFilterGroup.getInstance().createNewEntity();
472 entity.setFieldValue("name", aName);
474 String priority = "1";
476 if (filterGroups.size() > 0) {
478 String lastPriorityString = ((FilterGroup) filterGroups.get(filterGroups.size()-1)).getEntity().getFieldValue("priority");
479 int lastPriority = Integer.parseInt(lastPriorityString);
480 priority = Integer.toString(lastPriority + 1);
482 catch (Exception e) {
485 entity.setFieldValue("priority", priority);
488 FilterGroup filterGroup = new FilterGroup(entity);
489 introduceFilterGroup(filterGroup);
492 public synchronized void moveFilterGroupUp(String anId) {
493 FilterGroup group = (FilterGroup) idToFilterGroup.get(anId);
494 String priority = group.getEntity().getFieldValue("priority");
495 int index = filterGroups.indexOf(group);
497 FilterGroup groupBefore = (FilterGroup) filterGroups.remove(index-1);
498 filterGroups.add(index, groupBefore);
499 group.getEntity().setFieldValue("priority", groupBefore.getEntity().getFieldValue("priority"));
500 groupBefore.getEntity().setFieldValue("priority", priority);
501 group.getEntity().update();
502 groupBefore.getEntity().update();
506 public synchronized void moveFilterGroupDown(String anId) {
507 FilterGroup group = (FilterGroup) idToFilterGroup.get(anId);
508 String priority = group.getEntity().getFieldValue("priority");
509 int index = filterGroups.indexOf(group);
510 if (index<filterGroups.size()-1) {
511 FilterGroup groupAfter = (FilterGroup) filterGroups.remove(index+1);
512 filterGroups.add(index, groupAfter);
513 group.getEntity().setFieldValue("priority", groupAfter.getEntity().getFieldValue("priority"));
514 groupAfter.getEntity().setFieldValue("priority", priority);
515 group.getEntity().update();
516 groupAfter.getEntity().update();
520 public synchronized void deleteFilterGroup(String anId) {
522 FilterGroup filterGroup = getFilterGroupForId(anId);
523 removeFilterGroup(filterGroup);
524 DatabaseFilter.getInstance().deleteByWhereClause("filter_group_id = " + anId);
525 DatabaseFilterGroup.getInstance().delete(anId);
528 public synchronized void deleteFilter(String aGroupId, String anId) {
529 getFilterGroupForId(aGroupId).deleteFilter(anId);
533 public synchronized String updateFilter(String aGroupId, String anId,
534 String aType, String anExpression,
537 String anArticleAction,
538 String aCommentAction) {
539 return getFilterGroupForId(aGroupId).updateFilter(anId, aType,
540 anExpression, aComments, aTag, anArticleAction, aCommentAction);
543 public synchronized String addFilter(String aGroupId,
544 String aType, String anExpression,
547 String anArticleAction,
548 String aCommentAction) throws DatabaseExc {
549 return getFilterGroupForId(aGroupId).createFilter(aType, anExpression,
550 aComments, aTag, anArticleAction, aCommentAction);
553 public synchronized String moveFilterUp(String aGroupId, String anId) {
554 return getFilterGroupForId(aGroupId).moveFilterUp(anId);
557 public synchronized String moveFilterDown(String aGroupId, String anId) {
558 return getFilterGroupForId(aGroupId).moveFilterDown(anId);
561 public synchronized String moveFilterToTop(String aGroupId, String anId) {
562 return getFilterGroupForId(aGroupId).moveFilterToTop(anId);
565 public synchronized String moveFilterToBottom(String aGroupId, String anId) {
566 return getFilterGroupForId(aGroupId).moveFilterToBottom(anId);
571 public FilterGroup getFilterGroupForId(String anId) {
572 FilterGroup result = (FilterGroup) idToFilterGroup.get(anId);
573 if (result == null) {
574 throw new NullPointerException("No such filter group");
580 public Filter getFilterForId(String aGroupId, String anId) {
581 return getFilterGroupForId(aGroupId).getFilterForId(anId);
585 public List getFilters(String aFilterGroupId) {
586 return getFilterGroupForId(aFilterGroupId).getFilterEntityAdapterList();
589 private synchronized void introduceFilterGroup(FilterGroup aFilterGroup) {
590 filterGroups.add(aFilterGroup);
591 idToFilterGroup.put(aFilterGroup.getEntity().getId(), aFilterGroup);
594 private synchronized void removeFilterGroup(FilterGroup aFilterGroup) {
595 filterGroups.remove(aFilterGroup);
596 idToFilterGroup.remove(aFilterGroup.getEntity().getId());
599 private FilterType getFilterTypeForId(String anId) {
600 return (FilterType) filterTypes.get(anId);