- 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 = 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);
- }
-
- public void moveFilterToTop(String anIdentifier) {
- setFilterPosition(filterRules, anIdentifier, 0);
- }
-
- public void moveFilterToBottom(String anIdentifier) {
- setFilterPosition(filterRules, anIdentifier, Integer.MAX_VALUE);
- }
-
- 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 setFilterPosition(List aFilters, String anIdentifier, int aPosition) {
- synchronized (aFilters) {
- if (aPosition<0)
- aPosition=0;
-
- for (int i = 0; i < aFilters.size(); i++) {
- FilterRule rule = (FilterRule) aFilters.get(i);
-
- if (rule.getId().equals(anIdentifier)) {
- aFilters.remove(rule);
-
- if (aPosition<aFilters.size())
- aFilters.add(aPosition, rule);
- else
- aFilters.add(rule);
- break;
- }
- }
- }
- }
-
- 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;
- }
- }
-
- private String escapeFilterPart(String aFilterPart) {
- return StringRoutines.replaceStringCharacters(aFilterPart,
- new char[] {'\\', ':', '\n', '\r', '\t', ' '},
- new String[] {"\\\\", "\\:", "\\n", "\\r", "\\t", "\\ "});
- }
-
- private String deescapeFilterPart(String aFilterPart) {
- return StringRoutines.replaceEscapedStringCharacters(aFilterPart,
- '\\',
- new char[] {'\\', ':', 'n', 'r', 't', ' '},
- new String[] {"\\", ":", "\n", "\r", "\t", " "});
- }
-
- private void setFilterConfig(List aFilters, String aConfigKey, ExtendedProperties aConfiguration) {
- synchronized (aFilters) {
- Iterator i = aFilters.iterator();
-
- while (i.hasNext()) {
- FilterRule filter = (FilterRule) i.next();
-
- String filterconfig =
- escapeFilterPart(filter.getType()) + ":" +
- escapeFilterPart(filter.getExpression()) + ":" +
- escapeFilterPart(filter.getArticleAction()) + ":" +
- escapeFilterPart(filter.getCommentAction()) + ":" +
- escapeFilterPart(filter.getComments()) + ":";
-
- if (filter.getLastHit() != null)
- filterconfig = filterconfig + filter.getLastHit().getTime();
-
- aConfiguration.addProperty(aConfigKey, filterconfig);
- }
- }
- }
-
- private void getFilterConfig(List aFilters, String aConfigKey, ExtendedProperties aConfiguration) {
- synchronized (aFilters) {
- aFilters.clear();
-
- if (aConfiguration.getStringArray(aConfigKey) != null) {
-
- Iterator i = Arrays.asList(aConfiguration.getStringArray(aConfigKey)).
- iterator();
-
- while (i.hasNext()) {
- String filter = (String) i.next();
- List parts = StringRoutines.splitStringWithEscape(filter, ':', '\\');
- if (parts.size() == 2) {
- parts.add(articleBlockAction);
- parts.add(commentBlockAction);
- parts.add("");
- parts.add("");
- }
-
- if (parts.size() >= 5) {
- Date lastHit = null;
-
- if (parts.size() >= 6) {
- String lastHitString = (String) parts.get(5);
-
- try {
- lastHit = new Date(Long.parseLong(lastHitString));
- }
- catch (Throwable t) {
- }
- }
-
- addFilter(deescapeFilterPart( (String) parts.get(0)),
- deescapeFilterPart( (String) parts.get(1)),
- deescapeFilterPart( (String) parts.get(4)),
- deescapeFilterPart( (String) parts.get(3)),
- deescapeFilterPart( (String) parts.get(2)), lastHit);
- }
- }
- }
- }
- }
-