From 697e74da79abb52acf3060fb61795774820233ff Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Mon, 23 Sep 2019 21:05:23 -0700 Subject: [PATCH] Added Akismet Servlet example. --- README.md | 14 ++++- .../main/java/com/example/AkismetSample.java | 4 +- .../main/kotlin/com/example/AkismetExample.kt | 24 ++++---- .../main/kotlin/com/example/AkismetServlet.kt | 56 +++++++++++++++++++ 4 files changed, 82 insertions(+), 16 deletions(-) create mode 100644 examples/src/main/kotlin/com/example/AkismetServlet.kt diff --git a/README.md b/README.md index 2c79dc9..1d8daa5 100644 --- a/README.md +++ b/README.md @@ -3,9 +3,9 @@ # [Akismet](https://www.akismet.com) for Kotlin/Java -Akismet for Kotlin/Java is a pretty complete and straightforward implementation of the [Automattic's Akismet](https://www.akismet.com/) API, a free service that can be used to actively stop comments spam. +Akismet for Kotlin/Java is a pretty complete and straightforward implementation of the [Automattic's Akismet](https://akismet.com/development/api/) API, a free service that can be used to actively stop comments spam. -## Examples +## Examples (TL;DR) #### Kotlin @@ -20,6 +20,7 @@ comment.setAuthorEmail("test@test.com"); comment.setAuthorUrl("http://www.CheckOutMyCoolSite.com"); comment.setDateGmt(Akismet.dateToGmt(new Date())); comment.setContent("It means a lot that you would take the time to review our software."); +// ... val isSpam = akismet.checkComment(comment) if (isSpam) { @@ -27,6 +28,8 @@ if (isSpam) { } ``` +[View Full Example](https://github.com/ethauvin/akismet-kotlin/blob/master/examples/src/main/kotlin/com/example/AkismetExample.kt) + #### Java ```java @@ -40,6 +43,7 @@ comment.setAuthorEmail("test@test.com"); comment.setAuthorUrl("http://www.CheckOutMyCoolSite.com"); comment.setDateGmt(Akismet.dateToGmt(new Date())); comment.setContent("It means a lot that you would take the time to review our software."); +//... final boolean isSpam = akismet.checkComment(comment); if (isSpam) { @@ -47,6 +51,8 @@ if (isSpam) { } ``` +[View Full Example](https://github.com/ethauvin/akismet-kotlin/blob/master/examples/src/main/java/com/example/AkismetSample.java) + ### HttpServletRequest The more information is sent to Akismet, the more accurate the response is. An [HttpServletRequest](https://javaee.github.io/javaee-spec/javadocs/javax/servlet/http/HttpServletRequest.html) can be used as a parameter so that all of the relevant information is automatically included. @@ -55,6 +61,8 @@ The more information is sent to Akismet, the more accurate the response is. An [ AkismetComment(request = context.getRequest()) ``` +[View Full Example](https://github.com/ethauvin/akismet-kotlin/blob/master/examples/src/main/kotlin/com/example/AkismetServlet.kt) + This will ensure that the user's IP, agent, referrer and various environment variables are automatically extracted from the request. ### JSON @@ -67,7 +75,7 @@ var json = comment.toString() At a latter time, the comment can the be submitted: -``` +```kotlin akismet.submitSpam(Akismet.jsonComment(json)) ``` diff --git a/examples/src/main/java/com/example/AkismetSample.java b/examples/src/main/java/com/example/AkismetSample.java index 40849ef..19be57a 100644 --- a/examples/src/main/java/com/example/AkismetSample.java +++ b/examples/src/main/java/com/example/AkismetSample.java @@ -7,13 +7,13 @@ import java.util.Date; public class AkismetSample { public static void main(String[] args) { - final Akismet akismet = new Akismet("YOUR_API_KEY", "YOUR_BLOG_URL"); + final Akismet akismet = new Akismet("YOUR_API_KEY", "http://yourblogdomainname.com/blog/"); final AkismetComment comment = new AkismetComment("127.0.0.1", "curl/7.29.0"); comment.setTest(true); comment.setReferrer("http://www.google.com"); - comment.setPermalink("http://yourblogdomainname.com/blog/post=1"); + comment.setPermalink(akismet.getBlog() + "post=1"); comment.setType(AkismetComment.TYPE_COMMENT); comment.setAuthor("admin"); comment.setAuthorEmail("test@test.com"); diff --git a/examples/src/main/kotlin/com/example/AkismetExample.kt b/examples/src/main/kotlin/com/example/AkismetExample.kt index 48286d3..4a85a9d 100644 --- a/examples/src/main/kotlin/com/example/AkismetExample.kt +++ b/examples/src/main/kotlin/com/example/AkismetExample.kt @@ -6,20 +6,22 @@ import java.util.Date import kotlin.system.exitProcess fun main() { - val akismet = Akismet("YOUR_API_KEY", "YOUR_BLOG_URL") + val akismet = Akismet("YOUR_API_KEY", "http://yourblogdomainname.com/blog/") val comment = AkismetComment(userIp = "127.0.0.1", userAgent = "curl/7.29.0") - comment.isTest = true + with(comment) { + isTest = true - comment.referrer = "http://www.google.com" - comment.permalink = "http://yourblogdomainname.com/blog/post=1" - comment.type = AkismetComment.TYPE_COMMENT - comment.author = "admin" - comment.authorEmail = "test@test.com" - comment.authorUrl = "http://www.CheckOutMyCoolSite.com" - comment.dateGmt = Akismet.dateToGmt(Date()) -// comment.userRole = AkismetComment.ADMIN_ROLE - comment.content = "It means a lot that you would take the time to review our software. Thanks again." + referrer = "http://www.google.com" + permalink = "${akismet.blog}post=1" + type = AkismetComment.TYPE_COMMENT + author = "admin" + authorEmail = "test@test.com" + authorUrl = "http://www.CheckOutMyCoolSite.com" + dateGmt = Akismet.dateToGmt(Date()) +// userRole = AkismetComment.ADMIN_ROLE + content = "It means a lot that you would take the time to review our software. Thanks again." + } // with(akismet.logger) { // addHandler(ConsoleHandler().apply { level = Level.FINE }) diff --git a/examples/src/main/kotlin/com/example/AkismetServlet.kt b/examples/src/main/kotlin/com/example/AkismetServlet.kt new file mode 100644 index 0000000..7f45bcc --- /dev/null +++ b/examples/src/main/kotlin/com/example/AkismetServlet.kt @@ -0,0 +1,56 @@ +package com.example + +import net.thauvin.erik.akismet.Akismet +import net.thauvin.erik.akismet.AkismetComment +import java.io.IOException +import java.util.Date +import javax.servlet.ServletException +import javax.servlet.http.HttpServlet +import javax.servlet.http.HttpServletRequest +import javax.servlet.http.HttpServletResponse + +class AkismetServlet : HttpServlet() { + private val akismet = Akismet("YOUR_API_KEY", "http://yourblogdomainname.com/blog/") + + @Throws(ServletException::class, IOException::class) + public override fun service(request: HttpServletRequest, response: HttpServletResponse) { + val id = request.getParameter("id") + + val comment = AkismetComment(request) + with(comment) { + permalink = "${akismet.blog}/comment/$id" + type = AkismetComment.TYPE_COMMENT + author = request.getParameter("name") + authorEmail = request.getParameter("email") + dateGmt = Akismet.dateToGmt(Date()) + content = request.getParameter("comment") + } + + val isSpam = akismet.checkComment(comment) + + saveComment( + id = id, + name = comment.author, + email = comment.authorEmail, + date = comment.dateGmt, + comment = comment.content, + json = comment.toString(), + isSpam = isSpam + ) + + // ... + } + + @Suppress("UNUSED_PARAMETER") + private fun saveComment( + id: String, + name: String, + email: String, + date: String, + comment: String, + json: String, + isSpam: Boolean + ) { + // ... + } +}