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.plugin.comment.CommentModerationPlugin; 41 import org.blojsom.plugin.comment.CommentPlugin; 42 import org.blojsom.plugin.pingback.PingbackPlugin; 43 import org.blojsom.plugin.trackback.TrackbackModerationPlugin; 44 import org.blojsom.plugin.trackback.TrackbackPlugin; 45 import org.blojsom.plugin.weblogsping.WeblogsPingPlugin; 46 import org.blojsom.util.BlojsomConstants; 47 import org.blojsom.util.BlojsomUtils; 48 49 import javax.servlet.http.HttpServletRequest ; 50 import javax.servlet.http.HttpServletResponse ; 51 import java.util.Map ; 52 import java.util.Properties ; 53 54 61 public class EditBlogPropertiesPlugin extends BaseAdminPlugin { 62 63 private static Log _logger = LogFactory.getLog(EditBlogPropertiesPlugin.class); 64 65 private static final String EDIT_BLOG_PROPERTIES_PAGE = "/org/blojsom/plugin/admin/templates/admin-edit-blog-properties"; 66 67 private static final String FAILED_EDIT_PROPERTIES_PERMISSION_KEY = "failed.edit.properties.permission.text"; 69 private static final String UPDATED_BLOG_PROPERTIES_KEY = "updated.blog.properties.text"; 70 private static final String FAILED_SAVE_BLOG_PROPERTIES_KEY = "failed.save.blog.properties.text"; 71 private static final String BLOG_PROPERTY_HAS_VALUE_KEY = "blog.property.has.value.text"; 72 private static final String BLOG_PROPERTY_NOT_FOUND_KEY = "blog.property.not.found.text"; 73 74 private static final String EDIT_BLOG_PROPERTIES_ACTION = "edit-blog-properties"; 76 private static final String CHECK_BLOG_PROPERTY_ACTION = "check-blog-property"; 77 private static final String SET_BLOG_PROPERTY_ACTION = "set-blog-property"; 78 79 private static final String BLOJSOM_INSTALLED_LOCALES = "BLOJSOM_INSTALLED_LOCALES"; 80 private static final String BLOJSOM_JVM_LANGUAGES = "BLOJSOM_JVM_LANGUAGES"; 81 private static final String BLOJSOM_JVM_COUNTRIES = "BLOJSOM_JVM_COUNTRIES"; 82 private static final String BLOJSOM_JVM_TIMEZONES = "BLOJSOM_JVM_TIMEZONES"; 83 84 private static final String EDIT_BLOG_PROPERTIES_PERMISSION = "edit_blog_properties_permission"; 86 private static final String SET_ARBITRARY_PROPERTIES_PERMISSION = "set_arbitrary_properties_permission"; 87 private static final String CHECK_ARBITRARY_PROPERTIES_PERMISSION = "check_arbitrary_properties_permission"; 88 89 private static final String INDIVIDUAL_BLOG_PROPERTY = "individual-blog-property"; 91 private static final String INDIVIDUAL_BLOG_PROPERTY_VALUE = "individual-blog-property-value"; 92 93 private Properties _blojsomProperties; 94 private Fetcher _fetcher; 95 96 99 public EditBlogPropertiesPlugin() { 100 } 101 102 107 public void setFetcher(Fetcher fetcher) { 108 _fetcher = fetcher; 109 } 110 111 116 public void setBlojsomProperties(Properties blojsomProperties) { 117 _blojsomProperties = blojsomProperties; 118 } 119 120 131 public Entry[] process(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Blog blog, Map context, Entry[] entries) throws PluginException { 132 if (!authenticateUser(httpServletRequest, httpServletResponse, context, blog)) { 133 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, ADMIN_LOGIN_PAGE); 134 135 return entries; 136 } 137 138 String username = getUsernameFromSession(httpServletRequest, blog); 139 if (!checkPermission(blog, null, username, EDIT_BLOG_PROPERTIES_PERMISSION)) { 140 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, ADMIN_ADMINISTRATION_PAGE); 141 addOperationResultMessage(context, getAdminResource(FAILED_EDIT_PROPERTIES_PERMISSION_KEY, FAILED_EDIT_PROPERTIES_PERMISSION_KEY, blog.getBlogAdministrationLocale())); 142 143 return entries; 144 } 145 146 String action = BlojsomUtils.getRequestValue(ACTION_PARAM, httpServletRequest); 147 if (BlojsomUtils.checkNullOrBlank(action)) { 148 _logger.debug("User did not request edit action"); 149 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, ADMIN_ADMINISTRATION_PAGE); 150 } else if (PAGE_ACTION.equals(action)) { 151 _logger.debug("User requested edit page"); 152 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_PROPERTIES_PAGE); 153 } else if (EDIT_BLOG_PROPERTIES_ACTION.equals(action)) { 154 _logger.debug("User requested edit action"); 155 156 String blogPropertyValue = BlojsomUtils.getRequestValue(BlojsomConstants.BLOG_NAME_IP, httpServletRequest); 157 blog.setBlogName(blogPropertyValue); 158 blogPropertyValue = BlojsomUtils.getRequestValue(BlojsomConstants.BLOG_DESCRIPTION_IP, httpServletRequest); 159 blog.setBlogDescription(blogPropertyValue); 160 blogPropertyValue = BlojsomUtils.getRequestValue(BlojsomConstants.BLOG_COUNTRY_IP, httpServletRequest); 161 blog.setBlogCountry(blogPropertyValue); 162 blogPropertyValue = BlojsomUtils.getRequestValue(BlojsomConstants.BLOG_LANGUAGE_IP, httpServletRequest); 163 blog.setBlogLanguage(blogPropertyValue); 164 blogPropertyValue = BlojsomUtils.getRequestValue(BlojsomConstants.BLOG_ADMINISTRATION_LOCALE_IP, httpServletRequest); 165 blog.setBlogAdministrationLocale(blogPropertyValue); 166 blogPropertyValue = BlojsomUtils.getRequestValue("blog-timezone-id", httpServletRequest); 167 if (BlojsomUtils.checkNullOrBlank(blogPropertyValue)) { 168 blogPropertyValue = BlojsomConstants.BLOG_DEFAULT_TIMEZONE; 169 } 170 blog.setProperty("blog-timezone-id", blogPropertyValue); 171 blogPropertyValue = BlojsomUtils.getRequestValue("blog-display-entries", httpServletRequest); 172 try { 173 int blogDisplayEntries = Integer.parseInt(blogPropertyValue); 174 blog.setBlogDisplayEntries(blogDisplayEntries); 175 } catch (NumberFormatException e) { 176 _logger.error("Blog display entries parameter invalid.", e); 177 } 178 blogPropertyValue = BlojsomUtils.getRequestValue(BlojsomConstants.BLOG_OWNER, httpServletRequest); 179 blog.setBlogOwner(blogPropertyValue); 180 blogPropertyValue = BlojsomUtils.getRequestValue(BlojsomConstants.BLOG_OWNER_EMAIL, httpServletRequest); 181 blog.setBlogOwnerEmail(blogPropertyValue); 182 blogPropertyValue = BlojsomUtils.getRequestValue(BlojsomConstants.BLOG_COMMENTS_ENABLED_IP, httpServletRequest); 183 blog.setBlogCommentsEnabled(Boolean.valueOf(blogPropertyValue)); 184 blogPropertyValue = BlojsomUtils.getRequestValue(BlojsomConstants.BLOG_TRACKBACKS_ENABLED_IP, httpServletRequest); 185 blog.setBlogTrackbacksEnabled(Boolean.valueOf(blogPropertyValue)); 186 blogPropertyValue = BlojsomUtils.getRequestValue(BlojsomConstants.BLOG_EMAIL_ENABLED_IP, httpServletRequest); 187 blog.setBlogEmailEnabled(Boolean.valueOf(blogPropertyValue)); 188 blogPropertyValue = BlojsomUtils.getRequestValue(BlojsomConstants.BLOG_DEFAULT_FLAVOR_IP, httpServletRequest); 189 blog.setBlogDefaultFlavor(blogPropertyValue); 190 blogPropertyValue = BlojsomUtils.getRequestValue(BlojsomConstants.LINEAR_NAVIGATION_ENABLED_IP, httpServletRequest); 191 blog.setLinearNavigationEnabled(Boolean.valueOf(blogPropertyValue)); 192 blogPropertyValue = BlojsomUtils.getRequestValue(BlojsomConstants.BLOG_URL_IP, httpServletRequest); 193 blog.setBlogURL(blogPropertyValue); 194 blogPropertyValue = BlojsomUtils.getRequestValue(BlojsomConstants.BLOG_BASE_URL_IP, httpServletRequest); 195 blog.setBlogBaseURL(blogPropertyValue); 196 blogPropertyValue = BlojsomUtils.getRequestValue(BlojsomConstants.DEFAULT_POST_CATEGORY, httpServletRequest); 197 blog.setProperty(BlojsomConstants.DEFAULT_POST_CATEGORY, blogPropertyValue); 198 199 blogPropertyValue = BlojsomUtils.getRequestValue(CommentPlugin.COMMENT_AUTOFORMAT_IP, httpServletRequest); 201 blog.setProperty(CommentPlugin.COMMENT_AUTOFORMAT_IP, blogPropertyValue); 202 blogPropertyValue = BlojsomUtils.getRequestValue(CommentPlugin.COMMENT_PREFIX_IP, httpServletRequest); 203 blog.setProperty(CommentPlugin.COMMENT_PREFIX_IP, blogPropertyValue); 204 blogPropertyValue = BlojsomUtils.getRequestValue(CommentPlugin.COMMENT_COOKIE_EXPIRATION_DURATION_IP, httpServletRequest); 205 blog.setProperty(CommentPlugin.COMMENT_COOKIE_EXPIRATION_DURATION_IP, blogPropertyValue); 206 blogPropertyValue = BlojsomUtils.getRequestValue(CommentPlugin.COMMENT_THROTTLE_MINUTES_IP, httpServletRequest); 207 blog.setProperty(CommentPlugin.COMMENT_THROTTLE_MINUTES_IP, blogPropertyValue); 208 blogPropertyValue = BlojsomUtils.getRequestValue(CommentPlugin.COMMENT_DAYS_EXPIRATION_IP, httpServletRequest); 209 blog.setProperty(CommentPlugin.COMMENT_DAYS_EXPIRATION_IP, blogPropertyValue); 210 blogPropertyValue = BlojsomUtils.getRequestValue(CommentModerationPlugin.COMMENT_MODERATION_ENABLED, httpServletRequest); 211 blog.setProperty(CommentModerationPlugin.COMMENT_MODERATION_ENABLED, blogPropertyValue); 212 213 blogPropertyValue = BlojsomUtils.getRequestValue(TrackbackPlugin.TRACKBACK_THROTTLE_MINUTES_IP, httpServletRequest); 215 blog.setProperty(TrackbackPlugin.TRACKBACK_THROTTLE_MINUTES_IP, blogPropertyValue); 216 blogPropertyValue = BlojsomUtils.getRequestValue(TrackbackPlugin.TRACKBACK_PREFIX_IP, httpServletRequest); 217 blog.setProperty(TrackbackPlugin.TRACKBACK_PREFIX_IP, blogPropertyValue); 218 blogPropertyValue = BlojsomUtils.getRequestValue(TrackbackPlugin.TRACKBACK_DAYS_EXPIRATION_IP, httpServletRequest); 219 blog.setProperty(TrackbackPlugin.TRACKBACK_DAYS_EXPIRATION_IP, blogPropertyValue); 220 blogPropertyValue = BlojsomUtils.getRequestValue(TrackbackModerationPlugin.TRACKBACK_MODERATION_ENABLED, httpServletRequest); 221 blog.setProperty(TrackbackModerationPlugin.TRACKBACK_MODERATION_ENABLED, blogPropertyValue); 222 223 blogPropertyValue = BlojsomUtils.getRequestValue(BlojsomConstants.BLOG_PINGBACKS_ENABLED_IP, httpServletRequest); 225 blog.setBlogPingbacksEnabled(Boolean.valueOf(blogPropertyValue)); 226 blogPropertyValue = BlojsomUtils.getRequestValue(PingbackPlugin.PINGBACK_PREFIX_IP, httpServletRequest); 227 blog.setProperty(PingbackPlugin.PINGBACK_PREFIX_IP, blogPropertyValue); 228 blogPropertyValue = BlojsomUtils.getRequestValue(PingbackPlugin.PINGBACK_MODERATION_ENABLED, httpServletRequest); 229 blog.setProperty(PingbackPlugin.PINGBACK_MODERATION_ENABLED, blogPropertyValue); 230 231 blogPropertyValue = BlojsomUtils.getRequestValue(WeblogsPingPlugin.BLOG_PING_URLS_IP, httpServletRequest); 233 String [] pingURLs = BlojsomUtils.parseDelimitedList(blogPropertyValue, BlojsomUtils.WHITESPACE); 234 if (pingURLs != null && pingURLs.length > 0) { 235 blog.setProperty(WeblogsPingPlugin.BLOG_PING_URLS_IP, BlojsomUtils.arrayOfStringsToString(pingURLs, " ")); 236 } else { 237 blog.setProperty(WeblogsPingPlugin.BLOG_PING_URLS_IP, ""); 238 } 239 240 blogPropertyValue = BlojsomUtils.getRequestValue(BlojsomConstants.XMLRPC_ENABLED_IP, httpServletRequest); 242 blog.setXmlrpcEnabled(Boolean.valueOf(blogPropertyValue)); 243 244 try { 245 _fetcher.saveBlog(blog); 246 247 addOperationResultMessage(context, getAdminResource(UPDATED_BLOG_PROPERTIES_KEY, UPDATED_BLOG_PROPERTIES_KEY, blog.getBlogAdministrationLocale())); 248 } catch (FetcherException e) { 249 if (_logger.isErrorEnabled()) { 250 _logger.error(e); 251 } 252 253 addOperationResultMessage(context, getAdminResource(FAILED_SAVE_BLOG_PROPERTIES_KEY, FAILED_SAVE_BLOG_PROPERTIES_KEY, blog.getBlogAdministrationLocale())); 254 } 255 256 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_PROPERTIES_PAGE); 258 } else if (SET_BLOG_PROPERTY_ACTION.equals(action)) { 259 _logger.debug("User requested set blog property action"); 260 261 if (!checkPermission(blog, null, username, SET_ARBITRARY_PROPERTIES_PERMISSION)) { 262 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, ADMIN_ADMINISTRATION_PAGE); 263 addOperationResultMessage(context, getAdminResource(FAILED_EDIT_PROPERTIES_PERMISSION_KEY, FAILED_EDIT_PROPERTIES_PERMISSION_KEY, blog.getBlogAdministrationLocale())); 264 265 return entries; 266 } 267 268 String blogProperty = BlojsomUtils.getRequestValue(INDIVIDUAL_BLOG_PROPERTY, httpServletRequest); 269 if (!BlojsomUtils.checkNullOrBlank(blogProperty)) { 270 String blogPropertyValue = BlojsomUtils.getRequestValue(INDIVIDUAL_BLOG_PROPERTY_VALUE, httpServletRequest); 271 if (blogPropertyValue == null) { 272 blogPropertyValue = ""; 273 } 274 275 blog.setProperty(blogProperty, blogPropertyValue); 276 277 try { 278 _fetcher.saveBlog(blog); 279 280 addOperationResultMessage(context, getAdminResource(UPDATED_BLOG_PROPERTIES_KEY, UPDATED_BLOG_PROPERTIES_KEY, blog.getBlogAdministrationLocale())); 281 } catch (FetcherException e) { 282 if (_logger.isErrorEnabled()) { 283 _logger.error(e); 284 } 285 286 addOperationResultMessage(context, getAdminResource(FAILED_SAVE_BLOG_PROPERTIES_KEY, FAILED_SAVE_BLOG_PROPERTIES_KEY, blog.getBlogAdministrationLocale())); 287 } 288 } 289 290 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_PROPERTIES_PAGE); 292 } else if (CHECK_BLOG_PROPERTY_ACTION.equals(action)) { 293 _logger.debug("User requested check blog property action"); 294 295 if (!checkPermission(blog, null, username, CHECK_ARBITRARY_PROPERTIES_PERMISSION)) { 296 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, ADMIN_ADMINISTRATION_PAGE); 297 addOperationResultMessage(context, getAdminResource(FAILED_EDIT_PROPERTIES_PERMISSION_KEY, FAILED_EDIT_PROPERTIES_PERMISSION_KEY, blog.getBlogAdministrationLocale())); 298 299 return entries; 300 } 301 302 String blogProperty = BlojsomUtils.getRequestValue(INDIVIDUAL_BLOG_PROPERTY, httpServletRequest); 303 304 if (!BlojsomUtils.checkNullOrBlank(blogProperty)) { 305 if (blog.getProperty(blogProperty) != null) { 306 addOperationResultMessage(context, formatAdminResource(BLOG_PROPERTY_HAS_VALUE_KEY, BLOG_PROPERTY_HAS_VALUE_KEY, blog.getBlogAdministrationLocale(), new Object [] {blogProperty, blog.getProperty(blogProperty)})); 307 } else { 308 addOperationResultMessage(context, formatAdminResource(BLOG_PROPERTY_NOT_FOUND_KEY, BLOG_PROPERTY_NOT_FOUND_KEY, blog.getBlogAdministrationLocale(), new Object [] {blogProperty})); 309 } 310 } 311 312 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_PROPERTIES_PAGE); 314 } 315 316 String installedLocales = _blojsomProperties.getProperty(BlojsomConstants.INSTALLED_LOCALES_IP); 317 if (installedLocales != null) { 318 context.put(BLOJSOM_INSTALLED_LOCALES, BlojsomUtils.parseCommaList(installedLocales)); 319 } 320 context.put(BLOJSOM_JVM_LANGUAGES, BlojsomUtils.getLanguagesForSystem(blog.getBlogAdministrationLocale())); 321 context.put(BLOJSOM_JVM_COUNTRIES, BlojsomUtils.getCountriesForSystem(blog.getBlogAdministrationLocale())); 322 context.put(BLOJSOM_JVM_TIMEZONES, BlojsomUtils.getTimeZonesForSystem(blog.getBlogAdministrationLocale())); 323 324 return entries; 325 } 326 } 327 | Popular Tags |