KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > blojsom > plugin > akismet > AkismetModerationPlugin


1 /**
2  * Copyright (c) 2003-2006, David A. Czarnecki
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and the
9  * following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11  * following disclaimer in the documentation and/or other materials provided with the distribution.
12  * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
13  * endorse or promote products derived from this software without specific prior written permission.
14  * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
15  * without prior written permission of David A. Czarnecki.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
18  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
21  * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */

31 package org.blojsom.plugin.akismet;
32
33 import net.sf.akismet.Akismet;
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.blojsom.blog.Blog;
37 import org.blojsom.blog.Entry;
38 import org.blojsom.event.Event;
39 import org.blojsom.event.EventBroadcaster;
40 import org.blojsom.event.Listener;
41 import org.blojsom.plugin.Plugin;
42 import org.blojsom.plugin.PluginException;
43 import org.blojsom.plugin.comment.CommentModerationPlugin;
44 import org.blojsom.plugin.comment.CommentPlugin;
45 import org.blojsom.plugin.comment.event.CommentResponseSubmissionEvent;
46 import org.blojsom.plugin.comment.event.CommentMarkedSpamEvent;
47 import org.blojsom.plugin.comment.event.CommentUnmarkedSpamEvent;
48 import org.blojsom.plugin.pingback.event.PingbackResponseSubmissionEvent;
49 import org.blojsom.plugin.pingback.event.PingbackMarkedSpamEvent;
50 import org.blojsom.plugin.pingback.event.PingbackUnmarkedSpamEvent;
51 import org.blojsom.plugin.pingback.PingbackPlugin;
52 import org.blojsom.plugin.response.event.ResponseSubmissionEvent;
53 import org.blojsom.plugin.trackback.TrackbackModerationPlugin;
54 import org.blojsom.plugin.trackback.TrackbackPlugin;
55 import org.blojsom.plugin.trackback.event.TrackbackResponseSubmissionEvent;
56 import org.blojsom.plugin.trackback.event.TrackbackUnmarkedSpamEvent;
57 import org.blojsom.plugin.trackback.event.TrackbackMarkedSpamEvent;
58 import org.blojsom.util.BlojsomUtils;
59
60 import javax.servlet.http.HttpServletRequest JavaDoc;
61 import javax.servlet.http.HttpServletResponse JavaDoc;
62 import java.util.Map JavaDoc;
63
64 /**
65  * Akismet moderation plugin
66  *
67  * @author David Czarnecki
68  * @version $Id: AkismetModerationPlugin.java,v 1.7 2006/06/21 20:42:24 czarneckid Exp $
69  * @since blojsom 3.0
70  */

