Reworked constructors to match the API.
This commit is contained in:
parent
d4d97cd40a
commit
ef74c7e5d8
5 changed files with 64 additions and 130 deletions
|
@ -6,12 +6,11 @@ import net.thauvin.erik.akismet.AkismetComment;
|
||||||
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", "YOUR_BLOG_URL");
|
||||||
final AkismetComment comment = new AkismetComment();
|
final AkismetComment comment = new AkismetComment("127.0.0.1",
|
||||||
|
"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6");
|
||||||
|
|
||||||
comment.setTest(true);
|
comment.setTest(true);
|
||||||
|
|
||||||
comment.setUserIp("127.0.0.1");
|
|
||||||
comment.setUserAgent("Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6");
|
|
||||||
comment.setReferrer("http://www.google.com");
|
comment.setReferrer("http://www.google.com");
|
||||||
comment.setPermalink("http://yourblogdomainname.com/blog/post=1");
|
comment.setPermalink("http://yourblogdomainname.com/blog/post=1");
|
||||||
comment.setType(AkismetComment.TYPE_COMMENT);
|
comment.setType(AkismetComment.TYPE_COMMENT);
|
||||||
|
|
|
@ -8,16 +8,18 @@ fun main() {
|
||||||
val akismet = Akismet("YOUR_API_KEY", "YOUR_BLOG_URL")
|
val akismet = Akismet("YOUR_API_KEY", "YOUR_BLOG_URL")
|
||||||
val comment = AkismetComment(
|
val comment = AkismetComment(
|
||||||
userIp = "127.0.0.1",
|
userIp = "127.0.0.1",
|
||||||
userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6",
|
userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6")
|
||||||
referrer = "http://www.google.com",
|
|
||||||
permalink = "http://yourblogdomainname.com/blog/post=1",
|
comment.isTest = true
|
||||||
type = AkismetComment.TYPE_COMMENT,
|
|
||||||
author = "admin",
|
comment.referrer = "http://www.google.com"
|
||||||
authorEmail = "test@test.com",
|
comment.permalink = "http://yourblogdomainname.com/blog/post=1"
|
||||||
authorUrl = "http://www.CheckOutMyCoolSite.com",
|
comment.type = AkismetComment.TYPE_COMMENT
|
||||||
// userRole = AkismetComment.ADMIN_ROLE,
|
comment.author = "admin"
|
||||||
content = "It means a lot that you would take the time to review our software. Thanks again.",
|
comment.authorEmail = "test@test.com"
|
||||||
isTest = true)
|
comment.authorUrl = "http://www.CheckOutMyCoolSite.com"
|
||||||
|
// comment.userRole = AkismetComment.ADMIN_ROLE
|
||||||
|
comment.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 })
|
||||||
|
|
|
@ -84,7 +84,7 @@ open class Akismet(apiKey: String) {
|
||||||
private set
|
private set
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The actual response sent by Aksimet from the last operation.
|
* The actual response sent by Akismet from the last operation.
|
||||||
*
|
*
|
||||||
* For example: ```true```, ```false```, ```valid```, ```invalid```, etc.
|
* For example: ```true```, ```false```, ```valid```, ```invalid```, etc.
|
||||||
*/
|
*/
|
||||||
|
@ -295,7 +295,7 @@ open class Akismet(apiKey: String) {
|
||||||
add("recheck_reason", comment.recheckReason)
|
add("recheck_reason", comment.recheckReason)
|
||||||
}
|
}
|
||||||
|
|
||||||
comment.other.forEach { (k, v) -> add(k, v) }
|
comment.serverEnv.forEach { (k, v) -> add(k, v) }
|
||||||
}.build()
|
}.build()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,8 +39,11 @@ import javax.servlet.http.HttpServletRequest
|
||||||
*
|
*
|
||||||
* @constructor Create an Akismet comment instance. See the
|
* @constructor Create an Akismet comment instance. See the
|
||||||
* [Akismet API](https://akismet.com/development/api/#comment-check) for more details.
|
* [Akismet API](https://akismet.com/development/api/#comment-check) for more details.
|
||||||
|
*
|
||||||
|
* @param userIp IP address of the comment submitter.
|
||||||
|
* @param userAgent User agent string of the web browser submitting the comment.
|
||||||
*/
|
*/
|
||||||
open class AkismetComment() {
|
open class AkismetComment(val userIp: String, val userAgent: String) {
|
||||||
@Suppress("unused")
|
@Suppress("unused")
|
||||||
companion object {
|
companion object {
|
||||||
/** A blog comment. */
|
/** A blog comment. */
|
||||||
|
@ -61,10 +64,6 @@ open class AkismetComment() {
|
||||||
const val ADMIN_ROLE = "administrator"
|
const val ADMIN_ROLE = "administrator"
|
||||||
}
|
}
|
||||||
|
|
||||||
/** IP address of the comment submitter. */
|
|
||||||
var userIp: String = ""
|
|
||||||
/** User agent string of the web browser submitting the comment. */
|
|
||||||
var userAgent: String = ""
|
|
||||||
/** The content of the referer header should be set here. */
|
/** The content of the referer header should be set here. */
|
||||||
var referrer: String = ""
|
var referrer: String = ""
|
||||||
/** The full permanent URL of the entry the comment was submitted to. */
|
/** The full permanent URL of the entry the comment was submitted to. */
|
||||||
|
@ -124,98 +123,27 @@ open class AkismetComment() {
|
||||||
var recheckReason: String = ""
|
var recheckReason: String = ""
|
||||||
/**
|
/**
|
||||||
* In PHP, there is an array of environmental variables called $_SERVER that contains information about the Web
|
* In PHP, there is an array of environmental variables called $_SERVER that contains information about the Web
|
||||||
* server itself as well as a key/value for every HTTP header sent with the request.This data is highly useful to
|
* server itself as well as a key/value for every HTTP header sent with the request. This data is highly useful to
|
||||||
* Akismet.
|
* Akismet.
|
||||||
*
|
*
|
||||||
* How the submitted content interacts with the server can be very telling, so please include as much of it as
|
* How the submitted content interacts with the server can be very telling, so please include as much of it as
|
||||||
* possible.
|
* possible.
|
||||||
*/
|
*/
|
||||||
var other: Map<String, String> = emptyMap()
|
var serverEnv: Map<String, String> = emptyMap()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create an Akismet comment instance.
|
* Create an Akismet comment extracting the [userIp], [userAgent], [referrer] and [serverEnv] environment variables
|
||||||
* See the [Akismet API](https://akismet.com/development/api/#comment-check) for more details.
|
* from a Servlet request. See the
|
||||||
|
* [Akismet API](https://akismet.com/development/api/#comment-check) for more details.
|
||||||
|
*
|
||||||
|
* @see [serverEnv]
|
||||||
*/
|
*/
|
||||||
constructor(
|
constructor(request: HttpServletRequest) : this(request.remoteAddr, request.getHeader("User-Agent")) {
|
||||||
userIp: String,
|
serverEnv = buildPhpVars(request)
|
||||||
userAgent: String,
|
|
||||||
referrer: String = "",
|
|
||||||
permalink: String = "",
|
|
||||||
type: String = "",
|
|
||||||
author: String = "",
|
|
||||||
authorEmail: String = "",
|
|
||||||
authorUrl: String = "",
|
|
||||||
content: String = "",
|
|
||||||
dateGmt: String = "",
|
|
||||||
postModifiedGmt: String = "",
|
|
||||||
blogLang: String = "",
|
|
||||||
blogCharset: String = "",
|
|
||||||
userRole: String = "",
|
|
||||||
isTest: Boolean = false,
|
|
||||||
recheckReason: String = "",
|
|
||||||
other: Map<String, String> = emptyMap()
|
|
||||||
) : this() {
|
|
||||||
this.userIp = userIp
|
|
||||||
this.userAgent = userAgent
|
|
||||||
this.referrer = referrer
|
|
||||||
this.permalink = permalink
|
|
||||||
this.type = type
|
|
||||||
this.author = author
|
|
||||||
this.authorEmail = authorEmail
|
|
||||||
this.authorUrl = authorUrl
|
|
||||||
this.content = content
|
|
||||||
this.dateGmt = dateGmt
|
|
||||||
this.postModifiedGmt = postModifiedGmt
|
|
||||||
this.blogLang = blogLang
|
|
||||||
this.blogCharset = blogCharset
|
|
||||||
this.userRole = userRole
|
|
||||||
this.isTest = isTest
|
|
||||||
this.recheckReason = recheckReason
|
|
||||||
this.other = other
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Create Akismet comment extracting [userIp], [userAgent], [referrer] and [other] variables from a
|
|
||||||
* [HttpServletRequest](https://javaee.github.io/javaee-spec/javadocs/javax/servlet/http/HttpServletRequest.html).
|
|
||||||
* See the [Akismet API](https://akismet.com/development/api/#comment-check) for more details.
|
|
||||||
*/
|
|
||||||
constructor(
|
|
||||||
request: HttpServletRequest,
|
|
||||||
permalink: String = "",
|
|
||||||
type: String = "",
|
|
||||||
author: String = "",
|
|
||||||
authorEmail: String = "",
|
|
||||||
authorUrl: String = "",
|
|
||||||
content: String = "",
|
|
||||||
dateGmt: String = "",
|
|
||||||
postModifiedGmt: String = "",
|
|
||||||
blogLang: String = "",
|
|
||||||
blogCharset: String = "",
|
|
||||||
userRole: String = "",
|
|
||||||
isTest: Boolean = false,
|
|
||||||
recheckReason: String = "",
|
|
||||||
other: Map<String, String> = emptyMap()
|
|
||||||
) : this(
|
|
||||||
userIp = request.remoteAddr,
|
|
||||||
userAgent = request.getHeader("User-Agent"),
|
|
||||||
referrer = request.getHeader("Referer"),
|
|
||||||
permalink = permalink,
|
|
||||||
type = type,
|
|
||||||
author = author,
|
|
||||||
authorEmail = authorEmail,
|
|
||||||
authorUrl = authorUrl,
|
|
||||||
content = content,
|
|
||||||
dateGmt = dateGmt,
|
|
||||||
postModifiedGmt = postModifiedGmt,
|
|
||||||
blogLang = blogLang,
|
|
||||||
blogCharset = blogCharset,
|
|
||||||
userRole = userRole,
|
|
||||||
isTest = isTest,
|
|
||||||
recheckReason = recheckReason,
|
|
||||||
other = buildPhpVars(request, other))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun buildPhpVars(request: HttpServletRequest, other: Map<String, String>): HashMap<String, String> {
|
private fun buildPhpVars(request: HttpServletRequest): HashMap<String, String> {
|
||||||
val params = HashMap<String, String>()
|
val params = HashMap<String, String>()
|
||||||
params["REMOTE_ADDR"] = request.remoteAddr
|
params["REMOTE_ADDR"] = request.remoteAddr
|
||||||
params["REQUEST_URI"] = request.requestURI
|
params["REQUEST_URI"] = request.requestURI
|
||||||
|
@ -224,13 +152,9 @@ private fun buildPhpVars(request: HttpServletRequest, other: Map<String, String>
|
||||||
while (names.hasMoreElements()) {
|
while (names.hasMoreElements()) {
|
||||||
val name = names.nextElement()
|
val name = names.nextElement()
|
||||||
if (!name.equals("cookie", true)) {
|
if (!name.equals("cookie", true)) {
|
||||||
params["HTTP_${name.toUpperCase()}"] = request.getHeader(name)
|
params["HTTP_${name.toUpperCase().replace('-', '_')}"] = request.getHeader(name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (other.isNotEmpty()) {
|
|
||||||
params.putAll(other)
|
|
||||||
}
|
|
||||||
|
|
||||||
return params
|
return params
|
||||||
}
|
}
|
||||||
|
|
|
@ -76,26 +76,9 @@ class AkismetTest {
|
||||||
private val blog = getKey("AKISMET_BLOG")
|
private val blog = getKey("AKISMET_BLOG")
|
||||||
private val comment = AkismetComment(
|
private val comment = AkismetComment(
|
||||||
userIp = "127.0.0.1",
|
userIp = "127.0.0.1",
|
||||||
userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6",
|
userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6")
|
||||||
referrer = "http://www.google.com",
|
|
||||||
permalink = "http://yourblogdomainname.com/blog/post=1",
|
|
||||||
type = AkismetComment.TYPE_COMMENT,
|
|
||||||
author = "admin",
|
|
||||||
authorEmail = "test@test.com",
|
|
||||||
authorUrl = "http://www.CheckOutMyCoolSite.com",
|
|
||||||
userRole = AkismetComment.ADMIN_ROLE,
|
|
||||||
content = "It means a lot that you would take the time to review our software. Thanks again.",
|
|
||||||
isTest = true)
|
|
||||||
private val akismet = Akismet(getKey("AKISMET_API_KEY"), blog)
|
private val akismet = Akismet(getKey("AKISMET_API_KEY"), blog)
|
||||||
private val mockComment: AkismetComment = AkismetComment(
|
private val mockComment: AkismetComment = AkismetComment(request = getMockRequest())
|
||||||
request = getMockRequest(),
|
|
||||||
permalink = comment.permalink,
|
|
||||||
type = comment.type,
|
|
||||||
author = comment.author,
|
|
||||||
authorUrl = comment.authorUrl,
|
|
||||||
userRole = AkismetComment.ADMIN_ROLE,
|
|
||||||
content = comment.content,
|
|
||||||
isTest = true)
|
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
fun beforeClass() {
|
fun beforeClass() {
|
||||||
|
@ -103,6 +86,24 @@ class AkismetTest {
|
||||||
addHandler(ConsoleHandler().apply { level = Level.FINE })
|
addHandler(ConsoleHandler().apply { level = Level.FINE })
|
||||||
level = Level.FINE
|
level = Level.FINE
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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.userRole = AkismetComment.ADMIN_ROLE
|
||||||
|
comment.content = "It means a lot that you would take the time to review our software. Thanks again."
|
||||||
|
comment.isTest = true
|
||||||
|
|
||||||
|
mockComment.permalink = comment.permalink
|
||||||
|
mockComment.type = comment.type
|
||||||
|
mockComment.author = comment.author
|
||||||
|
mockComment.authorUrl = comment.authorUrl
|
||||||
|
mockComment.userRole = AkismetComment.ADMIN_ROLE
|
||||||
|
mockComment.content = comment.content
|
||||||
|
mockComment.isTest = true
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -131,6 +132,14 @@ class AkismetTest {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun checkMockComment() {
|
||||||
|
assertEquals(mockComment.userIp, comment.userIp, "userIp")
|
||||||
|
assertEquals(mockComment.userAgent, comment.userAgent, "userAgent")
|
||||||
|
assertTrue(mockComment.serverEnv.containsKey("HTTP_ACCEPT_ENCODING"), "HTTP_ACCEPT_ENCODING")
|
||||||
|
assertTrue(mockComment.serverEnv.containsKey("REQUEST_URI"), "REQUEST_URI")
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun checkCommentTest() {
|
fun checkCommentTest() {
|
||||||
assertFalse(akismet.checkComment(comment), "check_comment(admin) -> false")
|
assertFalse(akismet.checkComment(comment), "check_comment(admin) -> false")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue