KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > blojsom > plugin > admin > EditBlogPluginsPlugin


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.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.Plugin;
40 import org.blojsom.plugin.PluginException;
41 import org.blojsom.util.BlojsomConstants;
42 import org.blojsom.util.BlojsomUtils;
43 import org.springframework.context.ApplicationContext;
44
45 import javax.servlet.http.HttpServletRequest JavaDoc;
46 import javax.servlet.http.HttpServletResponse JavaDoc;
47 import java.util.Iterator JavaDoc;
48 import java.util.Map JavaDoc;
49 import java.util.TreeMap JavaDoc;
50
51 /**
52  * EditBlogPluginsPlugin
53  *
54  * @author David Czarnecki
55  * @version $Id: EditBlogPluginsPlugin.java,v 1.3 2006/04/19 20:32:37 czarneckid Exp $
56  * @since blojsom 3.0
57  */

58 public class EditBlogPluginsPlugin extends BaseAdminPlugin {
59
60     private Log _logger = LogFactory.getLog(EditBlogPluginsPlugin.class);
61
62     // Pages
63
private static final String JavaDoc EDIT_BLOG_PLUGINS_PAGE = "/org/blojsom/plugin/admin/templates/admin-edit-blog-plugins";
64
65     // Constants
66
private static final String JavaDoc BLOJSOM_PLUGIN_EDIT_BLOG_PLUGINS_MAP = "BLOJSOM_PLUGIN_EDIT_BLOG_PLUGINS_MAP";
67     private static final String JavaDoc BLOJSOM_PLUGIN_EDIT_BLOG_PLUGINS_AVAILABLE_PLUGINS = "BLOJSOM_PLUGIN_EDIT_BLOG_PLUGINS_AVAILABLE_PLUGINS";
68
69     // Localization constants
70
private static final String JavaDoc FAILED_EDIT_PLUGINS_PERMISSION_KEY = "failed.edit.plugins.permission.text";
71     private static final String JavaDoc SUCCESSFULLY_UPDATED_PLUGINS_KEY = "successfully.updated.plugins.text";
72     private static final String JavaDoc FAILED_UPDATE_PLUGINS_KEY = "failed.updated.plugins.text";
73
74     // Actions
75
private static final String JavaDoc MODIFY_PLUGIN_CHAINS = "modify-plugin-chains";
76
77     // Permissions
78
private static final String JavaDoc EDIT_BLOG_PLUGINS_PERMISSION = "edit_blog_plugins_permission";
79
80     private Fetcher _fetcher;
81
82     /**
83      * Default constructor
84      */

85     public EditBlogPluginsPlugin() {
86     }
87
88     /**
89      * Set the {@link Fetcher}
90      *
91      * @param fetcher {@link Fetcher}
92      */

93     public void setFetcher(Fetcher fetcher) {
94         _fetcher = fetcher;
95     }
96
97     /**
98      * Process the blog entries
99      *
100      * @param httpServletRequest Request
101      * @param httpServletResponse Response
102      * @param blog {@link Blog} instance
103      * @param context Context
104      * @param entries Blog entries retrieved for the particular request
105      * @return Modified set of blog entries
106      * @throws PluginException If there is an error processing the blog entries
107      */

108     public Entry[] process(HttpServletRequest JavaDoc httpServletRequest, HttpServletResponse JavaDoc httpServletResponse, Blog blog, Map JavaDoc context, Entry[] entries) throws PluginException {
109         if (!authenticateUser(httpServletRequest, httpServletResponse, context, blog)) {
110             httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, ADMIN_LOGIN_PAGE);
111
112             return entries;
113         }
114
115         String JavaDoc username = getUsernameFromSession(httpServletRequest, blog);
116         if (!checkPermission(blog, null, username, EDIT_BLOG_PLUGINS_PERMISSION)) {
117             httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, ADMIN_ADMINISTRATION_PAGE);
118             addOperationResultMessage(context, getAdminResource(FAILED_EDIT_PLUGINS_PERMISSION_KEY, FAILED_EDIT_PLUGINS_PERMISSION_KEY, blog.getBlogAdministrationLocale()));
119
120             return entries;
121         }
122
123         context.put(BLOJSOM_PLUGIN_EDIT_BLOG_PLUGINS_MAP, new TreeMap JavaDoc(blog.getPlugins()));
124
125         // Add the list of available plugins
126
ApplicationContext applicationContext = (ApplicationContext) _servletConfig.getServletContext().getAttribute(BlojsomConstants.BLOJSOM_APPLICATION_CONTEXT);
127         if (applicationContext != null) {
128             Map JavaDoc pluginBeans = applicationContext.getBeansOfType(Plugin.class);
129             String JavaDoc[] pluginNames = applicationContext.getBeanNamesForType(Plugin.class);
130             for (int i = 0; i < pluginNames.length; i++) {
131                 String JavaDoc pluginName = pluginNames[i];
132                 Object JavaDoc plugin = pluginBeans.get(pluginName);
133                 if (plugin.getClass().getName().indexOf("admin") != -1) {
134                     pluginBeans.remove(pluginName);
135                 }
136             }
137
138             context.put(BLOJSOM_PLUGIN_EDIT_BLOG_PLUGINS_AVAILABLE_PLUGINS, new TreeMap JavaDoc(pluginBeans));
139         }
140
141         String JavaDoc action = BlojsomUtils.getRequestValue(ACTION_PARAM, httpServletRequest);
142         if (BlojsomUtils.checkNullOrBlank(action)) {
143             _logger.debug("User did not request edit action");
144
145             httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, ADMIN_ADMINISTRATION_PAGE);
146         } else if (PAGE_ACTION.equals(action)) {
147             _logger.debug("User requested edit blog plugins page");
148
149             httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_PLUGINS_PAGE);
150         } else if (MODIFY_PLUGIN_CHAINS.equals(action)) {
151             _logger.debug("User requested modify blog plugins action");
152
153             Map JavaDoc pluginChain = new TreeMap JavaDoc(blog.getPlugins());
154
155             // Iterate over the user's flavors and update the plugin chains
156
Iterator JavaDoc flavorIterator = blog.getTemplates().keySet().iterator();
157             String JavaDoc updatedFlavor;
158             while (flavorIterator.hasNext()) {
159                 String JavaDoc flavor = (String JavaDoc) flavorIterator.next();
160                 updatedFlavor = BlojsomUtils.getRequestValue(flavor + "." + BlojsomConstants.BLOJSOM_PLUGIN_CHAIN, httpServletRequest);
161                 if (!BlojsomUtils.checkNullOrBlank(updatedFlavor)) {
162                     pluginChain.put(flavor, updatedFlavor);
163                 } else {
164                     pluginChain.put(flavor, "");
165                 }
166             }
167
168             // Check for the default flavor
169
updatedFlavor = BlojsomUtils.getRequestValue("default." + BlojsomConstants.BLOJSOM_PLUGIN_CHAIN, httpServletRequest);
170             pluginChain.put("default", updatedFlavor);
171
172             // Update the internal plugin chain map for the user
173
blog.setPlugins(pluginChain);
174
175             // Write out the updated plugin configuration file
176
try {
177                 _fetcher.saveBlog(blog);
178                 addOperationResultMessage(context, getAdminResource(SUCCESSFULLY_UPDATED_PLUGINS_KEY, SUCCESSFULLY_UPDATED_PLUGINS_KEY, blog.getBlogAdministrationLocale()));
179             } catch (FetcherException e) {
180                 _logger.error(e);
181                 addOperationResultMessage(context, getAdminResource(FAILED_UPDATE_PLUGINS_KEY, FAILED_UPDATE_PLUGINS_KEY, blog.getBlogAdministrationLocale()));
182             }
183
184             context.put(BLOJSOM_PLUGIN_EDIT_BLOG_PLUGINS_MAP, new TreeMap JavaDoc(blog.getPlugins()));
185             httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_PLUGINS_PAGE);
186         }
187
188         return entries;
189     }
190 }
191
Popular Tags