71 public class AkismetModerationPlugin implements Plugin, Listener {
72
73     private Log _logger = LogFactory.getLog(AkismetModerationPlugin.class);
74
75     // Initialization parameters
76
private static final String JavaDoc AKISMET_PLUGIN_API_KEY_IP = "akismet-plugin-api-key";
77     private static final String JavaDoc AKISMET_PLUGIN_DELETE_SPAM_IP = "akismet-plugin-delete-spam";
78     private static final String JavaDoc AKISMET_PLUGIN_AUTOMATIC_APPROVAL_IP = "akismet-plugin-automatic-approval";
79     private static final boolean DELETE_SPAM_DEFAULT = false;
80
81     // Context variables
82
private static final String JavaDoc AKISMET_PLUGIN_RESPONSE = "AKISMET_PLUGIN_RESPONSE";
83
84     private EventBroadcaster _eventBroadcaster;
85
86     /**
87      * Construct a new instance of the Akismet moderation plugin
88      */

89     public AkismetModerationPlugin() {
90     }
91
92     /**
93      * Set the {@link org.blojsom.event.EventBroadcaster} event broadcaster
94      *
95      * @param eventBroadcaster {@link org.blojsom.event.EventBroadcaster}
96      */

97     public void setEventBroadcaster(EventBroadcaster eventBroadcaster) {
98         _eventBroadcaster = eventBroadcaster;
99     }
100
101     /**
102      * Initialize this plugin. This method only called when the plugin is instantiated.
103      *
104      * @throws org.blojsom.plugin.PluginException
105      * If there is an error initializing the plugin
106      */

107     public void init() throws PluginException {
108         _eventBroadcaster.addListener(this);
109     }
110
111     /**
112      * Process the blog entries
113      *
114      * @param httpServletRequest Request
115      * @param httpServletResponse Response
116      * @param blog {@link Blog} instance
117      * @param context Context
118      * @param entries Blog entries retrieved for the particular request
119      * @return Modified set of blog entries
120      * @throws PluginException If there is an error processing the blog entries
121      */

122     public Entry[] process(HttpServletRequest JavaDoc httpServletRequest, HttpServletResponse JavaDoc httpServletResponse, Blog blog, Map JavaDoc context, Entry[] entries) throws PluginException {
123         return entries;
124     }
125
126     /**
127      * Perform any cleanup for the plugin. Called after {@link #process}.
128      *
129      * @throws org.blojsom.plugin.PluginException
130      * If there is an error performing cleanup for this plugin
131      */

132     public void cleanup() throws PluginException {
133     }
134
135     /**
136      * Called when BlojsomServlet is taken out of service
137      *
138      * @throws org.blojsom.plugin.PluginException
139      * If there is an error in finalizing this plugin
140      */

141     public void destroy() throws PluginException {
142     }
143
144     /**
145      * Handle an event broadcast from another component
146      *
147      * @param event {@link org.blojsom.event.Event} to be handled
148      */

149     public void handleEvent(Event event) {
150     }
151
152     /**
153      * Process an event from another component
154      *
155      * @param event {@link org.blojsom.event.Event} to be handled
156      */

157     public void processEvent(Event event) {
158         if (event instanceof ResponseSubmissionEvent) {
159             ResponseSubmissionEvent responseSubmissionEvent = (ResponseSubmissionEvent) event;
160             Blog blog = responseSubmissionEvent.getBlog();
161
162             String JavaDoc akismetAPIKey = blog.getProperty(AKISMET_PLUGIN_API_KEY_IP);
163             if (BlojsomUtils.checkNullOrBlank(akismetAPIKey)) {
164                 if (_logger.isInfoEnabled()) {
165                     _logger.info("No Akismet API key provided for blog property: " + AKISMET_PLUGIN_API_KEY_IP);
166                 }
167             } else {
168                 Akismet akismet = new Akismet(akismetAPIKey, blog.getBlogURL());
169                 String JavaDoc responseType = Akismet.COMMENT_TYPE_BLANK;
170
171                 if (responseSubmissionEvent instanceof CommentResponseSubmissionEvent) {
172                     responseType = Akismet.COMMENT_TYPE_COMMENT;
173                 } else if (responseSubmissionEvent instanceof TrackbackResponseSubmissionEvent) {
174                     responseType = Akismet.COMMENT_TYPE_TRACKBACK;
175                 } else if (responseSubmissionEvent instanceof PingbackResponseSubmissionEvent) {
176                     responseType = Akismet.COMMENT_TYPE_PINGBACK;
177                 }
178
179                 // Check the content from Akismet
180
HttpServletRequest JavaDoc httpServletRequest = responseSubmissionEvent.getHttpServletRequest();
181                 Map JavaDoc metaData = responseSubmissionEvent.getMetaData();
182
183                 StringBuffer JavaDoc entryLink = new StringBuffer JavaDoc().append(blog.getBlogURL())
184                         .append(responseSubmissionEvent.getEntry().getCategory())
185                         .append(responseSubmissionEvent.getEntry().getPostSlug());
186
187                 boolean isSpam = akismet.commentCheck(httpServletRequest.getRemoteAddr(),
188                         httpServletRequest.getHeader("User-Agent"),
189                         httpServletRequest.getHeader("Referer"),
190                         entryLink.toString(),
191                         responseType,
192                         responseSubmissionEvent.getSubmitter(),
193                         responseSubmissionEvent.getSubmitterItem1(),
194                         responseSubmissionEvent.getSubmitterItem2(),
195                         responseSubmissionEvent.getContent(), null);
196
197                 metaData.put(AKISMET_PLUGIN_RESPONSE, Boolean.toString(isSpam));
198                 
199                 // If Akismet identifies the content as spam, process for moderation or deletion accordingly
200
if (isSpam) {
201                     boolean deleteSpam = DELETE_SPAM_DEFAULT;
202
203                     String JavaDoc deleteSpamValue = responseSubmissionEvent.getBlog().getProperty(AKISMET_PLUGIN_DELETE_SPAM_IP);
204                     if (!BlojsomUtils.checkNullOrBlank(deleteSpamValue)) {
205                         deleteSpam = Boolean.valueOf(deleteSpamValue).booleanValue();
206                     }
207
208                     if (!deleteSpam) {
209                         if (_logger.isDebugEnabled()) {
210                             _logger.debug("Marking response for moderation");
211                         }
212                     } else {
213                         if (_logger.isDebugEnabled()) {
214                             _logger.debug("Marking response for automatic deletion");
215                         }
216                     }
217
218                     if (responseSubmissionEvent instanceof CommentResponseSubmissionEvent) {
219                         if (!deleteSpam) {
220                             metaData.put(CommentModerationPlugin.BLOJSOM_COMMENT_MODERATION_PLUGIN_APPROVED, Boolean.FALSE.toString());
221                         } else {
222                             metaData.put(CommentPlugin.BLOJSOM_PLUGIN_COMMENT_METADATA_DESTROY, Boolean.TRUE);
223                         }
224                     } else if (responseSubmissionEvent instanceof TrackbackResponseSubmissionEvent) {
225                         if (!deleteSpam) {
226                             metaData.put(TrackbackModerationPlugin.BLOJSOM_TRACKBACK_MODERATION_PLUGIN_APPROVED, Boolean.FALSE.toString());
227                         } else {
228                             metaData.put(TrackbackPlugin.BLOJSOM_PLUGIN_TRACKBACK_METADATA_DESTROY, Boolean.TRUE);
229                         }
230                     } else if (responseSubmissionEvent instanceof PingbackResponseSubmissionEvent) {
231                         if (!deleteSpam) {
232                             metaData.put(PingbackPlugin.BLOJSOM_PINGBACK_PLUGIN_APPROVED, Boolean.FALSE.toString());
233                         } else {
234                             metaData.put(PingbackPlugin.BLOJSOM_PLUGIN_PINGBACK_METADATA_DESTROY, Boolean.TRUE);
235                         }
236                     }
237                 } else {
238                     boolean automaticApproval = Boolean.valueOf(blog.getProperty(AKISMET_PLUGIN_AUTOMATIC_APPROVAL_IP)).booleanValue();
239                     if (automaticApproval) {
240                         if (responseSubmissionEvent instanceof CommentResponseSubmissionEvent) {
241                             if (!metaData.containsKey(CommentPlugin.BLOJSOM_PLUGIN_COMMENT_METADATA_DESTROY)) {
242                                 metaData.put(CommentModerationPlugin.BLOJSOM_COMMENT_MODERATION_PLUGIN_APPROVED, Boolean.TRUE.toString());
243                             }
244                         } else if (responseSubmissionEvent instanceof TrackbackResponseSubmissionEvent) {
245                             if (!metaData.containsKey(TrackbackPlugin.BLOJSOM_PLUGIN_TRACKBACK_METADATA_DESTROY)) {
246                                 metaData.put(TrackbackModerationPlugin.BLOJSOM_TRACKBACK_MODERATION_PLUGIN_APPROVED, Boolean.TRUE.toString());
247                             }
248                         } else if (responseSubmissionEvent instanceof PingbackResponseSubmissionEvent) {
249                             if (!metaData.containsKey(PingbackPlugin.BLOJSOM_PLUGIN_PINGBACK_METADATA_DESTROY)) {
250                                 metaData.put(PingbackPlugin.BLOJSOM_PINGBACK_PLUGIN_APPROVED, Boolean.TRUE.toString());
251                             }
252                         }
253                     }
254                 }
255
256                 responseSubmissionEvent.setMetaData(metaData);
257             }
258         } else if (event instanceof CommentMarkedSpamEvent) {
259             CommentMarkedSpamEvent commentMarkedSpamEvent = (CommentMarkedSpamEvent) event;
260             Blog blog = commentMarkedSpamEvent.getBlog();
261
262             String JavaDoc akismetAPIKey = blog.getProperty(AKISMET_PLUGIN_API_KEY_IP);
263             if (BlojsomUtils.checkNullOrBlank(akismetAPIKey)) {
264                 if (_logger.isInfoEnabled()) {
265                     _logger.info("No Akismet API key provided for blog property: " + AKISMET_PLUGIN_API_KEY_IP);
266                 }
267             } else {
268                 Akismet akismet = new Akismet(akismetAPIKey, blog.getBlogURL());
269                 String JavaDoc responseType = Akismet.COMMENT_TYPE_COMMENT;
270                 StringBuffer JavaDoc permalink = new StringBuffer JavaDoc();
271                 permalink.append(blog.getBlogURL()).append("/")
272                         .append(commentMarkedSpamEvent.getEntry().getBlogCategory().getName())
273                         .append(commentMarkedSpamEvent.getEntry().getPostSlug());
274
275                 akismet.submitSpam(commentMarkedSpamEvent.getComment().getIp(),
276                         null, null, permalink.toString(), responseType, commentMarkedSpamEvent.getComment().getAuthor(),
277                         commentMarkedSpamEvent.getComment().getAuthorEmail(),
278                         commentMarkedSpamEvent.getComment().getAuthorURL(),
279                         commentMarkedSpamEvent.getComment().getComment(), null);
280             }
281         } else if (event instanceof CommentUnmarkedSpamEvent) {
282             CommentUnmarkedSpamEvent commentUnmarkedSpamEvent = (CommentUnmarkedSpamEvent) event;
283             Blog blog = commentUnmarkedSpamEvent.getBlog();
284
285             String JavaDoc akismetAPIKey = blog.getProperty(AKISMET_PLUGIN_API_KEY_IP);
286             if (BlojsomUtils.checkNullOrBlank(akismetAPIKey)) {
287                 if (_logger.isInfoEnabled()) {
288                     _logger.info("No Akismet API key provided for blog property: " + AKISMET_PLUGIN_API_KEY_IP);
289                 }
290             } else {
291                 Akismet akismet = new Akismet(akismetAPIKey, blog.getBlogURL());
292                 String JavaDoc responseType = Akismet.COMMENT_TYPE_COMMENT;
293                 StringBuffer JavaDoc permalink = new StringBuffer JavaDoc();
294                 permalink.append(blog.getBlogURL()).append("/")
295                         .append(commentUnmarkedSpamEvent.getEntry().getBlogCategory().getName())
296                         .append(commentUnmarkedSpamEvent.getEntry().getPostSlug());
297
298                 akismet.submitHam(commentUnmarkedSpamEvent.getComment().getIp(),
299                         null, null, permalink.toString(), responseType, commentUnmarkedSpamEvent.getComment().getAuthor(),
300                         commentUnmarkedSpamEvent.getComment().getAuthorEmail(),
301                         commentUnmarkedSpamEvent.getComment().getAuthorURL(),
302                         commentUnmarkedSpamEvent.getComment().getComment(), null);
303             }
304         } else if (event instanceof TrackbackMarkedSpamEvent) {
305             TrackbackMarkedSpamEvent trackbackMarkedSpamEvent = (TrackbackMarkedSpamEvent) event;
306             Blog blog = trackbackMarkedSpamEvent.getBlog();
307
308             String JavaDoc akismetAPIKey = blog.getProperty(AKISMET_PLUGIN_API_KEY_IP);
309             if (BlojsomUtils.checkNullOrBlank(akismetAPIKey)) {
310                 if (_logger.isInfoEnabled()) {
311                     _logger.info("No Akismet API key provided for blog property: " + AKISMET_PLUGIN_API_KEY_IP);
312                 }
313             } else {
314                 Akismet akismet = new Akismet(akismetAPIKey, blog.getBlogURL());
315                 String JavaDoc responseType = Akismet.COMMENT_TYPE_TRACKBACK;
316                 StringBuffer JavaDoc permalink = new StringBuffer JavaDoc();
317                 permalink.append(blog.getBlogURL()).append("/")
318                         .append(trackbackMarkedSpamEvent.getEntry().getBlogCategory().getName())
319                         .append(trackbackMarkedSpamEvent.getEntry().getPostSlug());
320
321                 akismet.submitSpam(trackbackMarkedSpamEvent.getTrackback().getIp(),
322                         null, null, permalink.toString(), responseType, trackbackMarkedSpamEvent.getTrackback().getTitle(),
323                         trackbackMarkedSpamEvent.getTrackback().getBlogName(),
324                         trackbackMarkedSpamEvent.getTrackback().getUrl(),
325                         trackbackMarkedSpamEvent.getTrackback().getExcerpt(), null);
326             }
327         } else if (event instanceof TrackbackUnmarkedSpamEvent) {
328             TrackbackUnmarkedSpamEvent trackbackUnmarkedSpamEvent = (TrackbackUnmarkedSpamEvent) event;
329             Blog blog = trackbackUnmarkedSpamEvent.getBlog();
330
331             String JavaDoc akismetAPIKey = blog.getProperty(AKISMET_PLUGIN_API_KEY_IP);
332             if (BlojsomUtils.checkNullOrBlank(akismetAPIKey)) {
333                 if (_logger.isInfoEnabled()) {
334                     _logger.info("No Akismet API key provided for blog property: " + AKISMET_PLUGIN_API_KEY_IP);
335                 }
336             } else {
337                 Akismet akismet = new Akismet(akismetAPIKey, blog.getBlogURL());
338                 String JavaDoc responseType = Akismet.COMMENT_TYPE_TRACKBACK;
339                 StringBuffer JavaDoc permalink = new StringBuffer JavaDoc();
340                 permalink.append(blog.getBlogURL()).append("/")
341                         .append(trackbackUnmarkedSpamEvent.getEntry().getBlogCategory().getName())
342                         .append(trackbackUnmarkedSpamEvent.getEntry().getPostSlug());
343
344                 akismet.submitHam(trackbackUnmarkedSpamEvent.getTrackback().getIp(),
345                         null, null, permalink.toString(), responseType, trackbackUnmarkedSpamEvent.getTrackback().getTitle(),
346                         trackbackUnmarkedSpamEvent.getTrackback().getBlogName(),
347                         trackbackUnmarkedSpamEvent.getTrackback().getUrl(),
348                         trackbackUnmarkedSpamEvent.getTrackback().getExcerpt(), null);
349             }
350         } else if (event instanceof PingbackMarkedSpamEvent) {
351             PingbackMarkedSpamEvent pingbackMarkedSpamEvent = (PingbackMarkedSpamEvent) event;
352             Blog blog = pingbackMarkedSpamEvent.getBlog();
353
354             String JavaDoc akismetAPIKey = blog.getProperty(AKISMET_PLUGIN_API_KEY_IP);
355             if (BlojsomUtils.checkNullOrBlank(akismetAPIKey)) {
356                 if (_logger.isInfoEnabled()) {
357                     _logger.info("No Akismet API key provided for blog property: " + AKISMET_PLUGIN_API_KEY_IP);
358                 }
359             } else {
360                 Akismet akismet = new Akismet(akismetAPIKey, blog.getBlogURL());
361                 String JavaDoc responseType = Akismet.COMMENT_TYPE_PINGBACK;
362                 StringBuffer JavaDoc permalink = new StringBuffer JavaDoc();
363                 permalink.append(blog.getBlogURL()).append("/")
364                         .append(pingbackMarkedSpamEvent.getEntry().getBlogCategory().getName())
365                         .append(pingbackMarkedSpamEvent.getEntry().getPostSlug());
366
367                 akismet.submitSpam(pingbackMarkedSpamEvent.getPingback().getIp(),
368                         null, null, permalink.toString(), responseType, pingbackMarkedSpamEvent.getPingback().getTitle(),
369                         pingbackMarkedSpamEvent.getPingback().getBlogName(),
370                         pingbackMarkedSpamEvent.getPingback().getUrl(),
371                         pingbackMarkedSpamEvent.getPingback().getExcerpt(), null);
372             }
373         } else if (event instanceof PingbackUnmarkedSpamEvent) {
374             PingbackUnmarkedSpamEvent pingbackUnmarkedSpamEvent = (PingbackUnmarkedSpamEvent) event;
375             Blog blog = pingbackUnmarkedSpamEvent.getBlog();
376
377             String JavaDoc akismetAPIKey = blog.getProperty(AKISMET_PLUGIN_API_KEY_IP);
378             if (BlojsomUtils.checkNullOrBlank(akismetAPIKey)) {
379                 if (_logger.isInfoEnabled()) {
380                     _logger.info("No Akismet API key provided for blog property: " + AKISMET_PLUGIN_API_KEY_IP);
381                 }
382             } else {
383                 Akismet akismet = new Akismet(akismetAPIKey, blog.getBlogURL());
384                 String JavaDoc responseType = Akismet.COMMENT_TYPE_PINGBACK;
385                 StringBuffer JavaDoc permalink = new StringBuffer JavaDoc();
386                 permalink.append(blog.getBlogURL()).append("/")
387                         .append(pingbackUnmarkedSpamEvent.getEntry().getBlogCategory().getName())
388                         .append(pingbackUnmarkedSpamEvent.getEntry().getPostSlug());
389
390                 akismet.submitHam(pingbackUnmarkedSpamEvent.getPingback().getIp(),
391                         null, null, permalink.toString(), responseType, pingbackUnmarkedSpamEvent.getPingback().getTitle(),
392                         pingbackUnmarkedSpamEvent.getPingback().getBlogName(),
393                         pingbackUnmarkedSpamEvent.getPingback().getUrl(),
394                         pingbackUnmarkedSpamEvent.getPingback().getExcerpt(), null);
395             }
396         }
397     }
398 }
399
Popular Tags