Added Akismet Servlet example.

This commit is contained in:
Erik C. Thauvin 2019-09-23 21:05:23 -07:00
parent 47210fd592
commit 697e74da79
4 changed files with 82 additions and 16 deletions

View file

@ -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");

View file

@ -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 })

View file

@ -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
) {
// ...
}
}