A Tapestry 5 Module for Akismet API compatible services
This is Tapestry 5 IOC Module for interacting with services compatible to the Akismet API (http://akismet.com/development/api/). This includes Akismet itself (http://akismet.com/) as well as the free and open source alternative TypePad AntiSpam (http://antispam.typepad.com/).
This project is strongly based on the work by Michael J. Simons, java-akismet, but modified in order to be simpler in the context of a Tapestry 5 module and to take advantage of this as well.
Download the source code and do a Maven install with the following parameters:
mvn install -DargLine="-DakismetApiKey=[YOUR API KEY] -DakismetConsumer=[YOUR SITE URL]"
Add the dependency on your project’s pom.xml like this:
<dependency> <groupId>com.raulmt</groupId> <artifactId>quilt-akismet</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
Finally, configure your API Key (you can obtain one for free if you use TypePad AntiSpam here: http://antispam.typepad.com/info/get-api-key.html) and API Consumer (the URL of your site) by contributing the following symbols in your app module:
public void contributeApplicationDefaults(MappedConfiguration<String, String> configuration) { configuration.add(AkismetSymbols.API_CONSUMER, "[YOUR SITE URL]"); configuration.add(AkismetSymbols.API_KEY, "[YOUR API KEY]"); }
There are others symbols to use another service (the default is TypePad AntiSpam) or to specify the exact API endpoint you want to use.
And done!
Just inject the Akismet service and check your comments! The service’s methods natively receive a AkismetComment object, but if you provide a coercion to this class from your own Comment class, you can pass them directly to the service.
public static void contributeTypeCoercer(final org.apache.tapestry5.ioc.Configuration<CoercionTuple> configuration) { configuration.add(new CoercionTuple<Comment, AkismetComment>(Comment.class, AkismetComment.class, new Coercion<Comment, AkismetComment>() { @Override public AkismetComment coerce(Comment input) { AkismetComment output = new AkismetComment(); // set other fields of AkismetComment from your Comment class output.setCommentContent(input.getText()); // ... return output; } })); }
@Inject private Akismet<Comment> akismet; @Property private Comment comment; // ... void onSuccess() { if (akismet.commentCheck()) { // this comment is SPAM!! } // ... }