diff --git a/examples/src/main/java/com/example/AkismetSample.java b/examples/src/main/java/com/example/AkismetSample.java index 1b7bf4f..fb8adf5 100644 --- a/examples/src/main/java/com/example/AkismetSample.java +++ b/examples/src/main/java/com/example/AkismetSample.java @@ -6,12 +6,11 @@ import net.thauvin.erik.akismet.AkismetComment; public class AkismetSample { public static void main(String[] args) { 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.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.setPermalink("http://yourblogdomainname.com/blog/post=1"); comment.setType(AkismetComment.TYPE_COMMENT); @@ -21,11 +20,11 @@ public class AkismetSample { // comment.setUserRole(AkismetComment.ADMIN_ROLE); comment.setContent("It means a lot that you would take the time to review our software. Thanks again."); - // final ConsoleHandler consoleHandler = new ConsoleHandler(); - // consoleHandler.setLevel(Level.FINE); - // final Logger logger = akismet.getLogger(); - // logger.addHandler(consoleHandler); - // logger.setLevel(Level.FINE); + // final ConsoleHandler consoleHandler = new ConsoleHandler(); + // consoleHandler.setLevel(Level.FINE); + // final Logger logger = akismet.getLogger(); + // logger.addHandler(consoleHandler); + // logger.setLevel(Level.FINE); if (akismet.verifyKey()) { final boolean isSpam = akismet.checkComment(comment); diff --git a/examples/src/main/kotlin/com/example/AkismetExample.kt b/examples/src/main/kotlin/com/example/AkismetExample.kt index 8055f87..8acf680 100644 --- a/examples/src/main/kotlin/com/example/AkismetExample.kt +++ b/examples/src/main/kotlin/com/example/AkismetExample.kt @@ -8,16 +8,18 @@ fun main() { val akismet = Akismet("YOUR_API_KEY", "YOUR_BLOG_URL") val comment = AkismetComment( 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", - 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) + userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6") + + 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.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) { // addHandler(ConsoleHandler().apply { level = Level.FINE }) diff --git a/src/main/kotlin/net/thauvin/erik/akismet/Akismet.kt b/src/main/kotlin/net/thauvin/erik/akismet/Akismet.kt index c694f55..4f5f4d0 100644 --- a/src/main/kotlin/net/thauvin/erik/akismet/Akismet.kt +++ b/src/main/kotlin/net/thauvin/erik/akismet/Akismet.kt @@ -84,7 +84,7 @@ open class Akismet(apiKey: String) { 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. */ @@ -295,7 +295,7 @@ open class Akismet(apiKey: String) { add("recheck_reason", comment.recheckReason) } - comment.other.forEach { (k, v) -> add(k, v) } + comment.serverEnv.forEach { (k, v) -> add(k, v) } }.build() } } diff --git a/src/main/kotlin/net/thauvin/erik/akismet/AkismetComment.kt b/src/main/kotlin/net/thauvin/erik/akismet/AkismetComment.kt index 87625e9..6a6855b 100644 --- a/src/main/kotlin/net/thauvin/erik/akismet/AkismetComment.kt +++ b/src/main/kotlin/net/thauvin/erik/akismet/AkismetComment.kt @@ -39,8 +39,11 @@ import javax.servlet.http.HttpServletRequest * * @constructor Create an Akismet comment instance. See the * [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") companion object { /** A blog comment. */ @@ -61,10 +64,6 @@ open class AkismetComment() { 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. */ var referrer: String = "" /** The full permanent URL of the entry the comment was submitted to. */ @@ -124,98 +123,27 @@ open class AkismetComment() { var recheckReason: String = "" /** * 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. * * How the submitted content interacts with the server can be very telling, so please include as much of it as * possible. */ - var other: Map = emptyMap() + var serverEnv: Map = emptyMap() /** - * Create an Akismet comment instance. - * See the [Akismet API](https://akismet.com/development/api/#comment-check) for more details. + * Create an Akismet comment extracting the [userIp], [userAgent], [referrer] and [serverEnv] environment variables + * from a Servlet request. See the + * [Akismet API](https://akismet.com/development/api/#comment-check) for more details. + * + * @see [serverEnv] */ - constructor( - userIp: String, - 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 = 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 + constructor(request: HttpServletRequest) : this(request.remoteAddr, request.getHeader("User-Agent")) { + serverEnv = buildPhpVars(request) } - - /** - * 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 = 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): HashMap { +private fun buildPhpVars(request: HttpServletRequest): HashMap { val params = HashMap() params["REMOTE_ADDR"] = request.remoteAddr params["REQUEST_URI"] = request.requestURI @@ -224,13 +152,9 @@ private fun buildPhpVars(request: HttpServletRequest, other: Map while (names.hasMoreElements()) { val name = names.nextElement() 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 } diff --git a/src/test/kotlin/net/thauvin/erik/akismet/AkismetTest.kt b/src/test/kotlin/net/thauvin/erik/akismet/AkismetTest.kt index 8293b61..e84b857 100644 --- a/src/test/kotlin/net/thauvin/erik/akismet/AkismetTest.kt +++ b/src/test/kotlin/net/thauvin/erik/akismet/AkismetTest.kt @@ -76,26 +76,9 @@ class AkismetTest { private val blog = getKey("AKISMET_BLOG") private val comment = AkismetComment( 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", - 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) + userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6") private val akismet = Akismet(getKey("AKISMET_API_KEY"), blog) - private val mockComment: AkismetComment = AkismetComment( - request = getMockRequest(), - permalink = comment.permalink, - type = comment.type, - author = comment.author, - authorUrl = comment.authorUrl, - userRole = AkismetComment.ADMIN_ROLE, - content = comment.content, - isTest = true) + private val mockComment: AkismetComment = AkismetComment(request = getMockRequest()) @BeforeClass fun beforeClass() { @@ -103,6 +86,24 @@ class AkismetTest { addHandler(ConsoleHandler().apply { 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 @@ -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 fun checkCommentTest() { assertFalse(akismet.checkComment(comment), "check_comment(admin) -> false")