- public List getFilters() {
- List result = new Vector();
-
- synchronized (filterRules) {
- Iterator i = filterRules.iterator();
- while (i.hasNext()) {
- FilterRule filter = (FilterRule) i.next();
- result.add(filter.clone());
- }
- return result;
- }
- }
-
- public String addFilter(String aType, String anExpression, String aComments, String aCommentAction, String anArticleAction) {
- return addFilter(aType, anExpression, aComments, aCommentAction, anArticleAction, null);
- }
-
- public String addFilter(String aType, String anExpression, String aComments, String aCommentAction, String anArticleAction, Date aListHit) {
- return addFilter(filterRules, aType, anExpression, aComments, aCommentAction, anArticleAction, aListHit);
- }
-
- public FilterRule getFilter(String anId) {
- synchronized (filterRules) {
- FilterRule result = (FilterRule) findFilter(filterRules, anId);
- if (result == null)
- return result;
- else
- return (FilterRule) result.clone();
- }
- }
-
- public String setFilter(String anIdentifier, String aType, String anExpression, String aComments, String aCommentAction, String anArticleAction) {
- return setFilter(filterRules, anIdentifier, aType, anExpression, aComments, aCommentAction, anArticleAction);
- }
-
- public void deleteFilter(String anIdentifier) {
- deleteFilter(filterRules, anIdentifier);
- }
-
- public void moveFilterUp(String anIdentifier) {
- moveFilter(filterRules, anIdentifier, -1);
- }
-
- public void moveFilterDown(String anIdentifier) {
- moveFilter(filterRules, anIdentifier, 1);
- }
-
- private String addFilter(List aFilters, String aType, String anExpression, String aComments, String aCommentAction, String anArticleAction, Date aLastHit) {
- MirAntiAbuseFilterType type = (MirAntiAbuseFilterType) filterTypes.get(aType);
-
- if (type == null)
- return "invalidtype";
-
- if (!type.validate(anExpression)) {
- return "invalidexpression";
- }
-
- FilterRule filter = new FilterRule();
-
- filter.setId(generateId());
- filter.setExpression(anExpression);
- filter.setType(aType);
- filter.setComments(aComments);
- filter.setArticleAction(anArticleAction);
- filter.setCommentAction(aCommentAction);
- filter.setLastHit(aLastHit);
-
- synchronized (aFilters) {
- aFilters.add(filter);
- }
-
- return null;
- }
-
- private String setFilter(List aFilters, String anIdentifier, String aType, String anExpression, String aComments, String aCommentAction, String anArticleAction) {
- MirAntiAbuseFilterType type = (MirAntiAbuseFilterType) filterTypes.get(aType);
-
- if (type == null)
- return "invalidtype";
-
- if (!type.validate(anExpression)) {
- return "invalidexpression";
- }
-
- synchronized (aFilters) {
- FilterRule filter = findFilter(aFilters, anIdentifier);
-
- if (filter != null) {
- filter.setExpression(anExpression);
- filter.setType(aType);
- filter.setCommentAction(aCommentAction);
- filter.setArticleAction(anArticleAction);
- filter.setComments(aComments);
- }
-
- return null;
- }
- }
-
- private FilterRule findFilter(List aFilters, String anIdentifier) {
- synchronized (aFilters) {
- Iterator i = aFilters.iterator();
- while (i.hasNext()) {
- FilterRule filter = (FilterRule) i.next();
-
- if (filter.getId().equals(anIdentifier)) {
- return filter;
- }
- }
- }
-
- return null;
- }
-
- private void moveFilter(List aFilters, String anIdentifier, int aDirection) {
- synchronized (aFilters) {
- for (int i = 0; i < aFilters.size(); i++) {
- FilterRule rule = (FilterRule) aFilters.get(i);
-
- if (rule.getId().equals(anIdentifier) && (i + aDirection >= 0) && (i + aDirection < aFilters.size())) {
- aFilters.remove(rule);
- aFilters.add(i + aDirection, rule);
- break;
- }
- }
- }
- }
-
- private void deleteFilter(List aFilters, String anIdentifier) {
- synchronized (aFilters) {
- FilterRule filter = findFilter(aFilters, anIdentifier);
-
- if (filter != null) {
- aFilters.remove(filter);
- }
- }
- }
-
- private String generateId() {
- synchronized (this) {
- maxIdentifier = maxIdentifier + 1;
-
- return Integer.toString(maxIdentifier);
- }
- }
-
- public class FilterRule {
- private String identifier;
- private String expression;
- private String type;
- private String comments;
- private String articleAction;
- private String commentAction;
- private Date lastHit;
-
- public FilterRule() {
- expression = "";
- type = "";
- identifier = "";
- comments = "";
- articleAction = articleBlockAction;
- commentAction = commentBlockAction;
- lastHit = null;
- }
-
- public Date getLastHit() {
- return lastHit;
- }
-
- public void setLastHit(Date aDate) {
- lastHit = aDate;
- }
-
- public String getId() {
- return identifier;
- }
-
- public void setId(String anId) {
- identifier = anId;
- }
-
- public String getExpression() {
- return expression;
- }
-
- public void setExpression(String anExpression) {
- expression = anExpression;
- }
-
- public String getType() {
- return type;
- }
-
- public void setType(String aType) {
- type = aType;
- }
-
- public void setComments(String aComments) {
- comments = aComments;
- }
-
- public String getComments() {
- return comments;
- }
-
- public String getArticleAction() {
- return articleAction;
- }
-
- public void setArticleAction(String anArticleAction) {
- articleAction = anArticleAction;
- }
-
- public String getCommentAction() {
- return commentAction;
- }
-
- public void setCommentAction(String aCommentAction) {
- commentAction = aCommentAction;
- }
-
- public boolean test(Entity anEntity, Request aRequest) {
- MirAntiAbuseFilterType filterType = (MirAntiAbuseFilterType) filterTypes.get(type);
- try {
- if (filterType != null)
- return filterType.test(expression, anEntity, aRequest);
- }
- catch (Throwable t) {
- logger.error("error while testing " + type + "-filter '" + expression + "'");
- }
-
- return false;
- };
-
- public Object clone() {
- FilterRule result = new FilterRule();
- result.setComments(getComments());
- result.setExpression(getExpression());
- result.setId(getId());
- result.setType(getType());
- result.setArticleAction(getArticleAction());
- result.setCommentAction(getCommentAction());
- result.setLastHit(getLastHit());
-
- return result;
- }