KickJava   Java API By Example, From Geeks To Geeks.

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


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.comment.CommentModerationPlugin;
43 import org.blojsom.plugin.comment.CommentPlugin;
44 import org.blojsom.plugin.comment.event.CommentResponseSubmissionEvent;
45 import org.blojsom.plugin.pingback.PingbackPlugin;
46 import org.blojsom.plugin.pingback.event.PingbackResponseSubmissionEvent;
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.io.BufferedReader JavaDoc;
56 import java.io.IOException JavaDoc;
57 import java.io.StringReader JavaDoc;
58 import java.util.ArrayList JavaDoc;
59 import java.util.Iterator JavaDoc;
60 import java.util.List JavaDoc;
61 import java.util.Map JavaDoc;
62
63 /**
64  * Spam phrase moderation plugin
65  *
66  * @author David Czarnecki
67  * @since blojsom 3.0
68  * @version $Id: SpamPhraseModerationPlugin.java,v 1.2 2006/03/27 14:05:44 czarneckid Exp $
69  */

70 public class SpamPhraseModerationPlugin implements Plugin, Listener {
71
72     private Log _logger = LogFactory.getLog(SpamPhraseModerationPlugin.class);
73
74     private static final String JavaDoc SPAM_PHRASE_BLACKLIST_IP = "spam-phrase-blacklist";
75     private static final String JavaDoc DELETE_PHRASESPAM = "delete-phrasespam";
76     private static final boolean DEFAULT_DELETE_PHRASESPAM = false;
77
78     private EventBroadcaster _eventBroadcaster;
79
80     /**
81      * Create a new instance of the spam phrase moderation plugin
82      */

83     public SpamPhraseModerationPlugin() {
84     }
85
86     /**
87      * Set the {@link EventBroadcaster}
88      *
89      * @param eventBroadcaster {@link EventBroadcaster}
90      */

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

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

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

126     public void cleanup() throws PluginException {
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      * Handle an event broadcast from another component
140      *
141      * @param event {@link org.blojsom.event.Event} to be handled
142      */

143     public void handleEvent(Event event) {
144     }
145
146     /**
147      * Load the list of spam phrases from the blog
148      *
149      * @param blog {@link blog}
150      * @return List of spam phrases
151      */

152     protected List JavaDoc loadSpamPhrases(Blog blog) {
153         ArrayList JavaDoc spamPhrases = new ArrayList JavaDoc(25);
154
155         String JavaDoc spamPhrasesValues = blog.getProperty(SPAM_PHRASE_BLACKLIST_IP);
156         if (!BlojsomUtils.checkNullOrBlank(spamPhrasesValues)) {
157             try {
158                 StringReader JavaDoc stringReader = new StringReader JavaDoc(spamPhrasesValues);
159                 BufferedReader JavaDoc br = new BufferedReader JavaDoc(stringReader);
160                 String JavaDoc phrase;
161
162                 while ((phrase = br.readLine()) != null) {
163                     spamPhrases.add(phrase);
164                 }
165
166                 br.close();
167             } catch (IOException JavaDoc e) {
168                 if (_logger.isErrorEnabled()) {
169                     _logger.error(e);
170                 }
171             }
172         }
173
174         return spamPhrases;
175     }
176
177     /**
178      * Process an event from another component
179      *
180      * @param event {@link org.blojsom.event.Event} to be handled
181      */

182     public void processEvent(Event event) {
183         if (event instanceof ResponseSubmissionEvent) {
184             ResponseSubmissionEvent responseSubmissionEvent = (ResponseSubmissionEvent) event;
185             String JavaDoc content = responseSubmissionEvent.getContent();
186
187             if (!BlojsomUtils.checkNullOrBlank(content)) {
188                 List JavaDoc spamPhrases = loadSpamPhrases(responseSubmissionEvent.getBlog());
189
190                 boolean deletePhraseSpam = DEFAULT_DELETE_PHRASESPAM;
191                 Map JavaDoc metaData = responseSubmissionEvent.getMetaData();
192
193                 String JavaDoc deletePhraseSpamValue = responseSubmissionEvent.getBlog().getProperty(DELETE_PHRASESPAM);
194                 if (!BlojsomUtils.checkNullOrBlank(deletePhraseSpamValue)) {
195                     deletePhraseSpam = Boolean.valueOf(deletePhraseSpamValue).booleanValue();
196                 }
197
198                 String JavaDoc phrase;
199                 Iterator JavaDoc phraseIterator = spamPhrases.iterator();
200                 boolean phraseSpamFound = false;
201                 while (phraseIterator.hasNext()) {
202                     phrase = (String JavaDoc) phraseIterator.next();
203
204                     if (content.matches(phrase) || content.indexOf(phrase) != -1) {
205                         phraseSpamFound = true;
206                         break;
207                     }
208                 }
209
210                 if (phraseSpamFound) {
211                     if (!deletePhraseSpam) {
212                         if (_logger.isDebugEnabled()) {
213                             _logger.debug("Marking response for moderation");
214                         }
215                     } else{
216                         if (_logger.isDebugEnabled()) {
217                             _logger.debug("Marking response for automatic deletion");
218                         }
219                     }
220
221                     if (responseSubmissionEvent instanceof CommentResponseSubmissionEvent) {
222                         if (!deletePhraseSpam) {
223                             metaData.put(CommentModerationPlugin.BLOJSOM_COMMENT_MODERATION_PLUGIN_APPROVED, Boolean.FALSE.toString());
224                         } else {
225                             metaData.put(CommentPlugin.BLOJSOM_PLUGIN_COMMENT_METADATA_DESTROY, Boolean.TRUE);
226                         }
227                     } else if (responseSubmissionEvent instanceof TrackbackResponseSubmissionEvent) {
228                         if (!deletePhraseSpam) {
229                             metaData.put(TrackbackModerationPlugin.BLOJSOM_TRACKBACK_MODERATION_PLUGIN_APPROVED, Boolean.FALSE.toString());
230                         } else {
231                             metaData.put(TrackbackPlugin.BLOJSOM_PLUGIN_TRACKBACK_METADATA_DESTROY, Boolean.TRUE);
232                         }
233                     } else if (responseSubmissionEvent instanceof PingbackResponseSubmissionEvent) {
234                         if (deletePhraseSpam) {
235                             metaData.put(PingbackPlugin.BLOJSOM_PLUGIN_PINGBACK_METADATA_DESTROY, Boolean.TRUE);
236                         }
237                     }
238                 }
239             } else {
240                 if (_logger.isDebugEnabled()) {
241                     _logger.debug("No content to evaluate for response");
242                 }
243             }
244         }
245     }
246 }
Popular Tags