1 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.PluginException; 40 import org.blojsom.util.BlojsomConstants; 41 import org.blojsom.util.BlojsomUtils; 42 43 import javax.servlet.http.HttpServletRequest ; 44 import javax.servlet.http.HttpServletResponse ; 45 import java.io.File ; 46 import java.util.*; 47 48 55 public class EditBlogFlavorsPlugin extends BaseAdminPlugin { 56 57 private Log _logger = LogFactory.getLog(EditBlogFlavorsPlugin.class); 58 59 private static final String PROTECTED_FLAVORS_IP = "protected-flavors"; 60 private static final String DEFAULT_PROTECTED_FLAVORS = "admin"; 61 62 private static final String EDIT_BLOG_FLAVORS_PAGE = "/org/blojsom/plugin/admin/templates/admin-edit-blog-flavors"; 64 private static final String EDIT_BLOG_FLAVOR_PAGE = "/org/blojsom/plugin/admin/templates/admin-edit-blog-flavor"; 65 66 private static final String DEFAULT_MIME_TYPE = "text/html"; 68 private static final String DEFAULT_CHARACTER_SET = BlojsomConstants.UTF8; 69 private static final String BLOJSOM_PLUGIN_EDIT_BLOG_FLAVORS_EXISTING = "BLOJSOM_PLUGIN_EDIT_BLOG_FLAVORS_EXISTING"; 70 private static final String BLOJSOM_PLUGIN_EDIT_BLOG_FLAVORS_TEMPLATE_FILES = "BLOJSOM_PLUGIN_EDIT_BLOG_FLAVORS_TEMPLATE_FILES"; 71 private static final String BLOJSOM_PLUGIN_EDIT_BLOG_FLAVORS_FLAVORS = "BLOJSOM_PLUGIN_EDIT_BLOG_FLAVORS_FLAVORS"; 72 73 private static final String FLAVOR_NAME_EDIT = "FLAVOR_NAME_EDIT"; 74 private static final String FLAVOR_TYPE_EDIT = "FLAVOR_TYPE_EDIT"; 75 private static final String FLAVOR_CHARACTER_SET_EDIT = "FLAVOR_CHARACTER_SET_EDIT"; 76 private static final String FLAVOR_TEMPLATE_EDIT = "FLAVOR_TEMPLATE_EDIT"; 77 78 private static final String FAILED_EDIT_FLAVOR_PERMISSION_KEY = "failed.edit.flavor.permission.text"; 80 private static final String NO_FLAVOR_SPECIFIED_KEY = "no.flavor.specified.text"; 81 private static final String NO_BLOG_TEMPLATE_SPECIFIED_KEY = "no.blog.template.specified.text"; 82 private static final String SUCCESSFULLY_ADDED_FLAVOR_KEY = "successfully.added.flavor.text"; 83 private static final String FAILED_UPDATE_FLAVOR_KEY = "failed.update.flavor.text"; 84 private static final String FAILED_DELETE_DEFAULT_FLAVOR_KEY = "failed.delete.default.flavor.text"; 85 private static final String FAILED_DELETE_PROTECTED_FLAVOR_KEY = "failed.delete.protected.flavor.text"; 86 private static final String SUCCESSFULLY_DELETED_FLAVOR_KEY = "successfully.deleted.flavor.text"; 87 88 private static final String ADD_BLOG_FLAVOR_ACTION = "add-blog-flavor"; 90 private static final String MODIFY_BLOG_FLAVOR_ACTION = "modify-blog-flavor"; 91 private static final String DELETE_BLOG_FLAVOR_ACTION = "delete-blog-flavor"; 92 private static final String EDIT_BLOG_FLAVOR_ACTION = "edit-blog-flavor"; 93 94 private static final String FLAVOR_NAME = "flavor-name"; 96 private static final String FLAVOR_MIME_TYPE = "flavor-mime-type"; 97 private static final String FLAVOR_CHARACTER_SET = "flavor-character-set"; 98 private static final String BLOG_TEMPLATE = "blog-template"; 99 100 private static final String EDIT_BLOG_FLAVORS_PERMISSION = "edit_blog_flavors_permission"; 102 103 private Fetcher _fetcher; 104 private Properties _blojsomProperties; 105 private String _templatesDirectory; 106 private String _blogsDirectory; 107 108 111 public EditBlogFlavorsPlugin() { 112 } 113 114 119 public void setFetcher(Fetcher fetcher) { 120 _fetcher = fetcher; 121 } 122 123 128 public void setBlojsomProperties(Properties blojsomProperties) { 129 _blojsomProperties = blojsomProperties; 130 } 131 132 137 public void init() throws PluginException { 138 super.init(); 139 140 _templatesDirectory = _blojsomProperties.getProperty(BlojsomConstants.TEMPLATES_DIRECTORY_IP, BlojsomConstants.DEFAULT_TEMPLATES_DIRECTORY); 141 _blogsDirectory = _blojsomProperties.getProperty(BlojsomConstants.BLOGS_DIRECTORY_IP, BlojsomConstants.DEFAULT_BLOGS_DIRECTORY); 142 } 143 144 150 protected void addFlavorInformationToContext(Blog blog, Map context) { 151 File templatesDirectory = new File (_servletConfig.getServletContext().getRealPath("/") + BlojsomConstants.DEFAULT_CONFIGURATION_BASE_DIRECTORY + _blogsDirectory + blog.getBlogId() + _templatesDirectory); 153 _logger.debug("Looking for templates in directory: " + templatesDirectory.toString()); 154 155 File [] templates = templatesDirectory.listFiles(); 156 ArrayList templatesList = new ArrayList(templates.length); 157 for (int i = 0; i < templates.length; i++) { 158 File template = templates[i]; 159 if (template.isFile()) { 160 templatesList.add(template.getName()); 161 } 162 } 163 164 context.put(BLOJSOM_PLUGIN_EDIT_BLOG_FLAVORS_FLAVORS, new TreeMap(blog.getTemplates())); 166 context.put(BLOJSOM_PLUGIN_EDIT_BLOG_FLAVORS_TEMPLATE_FILES, templatesList); 167 context.put(BLOJSOM_PLUGIN_EDIT_BLOG_FLAVORS_EXISTING, new TreeMap(blog.getTemplates())); 168 } 169 170 181 public Entry[] process(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Blog blog, Map context, Entry[] entries) throws PluginException { 182 if (!authenticateUser(httpServletRequest, httpServletResponse, context, blog)) { 183 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, ADMIN_LOGIN_PAGE); 184 185 return entries; 186 } 187 188 String username = getUsernameFromSession(httpServletRequest, blog); 189 if (!checkPermission(blog, null, username, EDIT_BLOG_FLAVORS_PERMISSION)) { 190 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, ADMIN_ADMINISTRATION_PAGE); 191 addOperationResultMessage(context, getAdminResource(FAILED_EDIT_FLAVOR_PERMISSION_KEY, FAILED_EDIT_FLAVOR_PERMISSION_KEY, blog.getBlogAdministrationLocale())); 192 193 return entries; 194 } 195 196 String protectedFlavors = blog.getProperty(PROTECTED_FLAVORS_IP); 197 if (BlojsomUtils.checkNullOrBlank(protectedFlavors)) { 198 protectedFlavors = DEFAULT_PROTECTED_FLAVORS; 199 } 200 201 if (protectedFlavors.indexOf(DEFAULT_PROTECTED_FLAVORS) == -1) { 202 protectedFlavors = protectedFlavors + " " + DEFAULT_PROTECTED_FLAVORS; 203 } 204 205 addFlavorInformationToContext(blog, context); 206 207 String action = BlojsomUtils.getRequestValue(ACTION_PARAM, httpServletRequest); 208 if (BlojsomUtils.checkNullOrBlank(action)) { 209 _logger.debug("User did not request edit action"); 210 211 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, ADMIN_ADMINISTRATION_PAGE); 212 } else if (PAGE_ACTION.equals(action)) { 213 _logger.debug("User requested add blog flavor page"); 214 215 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_FLAVORS_PAGE); 216 } else if (ADD_BLOG_FLAVOR_ACTION.equals(action) || MODIFY_BLOG_FLAVOR_ACTION.equals(action)) { 217 _logger.debug("User requested add blog flavor action"); 218 219 String flavorName = BlojsomUtils.getRequestValue(FLAVOR_NAME, httpServletRequest); 220 if (BlojsomUtils.checkNullOrBlank(flavorName)) { 221 _logger.debug("No flavor name specified"); 222 addOperationResultMessage(context, getAdminResource(NO_FLAVOR_SPECIFIED_KEY, NO_FLAVOR_SPECIFIED_KEY, blog.getBlogAdministrationLocale())); 223 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_FLAVORS_PAGE); 224 225 return entries; 226 } 227 228 String blogTemplate = BlojsomUtils.getRequestValue(BLOG_TEMPLATE, httpServletRequest); 229 if (BlojsomUtils.checkNullOrBlank(blogTemplate)) { 230 _logger.debug("No blog template specified"); 231 addOperationResultMessage(context, getAdminResource(NO_BLOG_TEMPLATE_SPECIFIED_KEY, NO_BLOG_TEMPLATE_SPECIFIED_KEY, blog.getBlogAdministrationLocale())); 232 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_FLAVORS_PAGE); 233 234 return entries; 235 } 236 237 String flavorMimeType = BlojsomUtils.getRequestValue(FLAVOR_MIME_TYPE, httpServletRequest); 238 if (BlojsomUtils.checkNullOrBlank(flavorMimeType)) { 239 flavorMimeType = DEFAULT_MIME_TYPE; 240 _logger.debug("Flavor MIME type not specified. Using default: " + flavorMimeType); 241 } 242 243 String flavorCharacterSet = BlojsomUtils.getRequestValue(FLAVOR_CHARACTER_SET, httpServletRequest); 244 if (BlojsomUtils.checkNullOrBlank(flavorCharacterSet)) { 245 flavorCharacterSet = DEFAULT_CHARACTER_SET; 246 _logger.debug("Flavor character set not specified. Using default: " + flavorCharacterSet); 247 } 248 249 Map templateMap = new HashMap(blog.getTemplates()); 250 templateMap.put(flavorName, blogTemplate + ", " + flavorMimeType + ";charset=" + flavorCharacterSet); 251 blog.setTemplates(templateMap); 252 253 _logger.debug("Successfully added flavor: " + flavorName + " using template: " + blogTemplate + " with content type: " + flavorMimeType + ";" + flavorCharacterSet); 254 255 try { 256 _fetcher.saveBlog(blog); 257 addOperationResultMessage(context, formatAdminResource(SUCCESSFULLY_ADDED_FLAVOR_KEY, SUCCESSFULLY_ADDED_FLAVOR_KEY, blog.getBlogAdministrationLocale(), new Object [] {flavorName, blogTemplate, flavorMimeType, flavorCharacterSet})); 258 _logger.debug("Successfully wrote flavor configuration file for blog: " + blog.getBlogId()); 259 260 addFlavorInformationToContext(blog, context); 261 } catch (FetcherException e) { 262 addOperationResultMessage(context, getAdminResource(FAILED_UPDATE_FLAVOR_KEY, FAILED_UPDATE_FLAVOR_KEY, blog.getBlogAdministrationLocale())); 263 _logger.error(e); 264 } 265 266 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_FLAVORS_PAGE); 267 } else if (DELETE_BLOG_FLAVOR_ACTION.equals(action)) { 268 _logger.debug("User requested delete blog flavor action"); 269 270 String flavorName = BlojsomUtils.getRequestValue(FLAVOR_NAME, httpServletRequest); 271 if (BlojsomUtils.checkNullOrBlank(flavorName)) { 272 _logger.debug("No flavor name specified"); 273 addOperationResultMessage(context, getAdminResource(NO_FLAVOR_SPECIFIED_KEY, NO_FLAVOR_SPECIFIED_KEY, blog.getBlogAdministrationLocale())); 274 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_FLAVORS_PAGE); 275 276 return entries; 277 } 278 279 if (flavorName.equalsIgnoreCase(blog.getBlogDefaultFlavor())) { 280 _logger.debug("Cannot delete the default flavor"); 281 addOperationResultMessage(context, getAdminResource(FAILED_DELETE_DEFAULT_FLAVOR_KEY, FAILED_DELETE_DEFAULT_FLAVOR_KEY, blog.getBlogAdministrationLocale())); 282 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_FLAVORS_PAGE); 283 284 return entries; 285 } 286 287 if (protectedFlavors.indexOf(flavorName) != -1) { 288 _logger.debug("Cannot delete protected flavor: " + flavorName); 289 addOperationResultMessage(context, formatAdminResource(FAILED_DELETE_PROTECTED_FLAVOR_KEY, FAILED_DELETE_PROTECTED_FLAVOR_KEY, blog.getBlogAdministrationLocale(), new Object [] {flavorName})); 290 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_FLAVORS_PAGE); 291 292 return entries; 293 } 294 295 Map templateMap = new HashMap(blog.getTemplates()); 297 templateMap.remove(flavorName); 298 blog.setTemplates(templateMap); 299 300 Map pluginsMap = new HashMap(blog.getPlugins()); 301 pluginsMap.remove(flavorName); 302 blog.setPlugins(pluginsMap); 303 304 try { 306 _fetcher.saveBlog(blog); 307 _logger.debug("Successfully wrote flavor configuration file for blog: " + blog.getBlogId()); 308 addOperationResultMessage(context, formatAdminResource(SUCCESSFULLY_DELETED_FLAVOR_KEY, SUCCESSFULLY_DELETED_FLAVOR_KEY, blog.getBlogAdministrationLocale(), new Object [] {flavorName})); 309 310 addFlavorInformationToContext(blog, context); 311 } catch (FetcherException e) { 312 addOperationResultMessage(context, getAdminResource(FAILED_UPDATE_FLAVOR_KEY, FAILED_UPDATE_FLAVOR_KEY, blog.getBlogAdministrationLocale())); 313 _logger.error(e); 314 } 315 316 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_FLAVORS_PAGE); 317 } else if (EDIT_BLOG_FLAVOR_ACTION.equals(action)) { 318 String flavor = BlojsomUtils.getRequestValue(FLAVOR_NAME, httpServletRequest); 319 320 context.put(FLAVOR_NAME_EDIT, flavor); 321 322 Map templates = blog.getTemplates(); 323 String templateAndType = (String ) templates.get(flavor); 324 String [] templateType = BlojsomUtils.parseCommaList(templateAndType); 325 String [] typeAndCharacterSet; 326 if (templateType.length == 2) { 327 typeAndCharacterSet = BlojsomUtils.parseDelimitedList(templateType[1], ";", true); 328 } else { 329 typeAndCharacterSet = new String [] {"text/html"}; 330 } 331 332 context.put(FLAVOR_TYPE_EDIT, typeAndCharacterSet[0]); 333 if (typeAndCharacterSet.length == 2) { 334 context.put(FLAVOR_CHARACTER_SET_EDIT, typeAndCharacterSet[1].substring(typeAndCharacterSet[1].indexOf("=") + 1)); 335 } else { 336 context.put(FLAVOR_CHARACTER_SET_EDIT, BlojsomUtils.UTF8); 337 } 338 context.put(FLAVOR_TEMPLATE_EDIT, templateType[0]); 339 340 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_FLAVOR_PAGE); 341 } 342 343 return entries; 344 } 345 } 346 | Popular Tags |