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.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.Plugin;
039: import org.blojsom.plugin.PluginException;
040: import org.blojsom.util.BlojsomConstants;
041: import org.blojsom.util.BlojsomUtils;
042: import org.springframework.context.ApplicationContext;
043:
044: import javax.servlet.http.HttpServletRequest;
045: import javax.servlet.http.HttpServletResponse;
046: import java.util.Iterator;
047: import java.util.Map;
048: import java.util.TreeMap;
049:
050: /**
051: * EditBlogPluginsPlugin
052: *
053: * @author David Czarnecki
054: * @version $Id: EditBlogPluginsPlugin.java,v 1.4 2007/01/17 02:35:05 czarneckid Exp $
055: * @since blojsom 3.0
056: */
057: public class EditBlogPluginsPlugin extends BaseAdminPlugin {
058:
059: private Log _logger = LogFactory
060: .getLog(EditBlogPluginsPlugin.class);
061:
062: // Pages
063: private static final String EDIT_BLOG_PLUGINS_PAGE = "/org/blojsom/plugin/admin/templates/admin-edit-blog-plugins";
064:
065: // Constants
066: private static final String BLOJSOM_PLUGIN_EDIT_BLOG_PLUGINS_MAP = "BLOJSOM_PLUGIN_EDIT_BLOG_PLUGINS_MAP";
067: private static final String BLOJSOM_PLUGIN_EDIT_BLOG_PLUGINS_AVAILABLE_PLUGINS = "BLOJSOM_PLUGIN_EDIT_BLOG_PLUGINS_AVAILABLE_PLUGINS";
068:
069: // Localization constants
070: private static final String FAILED_EDIT_PLUGINS_PERMISSION_KEY = "failed.edit.plugins.permission.text";
071: private static final String SUCCESSFULLY_UPDATED_PLUGINS_KEY = "successfully.updated.plugins.text";
072: private static final String FAILED_UPDATE_PLUGINS_KEY = "failed.updated.plugins.text";
073:
074: // Actions
075: private static final String MODIFY_PLUGIN_CHAINS = "modify-plugin-chains";
076:
077: // Permissions
078: private static final String EDIT_BLOG_PLUGINS_PERMISSION = "edit_blog_plugins_permission";
079:
080: private Fetcher _fetcher;
081:
082: /**
083: * Default constructor
084: */
085: public EditBlogPluginsPlugin() {
086: }
087:
088: /**
089: * Set the {@link Fetcher}
090: *
091: * @param fetcher {@link Fetcher}
092: */
093: public void setFetcher(Fetcher fetcher) {
094: _fetcher = fetcher;
095: }
096:
097: /**
098: * Process the blog entries
099: *
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 httpServletRequest,
109: HttpServletResponse httpServletResponse, Blog blog,
110: Map context, Entry[] entries) throws PluginException {
111: if (!authenticateUser(httpServletRequest, httpServletResponse,
112: context, blog)) {
113: httpServletRequest.setAttribute(
114: BlojsomConstants.PAGE_PARAM, ADMIN_LOGIN_PAGE);
115:
116: return entries;
117: }
118:
119: String username = getUsernameFromSession(httpServletRequest,
120: blog);
121: if (!checkPermission(blog, null, username,
122: EDIT_BLOG_PLUGINS_PERMISSION)) {
123: httpServletRequest.setAttribute(
124: BlojsomConstants.PAGE_PARAM,
125: ADMIN_ADMINISTRATION_PAGE);
126: addOperationResultMessage(context, getAdminResource(
127: FAILED_EDIT_PLUGINS_PERMISSION_KEY,
128: FAILED_EDIT_PLUGINS_PERMISSION_KEY, blog
129: .getBlogAdministrationLocale()));
130:
131: return entries;
132: }
133:
134: context.put(BLOJSOM_PLUGIN_EDIT_BLOG_PLUGINS_MAP, new TreeMap(
135: blog.getPlugins()));
136:
137: // Add the list of available plugins
138: ApplicationContext applicationContext = (ApplicationContext) _servletConfig
139: .getServletContext().getAttribute(
140: BlojsomConstants.BLOJSOM_APPLICATION_CONTEXT);
141: if (applicationContext != null) {
142: Map pluginBeans = applicationContext
143: .getBeansOfType(Plugin.class);
144: String[] pluginNames = applicationContext
145: .getBeanNamesForType(Plugin.class);
146: for (int i = 0; i < pluginNames.length; i++) {
147: String pluginName = pluginNames[i];
148: Object plugin = pluginBeans.get(pluginName);
149: if (plugin.getClass().getName().indexOf("admin") != -1) {
150: pluginBeans.remove(pluginName);
151: }
152: }
153:
154: context.put(
155: BLOJSOM_PLUGIN_EDIT_BLOG_PLUGINS_AVAILABLE_PLUGINS,
156: new TreeMap(pluginBeans));
157: }
158:
159: String action = BlojsomUtils.getRequestValue(ACTION_PARAM,
160: httpServletRequest);
161: if (BlojsomUtils.checkNullOrBlank(action)) {
162: _logger.debug("User did not request edit action");
163:
164: httpServletRequest.setAttribute(
165: BlojsomConstants.PAGE_PARAM,
166: ADMIN_ADMINISTRATION_PAGE);
167: } else if (PAGE_ACTION.equals(action)) {
168: _logger.debug("User requested edit blog plugins page");
169:
170: httpServletRequest
171: .setAttribute(BlojsomConstants.PAGE_PARAM,
172: EDIT_BLOG_PLUGINS_PAGE);
173: } else if (MODIFY_PLUGIN_CHAINS.equals(action)) {
174: _logger.debug("User requested modify blog plugins action");
175:
176: Map pluginChain = new TreeMap(blog.getPlugins());
177:
178: // Iterate over the user's flavors and update the plugin chains
179: Iterator flavorIterator = blog.getTemplates().keySet()
180: .iterator();
181: String updatedFlavor;
182: while (flavorIterator.hasNext()) {
183: String flavor = (String) flavorIterator.next();
184: updatedFlavor = BlojsomUtils.getRequestValue(flavor
185: + "." + BlojsomConstants.BLOJSOM_PLUGIN_CHAIN,
186: httpServletRequest);
187: if (!BlojsomUtils.checkNullOrBlank(updatedFlavor)) {
188: pluginChain.put(flavor, updatedFlavor);
189: } else {
190: pluginChain.put(flavor, "");
191: }
192: }
193:
194: // Check for the default flavor
195: updatedFlavor = BlojsomUtils.getRequestValue("default."
196: + BlojsomConstants.BLOJSOM_PLUGIN_CHAIN,
197: httpServletRequest);
198: pluginChain.put("default", updatedFlavor);
199:
200: // Update the internal plugin chain map for the user
201: blog.setPlugins(pluginChain);
202:
203: // Write out the updated plugin configuration file
204: try {
205: _fetcher.saveBlog(blog);
206: addOperationResultMessage(context, getAdminResource(
207: SUCCESSFULLY_UPDATED_PLUGINS_KEY,
208: SUCCESSFULLY_UPDATED_PLUGINS_KEY, blog
209: .getBlogAdministrationLocale()));
210: } catch (FetcherException e) {
211: _logger.error(e);
212: addOperationResultMessage(context, getAdminResource(
213: FAILED_UPDATE_PLUGINS_KEY,
214: FAILED_UPDATE_PLUGINS_KEY, blog
215: .getBlogAdministrationLocale()));
216: }
217:
218: context.put(BLOJSOM_PLUGIN_EDIT_BLOG_PLUGINS_MAP,
219: new TreeMap(blog.getPlugins()));
220: httpServletRequest
221: .setAttribute(BlojsomConstants.PAGE_PARAM,
222: EDIT_BLOG_PLUGINS_PAGE);
223: }
224:
225: return entries;
226: }
227: }
|