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.admin;
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.fetcher.Fetcher;
037: import org.blojsom.fetcher.FetcherException;
038: import org.blojsom.plugin.PluginException;
039: import org.blojsom.plugin.admin.WebAdminPlugin;
040: import org.blojsom.util.BlojsomConstants;
041: import org.blojsom.util.BlojsomUtils;
042:
043: import javax.servlet.http.HttpServletRequest;
044: import javax.servlet.http.HttpServletResponse;
045: import java.io.BufferedReader;
046: import java.io.IOException;
047: import java.io.StringReader;
048: import java.util.ArrayList;
049: import java.util.List;
050: import java.util.Map;
051:
052: /**
053: * IP address moderation administration plugin
054: *
055: * @author David Czarnecki
056: * @version $Id: IPAddressModerationAdminPlugin.java,v 1.2 2007/01/17 02:35:12 czarneckid Exp $
057: * @since blojsom 3.0
058: */
059: public class IPAddressModerationAdminPlugin extends WebAdminPlugin {
060:
061: private Log _logger = LogFactory
062: .getLog(IPAddressModerationAdminPlugin.class);
063:
064: private static final String BLACKLIST = "blacklist";
065: private static final String IP_BLACKLIST_IP = "ip-blacklist";
066: private static final String IP_WHITELIST_IP = "ip-whitelist";
067:
068: // Localization constants
069: private static final String FAILED_IP_ADDRESS_MODERATION_PERMISSION_KEY = "failed.ip.address.moderation.permission.text";
070: private static final String ADDED_IP_TO_BLACKLIST_KEY = "added.ip.to.blacklist.text";
071: private static final String IP_ALREADY_ADDED_TO_BLACKLIST_KEY = "ip.already.added.to.blacklist.text";
072: private static final String ADDED_IP_TO_WHITELIST_KEY = "added.ip.to.whitelist.text";
073: private static final String IP_ALREADY_ADDED_TO_WHITELIST_KEY = "ip.already.added.to.whitelist.text";
074: private static final String DELETED_IP_ADDRESSES_BLACKLIST_KEY = "deleted.ip.addresses.blacklist.text";
075: private static final String DELETED_IP_ADDRESSES_WHITELIST_KEY = "deleted.ip.addresses.whitelist.text";
076: private static final String NO_IP_ADDRESSES_SELECTED_KEY = "no.ip.addresses.selected.text";
077:
078: // Context
079: private static final String BLOJSOM_PLUGIN_IP_WHITELIST = "BLOJSOM_PLUGIN_IP_WHITELIST";
080: private static final String BLOJSOM_PLUGIN_IP_BLACKLIST = "BLOJSOM_PLUGIN_IP_BLACKLIST";
081:
082: // Pages
083: private static final String EDIT_IP_MODERATION_SETTINGS_PAGE = "/org/blojsom/plugin/moderation/admin/templates/admin-edit-ip-moderation-settings";
084:
085: // Form itmes
086: private static final String IP_ADDRESS = "ip-address";
087: private static final String LIST_TYPE = "list-type";
088:
089: // Actions
090: private static final String ADD_IP_ADDRESS_ACTION = "add-ip-address";
091: private static final String DELETE_IP_ADDRESS_ACTION = "delete-ip-address";
092:
093: // Permissions
094: private static final String IP_MODERATION_PERMISSION = "ip_moderation";
095:
096: private Fetcher _fetcher;
097:
098: /**
099: * 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 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 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 httpServletRequest,
153: HttpServletResponse httpServletResponse, Blog blog,
154: Map context, Entry[] entries) throws PluginException {
155: entries = super .process(httpServletRequest,
156: httpServletResponse, blog, context, entries);
157:
158: String page = BlojsomUtils.getRequestValue(
159: BlojsomConstants.PAGE_PARAM, httpServletRequest);
160:
161: String username = getUsernameFromSession(httpServletRequest,
162: blog);
163: if (!checkPermission(blog, null, username,
164: IP_MODERATION_PERMISSION)) {
165: httpServletRequest.setAttribute(
166: BlojsomConstants.PAGE_PARAM,
167: ADMIN_ADMINISTRATION_PAGE);
168: addOperationResultMessage(context, getAdminResource(
169: FAILED_IP_ADDRESS_MODERATION_PERMISSION_KEY,
170: FAILED_IP_ADDRESS_MODERATION_PERMISSION_KEY, blog
171: .getBlogAdministrationLocale()));
172:
173: return entries;
174: }
175:
176: if (ADMIN_LOGIN_PAGE.equals(page)) {
177: return entries;
178: } else {
179: String action = BlojsomUtils.getRequestValue(ACTION_PARAM,
180: httpServletRequest);
181: List ipAddressesFromBlacklist = loadIPList(blog,
182: IP_BLACKLIST_IP);
183: List ipAddressesFromWhitelist = loadIPList(blog,
184: IP_WHITELIST_IP);
185: String listType = BlojsomUtils.getRequestValue(LIST_TYPE,
186: httpServletRequest);
187:
188: if (ADD_IP_ADDRESS_ACTION.equals(action)) {
189: String ipAddress = BlojsomUtils.getRequestValue(
190: IP_ADDRESS, httpServletRequest);
191:
192: if (BLACKLIST.equals(listType)) {
193: if (!ipAddressesFromBlacklist.contains(ipAddress)) {
194: ipAddressesFromBlacklist.add(ipAddress);
195: blog.setProperty(IP_BLACKLIST_IP, BlojsomUtils
196: .listToString(ipAddressesFromBlacklist,
197: "\n"));
198: try {
199: _fetcher.saveBlog(blog);
200: } catch (FetcherException e) {
201: if (_logger.isErrorEnabled()) {
202: _logger.error(e);
203: }
204: }
205: addOperationResultMessage(
206: context,
207: formatAdminResource(
208: ADDED_IP_TO_BLACKLIST_KEY,
209: ADDED_IP_TO_BLACKLIST_KEY,
210: blog
211: .getBlogAdministrationLocale(),
212: new Object[] { ipAddress }));
213: } else {
214: addOperationResultMessage(
215: context,
216: formatAdminResource(
217: IP_ALREADY_ADDED_TO_BLACKLIST_KEY,
218: IP_ALREADY_ADDED_TO_BLACKLIST_KEY,
219: blog
220: .getBlogAdministrationLocale(),
221: new Object[] { ipAddress }));
222: }
223: } else {
224: if (!ipAddressesFromWhitelist.contains(ipAddress)) {
225: ipAddressesFromWhitelist.add(ipAddress);
226: blog.setProperty(IP_WHITELIST_IP, BlojsomUtils
227: .listToString(ipAddressesFromWhitelist,
228: "\n"));
229: try {
230: _fetcher.saveBlog(blog);
231: } catch (FetcherException e) {
232: if (_logger.isErrorEnabled()) {
233: _logger.error(e);
234: }
235: }
236: addOperationResultMessage(
237: context,
238: formatAdminResource(
239: ADDED_IP_TO_WHITELIST_KEY,
240: ADDED_IP_TO_WHITELIST_KEY,
241: blog
242: .getBlogAdministrationLocale(),
243: new Object[] { ipAddress }));
244: } else {
245: addOperationResultMessage(
246: context,
247: formatAdminResource(
248: IP_ALREADY_ADDED_TO_WHITELIST_KEY,
249: IP_ALREADY_ADDED_TO_WHITELIST_KEY,
250: blog
251: .getBlogAdministrationLocale(),
252: new Object[] { ipAddress }));
253: }
254: }
255: } else if (DELETE_IP_ADDRESS_ACTION.equals(action)) {
256: String[] ipAddressesToDelete = httpServletRequest
257: .getParameterValues(IP_ADDRESS);
258:
259: if (ipAddressesToDelete != null
260: && ipAddressesToDelete.length > 0) {
261: if (BLACKLIST.equals(listType)) {
262: for (int i = 0; i < ipAddressesToDelete.length; i++) {
263: ipAddressesFromBlacklist.set(Integer
264: .parseInt(ipAddressesToDelete[i]),
265: null);
266: }
267:
268: ipAddressesFromBlacklist = BlojsomUtils
269: .removeNullValues(ipAddressesFromBlacklist);
270: blog.setProperty(IP_BLACKLIST_IP, BlojsomUtils
271: .listToString(ipAddressesFromBlacklist,
272: "\n"));
273: try {
274: _fetcher.saveBlog(blog);
275: } catch (FetcherException e) {
276: if (_logger.isErrorEnabled()) {
277: _logger.error(e);
278: }
279: }
280: addOperationResultMessage(
281: context,
282: formatAdminResource(
283: DELETED_IP_ADDRESSES_BLACKLIST_KEY,
284: DELETED_IP_ADDRESSES_BLACKLIST_KEY,
285: blog
286: .getBlogAdministrationLocale(),
287: new Object[] { new Integer(
288: ipAddressesToDelete.length) }));
289: } else {
290: for (int i = 0; i < ipAddressesToDelete.length; i++) {
291: ipAddressesFromWhitelist.set(Integer
292: .parseInt(ipAddressesToDelete[i]),
293: null);
294: }
295:
296: ipAddressesFromWhitelist = BlojsomUtils
297: .removeNullValues(ipAddressesFromWhitelist);
298: blog.setProperty(IP_WHITELIST_IP, BlojsomUtils
299: .listToString(ipAddressesFromWhitelist,
300: "\n"));
301: try {
302: _fetcher.saveBlog(blog);
303: } catch (FetcherException e) {
304: if (_logger.isErrorEnabled()) {
305: _logger.error(e);
306: }
307: }
308: addOperationResultMessage(
309: context,
310: formatAdminResource(
311: DELETED_IP_ADDRESSES_WHITELIST_KEY,
312: DELETED_IP_ADDRESSES_WHITELIST_KEY,
313: blog
314: .getBlogAdministrationLocale(),
315: new Object[] { new Integer(
316: ipAddressesToDelete.length) }));
317: }
318: } else {
319: addOperationResultMessage(context,
320: getAdminResource(
321: NO_IP_ADDRESSES_SELECTED_KEY,
322: NO_IP_ADDRESSES_SELECTED_KEY,
323: blog.getBlogAdministrationLocale()));
324: }
325: }
326:
327: context.put(BLOJSOM_PLUGIN_IP_BLACKLIST,
328: ipAddressesFromBlacklist);
329: context.put(BLOJSOM_PLUGIN_IP_WHITELIST,
330: ipAddressesFromWhitelist);
331: }
332:
333: return entries;
334: }
335:
336: /**
337: * Load the list of IP addresses from whitelist or blacklist from the blog properties
338: *
339: * @param blog {@link Blog}
340: * @param property Whitelist or Blacklist property
341: * @return List of IP addresses
342: */
343: protected List loadIPList(Blog blog, String property) {
344: ArrayList ipAddresses = new ArrayList();
345: String ipAddressValues = blog.getProperty(property);
346:
347: if (!BlojsomUtils.checkNullOrBlank(ipAddressValues)) {
348: try {
349: BufferedReader br = new BufferedReader(
350: new StringReader(ipAddressValues));
351: String ipAddress;
352:
353: while ((ipAddress = br.readLine()) != null) {
354: ipAddresses.add(ipAddress);
355: }
356:
357: br.close();
358: } catch (IOException e) {
359: if (_logger.isErrorEnabled()) {
360: _logger.error(e);
361: }
362: }
363: }
364:
365: return ipAddresses;
366: }
367: }
|