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.fetcher.Fetcher;
035: import org.blojsom.fetcher.FetcherException;
036: import org.blojsom.blog.Blog;
037: import org.blojsom.blog.Entry;
038: import org.blojsom.blog.User;
039: import org.blojsom.plugin.PluginException;
040: import org.blojsom.util.BlojsomConstants;
041: import org.blojsom.util.BlojsomUtils;
042:
043: import javax.servlet.http.HttpServletRequest;
044: import javax.servlet.http.HttpServletResponse;
045: import java.util.Map;
046:
047: /**
048: * @author David Czarnecki
049: * @version $Id: EditBlogPreferencesPlugin.java,v 1.3 2007/01/17 02:35:05 czarneckid Exp $
050: */
051: public class EditBlogPreferencesPlugin extends BaseAdminPlugin {
052:
053: private static Log _logger = LogFactory
054: .getLog(EditBlogPropertiesPlugin.class);
055:
056: private static final String EDIT_BLOG_PREFERENCES_PAGE = "/org/blojsom/plugin/admin/templates/admin-edit-blog-preferences";
057:
058: // Localization constants
059: private static final String FAILED_EDIT_PREFERENCES_PERMISSION_KEY = "failed.edit.preferences.permission.text";
060:
061: // Permissions
062: private static final String EDIT_BLOG_PREFERENCES_PERMISSION = "edit_blog_preferences_permission";
063:
064: // Actions
065: private static final String EDIT_BLOG_PREFERENCES_ACTION = "edit-blog-preferences";
066:
067: private static final String BLOJSOM_USER_OBJECT = "BLOJSOM_USER_OBJECT";
068:
069: private Fetcher _fetcher;
070:
071: /**
072: * Default constructor.
073: */
074: public EditBlogPreferencesPlugin() {
075: }
076:
077: /**
078: * Set the {@link Fetcher}
079: *
080: * @param fetcher {@link Fetcher}
081: */
082: public void setFetcher(Fetcher fetcher) {
083: _fetcher = fetcher;
084: }
085:
086: /**
087: * Process the blog entries
088: *
089: * @param httpServletRequest Request
090: * @param httpServletResponse Response
091: * @param blog {@link Blog} instance
092: * @param context Context
093: * @param entries Blog entries retrieved for the particular request
094: * @return Modified set of blog entries
095: * @throws PluginException If there is an error processing the blog entries
096: */
097: public Entry[] process(HttpServletRequest httpServletRequest,
098: HttpServletResponse httpServletResponse, Blog blog,
099: Map context, Entry[] entries) throws PluginException {
100: if (!authenticateUser(httpServletRequest, httpServletResponse,
101: context, blog)) {
102: httpServletRequest.setAttribute(
103: BlojsomConstants.PAGE_PARAM, ADMIN_LOGIN_PAGE);
104:
105: return entries;
106: }
107:
108: String username = getUsernameFromSession(httpServletRequest,
109: blog);
110: if (!checkPermission(blog, null, username,
111: EDIT_BLOG_PREFERENCES_PERMISSION)) {
112: httpServletRequest.setAttribute(
113: BlojsomConstants.PAGE_PARAM,
114: ADMIN_ADMINISTRATION_PAGE);
115: addOperationResultMessage(context, getAdminResource(
116: FAILED_EDIT_PREFERENCES_PERMISSION_KEY,
117: FAILED_EDIT_PREFERENCES_PERMISSION_KEY, blog
118: .getBlogAdministrationLocale()));
119:
120: return entries;
121: }
122:
123: String action = BlojsomUtils.getRequestValue(ACTION_PARAM,
124: httpServletRequest);
125: if (BlojsomUtils.checkNullOrBlank(action)) {
126: _logger.debug("User did not request edit action");
127: httpServletRequest.setAttribute(
128: BlojsomConstants.PAGE_PARAM,
129: ADMIN_ADMINISTRATION_PAGE);
130: } else if (PAGE_ACTION.equals(action)) {
131: _logger.debug("User requested edit page");
132: httpServletRequest.setAttribute(
133: BlojsomConstants.PAGE_PARAM,
134: EDIT_BLOG_PREFERENCES_PAGE);
135: } else if (EDIT_BLOG_PREFERENCES_ACTION.equals(action)) {
136: _logger.debug("User requested edit action");
137:
138: // User-specific settings
139: try {
140: User user = _fetcher.loadUser(blog, username);
141:
142: String blogPropertyValue = BlojsomUtils
143: .getRequestValue(
144: BlojsomConstants.USE_RICHTEXT_EDITOR_PREFERENCE,
145: httpServletRequest);
146: user
147: .getMetaData()
148: .put(
149: BlojsomConstants.USE_RICHTEXT_EDITOR_PREFERENCE,
150: blogPropertyValue);
151:
152: blogPropertyValue = BlojsomUtils
153: .getRequestValue(
154: BlojsomConstants.DISPLAY_RESPONSE_TEXT_PREFERENCE,
155: httpServletRequest);
156: user
157: .getMetaData()
158: .put(
159: BlojsomConstants.DISPLAY_RESPONSE_TEXT_PREFERENCE,
160: blogPropertyValue);
161:
162: _fetcher.saveUser(blog, user);
163: } catch (FetcherException e) {
164: if (_logger.isErrorEnabled()) {
165: _logger.error(e);
166: }
167: }
168:
169: // Request that we go back to the edit blog properties page
170: httpServletRequest.setAttribute(
171: BlojsomConstants.PAGE_PARAM,
172: EDIT_BLOG_PREFERENCES_PAGE);
173: }
174:
175: try {
176: context.put(BLOJSOM_USER_OBJECT, _fetcher.loadUser(blog,
177: username));
178: } catch (FetcherException e) {
179: if (_logger.isErrorEnabled()) {
180: _logger.error(e);
181: }
182: }
183:
184: return entries;
185: }
186: }
|