KickJava   Java API By Example, From Geeks To Geeks.

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


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
52 import javax.servlet.http.HttpServletRequest JavaDoc;
53 import javax.servlet.http.HttpServletResponse JavaDoc;
54 import java.net.InetAddress JavaDoc;
55 import java.net.UnknownHostException JavaDoc;
56 import java.util.Map JavaDoc;
57
58 /**
59  * Open proxy check plugin for comments and trackbacks. This plugin queries the <a HREF="http://dsbl.org/main">Distributed
60  * Sender Blackhole List</a> if a comment or trackback is submitted. If the IP address of the requesting
61  * host is on the blacklist, the comment or trackback is marked for moderation if moderation is enabled,
62  * otherwise it is destroyed.
63  * This plugin can work in conjunction with other moderation plugins as it looks for the comment or
64  * trackback metadata.
65  *
66  * @author David Czarnecki
67  * @version $Id: OpenProxyModerationPlugin.java,v 1.1 2006/03/26 21:36:28 czarneckid Exp $
68  * @since blojsom 3.0
69  */

70 public class OpenProxyModerationPlugin implements Plugin, Listener {
71
72     private Log _logger = LogFactory.getLog(OpenProxyModerationPlugin.class);
73
74     private static final String JavaDoc DELETE_OPENPROXY_SPAM_IP = "delete-openproxy-spam";
75
76     private EventBroadcaster _eventBroadcaster;
77
78     /**
79      * Create a new instance of the open proxy comment check plugin
80      */

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

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

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

114     public Entry[] process(HttpServletRequest JavaDoc httpServletRequest, HttpServletResponse JavaDoc httpServletResponse, Blog blog, Map JavaDoc context, Entry[] entries) throws PluginException {
115         return entries;
116     }
117
118     /**
119      * Handle an event broadcast from another component
120      *
121      * @param event {@link Event} to be handled
122      */

123     public void handleEvent(Event event) {
124     }
125
126     /**
127      * Process an event from another component
128      *
129      * @param event {@link Event} to be handled
130      */

131     public void processEvent(Event event) {
132         if (event instanceof ResponseSubmissionEvent) {
133             ResponseSubmissionEvent responseSubmissionEvent = (ResponseSubmissionEvent) event;
134
135             String JavaDoc remoteIP = responseSubmissionEvent.getHttpServletRequest().getRemoteAddr();
136             String JavaDoc[] ipAddress = remoteIP.split("\\.");
137             StringBuffer JavaDoc reversedAddress = new StringBuffer JavaDoc();
138             reversedAddress.append(ipAddress[3]).append(".").append(ipAddress[2]).append(".").append(ipAddress[1]).append(".").append(ipAddress[0]);
139
140             try {
141                 InetAddress JavaDoc inetAddress = InetAddress.getByName(reversedAddress + ".list.dsbl.org");
142                 Map JavaDoc metaData = responseSubmissionEvent.getMetaData();
143
144                 String JavaDoc deleteOpenProxySpamPropertyValue = responseSubmissionEvent.getBlog().getProperty(DELETE_OPENPROXY_SPAM_IP);
145                 boolean deleteOpenProxySpam = Boolean.valueOf(deleteOpenProxySpamPropertyValue).booleanValue();
146
147                 if (responseSubmissionEvent instanceof CommentResponseSubmissionEvent) {
148                     if (!deleteOpenProxySpam) {
149                         metaData.put(CommentModerationPlugin.BLOJSOM_COMMENT_MODERATION_PLUGIN_APPROVED, Boolean.FALSE.toString());
150                     } else {
151                         metaData.put(CommentPlugin.BLOJSOM_PLUGIN_COMMENT_METADATA_DESTROY, Boolean.TRUE);
152                     }
153                 } else if (responseSubmissionEvent instanceof TrackbackResponseSubmissionEvent) {
154                     if (!deleteOpenProxySpam) {
155                         metaData.put(TrackbackModerationPlugin.BLOJSOM_TRACKBACK_MODERATION_PLUGIN_APPROVED, Boolean.FALSE.toString());
156                     } else {
157                         metaData.put(TrackbackPlugin.BLOJSOM_PLUGIN_TRACKBACK_METADATA_DESTROY, Boolean.TRUE);
158                     }
159                 } else if (responseSubmissionEvent instanceof PingbackResponseSubmissionEvent) {
160                     if (deleteOpenProxySpam) {
161                         metaData.put(PingbackPlugin.BLOJSOM_PLUGIN_PINGBACK_METADATA_DESTROY, Boolean.TRUE);
162                     }
163                 }
164
165                 if (_logger.isDebugEnabled()) {
166                     _logger.debug("Failed open proxy check for response submission for IP: " + inetAddress.getHostAddress() + "/" + inetAddress.getHostName());
167                 }
168             } catch (UnknownHostException JavaDoc e) {
169                 // The IP address is unknown to the DSBL server
170
}
171         }
172     }
173
174     /**
175      * Perform any cleanup for the plugin. Called after {@link #process}.
176      *
177      * @throws org.blojsom.plugin.PluginException
178      * If there is an error performing cleanup for this plugin
179      */

180     public void cleanup() throws PluginException {
181     }
182
183     /**
184      * Called when BlojsomServlet is taken out of service
185      *
186      * @throws org.blojsom.plugin.PluginException
187      * If there is an error in finalizing this plugin
188      */

189     public void destroy() throws PluginException {
190     }
191 }
Popular Tags