Added Akismet Servlet example.
This commit is contained in:
parent
47210fd592
commit
697e74da79
4 changed files with 82 additions and 16 deletions
14
README.md
14
README.md
|
@ -3,9 +3,9 @@
|
||||||
|
|
||||||
# [Akismet](https://www.akismet.com) for Kotlin/Java
|
# [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
|
#### Kotlin
|
||||||
|
|
||||||
|
@ -20,6 +20,7 @@ comment.setAuthorEmail("test@test.com");
|
||||||
comment.setAuthorUrl("http://www.CheckOutMyCoolSite.com");
|
comment.setAuthorUrl("http://www.CheckOutMyCoolSite.com");
|
||||||
comment.setDateGmt(Akismet.dateToGmt(new Date()));
|
comment.setDateGmt(Akismet.dateToGmt(new Date()));
|
||||||
comment.setContent("It means a lot that you would take the time to review our software.");
|
comment.setContent("It means a lot that you would take the time to review our software.");
|
||||||
|
// ...
|
||||||
|
|
||||||
val isSpam = akismet.checkComment(comment)
|
val isSpam = akismet.checkComment(comment)
|
||||||
if (isSpam) {
|
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
|
||||||
|
|
||||||
```java
|
```java
|
||||||
|
@ -40,6 +43,7 @@ comment.setAuthorEmail("test@test.com");
|
||||||
comment.setAuthorUrl("http://www.CheckOutMyCoolSite.com");
|
comment.setAuthorUrl("http://www.CheckOutMyCoolSite.com");
|
||||||
comment.setDateGmt(Akismet.dateToGmt(new Date()));
|
comment.setDateGmt(Akismet.dateToGmt(new Date()));
|
||||||
comment.setContent("It means a lot that you would take the time to review our software.");
|
comment.setContent("It means a lot that you would take the time to review our software.");
|
||||||
|
//...
|
||||||
|
|
||||||
final boolean isSpam = akismet.checkComment(comment);
|
final boolean isSpam = akismet.checkComment(comment);
|
||||||
if (isSpam) {
|
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
|
### 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.
|
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())
|
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.
|
This will ensure that the user's IP, agent, referrer and various environment variables are automatically extracted from the request.
|
||||||
|
|
||||||
### JSON
|
### JSON
|
||||||
|
@ -67,7 +75,7 @@ var json = comment.toString()
|
||||||
|
|
||||||
At a latter time, the comment can the be submitted:
|
At a latter time, the comment can the be submitted:
|
||||||
|
|
||||||
```
|
```kotlin
|
||||||
akismet.submitSpam(Akismet.jsonComment(json))
|
akismet.submitSpam(Akismet.jsonComment(json))
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -7,13 +7,13 @@ import java.util.Date;
|
||||||
|
|
||||||
public class AkismetSample {
|
public class AkismetSample {
|
||||||
public static void main(String[] args) {
|
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");
|
final AkismetComment comment = new AkismetComment("127.0.0.1", "curl/7.29.0");
|
||||||
|
|
||||||
comment.setTest(true);
|
comment.setTest(true);
|
||||||
|
|
||||||
comment.setReferrer("http://www.google.com");
|
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.setType(AkismetComment.TYPE_COMMENT);
|
||||||
comment.setAuthor("admin");
|
comment.setAuthor("admin");
|
||||||
comment.setAuthorEmail("test@test.com");
|
comment.setAuthorEmail("test@test.com");
|
||||||
|
|
|
@ -6,20 +6,22 @@ import java.util.Date
|
||||||
import kotlin.system.exitProcess
|
import kotlin.system.exitProcess
|
||||||
|
|
||||||
fun main() {
|
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")
|
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"
|
referrer = "http://www.google.com"
|
||||||
comment.permalink = "http://yourblogdomainname.com/blog/post=1"
|
permalink = "${akismet.blog}post=1"
|
||||||
comment.type = AkismetComment.TYPE_COMMENT
|
type = AkismetComment.TYPE_COMMENT
|
||||||
comment.author = "admin"
|
author = "admin"
|
||||||
comment.authorEmail = "test@test.com"
|
authorEmail = "test@test.com"
|
||||||
comment.authorUrl = "http://www.CheckOutMyCoolSite.com"
|
authorUrl = "http://www.CheckOutMyCoolSite.com"
|
||||||
comment.dateGmt = Akismet.dateToGmt(Date())
|
dateGmt = Akismet.dateToGmt(Date())
|
||||||
// comment.userRole = AkismetComment.ADMIN_ROLE
|
// userRole = AkismetComment.ADMIN_ROLE
|
||||||
comment.content = "It means a lot that you would take the time to review our software. Thanks again."
|
content = "It means a lot that you would take the time to review our software. Thanks again."
|
||||||
|
}
|
||||||
|
|
||||||
// with(akismet.logger) {
|
// with(akismet.logger) {
|
||||||
// addHandler(ConsoleHandler().apply { level = Level.FINE })
|
// addHandler(ConsoleHandler().apply { level = Level.FINE })
|
||||||
|
|
56
examples/src/main/kotlin/com/example/AkismetServlet.kt
Normal file
56
examples/src/main/kotlin/com/example/AkismetServlet.kt
Normal 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
|
||||||
|
) {
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue