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.PluginException;
039: import org.blojsom.plugin.comment.CommentModerationPlugin;
040: import org.blojsom.plugin.comment.CommentPlugin;
041: import org.blojsom.plugin.pingback.PingbackPlugin;
042: import org.blojsom.plugin.trackback.TrackbackModerationPlugin;
043: import org.blojsom.plugin.trackback.TrackbackPlugin;
044: import org.blojsom.plugin.weblogsping.WeblogsPingPlugin;
045: import org.blojsom.util.BlojsomConstants;
046: import org.blojsom.util.BlojsomUtils;
047:
048: import javax.servlet.http.HttpServletRequest;
049: import javax.servlet.http.HttpServletResponse;
050: import java.util.Map;
051: import java.util.Properties;
052:
053: /**
054: * EditBlogPropertiesPlugin
055: *
056: * @author David Czarnecki
057: * @version $Id: EditBlogPropertiesPlugin.java,v 1.11 2007/01/17 02:35:05 czarneckid Exp $
058: * @since blojsom 3.0
059: */
060: public class EditBlogPropertiesPlugin extends BaseAdminPlugin {
061:
062: private static Log _logger = LogFactory
063: .getLog(EditBlogPropertiesPlugin.class);
064:
065: private static final String EDIT_BLOG_PROPERTIES_PAGE = "/org/blojsom/plugin/admin/templates/admin-edit-blog-properties";
066:
067: // Localization constants
068: private static final String FAILED_EDIT_PROPERTIES_PERMISSION_KEY = "failed.edit.properties.permission.text";
069: private static final String UPDATED_BLOG_PROPERTIES_KEY = "updated.blog.properties.text";
070: private static final String FAILED_SAVE_BLOG_PROPERTIES_KEY = "failed.save.blog.properties.text";
071: private static final String BLOG_PROPERTY_HAS_VALUE_KEY = "blog.property.has.value.text";
072: private static final String BLOG_PROPERTY_NOT_FOUND_KEY = "blog.property.not.found.text";
073:
074: // Actions
075: private static final String EDIT_BLOG_PROPERTIES_ACTION = "edit-blog-properties";
076: private static final String CHECK_BLOG_PROPERTY_ACTION = "check-blog-property";
077: private static final String SET_BLOG_PROPERTY_ACTION = "set-blog-property";
078:
079: private static final String BLOJSOM_INSTALLED_LOCALES = "BLOJSOM_INSTALLED_LOCALES";
080: private static final String BLOJSOM_JVM_LANGUAGES = "BLOJSOM_JVM_LANGUAGES";
081: private static final String BLOJSOM_JVM_COUNTRIES = "BLOJSOM_JVM_COUNTRIES";
082: private static final String BLOJSOM_JVM_TIMEZONES = "BLOJSOM_JVM_TIMEZONES";
083:
084: // Permissions
085: private static final String EDIT_BLOG_PROPERTIES_PERMISSION = "edit_blog_properties_permission";
086: private static final String SET_ARBITRARY_PROPERTIES_PERMISSION = "set_arbitrary_properties_permission";
087: private static final String CHECK_ARBITRARY_PROPERTIES_PERMISSION = "check_arbitrary_properties_permission";
088:
089: // Form items
090: private static final String INDIVIDUAL_BLOG_PROPERTY = "individual-blog-property";
091: private static final String INDIVIDUAL_BLOG_PROPERTY_VALUE = "individual-blog-property-value";
092:
093: private Properties _blojsomProperties;
094: private Fetcher _fetcher;
095:
096: /**
097: * Default constructor.
098: */
099: public EditBlogPropertiesPlugin() {
100: }
101:
102: /**
103: * Set the {@link Fetcher}
104: *
105: * @param fetcher {@link Fetcher}
106: */
107: public void setFetcher(Fetcher fetcher) {
108: _fetcher = fetcher;
109: }
110:
111: /**
112: * Set the default properties
113: *
114: * @param properties Default properties
115: */
116: public void setBlojsomProperties(Properties blojsomProperties) {
117: _blojsomProperties = blojsomProperties;
118: }
119:
120: /**
121: * Process the blog entries
122: *
123: * @param httpServletRequest Request
124: * @param httpServletResponse Response
125: * @param blog {@link Blog} instance
126: * @param context Context
127: * @param entries Blog entries retrieved for the particular request
128: * @return Modified set of blog entries
129: * @throws PluginException If there is an error processing the blog entries
130: */
131: public Entry[] process(HttpServletRequest httpServletRequest,
132: HttpServletResponse httpServletResponse, Blog blog,
133: Map context, Entry[] entries) throws PluginException {
134: if (!authenticateUser(httpServletRequest, httpServletResponse,
135: context, blog)) {
136: httpServletRequest.setAttribute(
137: BlojsomConstants.PAGE_PARAM, ADMIN_LOGIN_PAGE);
138:
139: return entries;
140: }
141:
142: String username = getUsernameFromSession(httpServletRequest,
143: blog);
144: if (!checkPermission(blog, null, username,
145: EDIT_BLOG_PROPERTIES_PERMISSION)) {
146: httpServletRequest.setAttribute(
147: BlojsomConstants.PAGE_PARAM,
148: ADMIN_ADMINISTRATION_PAGE);
149: addOperationResultMessage(context, getAdminResource(
150: FAILED_EDIT_PROPERTIES_PERMISSION_KEY,
151: FAILED_EDIT_PROPERTIES_PERMISSION_KEY, blog
152: .getBlogAdministrationLocale()));
153:
154: return entries;
155: }
156:
157: String action = BlojsomUtils.getRequestValue(ACTION_PARAM,
158: httpServletRequest);
159: if (BlojsomUtils.checkNullOrBlank(action)) {
160: _logger.debug("User did not request edit action");
161: httpServletRequest.setAttribute(
162: BlojsomConstants.PAGE_PARAM,
163: ADMIN_ADMINISTRATION_PAGE);
164: } else if (PAGE_ACTION.equals(action)) {
165: _logger.debug("User requested edit page");
166: httpServletRequest.setAttribute(
167: BlojsomConstants.PAGE_PARAM,
168: EDIT_BLOG_PROPERTIES_PAGE);
169: } else if (EDIT_BLOG_PROPERTIES_ACTION.equals(action)) {
170: _logger.debug("User requested edit action");
171:
172: String blogPropertyValue = BlojsomUtils.getRequestValue(
173: BlojsomConstants.BLOG_NAME_IP, httpServletRequest);
174: blog.setBlogName(blogPropertyValue);
175: blogPropertyValue = BlojsomUtils.getRequestValue(
176: BlojsomConstants.BLOG_DESCRIPTION_IP,
177: httpServletRequest);
178: blog.setBlogDescription(blogPropertyValue);
179: blogPropertyValue = BlojsomUtils.getRequestValue(
180: BlojsomConstants.BLOG_COUNTRY_IP,
181: httpServletRequest);
182: blog.setBlogCountry(blogPropertyValue);
183: blogPropertyValue = BlojsomUtils.getRequestValue(
184: BlojsomConstants.BLOG_LANGUAGE_IP,
185: httpServletRequest);
186: blog.setBlogLanguage(blogPropertyValue);
187: blogPropertyValue = BlojsomUtils.getRequestValue(
188: BlojsomConstants.BLOG_ADMINISTRATION_LOCALE_IP,
189: httpServletRequest);
190: blog.setBlogAdministrationLocale(blogPropertyValue);
191: blogPropertyValue = BlojsomUtils.getRequestValue(
192: "blog-timezone-id", httpServletRequest);
193: if (BlojsomUtils.checkNullOrBlank(blogPropertyValue)) {
194: blogPropertyValue = BlojsomConstants.BLOG_DEFAULT_TIMEZONE;
195: }
196: blog.setProperty("blog-timezone-id", blogPropertyValue);
197: blogPropertyValue = BlojsomUtils.getRequestValue(
198: "blog-display-entries", httpServletRequest);
199: try {
200: int blogDisplayEntries = Integer
201: .parseInt(blogPropertyValue);
202: blog.setBlogDisplayEntries(blogDisplayEntries);
203: } catch (NumberFormatException e) {
204: _logger.error(
205: "Blog display entries parameter invalid.", e);
206: }
207: blogPropertyValue = BlojsomUtils.getRequestValue(
208: BlojsomConstants.BLOG_OWNER, httpServletRequest);
209: blog.setBlogOwner(blogPropertyValue);
210: blogPropertyValue = BlojsomUtils.getRequestValue(
211: BlojsomConstants.BLOG_OWNER_EMAIL,
212: httpServletRequest);
213: blog.setBlogOwnerEmail(blogPropertyValue);
214: blogPropertyValue = BlojsomUtils.getRequestValue(
215: BlojsomConstants.BLOG_COMMENTS_ENABLED_IP,
216: httpServletRequest);
217: blog.setBlogCommentsEnabled(Boolean
218: .valueOf(blogPropertyValue));
219: blogPropertyValue = BlojsomUtils.getRequestValue(
220: BlojsomConstants.BLOG_TRACKBACKS_ENABLED_IP,
221: httpServletRequest);
222: blog.setBlogTrackbacksEnabled(Boolean
223: .valueOf(blogPropertyValue));
224: blogPropertyValue = BlojsomUtils.getRequestValue(
225: BlojsomConstants.BLOG_EMAIL_ENABLED_IP,
226: httpServletRequest);
227: blog
228: .setBlogEmailEnabled(Boolean
229: .valueOf(blogPropertyValue));
230: blogPropertyValue = BlojsomUtils.getRequestValue(
231: BlojsomConstants.BLOG_DEFAULT_FLAVOR_IP,
232: httpServletRequest);
233: blog.setBlogDefaultFlavor(blogPropertyValue);
234: blogPropertyValue = BlojsomUtils.getRequestValue(
235: BlojsomConstants.LINEAR_NAVIGATION_ENABLED_IP,
236: httpServletRequest);
237: blog.setLinearNavigationEnabled(Boolean
238: .valueOf(blogPropertyValue));
239: blogPropertyValue = BlojsomUtils.getRequestValue(
240: BlojsomConstants.BLOG_URL_IP, httpServletRequest);
241: blog.setBlogURL(blogPropertyValue);
242: blogPropertyValue = BlojsomUtils.getRequestValue(
243: BlojsomConstants.BLOG_BASE_URL_IP,
244: httpServletRequest);
245: blog.setBlogBaseURL(blogPropertyValue);
246: blogPropertyValue = BlojsomUtils.getRequestValue(
247: BlojsomConstants.DEFAULT_POST_CATEGORY,
248: httpServletRequest);
249: blog.setProperty(BlojsomConstants.DEFAULT_POST_CATEGORY,
250: blogPropertyValue);
251: blogPropertyValue = BlojsomUtils.getRequestValue(
252: BlojsomConstants.USE_DYNAMIC_BLOG_URLS,
253: httpServletRequest);
254: if (!BlojsomUtils.checkNullOrBlank(blogPropertyValue)) {
255: blog.setProperty(
256: BlojsomConstants.USE_DYNAMIC_BLOG_URLS, "true");
257: } else {
258: blog
259: .setProperty(
260: BlojsomConstants.USE_DYNAMIC_BLOG_URLS,
261: "false");
262: }
263:
264: // Comment plugin properties
265: blogPropertyValue = BlojsomUtils.getRequestValue(
266: CommentPlugin.COMMENT_AUTOFORMAT_IP,
267: httpServletRequest);
268: blog.setProperty(CommentPlugin.COMMENT_AUTOFORMAT_IP,
269: blogPropertyValue);
270: blogPropertyValue = BlojsomUtils
271: .getRequestValue(CommentPlugin.COMMENT_PREFIX_IP,
272: httpServletRequest);
273: blog.setProperty(CommentPlugin.COMMENT_PREFIX_IP,
274: blogPropertyValue);
275: blogPropertyValue = BlojsomUtils
276: .getRequestValue(
277: CommentPlugin.COMMENT_COOKIE_EXPIRATION_DURATION_IP,
278: httpServletRequest);
279: blog
280: .setProperty(
281: CommentPlugin.COMMENT_COOKIE_EXPIRATION_DURATION_IP,
282: blogPropertyValue);
283: blogPropertyValue = BlojsomUtils.getRequestValue(
284: CommentPlugin.COMMENT_THROTTLE_MINUTES_IP,
285: httpServletRequest);
286: blog.setProperty(CommentPlugin.COMMENT_THROTTLE_MINUTES_IP,
287: blogPropertyValue);
288: blogPropertyValue = BlojsomUtils.getRequestValue(
289: CommentPlugin.COMMENT_DAYS_EXPIRATION_IP,
290: httpServletRequest);
291: blog.setProperty(CommentPlugin.COMMENT_DAYS_EXPIRATION_IP,
292: blogPropertyValue);
293: blogPropertyValue = BlojsomUtils.getRequestValue(
294: CommentModerationPlugin.COMMENT_MODERATION_ENABLED,
295: httpServletRequest);
296: blog.setProperty(
297: CommentModerationPlugin.COMMENT_MODERATION_ENABLED,
298: blogPropertyValue);
299:
300: // Trackback plugin properties
301: blogPropertyValue = BlojsomUtils.getRequestValue(
302: TrackbackPlugin.TRACKBACK_THROTTLE_MINUTES_IP,
303: httpServletRequest);
304: blog.setProperty(
305: TrackbackPlugin.TRACKBACK_THROTTLE_MINUTES_IP,
306: blogPropertyValue);
307: blogPropertyValue = BlojsomUtils.getRequestValue(
308: TrackbackPlugin.TRACKBACK_PREFIX_IP,
309: httpServletRequest);
310: blog.setProperty(TrackbackPlugin.TRACKBACK_PREFIX_IP,
311: blogPropertyValue);
312: blogPropertyValue = BlojsomUtils.getRequestValue(
313: TrackbackPlugin.TRACKBACK_DAYS_EXPIRATION_IP,
314: httpServletRequest);
315: blog.setProperty(
316: TrackbackPlugin.TRACKBACK_DAYS_EXPIRATION_IP,
317: blogPropertyValue);
318: blogPropertyValue = BlojsomUtils
319: .getRequestValue(
320: TrackbackModerationPlugin.TRACKBACK_MODERATION_ENABLED,
321: httpServletRequest);
322: blog
323: .setProperty(
324: TrackbackModerationPlugin.TRACKBACK_MODERATION_ENABLED,
325: blogPropertyValue);
326:
327: // Pingback properties
328: blogPropertyValue = BlojsomUtils.getRequestValue(
329: BlojsomConstants.BLOG_PINGBACKS_ENABLED_IP,
330: httpServletRequest);
331: blog.setBlogPingbacksEnabled(Boolean
332: .valueOf(blogPropertyValue));
333: blogPropertyValue = BlojsomUtils.getRequestValue(
334: PingbackPlugin.PINGBACK_PREFIX_IP,
335: httpServletRequest);
336: blog.setProperty(PingbackPlugin.PINGBACK_PREFIX_IP,
337: blogPropertyValue);
338: blogPropertyValue = BlojsomUtils.getRequestValue(
339: PingbackPlugin.PINGBACK_MODERATION_ENABLED,
340: httpServletRequest);
341: blog.setProperty(
342: PingbackPlugin.PINGBACK_MODERATION_ENABLED,
343: blogPropertyValue);
344:
345: // Weblogs Ping plugin properties
346: blogPropertyValue = BlojsomUtils.getRequestValue(
347: WeblogsPingPlugin.BLOG_PING_URLS_IP,
348: httpServletRequest);
349: String[] pingURLs = BlojsomUtils.parseDelimitedList(
350: blogPropertyValue, BlojsomUtils.WHITESPACE);
351: if (pingURLs != null && pingURLs.length > 0) {
352: blog.setProperty(WeblogsPingPlugin.BLOG_PING_URLS_IP,
353: BlojsomUtils.arrayOfStringsToString(pingURLs,
354: " "));
355: } else {
356: blog.setProperty(WeblogsPingPlugin.BLOG_PING_URLS_IP,
357: "");
358: }
359:
360: // XML-RPC settings
361: blogPropertyValue = BlojsomUtils.getRequestValue(
362: BlojsomConstants.XMLRPC_ENABLED_IP,
363: httpServletRequest);
364: blog.setXmlrpcEnabled(Boolean.valueOf(blogPropertyValue));
365:
366: try {
367: _fetcher.saveBlog(blog);
368:
369: addOperationResultMessage(context, getAdminResource(
370: UPDATED_BLOG_PROPERTIES_KEY,
371: UPDATED_BLOG_PROPERTIES_KEY, blog
372: .getBlogAdministrationLocale()));
373: } catch (FetcherException e) {
374: if (_logger.isErrorEnabled()) {
375: _logger.error(e);
376: }
377:
378: addOperationResultMessage(context, getAdminResource(
379: FAILED_SAVE_BLOG_PROPERTIES_KEY,
380: FAILED_SAVE_BLOG_PROPERTIES_KEY, blog
381: .getBlogAdministrationLocale()));
382: }
383:
384: // Request that we go back to the edit blog properties page
385: httpServletRequest.setAttribute(
386: BlojsomConstants.PAGE_PARAM,
387: EDIT_BLOG_PROPERTIES_PAGE);
388: } else if (SET_BLOG_PROPERTY_ACTION.equals(action)) {
389: _logger.debug("User requested set blog property action");
390:
391: if (!checkPermission(blog, null, username,
392: SET_ARBITRARY_PROPERTIES_PERMISSION)) {
393: httpServletRequest.setAttribute(
394: BlojsomConstants.PAGE_PARAM,
395: ADMIN_ADMINISTRATION_PAGE);
396: addOperationResultMessage(context, getAdminResource(
397: FAILED_EDIT_PROPERTIES_PERMISSION_KEY,
398: FAILED_EDIT_PROPERTIES_PERMISSION_KEY, blog
399: .getBlogAdministrationLocale()));
400:
401: return entries;
402: }
403:
404: String blogProperty = BlojsomUtils.getRequestValue(
405: INDIVIDUAL_BLOG_PROPERTY, httpServletRequest);
406: if (!BlojsomUtils.checkNullOrBlank(blogProperty)) {
407: String blogPropertyValue = BlojsomUtils
408: .getRequestValue(
409: INDIVIDUAL_BLOG_PROPERTY_VALUE,
410: httpServletRequest);
411: if (blogPropertyValue == null) {
412: blogPropertyValue = "";
413: }
414:
415: blog.setProperty(blogProperty, blogPropertyValue);
416:
417: try {
418: _fetcher.saveBlog(blog);
419:
420: addOperationResultMessage(context,
421: getAdminResource(
422: UPDATED_BLOG_PROPERTIES_KEY,
423: UPDATED_BLOG_PROPERTIES_KEY,
424: blog.getBlogAdministrationLocale()));
425: } catch (FetcherException e) {
426: if (_logger.isErrorEnabled()) {
427: _logger.error(e);
428: }
429:
430: addOperationResultMessage(context,
431: getAdminResource(
432: FAILED_SAVE_BLOG_PROPERTIES_KEY,
433: FAILED_SAVE_BLOG_PROPERTIES_KEY,
434: blog.getBlogAdministrationLocale()));
435: }
436: }
437:
438: // Request that we go back to the edit blog properties page
439: httpServletRequest.setAttribute(
440: BlojsomConstants.PAGE_PARAM,
441: EDIT_BLOG_PROPERTIES_PAGE);
442: } else if (CHECK_BLOG_PROPERTY_ACTION.equals(action)) {
443: _logger.debug("User requested check blog property action");
444:
445: if (!checkPermission(blog, null, username,
446: CHECK_ARBITRARY_PROPERTIES_PERMISSION)) {
447: httpServletRequest.setAttribute(
448: BlojsomConstants.PAGE_PARAM,
449: ADMIN_ADMINISTRATION_PAGE);
450: addOperationResultMessage(context, getAdminResource(
451: FAILED_EDIT_PROPERTIES_PERMISSION_KEY,
452: FAILED_EDIT_PROPERTIES_PERMISSION_KEY, blog
453: .getBlogAdministrationLocale()));
454:
455: return entries;
456: }
457:
458: String blogProperty = BlojsomUtils.getRequestValue(
459: INDIVIDUAL_BLOG_PROPERTY, httpServletRequest);
460:
461: if (!BlojsomUtils.checkNullOrBlank(blogProperty)) {
462: if (blog.getProperty(blogProperty) != null) {
463: addOperationResultMessage(
464: context,
465: formatAdminResource(
466: BLOG_PROPERTY_HAS_VALUE_KEY,
467: BLOG_PROPERTY_HAS_VALUE_KEY,
468: blog.getBlogAdministrationLocale(),
469: new Object[] {
470: blogProperty,
471: blog
472: .getProperty(blogProperty) }));
473: } else {
474: addOperationResultMessage(context,
475: formatAdminResource(
476: BLOG_PROPERTY_NOT_FOUND_KEY,
477: BLOG_PROPERTY_NOT_FOUND_KEY,
478: blog.getBlogAdministrationLocale(),
479: new Object[] { blogProperty }));
480: }
481: }
482:
483: // Request that we go back to the edit blog properties page
484: httpServletRequest.setAttribute(
485: BlojsomConstants.PAGE_PARAM,
486: EDIT_BLOG_PROPERTIES_PAGE);
487: }
488:
489: String installedLocales = _blojsomProperties
490: .getProperty(BlojsomConstants.INSTALLED_LOCALES_IP);
491: if (installedLocales != null) {
492: context.put(BLOJSOM_INSTALLED_LOCALES, BlojsomUtils
493: .parseCommaList(installedLocales));
494: }
495: context.put(BLOJSOM_JVM_LANGUAGES, BlojsomUtils
496: .getLanguagesForSystem(blog
497: .getBlogAdministrationLocale()));
498: context.put(BLOJSOM_JVM_COUNTRIES, BlojsomUtils
499: .getCountriesForSystem(blog
500: .getBlogAdministrationLocale()));
501: context.put(BLOJSOM_JVM_TIMEZONES, BlojsomUtils
502: .getTimeZonesForSystem(blog
503: .getBlogAdministrationLocale()));
504:
505: return entries;
506: }
507: }
|