KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Spam phrase moderation administration plugin
55  *
56  * @author David Czarnecki
57  * @version $Id: SpamPhraseModerationAdminPlugin.java,v 1.2 2006/03/27 14:05:46 czarneckid Exp $
58  * @since blojsom 3.0
59  */

60 public class SpamPhraseModerationAdminPlugin extends WebAdminPlugin {
61
62     private Log _logger = LogFactory.getLog(SpamPhraseModerationAdminPlugin.class);
63
64     private static final String JavaDoc SPAM_PHRASE_BLACKLIST_IP = "spam-phrase-blacklist";
65
66     // Localization constants
67
private static final String JavaDoc FAILED_SPAM_PHRASE_PERMISSION_KEY = "failed.spam.phrase.permission.text";
68     private static final String JavaDoc ADDED_SPAM_PHRASE_KEY = "added.spam.phrase.text";
69     private static final String JavaDoc SPAM_PHRASE_ALREADY_ADDED_KEY = "spam.phrase.already.added.text";
70     private static final String JavaDoc DELETED_SPAM_PHRASE_KEY = "deleted.spam.phrase.text";
71     private static final String JavaDoc NO_SPAM_PHRASES_TO_DELETE_KEY = "no.spam.phrases.to.delete.text";
72
73     // Context
74
private static final String JavaDoc BLOJSOM_PLUGIN_SPAM_PHRASES = "BLOJSOM_PLUGIN_SPAM_PHRASES";
75
76     // Pages
77
private static final String JavaDoc EDIT_SPAM_PHRASE_MODERATION_SETTINGS_PAGE = "/org/blojsom/plugin/moderation/admin/templates/admin-edit-spam-phrase-moderation-settings";
78
79     // Form itmes
80
private static final String JavaDoc SPAM_PHRASE = "spam-phrase";
81
82     // Actions
83
private static final String JavaDoc ADD_SPAM_PHRASE_ACTION = "add-spam-phrase";
84     private static final String JavaDoc DELETE_SPAM_PHRASE_ACTION = "delete-spam-phrase";
85
86     // Permissions
87
private static final String JavaDoc SPAM_PHRASE_MODERATION_PERMISSION = "spam_phrase_moderation";
88
89     private Fetcher _fetcher;
90
91     /**
92      * Create a new instance of the spam phrase moderation administration plugin
93      */

94     public SpamPhraseModerationAdminPlugin() {
95     }
96
97     /**
98      * Return the display name for the plugin
99      *
100      * @return Display name for the plugin
101      */

102     public String JavaDoc getDisplayName() {
103         return "Spam Phrase Moderation plugin";
104     }
105
106     /**
107      * Return the name of the initial editing page for the plugin
108      *
109      * @return Name of the initial editing page for the plugin
110      */

111     public String JavaDoc getInitialPage() {
112         return EDIT_SPAM_PHRASE_MODERATION_SETTINGS_PAGE;
113     }
114
115     /**
116      * Set the {@link Fetcher}
117      *
118      * @param fetcher {@link Fetcher}
119      */

120     public void setFetcher(Fetcher fetcher) {
121         _fetcher = fetcher;
122     }
123
124     /**
125      * Initialize this plugin. This method only called when the plugin is instantiated.
126      *
127      * @throws org.blojsom.plugin.PluginException
128      * If there is an error initializing the plugin
129      */

130     public void init() throws PluginException {
131         super.init();
132     }
133
134     /**
135      * Process the blog entries
136      *
137      * @param httpServletRequest Request
138      * @param httpServletResponse Response
139      * @param blog {@link Blog} instance
140      * @param context Context
141      * @param entries Blog entries retrieved for the particular request
142      * @return Modified set of blog entries
143      * @throws PluginException If there is an error processing the blog entries
144      */

145     public Entry[] process(HttpServletRequest JavaDoc httpServletRequest, HttpServletResponse JavaDoc httpServletResponse, Blog blog, Map JavaDoc context, Entry[] entries) throws PluginException {
146         entries = super.process(httpServletRequest, httpServletResponse, blog, context, entries);
147
148         String JavaDoc page = BlojsomUtils.getRequestValue(BlojsomConstants.PAGE_PARAM, httpServletRequest);
149
150         String JavaDoc username = getUsernameFromSession(httpServletRequest, blog);
151         if (!checkPermission(blog, null, username, SPAM_PHRASE_MODERATION_PERMISSION)) {
152             httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, ADMIN_ADMINISTRATION_PAGE);
153             addOperationResultMessage(context, getAdminResource(FAILED_SPAM_PHRASE_PERMISSION_KEY, FAILED_SPAM_PHRASE_PERMISSION_KEY, blog.getBlogAdministrationLocale()));
154
155             return entries;
156         }
157
158         if (ADMIN_LOGIN_PAGE.equals(page)) {
159             return entries;
160         } else {
161             String JavaDoc action = BlojsomUtils.getRequestValue(ACTION_PARAM, httpServletRequest);
162             List JavaDoc spamPhrases = loadSpamPhrases(blog);
163             String JavaDoc spamPhrase = BlojsomUtils.getRequestValue(SPAM_PHRASE, httpServletRequest);
164
165             if (ADD_SPAM_PHRASE_ACTION.equals(action)) {
166                 if (!spamPhrases.contains(spamPhrase)) {
167                     spamPhrases.add(spamPhrase);
168                     blog.setProperty(SPAM_PHRASE_BLACKLIST_IP, BlojsomUtils.listToString(spamPhrases, "\n"));
169
170                     try {
171                         _fetcher.saveBlog(blog);
172                     } catch (FetcherException e) {
173                         if (_logger.isErrorEnabled()) {
174                             _logger.error(e);
175                         }
176                     }
177
178                     addOperationResultMessage(context, formatAdminResource(ADDED_SPAM_PHRASE_KEY, ADDED_SPAM_PHRASE_KEY, blog.getBlogAdministrationLocale(), new Object JavaDoc[] {spamPhrase}));
179                 } else {
180                     addOperationResultMessage(context, formatAdminResource(SPAM_PHRASE_ALREADY_ADDED_KEY, SPAM_PHRASE_ALREADY_ADDED_KEY, blog.getBlogAdministrationLocale(), new Object JavaDoc[] {spamPhrase}));
181                 }
182             } else if (DELETE_SPAM_PHRASE_ACTION.equals(action)) {
183                 String JavaDoc[] spamPhrasesToDelete = httpServletRequest.getParameterValues(SPAM_PHRASE);
184                 if (spamPhrasesToDelete != null && spamPhrasesToDelete.length > 0) {
185                     for (int i = 0; i < spamPhrasesToDelete.length; i++) {
186                         spamPhrases.set(Integer.parseInt(spamPhrasesToDelete[i]), null);
187                     }
188
189                     spamPhrases = BlojsomUtils.removeNullValues(spamPhrases);
190                     blog.setProperty(SPAM_PHRASE_BLACKLIST_IP, BlojsomUtils.listToString(spamPhrases, "\n"));
191
192                     try {
193                         _fetcher.saveBlog(blog);
194                     } catch (FetcherException e) {
195                         if (_logger.isErrorEnabled()) {
196                             _logger.error(e);
197                         }
198                     }
199
200                     addOperationResultMessage(context, formatAdminResource(DELETED_SPAM_PHRASE_KEY, DELETED_SPAM_PHRASE_KEY, blog.getBlogAdministrationLocale(), new Object JavaDoc[] {new Integer JavaDoc(spamPhrasesToDelete.length)}));
201                 } else {
202                     addOperationResultMessage(context, getAdminResource(NO_SPAM_PHRASES_TO_DELETE_KEY, NO_SPAM_PHRASES_TO_DELETE_KEY, blog.getBlogAdministrationLocale()));
203                 }
204             }
205
206             context.put(BLOJSOM_PLUGIN_SPAM_PHRASES, spamPhrases);
207         }
208
209         return entries;
210     }
211
212     /**
213      * Load the list of spam phrases from the blog
214      *
215      * @param blog {@link blog}
216      * @return List of spam phrases
217      */

218     protected List JavaDoc loadSpamPhrases(Blog blog) {
219         ArrayList JavaDoc spamPhrases = new ArrayList JavaDoc(25);
220
221         String JavaDoc spamPhrasesValues = blog.getProperty(SPAM_PHRASE_BLACKLIST_IP);
222         if (!BlojsomUtils.checkNullOrBlank(spamPhrasesValues)) {
223             try {
224                 StringReader JavaDoc stringReader = new StringReader JavaDoc(spamPhrasesValues);
225                 BufferedReader JavaDoc br = new BufferedReader JavaDoc(stringReader);
226                 String JavaDoc phrase;
227
228                 while ((phrase = br.readLine()) != null) {
229                     spamPhrases.add(phrase);
230                 }
231
232                 br.close();
233             } catch (IOException JavaDoc e) {
234                 if (_logger.isErrorEnabled()) {
235                     _logger.error(e);
236                 }
237             }
238         }
239
240         return spamPhrases;
241     }
242 }
Popular Tags