KickJava   Java API By Example, From Geeks To Geeks.

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


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  * IP address moderation plugin
65  *
66  * @author David Czarnecki
67  * @version $Id: IPAddressModerationPlugin.java,v 1.1 2006/03/27 13:34:43 czarneckid Exp $
68  * @since blojsom 3.0
69  */

70 public class IPAddressModerationPlugin implements Plugin, Listener {
71
72     private Log _logger = LogFactory.getLog(IPAddressModerationPlugin.class);
73
74     private static final String JavaDoc IP_BLACKLIST_IP = "ip-blacklist";
75     private static final String JavaDoc IP_WHITELIST_IP = "ip-whitelist";
76     private static final String JavaDoc DELETE_IPSPAM = "delete-ipspam";
77     private static final boolean DEFAULT_DELETE_IPSPAM = false;
78
79     private EventBroadcaster _eventBroadcaster;
80
81     /**
82      * Create a new instance of the IP address moderation plugin
83      */

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

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

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

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

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

136     public void destroy() throws PluginException {
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      * Load the list of IP addresses from whitelist or blacklist from the blog properties
149      *
150      * @param blog {@link Blog}
151      * @param property Whitelist or Blacklist property
152      * @return List of IP addresses
153      */

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

183     public void processEvent(Event event) {
184         if (event instanceof ResponseSubmissionEvent) {
185             ResponseSubmissionEvent responseSubmissionEvent = (ResponseSubmissionEvent) event;
186             String JavaDoc requestIPAddress = responseSubmissionEvent.getHttpServletRequest().getRemoteAddr();
187
188             if (!BlojsomUtils.checkNullOrBlank(requestIPAddress)) {
189                 boolean ipInWhitelist = false;
190                 List JavaDoc ipAddresses = loadIPList(responseSubmissionEvent.getBlog(), IP_WHITELIST_IP);
191
192                 if (ipAddresses.size() > 0) {
193                     String JavaDoc ipAddress;
194                     Iterator JavaDoc ipIterator = ipAddresses.iterator();
195
196                     while (ipIterator.hasNext()) {
197                         ipAddress = (String JavaDoc) ipIterator.next();
198                         if (requestIPAddress.matches(ipAddress) || requestIPAddress.indexOf(ipAddress) != -1) {
199                             if (_logger.isDebugEnabled()) {
200                                 _logger.debug("IP address found in whitelist: " + requestIPAddress);
201                             }
202
203                             ipInWhitelist = true;
204                             break;
205                         }
206                     }
207                 } else {
208                     if (_logger.isDebugEnabled()) {
209                         _logger.debug("IP address whitelist not populated");
210                     }
211                 }
212
213                 ipAddresses = loadIPList(responseSubmissionEvent.getBlog(), IP_BLACKLIST_IP);
214                 if (ipAddresses.size() > 0 && !ipInWhitelist) {
215                     String JavaDoc ipAddress;
216                     Iterator JavaDoc ipIterator = ipAddresses.iterator();
217
218                     boolean deleteIPSpam = DEFAULT_DELETE_IPSPAM;
219                     Map JavaDoc metaData = responseSubmissionEvent.getMetaData();
220
221                     String JavaDoc deleteIPSpamValue = responseSubmissionEvent.getBlog().getProperty(DELETE_IPSPAM);
222                     if (!BlojsomUtils.checkNullOrBlank(deleteIPSpamValue)) {
223                         deleteIPSpam = Boolean.valueOf(deleteIPSpamValue).booleanValue();
224                     }
225
226                     boolean ipSpamFound = false;
227                     while (ipIterator.hasNext()) {
228                         ipAddress = (String JavaDoc) ipIterator.next();
229                         if (requestIPAddress.matches(ipAddress) || requestIPAddress.indexOf(ipAddress) != -1) {
230                             if (_logger.isDebugEnabled()) {
231                                 _logger.debug("IP address found in blacklist: " + requestIPAddress);
232                             }
233
234                             ipSpamFound = true;
235                             break;
236                         }
237                     }
238
239                     if (ipSpamFound) {
240                         if (!deleteIPSpam) {
241                             if (_logger.isDebugEnabled()) {
242                                 _logger.debug("Marking response for moderation");
243                             }
244                         } else {
245                             if (_logger.isDebugEnabled()) {
246                                 _logger.debug("Marking response for automatic deletion");
247                             }
248                         }
249
250                         if (responseSubmissionEvent instanceof CommentResponseSubmissionEvent) {
251                             if (!deleteIPSpam) {
252                                 metaData.put(CommentModerationPlugin.BLOJSOM_COMMENT_MODERATION_PLUGIN_APPROVED, Boolean.FALSE.toString());
253                             } else {
254                                 metaData.put(CommentPlugin.BLOJSOM_PLUGIN_COMMENT_METADATA_DESTROY, Boolean.TRUE);
255                             }
256                         } else if (responseSubmissionEvent instanceof TrackbackResponseSubmissionEvent) {
257                             if (!deleteIPSpam) {
258                                 metaData.put(TrackbackModerationPlugin.BLOJSOM_TRACKBACK_MODERATION_PLUGIN_APPROVED, Boolean.FALSE.toString());
259                             } else {
260                                 metaData.put(TrackbackPlugin.BLOJSOM_PLUGIN_TRACKBACK_METADATA_DESTROY, Boolean.TRUE);
261                             }
262                         } else if (responseSubmissionEvent instanceof PingbackResponseSubmissionEvent) {
263                             if (deleteIPSpam) {
264                                 metaData.put(PingbackPlugin.BLOJSOM_PLUGIN_PINGBACK_METADATA_DESTROY, Boolean.TRUE);
265                             }
266                         }
267                     }
268                 } else {
269                     if (_logger.isDebugEnabled()) {
270                         _logger.debug("IP address blacklist not populated");
271                     }
272                 }
273             } else {
274                 if (_logger.isDebugEnabled()) {
275                     _logger.debug("IP address not available");
276                 }
277             }
278         }
279     }
280 }
Popular Tags