KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > blojsom > plugin > moderation > admin > IPAddressModerationAdminPlugin


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.admin;
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.fetcher.Fetcher;
38 import org.blojsom.fetcher.FetcherException;
39 import org.blojsom.plugin.PluginException;
40 import org.blojsom.plugin.admin.WebAdminPlugin;
41 import org.blojsom.util.BlojsomConstants;
42 import org.blojsom.util.BlojsomUtils;
43
44 import javax.servlet.http.HttpServletRequest JavaDoc;
45 import javax.servlet.http.HttpServletResponse JavaDoc;
46 import java.io.BufferedReader JavaDoc;
47 import java.io.IOException JavaDoc;
48 import java.io.StringReader JavaDoc;
49 import java.util.ArrayList JavaDoc;
50 import java.util.List JavaDoc;
51 import java.util.Map JavaDoc;
52
53 /**
54  * IP address moderation administration plugin
55  *
56  * @author David Czarnecki
57  * @version $Id: IPAddressModerationAdminPlugin.java,v 1.1 2006/03/27 13:34:43 czarneckid Exp $
58  * @since blojsom 3.0
59  */

60 public class IPAddressModerationAdminPlugin extends WebAdminPlugin {
61
62     private Log _logger = LogFactory.getLog(IPAddressModerationAdminPlugin.class);
63
64     private static final String JavaDoc BLACKLIST = "blacklist";
65     private static final String JavaDoc IP_BLACKLIST_IP = "ip-blacklist";
66     private static final String JavaDoc IP_WHITELIST_IP = "ip-whitelist";
67
68     // Localization constants
69
private static final String JavaDoc FAILED_IP_ADDRESS_MODERATION_PERMISSION_KEY = "failed.ip.address.moderation.permission.text";
70     private static final String JavaDoc ADDED_IP_TO_BLACKLIST_KEY = "added.ip.to.blacklist.text";
71     private static final String JavaDoc IP_ALREADY_ADDED_TO_BLACKLIST_KEY = "ip.already.added.to.blacklist.text";
72     private static final String JavaDoc ADDED_IP_TO_WHITELIST_KEY = "added.ip.to.whitelist.text";
73     private static final String JavaDoc IP_ALREADY_ADDED_TO_WHITELIST_KEY = "ip.already.added.to.whitelist.text";
74     private static final String JavaDoc DELETED_IP_ADDRESSES_BLACKLIST_KEY = "deleted.ip.addresses.blacklist.text";
75     private static final String JavaDoc DELETED_IP_ADDRESSES_WHITELIST_KEY = "deleted.ip.addresses.whitelist.text";
76     private static final String JavaDoc NO_IP_ADDRESSES_SELECTED_KEY = "no.ip.addresses.selected.text";
77
78     // Context
79
private static final String JavaDoc BLOJSOM_PLUGIN_IP_WHITELIST = "BLOJSOM_PLUGIN_IP_WHITELIST";
80     private static final String JavaDoc BLOJSOM_PLUGIN_IP_BLACKLIST = "BLOJSOM_PLUGIN_IP_BLACKLIST";
81
82     // Pages
83
private static final String JavaDoc EDIT_IP_MODERATION_SETTINGS_PAGE = "/org/blojsom/plugin/moderation/admin/templates/admin-edit-ip-moderation-settings";
84
85     // Form itmes
86
private static final String JavaDoc IP_ADDRESS = "ip-address";
87     private static final String JavaDoc LIST_TYPE = "list-type";
88
89     // Actions
90
private static final String JavaDoc ADD_IP_ADDRESS_ACTION = "add-ip-address";
91     private static final String JavaDoc DELETE_IP_ADDRESS_ACTION = "delete-ip-address";
92
93     // Permissions
94
private static final String JavaDoc IP_MODERATION_PERMISSION = "ip_moderation";
95
96     private Fetcher _fetcher;
97
98     /**
99      * Create a new instance of the IP address moderation administration plugin
100      */

101     public IPAddressModerationAdminPlugin() {
102     }
103
104     /**
105      * Return the display name for the plugin
106      *
107      * @return Display name for the plugin
108      */

109     public String JavaDoc getDisplayName() {
110         return "IP Address Moderation plugin";
111     }
112
113     /**
114      * Return the name of the initial editing page for the plugin
115      *
116      * @return Name of the initial editing page for the plugin
117      */

118     public String JavaDoc getInitialPage() {
119         return EDIT_IP_MODERATION_SETTINGS_PAGE;
120     }
121
122     /**
123      * Set the {@link Fetcher}
124      *
125      * @param fetcher {@link Fetcher}
126      */

127     public void setFetcher(Fetcher fetcher) {
128         _fetcher = fetcher;
129     }
130
131     /**
132      * Initialize this plugin. This method only called when the plugin is instantiated.
133      *
134      * @throws org.blojsom.plugin.PluginException
135      * If there is an error initializing the plugin
136      */

137     public void init() throws PluginException {
138         super.init();
139     }
140
141     /**
142      * Process the blog entries
143      *
144      * @param httpServletRequest Request
145      * @param httpServletResponse Response
146      * @param blog {@link Blog} instance
147      * @param context Context
148      * @param entries Blog entries retrieved for the particular request
149      * @return Modified set of blog entries
150      * @throws PluginException If there is an error processing the blog entries
151      */

152     public Entry[] process(HttpServletRequest JavaDoc httpServletRequest, HttpServletResponse JavaDoc httpServletResponse, Blog blog, Map JavaDoc context, Entry[] entries) throws PluginException {
153         entries = super.process(httpServletRequest, httpServletResponse, blog, context, entries);
154
155         String JavaDoc page = BlojsomUtils.getRequestValue(BlojsomConstants.PAGE_PARAM, httpServletRequest);
156
157         String JavaDoc username = getUsernameFromSession(httpServletRequest, blog);
158         if (!checkPermission(blog, null, username, IP_MODERATION_PERMISSION)) {
159             httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, ADMIN_ADMINISTRATION_PAGE);
160             addOperationResultMessage(context, getAdminResource(FAILED_IP_ADDRESS_MODERATION_PERMISSION_KEY, FAILED_IP_ADDRESS_MODERATION_PERMISSION_KEY, blog.getBlogAdministrationLocale()));
161
162             return entries;
163         }
164
165         if (ADMIN_LOGIN_PAGE.equals(page)) {
166             return entries;
167         } else {
168             String JavaDoc action = BlojsomUtils.getRequestValue(ACTION_PARAM, httpServletRequest);
169             List JavaDoc ipAddressesFromBlacklist = loadIPList(blog, IP_BLACKLIST_IP);
170             List JavaDoc ipAddressesFromWhitelist = loadIPList(blog, IP_WHITELIST_IP);
171             String JavaDoc listType = BlojsomUtils.getRequestValue(LIST_TYPE, httpServletRequest);
172
173             if (ADD_IP_ADDRESS_ACTION.equals(action)) {
174                 String JavaDoc ipAddress = BlojsomUtils.getRequestValue(IP_ADDRESS, httpServletRequest);
175
176                 if (BLACKLIST.equals(listType)) {
177                     if (!ipAddressesFromBlacklist.contains(ipAddress)) {
178                         ipAddressesFromBlacklist.add(ipAddress);
179                         blog.setProperty(IP_BLACKLIST_IP, BlojsomUtils.listToString(ipAddressesFromBlacklist, "\n"));
180                         try {
181                             _fetcher.saveBlog(blog);
182                         } catch (FetcherException e) {
183                             if (_logger.isErrorEnabled()) {
184                                 _logger.error(e);
185                             }
186                         }
187                         addOperationResultMessage(context, formatAdminResource(ADDED_IP_TO_BLACKLIST_KEY, ADDED_IP_TO_BLACKLIST_KEY, blog.getBlogAdministrationLocale(), new Object JavaDoc[] {ipAddress}));
188                     } else {
189                         addOperationResultMessage(context, formatAdminResource(IP_ALREADY_ADDED_TO_BLACKLIST_KEY, IP_ALREADY_ADDED_TO_BLACKLIST_KEY, blog.getBlogAdministrationLocale(), new Object JavaDoc[] {ipAddress}));
190                     }
191                 } else {
192                     if (!ipAddressesFromWhitelist.contains(ipAddress)) {
193                         ipAddressesFromWhitelist.add(ipAddress);
194                         blog.setProperty(IP_WHITELIST_IP, BlojsomUtils.listToString(ipAddressesFromWhitelist, "\n"));
195                         try {
196                             _fetcher.saveBlog(blog);
197                         } catch (FetcherException e) {
198                             if (_logger.isErrorEnabled()) {
199                                 _logger.error(e);
200                             }
201                         }
202                         addOperationResultMessage(context, formatAdminResource(ADDED_IP_TO_WHITELIST_KEY, ADDED_IP_TO_WHITELIST_KEY, blog.getBlogAdministrationLocale(), new Object JavaDoc[] {ipAddress}));
203                     } else {
204                         addOperationResultMessage(context, formatAdminResource(IP_ALREADY_ADDED_TO_WHITELIST_KEY, IP_ALREADY_ADDED_TO_WHITELIST_KEY, blog.getBlogAdministrationLocale(), new Object JavaDoc[] {ipAddress}));
205                     }
206                 }
207             } else if (DELETE_IP_ADDRESS_ACTION.equals(action)) {
208                 String JavaDoc[] ipAddressesToDelete = httpServletRequest.getParameterValues(IP_ADDRESS);
209
210                 if (ipAddressesToDelete != null && ipAddressesToDelete.length > 0) {
211                     if (BLACKLIST.equals(listType)) {
212                         for (int i = 0; i < ipAddressesToDelete.length; i++) {
213                             ipAddressesFromBlacklist.set(Integer.parseInt(ipAddressesToDelete[i]), null);
214                         }
215
216                         ipAddressesFromBlacklist = BlojsomUtils.removeNullValues(ipAddressesFromBlacklist);
217                         blog.setProperty(IP_BLACKLIST_IP, BlojsomUtils.listToString(ipAddressesFromBlacklist, "\n"));
218                         try {
219                             _fetcher.saveBlog(blog);
220                         } catch (FetcherException e) {
221                             if (_logger.isErrorEnabled()) {
222                                 _logger.error(e);
223                             }
224                         }
225                         addOperationResultMessage(context, formatAdminResource(DELETED_IP_ADDRESSES_BLACKLIST_KEY, DELETED_IP_ADDRESSES_BLACKLIST_KEY, blog.getBlogAdministrationLocale(), new Object JavaDoc[] {new Integer JavaDoc(ipAddressesToDelete.length)}));
226                     } else {
227                         for (int i = 0; i < ipAddressesToDelete.length; i++) {
228                             ipAddressesFromWhitelist.set(Integer.parseInt(ipAddressesToDelete[i]), null);
229                         }
230
231                         ipAddressesFromWhitelist = BlojsomUtils.removeNullValues(ipAddressesFromWhitelist);
232                         blog.setProperty(IP_WHITELIST_IP, BlojsomUtils.listToString(ipAddressesFromWhitelist, "\n"));
233                         try {
234                             _fetcher.saveBlog(blog);
235                         } catch (FetcherException e) {
236                             if (_logger.isErrorEnabled()) {
237                                 _logger.error(e);
238                             }
239                         }
240                         addOperationResultMessage(context, formatAdminResource(DELETED_IP_ADDRESSES_WHITELIST_KEY, DELETED_IP_ADDRESSES_WHITELIST_KEY, blog.getBlogAdministrationLocale(), new Object JavaDoc[] {new Integer JavaDoc(ipAddressesToDelete.length)}));
241                     }
242                 } else {
243                     addOperationResultMessage(context, getAdminResource(NO_IP_ADDRESSES_SELECTED_KEY, NO_IP_ADDRESSES_SELECTED_KEY, blog.getBlogAdministrationLocale()));
244                 }
245             }
246
247             context.put(BLOJSOM_PLUGIN_IP_BLACKLIST, ipAddressesFromBlacklist);
248             context.put(BLOJSOM_PLUGIN_IP_WHITELIST, ipAddressesFromWhitelist);
249         }
250
251         return entries;
252     }
253
254     /**
255      * Load the list of IP addresses from whitelist or blacklist from the blog properties
256      *
257      * @param blog {@link Blog}
258      * @param property Whitelist or Blacklist property
259      * @return List of IP addresses
260      */

261     protected List JavaDoc loadIPList(Blog blog, String JavaDoc property) {
262         ArrayList JavaDoc ipAddresses = new ArrayList JavaDoc();
263         String JavaDoc ipAddressValues = blog.getProperty(property);
264
265         if (!BlojsomUtils.checkNullOrBlank(ipAddressValues)) {
266             try {
267                 BufferedReader JavaDoc br = new BufferedReader JavaDoc(new StringReader JavaDoc(ipAddressValues));
268                 String JavaDoc ipAddress;
269
270                 while ((ipAddress = br.readLine()) != null) {
271                     ipAddresses.add(ipAddress);
272                 }
273
274                 br.close();
275             } catch (IOException JavaDoc e) {
276                 if (_logger.isErrorEnabled()) {
277                     _logger.error(e);
278                 }
279             }
280         }
281
282         return ipAddresses;
283     }
284 }
Popular Tags