KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > blojsom > plugin > moderation > LinkSpamModerationPlugin


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.moderation;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.blojsom.blog.Blog;
36 import org.blojsom.blog.Entry;
37 import org.blojsom.event.Event;
38 import org.blojsom.event.EventBroadcaster;
39 import org.blojsom.event.Listener;
40 import org.blojsom.plugin.Plugin;
41 import org.blojsom.plugin.PluginException;
42 import org.blojsom.plugin.pingback.event.PingbackResponseSubmissionEvent;
43 import org.blojsom.plugin.pingback.PingbackPlugin;
44 import org.blojsom.plugin.comment.CommentModerationPlugin;
45 import org.blojsom.plugin.comment.CommentPlugin;
46 import org.blojsom.plugin.comment.event.CommentResponseSubmissionEvent;
47 import org.blojsom.plugin.response.event.ResponseSubmissionEvent;
48 import org.blojsom.plugin.trackback.TrackbackModerationPlugin;
49 import org.blojsom.plugin.trackback.TrackbackPlugin;
50 import org.blojsom.plugin.trackback.event.TrackbackResponseSubmissionEvent;
51 import org.blojsom.util.BlojsomUtils;
52
53 import javax.servlet.http.HttpServletRequest JavaDoc;
54 import javax.servlet.http.HttpServletResponse JavaDoc;
55 import java.util.Map JavaDoc;
56 import java.util.regex.Matcher JavaDoc;
57 import java.util.regex.Pattern JavaDoc;
58
59 /**
60  * Link spam moderation plugin
61  *
62  * @author David Czarnecki
63  * @version $Id: LinkSpamModerationPlugin.java,v 1.2 2006/03/26 21:46:57 czarneckid Exp $
64  * @since blojsom 3.0
65  */

