001: /**
002: * Copyright (c) 2003-2007, David A. Czarnecki
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * Redistributions of source code must retain the above copyright notice, this list of conditions and the
009: * following disclaimer.
010: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
011: * following disclaimer in the documentation and/or other materials provided with the distribution.
012: * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
013: * endorse or promote products derived from this software without specific prior written permission.
014: * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
015: * without prior written permission of David A. Czarnecki.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
018: * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
019: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
021: * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
022: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
025: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026: * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
027: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
028: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
029: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030: */package org.blojsom.plugin.moderation;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.blojsom.blog.Blog;
035: import org.blojsom.blog.Entry;
036: import org.blojsom.event.Event;
037: import org.blojsom.event.EventBroadcaster;
038: import org.blojsom.event.Listener;
039: import org.blojsom.plugin.Plugin;
040: import org.blojsom.plugin.PluginException;
041: import org.blojsom.plugin.comment.CommentModerationPlugin;
042: import org.blojsom.plugin.comment.CommentPlugin;
043: import org.blojsom.plugin.comment.event.CommentResponseSubmissionEvent;
044: import org.blojsom.plugin.pingback.PingbackPlugin;
045: import org.blojsom.plugin.pingback.event.PingbackResponseSubmissionEvent;
046: import org.blojsom.plugin.response.event.ResponseSubmissionEvent;
047: import org.blojsom.plugin.trackback.TrackbackModerationPlugin;
048: import org.blojsom.plugin.trackback.TrackbackPlugin;
049: import org.blojsom.plugin.trackback.event.TrackbackResponseSubmissionEvent;
050: import org.blojsom.util.BlojsomUtils;
051:
052: import javax.servlet.http.HttpServletRequest;
053: import javax.servlet.http.HttpServletResponse;
054: import java.io.BufferedReader;
055: import java.io.IOException;
056: import java.io.StringReader;
057: import java.util.ArrayList;
058: import java.util.Iterator;
059: import java.util.List;
060: import java.util.Map;
061:
062: /**
063: * IP address moderation plugin
064: *
065: * @author David Czarnecki
066: * @version $Id: IPAddressModerationPlugin.java,v 1.2 2007/01/17 02:35:12 czarneckid Exp $
067: * @since blojsom 3.0
068: */
069: public class IPAddressModerationPlugin implements Plugin, Listener {
070:
071: private Log _logger = LogFactory
072: .getLog(IPAddressModerationPlugin.class);
073:
074: private static final String IP_BLACKLIST_IP = "ip-blacklist";
075: private static final String IP_WHITELIST_IP = "ip-whitelist";
076: private static final String DELETE_IPSPAM = "delete-ipspam";
077: private static final boolean DEFAULT_DELETE_IPSPAM = false;
078:
079: private EventBroadcaster _eventBroadcaster;
080:
081: /**
082: * Create a new instance of the IP address moderation plugin
083: */
084: public IPAddressModerationPlugin() {
085: }
086:
087: /**
088: * Set the {@link EventBroadcaster}
089: *
090: * @param eventBroadcaster {@link EventBroadcaster}
091: */
092: public void setEventBroadcaster(EventBroadcaster eventBroadcaster) {
093: _eventBroadcaster = eventBroadcaster;
094: }
095:
096: /**
097: * Initialize this plugin. This method only called when the plugin is instantiated.
098: *
099: * @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 httpServletRequest,
118: HttpServletResponse httpServletResponse, Blog blog,
119: Map context, Entry[] entries) throws PluginException {
120: return entries;
121: }
122:
123: /**
124: * Perform any cleanup for the plugin. Called after {@link #process}.
125: *
126: * @throws org.blojsom.plugin.PluginException
127: * If there is an error performing cleanup for this plugin
128: */
129: public void cleanup() throws PluginException {
130: }
131:
132: /**
133: * Called when BlojsomServlet is taken out of service
134: *
135: * @throws org.blojsom.plugin.PluginException
136: * If there is an error in finalizing this plugin
137: */
138: public void destroy() throws PluginException {
139: }
140:
141: /**
142: * Handle an event broadcast from another component
143: *
144: * @param event {@link org.blojsom.event.Event} to be handled
145: */
146: public void handleEvent(Event event) {
147: }
148:
149: /**
150: * Load the list of IP addresses from whitelist or blacklist from the blog properties
151: *
152: * @param blog {@link Blog}
153: * @param property Whitelist or Blacklist property
154: * @return List of IP addresses
155: */
156: protected List loadIPList(Blog blog, String property) {
157: ArrayList ipAddresses = new ArrayList();
158: String ipAddressValues = blog.getProperty(property);
159:
160: if (!BlojsomUtils.checkNullOrBlank(ipAddressValues)) {
161: try {
162: BufferedReader br = new BufferedReader(
163: new StringReader(ipAddressValues));
164: String ipAddress;
165:
166: while ((ipAddress = br.readLine()) != null) {
167: ipAddresses.add(ipAddress);
168: }
169:
170: br.close();
171: } catch (IOException e) {
172: if (_logger.isErrorEnabled()) {
173: _logger.error(e);
174: }
175: }
176: }
177:
178: return ipAddresses;
179: }
180:
181: /**
182: * Process an event from another component
183: *
184: * @param event {@link org.blojsom.event.Event} to be handled
185: */
186: public void processEvent(Event event) {
187: if (event instanceof ResponseSubmissionEvent) {
188: ResponseSubmissionEvent responseSubmissionEvent = (ResponseSubmissionEvent) event;
189: String requestIPAddress = responseSubmissionEvent
190: .getHttpServletRequest().getRemoteAddr();
191:
192: if (!BlojsomUtils.checkNullOrBlank(requestIPAddress)) {
193: boolean ipInWhitelist = false;
194: List ipAddresses = loadIPList(responseSubmissionEvent
195: .getBlog(), IP_WHITELIST_IP);
196:
197: if (ipAddresses.size() > 0) {
198: String ipAddress;
199: Iterator ipIterator = ipAddresses.iterator();
200:
201: while (ipIterator.hasNext()) {
202: ipAddress = (String) ipIterator.next();
203: if (requestIPAddress.matches(ipAddress)
204: || requestIPAddress.indexOf(ipAddress) != -1) {
205: if (_logger.isDebugEnabled()) {
206: _logger
207: .debug("IP address found in whitelist: "
208: + requestIPAddress);
209: }
210:
211: ipInWhitelist = true;
212: break;
213: }
214: }
215: } else {
216: if (_logger.isDebugEnabled()) {
217: _logger
218: .debug("IP address whitelist not populated");
219: }
220: }
221:
222: ipAddresses = loadIPList(responseSubmissionEvent
223: .getBlog(), IP_BLACKLIST_IP);
224: if (ipAddresses.size() > 0 && !ipInWhitelist) {
225: String ipAddress;
226: Iterator ipIterator = ipAddresses.iterator();
227:
228: boolean deleteIPSpam = DEFAULT_DELETE_IPSPAM;
229: Map metaData = responseSubmissionEvent
230: .getMetaData();
231:
232: String deleteIPSpamValue = responseSubmissionEvent
233: .getBlog().getProperty(DELETE_IPSPAM);
234: if (!BlojsomUtils
235: .checkNullOrBlank(deleteIPSpamValue)) {
236: deleteIPSpam = Boolean.valueOf(
237: deleteIPSpamValue).booleanValue();
238: }
239:
240: boolean ipSpamFound = false;
241: while (ipIterator.hasNext()) {
242: ipAddress = (String) ipIterator.next();
243: if (requestIPAddress.matches(ipAddress)
244: || requestIPAddress.indexOf(ipAddress) != -1) {
245: if (_logger.isDebugEnabled()) {
246: _logger
247: .debug("IP address found in blacklist: "
248: + requestIPAddress);
249: }
250:
251: ipSpamFound = true;
252: break;
253: }
254: }
255:
256: if (ipSpamFound) {
257: if (!deleteIPSpam) {
258: if (_logger.isDebugEnabled()) {
259: _logger
260: .debug("Marking response for moderation");
261: }
262: } else {
263: if (_logger.isDebugEnabled()) {
264: _logger
265: .debug("Marking response for automatic deletion");
266: }
267: }
268:
269: if (responseSubmissionEvent instanceof CommentResponseSubmissionEvent) {
270: if (!deleteIPSpam) {
271: metaData
272: .put(
273: CommentModerationPlugin.BLOJSOM_COMMENT_MODERATION_PLUGIN_APPROVED,
274: Boolean.FALSE
275: .toString());
276: } else {
277: metaData
278: .put(
279: CommentPlugin.BLOJSOM_PLUGIN_COMMENT_METADATA_DESTROY,
280: Boolean.TRUE);
281: }
282: } else if (responseSubmissionEvent instanceof TrackbackResponseSubmissionEvent) {
283: if (!deleteIPSpam) {
284: metaData
285: .put(
286: TrackbackModerationPlugin.BLOJSOM_TRACKBACK_MODERATION_PLUGIN_APPROVED,
287: Boolean.FALSE
288: .toString());
289: } else {
290: metaData
291: .put(
292: TrackbackPlugin.BLOJSOM_PLUGIN_TRACKBACK_METADATA_DESTROY,
293: Boolean.TRUE);
294: }
295: } else if (responseSubmissionEvent instanceof PingbackResponseSubmissionEvent) {
296: if (deleteIPSpam) {
297: metaData
298: .put(
299: PingbackPlugin.BLOJSOM_PLUGIN_PINGBACK_METADATA_DESTROY,
300: Boolean.TRUE);
301: }
302: }
303: }
304: } else {
305: if (_logger.isDebugEnabled()) {
306: _logger
307: .debug("IP address blacklist not populated");
308: }
309: }
310: } else {
311: if (_logger.isDebugEnabled()) {
312: _logger.debug("IP address not available");
313: }
314: }
315: }
316: }
317: }
|