KickJava   Java API By Example, From Geeks To Geeks.

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


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.PluginException;
40 import org.blojsom.util.BlojsomConstants;
41 import org.blojsom.util.BlojsomUtils;
42
43 import javax.servlet.http.HttpServletRequest JavaDoc;
44 import javax.servlet.http.HttpServletResponse JavaDoc;
45 import java.io.File JavaDoc;
46 import java.util.*;
47
48 /**
49  * EditBlogFlavorsPlugin
50  *
51  * @author David Czarnecki
52  * @version $Id: EditBlogFlavorsPlugin.java,v 1.5 2006/05/02 12:57:53 czarneckid Exp $
53  * @since blojsom 3.0
54  */

55 public class EditBlogFlavorsPlugin extends BaseAdminPlugin {
56
57     private Log _logger = LogFactory.getLog(EditBlogFlavorsPlugin.class);
58
59     private static final String JavaDoc PROTECTED_FLAVORS_IP = "protected-flavors";
60     private static final String JavaDoc DEFAULT_PROTECTED_FLAVORS = "admin";
61
62     // Pages
63
private static final String JavaDoc EDIT_BLOG_FLAVORS_PAGE = "/org/blojsom/plugin/admin/templates/admin-edit-blog-flavors";
64     private static final String JavaDoc EDIT_BLOG_FLAVOR_PAGE = "/org/blojsom/plugin/admin/templates/admin-edit-blog-flavor";
65
66     // Constants
67
private static final String JavaDoc DEFAULT_MIME_TYPE = "text/html";
68     private static final String JavaDoc DEFAULT_CHARACTER_SET = BlojsomConstants.UTF8;
69     private static final String JavaDoc BLOJSOM_PLUGIN_EDIT_BLOG_FLAVORS_EXISTING = "BLOJSOM_PLUGIN_EDIT_BLOG_FLAVORS_EXISTING";
70     private static final String JavaDoc BLOJSOM_PLUGIN_EDIT_BLOG_FLAVORS_TEMPLATE_FILES = "BLOJSOM_PLUGIN_EDIT_BLOG_FLAVORS_TEMPLATE_FILES";
71     private static final String JavaDoc BLOJSOM_PLUGIN_EDIT_BLOG_FLAVORS_FLAVORS = "BLOJSOM_PLUGIN_EDIT_BLOG_FLAVORS_FLAVORS";
72
73     private static final String JavaDoc FLAVOR_NAME_EDIT = "FLAVOR_NAME_EDIT";
74     private static final String JavaDoc FLAVOR_TYPE_EDIT = "FLAVOR_TYPE_EDIT";
75     private static final String JavaDoc FLAVOR_CHARACTER_SET_EDIT = "FLAVOR_CHARACTER_SET_EDIT";
76     private static final String JavaDoc FLAVOR_TEMPLATE_EDIT = "FLAVOR_TEMPLATE_EDIT";
77
78     // Localization constants
79
private static final String JavaDoc FAILED_EDIT_FLAVOR_PERMISSION_KEY = "failed.edit.flavor.permission.text";
80     private static final String JavaDoc NO_FLAVOR_SPECIFIED_KEY = "no.flavor.specified.text";
81     private static final String JavaDoc NO_BLOG_TEMPLATE_SPECIFIED_KEY = "no.blog.template.specified.text";
82     private static final String JavaDoc SUCCESSFULLY_ADDED_FLAVOR_KEY = "successfully.added.flavor.text";
83     private static final String JavaDoc FAILED_UPDATE_FLAVOR_KEY = "failed.update.flavor.text";
84     private static final String JavaDoc FAILED_DELETE_DEFAULT_FLAVOR_KEY = "failed.delete.default.flavor.text";
85     private static final String JavaDoc FAILED_DELETE_PROTECTED_FLAVOR_KEY = "failed.delete.protected.flavor.text";
86     private static final String JavaDoc SUCCESSFULLY_DELETED_FLAVOR_KEY = "successfully.deleted.flavor.text";
87
88     // Actions
89
private static final String JavaDoc ADD_BLOG_FLAVOR_ACTION = "add-blog-flavor";
90     private static final String JavaDoc MODIFY_BLOG_FLAVOR_ACTION = "modify-blog-flavor";
91     private static final String JavaDoc DELETE_BLOG_FLAVOR_ACTION = "delete-blog-flavor";
92     private static final String JavaDoc EDIT_BLOG_FLAVOR_ACTION = "edit-blog-flavor";
93
94     // Form elements
95
private static final String JavaDoc FLAVOR_NAME = "flavor-name";
96     private static final String JavaDoc FLAVOR_MIME_TYPE = "flavor-mime-type";
97     private static final String JavaDoc FLAVOR_CHARACTER_SET = "flavor-character-set";
98     private static final String JavaDoc BLOG_TEMPLATE = "blog-template";
99
100     // Permissions
101
private static final String JavaDoc EDIT_BLOG_FLAVORS_PERMISSION = "edit_blog_flavors_permission";
102
103     private Fetcher _fetcher;
104     private Properties _blojsomProperties;
105     private String JavaDoc _templatesDirectory;
106     private String JavaDoc _blogsDirectory;
107
108     /**
109      * Default constructor.
110      */

111     public EditBlogFlavorsPlugin() {
112     }
113
114     /**
115      * Set the {@link Fetcher}
116      *
117      * @param fetcher {@link Fetcher}
118      */

119     public void setFetcher(Fetcher fetcher) {
120         _fetcher = fetcher;
121     }
122
123     /**
124      * Set the default blojsom properties
125      *
126      * @param defaultProperties Default blojsom properties
127      */

128     public void setBlojsomProperties(Properties blojsomProperties) {
129         _blojsomProperties = blojsomProperties;
130     }
131
132     /**
133      * Initialize this plugin. This method only called when the plugin is instantiated.
134      *
135      * @throws If there is an error initializing the plugin
136      */

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     /**
145      * Add flavor information to the context
146      *
147      * @param blog {@link Blog}
148      * @param context Context
149      */

150     protected void addFlavorInformationToContext(Blog blog, Map context) {
151         // Put the available templates in the context for the edit flavors template
152
File JavaDoc templatesDirectory = new File JavaDoc(_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 JavaDoc[] templates = templatesDirectory.listFiles();
156         ArrayList templatesList = new ArrayList(templates.length);
157         for (int i = 0; i < templates.length; i++) {
158             File JavaDoc template = templates[i];
159             if (template.isFile()) {
160                 templatesList.add(template.getName());
161             }
162         }
163
164         // Put the available flavors in the context for the edit flavors template
165
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     /**
171      * Process the blog entries
172      *
173      * @param httpServletRequest Request
174      * @param httpServletResponse Response
175      * @param blog {@link Blog} instance
176      * @param context Context
177      * @param entries Blog entries retrieved for the particular request
178      * @return Modified set of blog entries
179      * @throws PluginException If there is an error processing the blog entries
180      */

181     public Entry[] process(HttpServletRequest JavaDoc httpServletRequest, HttpServletResponse JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc[] {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 JavaDoc 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 JavaDoc[] {flavorName}));
290                 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_FLAVORS_PAGE);
291
292                 return entries;
293             }
294
295             // Delete the flavor from the maps
296
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             // Write out the flavor configuration file
305
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 JavaDoc[] {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 JavaDoc flavor = BlojsomUtils.getRequestValue(FLAVOR_NAME, httpServletRequest);
319
320             context.put(FLAVOR_NAME_EDIT, flavor);
321
322             Map templates = blog.getTemplates();
323             String JavaDoc templateAndType = (String JavaDoc) templates.get(flavor);
324             String JavaDoc[] templateType = BlojsomUtils.parseCommaList(templateAndType);
325             String JavaDoc[] typeAndCharacterSet;
326             if (templateType.length == 2) {
327                 typeAndCharacterSet = BlojsomUtils.parseDelimitedList(templateType[1], ";", true);
328             } else {
329                 typeAndCharacterSet = new String JavaDoc[] {"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