66 public class LinkSpamModerationPlugin implements Plugin, Listener {
67
68     private Log _logger = LogFactory.getLog(LinkSpamModerationPlugin.class);
69
70     private static final Pattern JavaDoc LINK_PATTERN = Pattern.compile("<a.*?href=.*?>", Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
71     private static final String JavaDoc LINKSPAM_COMMENT_THRESHOLD = "linkspam-comment-threshold";
72     private static final String JavaDoc LINKSPAM_TRACKBACK_THRESHOLD = "linkspam-trackback-threshold";
73     private static final String JavaDoc DELETE_LINKSPAM = "delete-linkspam";
74
75     private static final int DEFAULT_LINK_THRESHOLD = 3;
76
77     private EventBroadcaster _eventBroadcaster;
78
79     /**
80      * Create a new instance of the link spam moderation plugin
81      */

82     public LinkSpamModerationPlugin() {
83     }
84
85     /**
86      * Set the {@link EventBroadcaster} event broadcaster
87      *
88      * @param eventBroadcaster {@link EventBroadcaster}
89      */

90     public void setEventBroadcaster(EventBroadcaster eventBroadcaster) {
91         _eventBroadcaster = eventBroadcaster;
92     }
93
94     /**
95      * Initialize this plugin. This method only called when the plugin is instantiated.
96      *
97      * @throws org.blojsom.plugin.PluginException
98      * If there is an error initializing the plugin
99      */

100     public void init() throws PluginException {
101         _eventBroadcaster.addListener(this);
102     }
103
104     /**
105      * Process the blog entries
106      *
107      * @param httpServletRequest Request
108      * @param httpServletResponse Response
109      * @param blog {@link Blog} instance
110      * @param context Context
111      * @param entries Blog entries retrieved for the particular request
112      * @return Modified set of blog entries
113      * @throws PluginException If there is an error processing the blog entries
114      */

115     public Entry[] process(HttpServletRequest JavaDoc httpServletRequest, HttpServletResponse JavaDoc httpServletResponse, Blog blog, Map JavaDoc context, Entry[] entries) throws PluginException {
116         return entries;
117     }
118
119     /**
120      * Perform any cleanup for the plugin. Called after {@link #process}.
121      *
122      * @throws org.blojsom.plugin.PluginException
123      * If there is an error performing cleanup for this plugin
124      */

125     public void cleanup() throws PluginException {
126
127     }
128
129     /**
130      * Called when BlojsomServlet is taken out of service
131      *
132      * @throws org.blojsom.plugin.PluginException
133      * If there is an error in finalizing this plugin
134      */

135     public void destroy() throws PluginException {
136
137     }
138
139     /**
140      * Handle an event broadcast from another component
141      *
142      * @param event {@link org.blojsom.event.Event} to be handled
143      */

144     public void handleEvent(Event event) {
145
146     }
147
148     /**
149      * Process an event from another component
150      *
151      * @param event {@link org.blojsom.event.Event} to be handled
152      */

153     public void processEvent(Event event) {
154         if (event instanceof ResponseSubmissionEvent) {
155             ResponseSubmissionEvent responseSubmissionEvent = (ResponseSubmissionEvent) event;
156             String JavaDoc text = responseSubmissionEvent.getContent();
157             Map JavaDoc metaData = responseSubmissionEvent.getMetaData();
158
159             if (!BlojsomUtils.checkNullOrBlank(text)) {
160                 Matcher JavaDoc linkMatcher = LINK_PATTERN.matcher(text);
161                 int linkCount = 0;
162
163                 while (linkMatcher.find()) {
164                     linkCount++;
165                 }
166
167                 int linkThreshold;
168                 String JavaDoc thresholdProperty = "";
169
170                 if (responseSubmissionEvent instanceof CommentResponseSubmissionEvent) {
171                     thresholdProperty = LINKSPAM_COMMENT_THRESHOLD;
172                 } else if (responseSubmissionEvent instanceof TrackbackResponseSubmissionEvent) {
173                     thresholdProperty = LINKSPAM_TRACKBACK_THRESHOLD;
174                 }
175
176                 String JavaDoc thresholdPropertyValue = responseSubmissionEvent.getBlog().getProperty(thresholdProperty);
177                 if (BlojsomUtils.checkNullOrBlank(thresholdPropertyValue)) {
178                     thresholdPropertyValue = Integer.toString(DEFAULT_LINK_THRESHOLD);
179                 }
180
181                 String JavaDoc deleteLinkSpamPropertyValue = responseSubmissionEvent.getBlog().getProperty(DELETE_LINKSPAM);
182                 boolean deleteLinkSpam;
183
184                 try {
185                     linkThreshold = Integer.parseInt(thresholdPropertyValue);
186                 } catch (NumberFormatException JavaDoc e) {
187                     linkThreshold = DEFAULT_LINK_THRESHOLD;
188                 }
189
190                 deleteLinkSpam = Boolean.valueOf(deleteLinkSpamPropertyValue).booleanValue();
191
192                 if (linkCount >= linkThreshold) {
193                     _logger.debug("Exceeded threshold for links in response: " + linkCount + " > " + linkThreshold);
194
195                     if (responseSubmissionEvent instanceof CommentResponseSubmissionEvent) {
196                         if (!deleteLinkSpam) {
197                             metaData.put(CommentModerationPlugin.BLOJSOM_COMMENT_MODERATION_PLUGIN_APPROVED, Boolean.FALSE.toString());
198                         } else {
199                             metaData.put(CommentPlugin.BLOJSOM_PLUGIN_COMMENT_METADATA_DESTROY, Boolean.TRUE);
200                         }
201                     } else if (responseSubmissionEvent instanceof TrackbackResponseSubmissionEvent) {
202                         if (!deleteLinkSpam) {
203                             metaData.put(TrackbackModerationPlugin.BLOJSOM_TRACKBACK_MODERATION_PLUGIN_APPROVED, Boolean.FALSE.toString());
204                         } else {
205                             metaData.put(TrackbackPlugin.BLOJSOM_PLUGIN_TRACKBACK_METADATA_DESTROY, Boolean.TRUE);
206                         }
207                     } else if (responseSubmissionEvent instanceof PingbackResponseSubmissionEvent) {
208                         if (deleteLinkSpam) {
209                             metaData.put(PingbackPlugin.BLOJSOM_PLUGIN_PINGBACK_METADATA_DESTROY, Boolean.TRUE);
210                         }
211                     }
212                 }
213             }
214
215         }
216     }
217 }
Popular Tags