Sample Java spam filter implementation
This sample spam filter in Java rejects comments if they contain one of the configured strings.
package com.example;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.InputSource;
import com.tridion.ambientdata.claimstore.ClaimStore;
import com.tridion.configuration.Configuration;
import com.tridion.configuration.ConfigurationException;
import com.tridion.configuration.XMLConfigurationReader;
import com.tridion.storage.ugc.SpamFilter;
import com.tridion.storage.ugc.SpamFilterException;
import com.tridion.ugc.taglib.model.Comment;
import com.tridion.util.StringUtils;
public class WordsSpamFilter implements SpamFilter {
// Read the configuration items (that is, blacklisted words) into a list.
final List<String> spamWords = new ArrayList<String>();
public void configure(Configuration configuration) throws ConfigurationException {
if (configuration != null) {
final List<Configuration> childrenConfigs = configuration.getChildrenByName("exclude");
for (Configuration word : childrenConfigs) {
spamWords.add(word.getContent());
}
}
}
// Check for the presence of any or all of the configured words in the submitted comment.
public Comment validateComment(final ClaimStore claimStore, final Comment comment) throws SpamFilterException {
final String commentContent = comment.getContent();
if (StringUtils.isEmpty(commentContent)) {
throw new SpamFilterException("Comment cannot be empty.");
} else {
for (String spamWord : spamWords) {
if (commentContent.contains(spamWord)) {
throw new SpamFilterException("Comment rejected: contains the word '" + spamWord + "'");
}
}
// The comment contains none of the configured words, so it is acceptable.
return comment;
}
}
}
The accompanying configuration fragment found in the UGC configuration could, for example, be as follows:
<SpamFilter Implementation="com.example.WordsSpamFilter">
<exclude>poker</exclude>
<exclude>enlargement</exclude>
<exclude>NOW!</exclude>
</SpamFilter>
This would make the class reject any comment containing the strings "poker", "enlargement" and "NOW!"