1 31 package org.blojsom.plugin.moblog.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.admin.WebAdminPlugin; 41 import org.blojsom.plugin.moblog.MoblogPlugin; 42 import org.blojsom.plugin.moblog.MoblogPluginUtils; 43 import org.blojsom.util.BlojsomConstants; 44 import org.blojsom.util.BlojsomUtils; 45 46 import javax.servlet.http.HttpServletRequest ; 47 import javax.servlet.http.HttpServletResponse ; 48 import java.util.Map ; 49 50 57 public class MoblogAdminPlugin extends WebAdminPlugin { 58 59 private Log _logger = LogFactory.getLog(MoblogAdminPlugin.class); 60 61 private static final String EDIT_MOBLOG_SETTINGS_PAGE = "/org/blojsom/plugin/moblog/admin/templates/admin-edit-moblog-settings"; 62 63 private static final String FAILED_MOBLOG_PERMISSIONS_KEY = "failed.moblog.permissions.text"; 65 private static final String UPDATED_MOBLOG_CONFIGURATION_KEY = "updated.moblog.configuration.text"; 66 private static final String FAILED_WRITE_MOBLOG_CONFIGURATION_KEY = "failed.write.moblog.configuration.text"; 67 private static final String ADDED_AUTHORIZED_EMAIL_KEY = "added.authorized.email.text"; 68 private static final String FAILED_ADD_EMAIL_KEY = "failed.add.email.text"; 69 private static final String NO_EMAIL_ADDRESS_KEY = "no.email.address.text"; 70 private static final String REMOVED_AUTHORIZED_EMAIL_KEY = "removed.authorized.email.text"; 71 private static final String FAILED_DELETE_EMAIL_KEY = "failed.delete.email.text"; 72 73 private static final String MOBLOG_ENABLED = "moblog-enabled"; 75 private static final String MOBLOG_HOSTNAME = "moblog-hostname"; 76 private static final String MOBLOG_USERID = "moblog-userid"; 77 private static final String MOBLOG_PASSWORD = "moblog-password"; 78 private static final String MOBLOG_CATEGORY = "moblog-category"; 79 private static final String MOBLOG_SECRET_WORD = "moblog-secret-word"; 80 private static final String MOBLOG_IMAGE_MIME_TYPES = "moblog-image-mime-types"; 81 private static final String MOBLOG_ATTACHMENT_MIME_TYPES = "moblog-attachment-mime-types"; 82 private static final String MOBLOG_TEXT_MIME_TYPES = "moblog-text-mime-types"; 83 private static final String MOBLOG_AUTHORIZED_ADDRESS = "moblog-authorized-address"; 84 private static final String MOBLOG_IGNORE_EXPRESSION = "moblog-ignore-expression"; 85 86 private static final String UPDATE_MOBLOG_SETTINGS_ACTIONS = "update-moblog-settings"; 88 private static final String ADD_AUTHORIZED_ADDRESS_ACTION = "add-authorized-address"; 89 private static final String DELETE_AUTHORIZED_ADDRESS_ACTION = "delete-authorized-address"; 90 91 private static final String MOBLOG_ADMIN_PERMISSION = "moblog_admin"; 92 93 private static final String BLOJSOM_PLUGIN_MOBLOG_MAILBOX = "BLOJSOM_PLUGIN_MOBLOG_MAILBOX"; 94 95 private Fetcher _fetcher; 96 97 100 public MoblogAdminPlugin() { 101 } 102 103 108 public void setFetcher(Fetcher fetcher) { 109 _fetcher = fetcher; 110 } 111 112 117 public String getDisplayName() { 118 return "Moblog plugin"; 119 } 120 121 126 public String getInitialPage() { 127 return EDIT_MOBLOG_SETTINGS_PAGE; 128 } 129 130 141 public Entry[] process(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Blog blog, Map context, Entry[] entries) throws PluginException { 142 entries = super.process(httpServletRequest, httpServletResponse, blog, context, entries); 143 144 String page = BlojsomUtils.getRequestValue(BlojsomConstants.PAGE_PARAM, httpServletRequest); 145 146 String username = getUsernameFromSession(httpServletRequest, blog); 147 if (!checkPermission(blog, null, username, MOBLOG_ADMIN_PERMISSION)) { 148 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, ADMIN_ADMINISTRATION_PAGE); 149 addOperationResultMessage(context, getAdminResource(FAILED_MOBLOG_PERMISSIONS_KEY, FAILED_MOBLOG_PERMISSIONS_KEY, blog.getBlogAdministrationLocale())); 150 151 return entries; 152 } 153 154 if (ADMIN_LOGIN_PAGE.equals(page)) { 155 return entries; 156 } else { 157 String action = BlojsomUtils.getRequestValue(ACTION_PARAM, httpServletRequest); 158 159 if (UPDATE_MOBLOG_SETTINGS_ACTIONS.equals(action)) { 160 boolean mailboxEnabled = Boolean.valueOf(BlojsomUtils.getRequestValue(MOBLOG_ENABLED, httpServletRequest)).booleanValue(); 161 blog.setProperty(MoblogPlugin.PROPERTY_ENABLED, Boolean.toString(mailboxEnabled)); 162 163 String hostname = BlojsomUtils.getRequestValue(MOBLOG_HOSTNAME, httpServletRequest); 164 blog.setProperty(MoblogPlugin.PROPERTY_HOSTNAME, hostname); 165 166 String userID = BlojsomUtils.getRequestValue(MOBLOG_USERID, httpServletRequest); 167 blog.setProperty(MoblogPlugin.PROPERTY_USERID, userID); 168 169 String password = BlojsomUtils.getRequestValue(MOBLOG_PASSWORD, httpServletRequest); 170 blog.setProperty(MoblogPlugin.PROPERTY_PASSWORD, password); 171 172 String category = BlojsomUtils.getRequestValue(MOBLOG_CATEGORY, httpServletRequest); 173 blog.setProperty(MoblogPlugin.PROPERTY_CATEGORY, category); 174 175 String textMimeTypeValue = BlojsomUtils.getRequestValue(MOBLOG_TEXT_MIME_TYPES, httpServletRequest); 176 blog.setProperty(MoblogPlugin.PLUGIN_MOBLOG_TEXT_MIME_TYPES, textMimeTypeValue); 177 178 String attachmentMimeTypeValue = BlojsomUtils.getRequestValue(MOBLOG_ATTACHMENT_MIME_TYPES, httpServletRequest); 179 blog.setProperty(MoblogPlugin.PLUGIN_MOBLOG_ATTACHMENT_MIME_TYPES, attachmentMimeTypeValue); 180 181 String imageMimeTypeValue = BlojsomUtils.getRequestValue(MOBLOG_IMAGE_MIME_TYPES, httpServletRequest); 182 blog.setProperty(MoblogPlugin.PLUGIN_MOBLOG_IMAGE_MIME_TYPES, imageMimeTypeValue); 183 184 String secretWord = BlojsomUtils.getRequestValue(MOBLOG_SECRET_WORD, httpServletRequest); 185 blog.setProperty(MoblogPlugin.PLUGIN_MOBLOG_SECRET_WORD, secretWord); 186 187 String ignoreExpression = BlojsomUtils.getRequestValue(MOBLOG_IGNORE_EXPRESSION, httpServletRequest); 188 blog.setProperty(MoblogPlugin.PLUGIN_MOBLOG_IGNORE_EXPRESSION, ignoreExpression); 189 190 try { 191 _fetcher.saveBlog(blog); 192 addOperationResultMessage(context, getAdminResource(UPDATED_MOBLOG_CONFIGURATION_KEY, UPDATED_MOBLOG_CONFIGURATION_KEY, blog.getBlogAdministrationLocale())); 193 } catch (FetcherException e) { 194 _logger.error(e); 195 addOperationResultMessage(context, getAdminResource(FAILED_WRITE_MOBLOG_CONFIGURATION_KEY, FAILED_WRITE_MOBLOG_CONFIGURATION_KEY, blog.getBlogAdministrationLocale())); 196 } 197 } else if (ADD_AUTHORIZED_ADDRESS_ACTION.equals(action)) { 198 String addressToAdd = BlojsomUtils.getRequestValue(MOBLOG_AUTHORIZED_ADDRESS, httpServletRequest); 199 if (!BlojsomUtils.checkNullOrBlank(addressToAdd)) { 200 String authorizedAddresses = blog.getProperty(MoblogPlugin.PLUGIN_MOBLOG_AUTHORIZED_ADDRESSES); 201 String updatedAddresses; 202 Map addresses; 203 204 if (!BlojsomUtils.checkNullOrBlank(authorizedAddresses)) { 205 addresses = BlojsomUtils.arrayOfStringsToMap(BlojsomUtils.parseCommaList(authorizedAddresses)); 206 addresses.put(addressToAdd, addressToAdd); 207 updatedAddresses = BlojsomUtils.getKeysAsStringList(addresses); 208 } else { 209 updatedAddresses = addressToAdd; 210 } 211 212 blog.setProperty(MoblogPlugin.PLUGIN_MOBLOG_AUTHORIZED_ADDRESSES, updatedAddresses); 213 214 try { 215 _fetcher.saveBlog(blog); 216 addOperationResultMessage(context, formatAdminResource(ADDED_AUTHORIZED_EMAIL_KEY, ADDED_AUTHORIZED_EMAIL_KEY, blog.getBlogAdministrationLocale(), new Object [] {addressToAdd})); 217 } catch (FetcherException e) { 218 _logger.error(e); 219 addOperationResultMessage(context, getAdminResource(FAILED_ADD_EMAIL_KEY, FAILED_ADD_EMAIL_KEY, blog.getBlogAdministrationLocale())); 220 } 221 } else { 222 addOperationResultMessage(context, getAdminResource(NO_EMAIL_ADDRESS_KEY, NO_EMAIL_ADDRESS_KEY, blog.getBlogAdministrationLocale())); 223 } 224 } else if (DELETE_AUTHORIZED_ADDRESS_ACTION.equals(action)) { 225 String addressToDelete = BlojsomUtils.getRequestValue(MOBLOG_AUTHORIZED_ADDRESS, httpServletRequest); 226 if (!BlojsomUtils.checkNullOrBlank(addressToDelete)) { 227 String authorizedAddresses = blog.getProperty(MoblogPlugin.PLUGIN_MOBLOG_AUTHORIZED_ADDRESSES); 228 String updatedAddresses = ""; 229 Map addresses; 230 231 if (!BlojsomUtils.checkNullOrBlank(authorizedAddresses)) { 232 addresses = BlojsomUtils.arrayOfStringsToMap(BlojsomUtils.parseCommaList(authorizedAddresses)); 233 addresses.remove(addressToDelete); 234 updatedAddresses = BlojsomUtils.getKeysAsStringList(addresses); 235 } 236 237 blog.setProperty(MoblogPlugin. PLUGIN_MOBLOG_AUTHORIZED_ADDRESSES, updatedAddresses); 238 239 try { 240 _fetcher.saveBlog(blog); 241 addOperationResultMessage(context, formatAdminResource(REMOVED_AUTHORIZED_EMAIL_KEY, REMOVED_AUTHORIZED_EMAIL_KEY, blog.getBlogAdministrationLocale(), new Object [] {addressToDelete})); 242 } catch (FetcherException e) { 243 _logger.error(e); 244 addOperationResultMessage(context, getAdminResource(FAILED_DELETE_EMAIL_KEY, FAILED_DELETE_EMAIL_KEY, blog.getBlogAdministrationLocale())); 245 } 246 } else { 247 addOperationResultMessage(context, getAdminResource(NO_EMAIL_ADDRESS_KEY, NO_EMAIL_ADDRESS_KEY, blog.getBlogAdministrationLocale())); 248 } 249 } 250 251 context.put(BLOJSOM_PLUGIN_MOBLOG_MAILBOX, MoblogPluginUtils.readMailboxSettingsForBlog(_servletConfig, blog)); 252 } 253 254 return entries; 255 } 256 } | Popular Tags |