EditBlogPluginsPlugin.java [plain text]
package org.blojsom.plugin.admin;
import org.blojsom.blog.BlogEntry;
import org.blojsom.blog.BlogUser;
import org.blojsom.blog.BlojsomConfiguration;
import org.blojsom.plugin.BlojsomPluginException;
import org.blojsom.util.BlojsomUtils;
import org.blojsom.BlojsomException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletConfig;
import java.util.*;
import java.io.IOException;
import java.io.File;
import java.io.FileOutputStream;
public class EditBlogPluginsPlugin extends BaseAdminPlugin {
private Log _logger = LogFactory.getLog(EditBlogPluginsPlugin.class);
private static final String EDIT_BLOG_PLUGINS_PAGE = "/org/blojsom/plugin/admin/templates/admin-edit-blog-plugins";
private static final String BLOJSOM_PLUGIN_EDIT_BLOG_PLUGINS_MAP = "BLOJSOM_PLUGIN_EDIT_BLOG_PLUGINS_MAP";
private static final String BLOJSOM_PLUGIN_EDIT_BLOG_PLUGINS_AVAILABLE_PLUGINS = "BLOJSOM_PLUGIN_EDIT_BLOG_PLUGINS_AVAILABLE_PLUGINS";
private static final String MODIFY_PLUGIN_CHAINS = "modify-plugin-chains";
private static final String EDIT_BLOG_PLUGINS_PERMISSION = "edit_blog_plugins";
private String _pluginConfiguration;
private Map _plugins;
public EditBlogPluginsPlugin() {
}
public void init(ServletConfig servletConfig, BlojsomConfiguration blojsomConfiguration) throws BlojsomPluginException {
super.init(servletConfig, blojsomConfiguration);
_pluginConfiguration = servletConfig.getInitParameter(BLOJSOM_PLUGIN_CONFIGURATION_IP);
try {
Properties pluginProperties = BlojsomUtils.loadProperties(servletConfig, _blojsomConfiguration.getBaseConfigurationDirectory() + _pluginConfiguration);
_plugins = BlojsomUtils.propertiesToMap(pluginProperties);
Map plugins = BlojsomUtils.propertiesToMap(pluginProperties);
Iterator pluginsIterator = plugins.keySet().iterator();
while (pluginsIterator.hasNext()) {
String pluginName = (String) pluginsIterator.next();
if (pluginName.indexOf(BLOJSOM_PLUGIN_CHAIN) != -1) {
_plugins.remove(pluginName);
}
if (((String)plugins.get(pluginName)).indexOf("admin") != -1) {
_plugins.remove(pluginName);
}
}
} catch (BlojsomException e) {
_logger.error(e);
throw new BlojsomPluginException(e);
}
_plugins = new TreeMap(_plugins);
}
public BlogEntry[] process(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlogUser user, Map context, BlogEntry[] entries) throws BlojsomPluginException {
if (!authenticateUser(httpServletRequest, httpServletResponse, context, user)) {
httpServletRequest.setAttribute(PAGE_PARAM, ADMIN_LOGIN_PAGE);
return entries;
}
String username = getUsernameFromSession(httpServletRequest, user.getBlog());
if (!checkPermission(user, null, username, EDIT_BLOG_PLUGINS_PERMISSION)) {
httpServletRequest.setAttribute(PAGE_PARAM, ADMIN_LOGIN_PAGE);
addOperationResultMessage(context, "You are not allowed to edit blog plugins");
return entries;
}
Iterator flavorIterator = user.getFlavors().keySet().iterator();
Map userPluginChain = user.getPluginChain();
Map updatedPluginChain = new TreeMap();
while (flavorIterator.hasNext()) {
String flavor = (String) flavorIterator.next();
if (userPluginChain.containsKey(flavor + "." + BLOJSOM_PLUGIN_CHAIN)) {
updatedPluginChain.put(flavor, BlojsomUtils.arrayOfStringsToString((String[]) userPluginChain.get(flavor + "." + BLOJSOM_PLUGIN_CHAIN)));
} else {
updatedPluginChain.put(flavor, "");
}
}
if (userPluginChain.containsKey(BLOJSOM_PLUGIN_CHAIN)) {
updatedPluginChain.put("", BlojsomUtils.arrayOfStringsToString((String[]) userPluginChain.get(BLOJSOM_PLUGIN_CHAIN)));
}
context.put(BLOJSOM_PLUGIN_EDIT_BLOG_PLUGINS_MAP, updatedPluginChain);
context.put(BLOJSOM_PLUGIN_EDIT_BLOG_PLUGINS_AVAILABLE_PLUGINS, _plugins);
String action = BlojsomUtils.getRequestValue(ACTION_PARAM, httpServletRequest);
if (BlojsomUtils.checkNullOrBlank(action)) {
_logger.debug("User did not request edit action");
httpServletRequest.setAttribute(PAGE_PARAM, ADMIN_ADMINISTRATION_PAGE);
} else if (PAGE_ACTION.equals(action)) {
_logger.debug("User requested edit blog plugins page");
httpServletRequest.setAttribute(PAGE_PARAM, EDIT_BLOG_PLUGINS_PAGE);
} else if (MODIFY_PLUGIN_CHAINS.equals(action)) {
_logger.debug("User requested modify blog plugins action");
Map pluginChain = user.getPluginChain();
Map pluginChainForWriting = new HashMap();
Map pluginChainForContext = new HashMap();
flavorIterator = user.getFlavors().keySet().iterator();
String updatedFlavor;
String[] updatedFlavorArray;
while (flavorIterator.hasNext()) {
String flavor = (String) flavorIterator.next();
updatedFlavor = BlojsomUtils.getRequestValue(flavor + "." + BLOJSOM_PLUGIN_CHAIN, httpServletRequest);
updatedFlavorArray = BlojsomUtils.parseCommaList(updatedFlavor);
pluginChain.put(flavor + "." + BLOJSOM_PLUGIN_CHAIN, updatedFlavorArray);
pluginChainForWriting.put(flavor + "." + BLOJSOM_PLUGIN_CHAIN, updatedFlavor);
pluginChainForContext.put(flavor, updatedFlavor);
}
updatedFlavor = BlojsomUtils.getRequestValue("." + BLOJSOM_PLUGIN_CHAIN, httpServletRequest);
updatedFlavorArray = BlojsomUtils.parseCommaList(updatedFlavor);
pluginChain.put(BLOJSOM_PLUGIN_CHAIN, updatedFlavorArray);
pluginChainForWriting.put(BLOJSOM_PLUGIN_CHAIN, updatedFlavor);
pluginChainForContext.put("", updatedFlavor);
user.setPluginChain(pluginChain);
try {
writePluginsConfiguration(user.getId(), pluginChainForWriting);
addOperationResultMessage(context, "Successfully updated plugin configuration");
} catch (IOException e) {
_logger.error(e);
addOperationResultMessage(context, "Unable to update plugin configuration");
}
context.put(BLOJSOM_PLUGIN_EDIT_BLOG_PLUGINS_MAP, new TreeMap(pluginChainForContext));
httpServletRequest.setAttribute(PAGE_PARAM, EDIT_BLOG_PLUGINS_PAGE);
}
return entries;
}
private void writePluginsConfiguration(String userId, Map pluginChain) throws IOException {
File pluginFile = new File(_blojsomConfiguration.getInstallationDirectory() + _blojsomConfiguration.getBaseConfigurationDirectory() + userId + "/" + _pluginConfiguration);
FileOutputStream fos = new FileOutputStream(pluginFile);
Properties pluginChainProperties = BlojsomUtils.mapToProperties(pluginChain);
pluginChainProperties.store(fos, null);
fos.close();
}
}