File Source: AkismetCommentValidator.java
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */
18
19 package org.apache.roller.weblogger.ui.rendering.plugins.comments;
20
21 import java.io.BufferedReader;
22 import java.io.InputStreamReader;
23 import java.io.OutputStreamWriter;
24 import java.net.URL;
25 import java.net.URLConnection;
26 import java.util.ResourceBundle;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.roller.weblogger.business.WebloggerFactory;
30 import org.apache.roller.weblogger.config.WebloggerConfig;
31 import org.apache.roller.weblogger.pojos.WeblogEntryComment;
32 import org.apache.roller.weblogger.ui.rendering.util.*;
33 import org.apache.roller.weblogger.util.RollerMessages;
34
35
36 /**
37 * Check against Akismet service. Expects to a valid Akismet API key in the
38 * Roller startup config property comment.validator.akismet.apikey.
39 * You can get a free personal use key by registering as a user at wordpress.com.
40 * See Akismet site for API details (http://akismet.com/development/api/)
41 */
42 public class AkismetCommentValidator implements CommentValidator {
/*
P/P * Method: org.apache.roller.weblogger.ui.rendering.plugins.comments.AkismetCommentValidator__static_init
*
* Postconditions:
* init'ed(log)
*/
43 private static Log log = LogFactory.getLog(AkismetCommentValidator.class);
44 private ResourceBundle bundle = ResourceBundle.getBundle("ApplicationResources");
45 private String apikey;
46
47 /** Creates a new instance of AkismetCommentValidator */
/*
P/P * Method: void org.apache.roller.weblogger.ui.rendering.plugins.comments.AkismetCommentValidator()
*
* Postconditions:
* init'ed(this.apikey)
* init'ed(this.bundle)
*/
48 public AkismetCommentValidator() {
49 apikey = WebloggerConfig.getProperty("comment.validator.akismet.apikey");
50 }
51
52 public String getName() {
/*
P/P * Method: String getName()
*
* Preconditions:
* this.bundle != null
*
* Postconditions:
* init'ed(return_value)
*/
53 return bundle.getString("comment.validator.akismetName");
54 }
55
56 public int validate(WeblogEntryComment comment, RollerMessages messages) {
/*
P/P * Method: int validate(WeblogEntryComment, RollerMessages)
*
* Preconditions:
* comment != null
* (soft) log != null
* (soft) messages != null
* (soft) messages.mErrors != null
* (soft) init'ed(this.apikey)
*
* Presumptions:
* java.net.URL:openConnection(...)@72 != null
* org.apache.roller.weblogger.business.Weblogger:getUrlStrategy(...)@58 != null
* org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@58 != null
* org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@75 != null
* org.apache.roller.weblogger.pojos.WeblogEntryComment:getWeblogEntry(...)@58 != null
* ...
*
* Postconditions:
* return_value in {0, 100}
*
* Test Vectors:
* java.lang.String:equals(...)@86: {0}, {1}
*/
57 StringBuffer sb = new StringBuffer();
58 sb.append("blog=").append(
59 WebloggerFactory.getWeblogger().getUrlStrategy().getWeblogURL(comment.getWeblogEntry().getWebsite(), null, true)).append("&");
60 sb.append("user_ip=" ).append(comment.getRemoteHost()).append("&");
61 sb.append("user_agent=" ).append(comment.getUserAgent()).append("&");
62 sb.append("referrer=" ).append(comment.getReferrer()).append("&");
63 sb.append("permalink=" ).append(comment.getWeblogEntry().getPermalink()).append("&");
64 sb.append("comment_type=" ).append("comment").append("&");
65 sb.append("comment_author=" ).append(comment.getName()).append("&");
66 sb.append("comment_author_email=").append(comment.getEmail()).append("&");
67 sb.append("comment_author_url=" ).append(comment.getUrl()).append("&");
68 sb.append("comment_content=" ).append(comment.getContent());
69
70 try {
71 URL url = new URL("http://" + apikey + ".rest.akismet.com/1.1/comment-check");
72 URLConnection conn = url.openConnection();
73 conn.setDoOutput(true);
74
75 conn.setRequestProperty("User_Agent", "Roller " + WebloggerFactory.getWeblogger().getVersion());
76 conn.setRequestProperty("Content-type", "application/x-www-form-urlencoded;charset=utf8");
77 conn.setRequestProperty("Content-length", Integer.toString(sb.length()));
78
79 OutputStreamWriter osr = new OutputStreamWriter(conn.getOutputStream());
80 osr.write(sb.toString(), 0, sb.length());
81 osr.flush();
82 osr.close();
83
84 BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
85 String response = br.readLine();
86 if ("true".equals(response)) {
87 messages.addError("comment.validator.akismetMessage");
88 return 0;
89 }
90 else return 100;
91 } catch (Exception e) {
92 log.error("ERROR checking comment against Akismet", e);
93 }
94 return 0; // interpret error as spam: better safe than sorry?
95 }
96 }
97
98
99
SofCheck Inspector Build Version : 2.18479
| AkismetCommentValidator.java |
2009-Jan-02 14:25:44 |
| AkismetCommentValidator.class |
2009-Sep-04 03:12:44 |