1 18 19 package org.apache.roller.ui.authoring.struts.actions; 20 21 import java.io.IOException ; 22 import java.util.ArrayList ; 23 import java.util.Arrays ; 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Map ; 28 29 import javax.servlet.ServletContext ; 30 import javax.servlet.ServletException ; 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.http.HttpServletResponse ; 33 34 import org.apache.commons.lang.StringUtils; 35 import org.apache.commons.logging.Log; 36 import org.apache.commons.logging.LogFactory; 37 import org.apache.struts.action.ActionError; 38 import org.apache.struts.action.ActionErrors; 39 import org.apache.struts.action.ActionForm; 40 import org.apache.struts.action.ActionForward; 41 import org.apache.struts.action.ActionMapping; 42 import org.apache.struts.action.ActionMessage; 43 import org.apache.struts.action.ActionMessages; 44 import org.apache.struts.actions.DispatchAction; 45 import org.apache.roller.RollerException; 46 import org.apache.roller.RollerPermissionsException; 47 import org.apache.roller.config.RollerConfig; 48 import org.apache.roller.config.RollerRuntimeConfig; 49 import org.apache.roller.model.PluginManager; 50 import org.apache.roller.model.Roller; 51 import org.apache.roller.model.RollerFactory; 52 import org.apache.roller.model.UserManager; 53 import org.apache.roller.model.WeblogManager; 54 import org.apache.roller.pojos.UserData; 55 import org.apache.roller.pojos.WebsiteData; 56 import org.apache.roller.ui.core.BasePageModel; 57 import org.apache.roller.ui.core.RollerRequest; 58 import org.apache.roller.ui.core.RollerSession; 59 import org.apache.roller.util.cache.CacheManager; 60 import org.apache.roller.ui.authoring.struts.formbeans.WebsiteFormEx; 61 import org.apache.roller.ui.core.RequestConstants; 62 import org.apache.roller.util.Blacklist; 63 64 65 75 public final class WebsiteFormAction extends DispatchAction { 76 private static Log mLogger = 77 LogFactory.getFactory().getInstance(WebsiteFormAction.class); 78 79 public ActionForward add( 80 ActionMapping mapping, 81 ActionForm actionForm, 82 HttpServletRequest request, 83 HttpServletResponse response) 84 throws Exception { 85 return update( mapping, actionForm, request, response ); 86 } 87 88 public ActionForward edit( 90 ActionMapping mapping, 91 ActionForm actionForm, 92 HttpServletRequest request, 93 HttpServletResponse response) 94 throws IOException , ServletException { 95 ActionForward forward = mapping.findForward("editWebsite.page"); 96 try { 97 RollerRequest rreq = RollerRequest.getRollerRequest(request); 98 WebsiteData website = rreq.getWebsite(); 99 RollerSession rses = RollerSession.getRollerSession(request); 100 if (rses.isUserAuthorizedToAdmin(website)) { 101 Roller roller = RollerFactory.getRoller(); 102 UserManager umgr = roller.getUserManager(); 103 WeblogManager wmgr = roller.getWeblogManager(); 104 UserData ud = rses.getAuthenticatedUser(); 105 request.setAttribute("user",ud); 106 107 WebsiteFormEx wf = (WebsiteFormEx)actionForm; 108 wf.copyFrom(website, request.getLocale()); 109 110 List cd = wmgr.getWeblogCategories(website, true); 111 request.setAttribute("categories",cd); 112 113 List bcd = wmgr.getWeblogCategories(website, false); 114 request.setAttribute("bloggerCategories",bcd); 115 116 List pages = umgr.getPages(website); 117 request.setAttribute("pages",pages); 118 119 ServletContext ctx = request.getSession().getServletContext(); 120 String editorPages = 121 RollerRuntimeConfig.getProperty("users.editor.pages"); 122 123 List epages = Arrays.asList(StringUtils.split( 124 StringUtils.deleteWhitespace(editorPages), ",")); 125 request.setAttribute("editorPagesList", epages); 126 127 128 WebsitePageModel pageModel = new WebsitePageModel( 129 "websiteSettings.title", request, response, mapping, website); 130 request.setAttribute("model",pageModel); 131 } else { 132 forward = mapping.findForward("access-denied"); 133 } 134 } catch (Exception e) { 135 throw new ServletException (e); 136 } 137 return forward; 138 } 139 140 141 public ActionForward update( 143 ActionMapping mapping, 144 ActionForm actionForm, 145 HttpServletRequest request, 146 HttpServletResponse response) 147 throws Exception { 148 ActionErrors errors = new ActionErrors(); 149 ActionMessages messages = new ActionMessages(); 150 ActionForward forward = mapping.findForward("editWebsite"); 151 try { 152 WebsiteFormEx form = (WebsiteFormEx)actionForm; 153 WeblogManager wmgr = RollerFactory.getRoller().getWeblogManager(); 154 UserManager umgr = RollerFactory.getRoller().getUserManager(); 155 156 WebsiteData wd = umgr.getWebsite(form.getId()); 157 158 RollerRequest.getRollerRequest(request).setWebsite(wd); 160 161 RollerSession rollerSession = RollerSession.getRollerSession(request); 162 if ( rollerSession.isUserAuthorizedToAdmin(wd)) { 163 checkBlacklist(errors, messages, form.getBlacklist()); 164 if (errors.isEmpty()) { 165 form.setEnabled(wd.getEnabled()); 167 form.copyTo(wd, request.getLocale()); 168 169 if (wd.getActive() != null && !wd.getActive().booleanValue()) { 171 wd.setAllowComments(Boolean.FALSE); 172 messages.add(null, new ActionMessage( 173 "websiteSettings.commentsOffForInactiveWeblog")); 174 } 175 176 umgr.saveWebsite(wd); 177 178 if (form.getApplyCommentDefaults() != null && form.getApplyCommentDefaults().booleanValue()) { 180 wmgr.applyCommentDefaultsToEntries(wd); 181 } 182 183 RollerFactory.getRoller().getRefererManager().applyRefererFilters(wd); 184 185 RollerFactory.getRoller().flush(); 186 187 messages.add(null, 188 new ActionMessage("websiteSettings.savedChanges")); 189 190 request.getSession().setAttribute( 191 RequestConstants.WEBLOG_ID, form.getId()); 192 193 CacheManager.invalidate(wd); 195 196 actionForm.reset(mapping,request); 197 } 198 199 ServletContext ctx = request.getSession().getServletContext(); 201 String editorPages = 202 RollerRuntimeConfig.getProperty("users.editor.pages"); 203 204 List epages = Arrays.asList(StringUtils.split( 205 org.apache.commons.lang.StringUtils.deleteWhitespace(editorPages), ",")); 206 request.setAttribute("editorPagesList", epages); 207 208 WebsitePageModel pageModel = 209 new WebsitePageModel("websiteSettings.title", 210 request, response, mapping, wd); 211 request.setAttribute("model",pageModel); 212 213 } else { 214 forward = mapping.findForward("access-denied"); 215 } 216 217 } catch (RollerPermissionsException e) { 218 errors.add(null, new ActionError("error.permissions.deniedSave")); 219 forward = mapping.findForward("access-denied"); 220 } catch (RollerException re) { 221 mLogger.error("Unexpected exception",re.getRootCause()); 222 throw new ServletException (re); 223 } catch (Exception e) { 224 mLogger.error("Unexpected exception",e); 225 throw new ServletException (e); 226 } 227 if (errors.size() > 0) saveErrors(request, errors); 228 if (messages.size() > 0) saveMessages(request, messages); 229 return forward; 230 } 231 232 233 private void checkBlacklist( 234 ActionErrors errors, ActionMessages messages, String blacklist) { 235 List regexRules = new ArrayList (); 236 List stringRules = new ArrayList (); 237 try { 238 Blacklist.populateSpamRules(blacklist, stringRules, regexRules, null); 240 messages.add(null, new ActionMessage( 241 "websiteSettings.acceptedBlacklist", 242 new Integer (stringRules.size()), new Integer (regexRules.size()))); 243 } catch (Throwable e) { 244 errors.add(null, new ActionMessage( 245 "websiteSettings.error.processingBlacklist", e.getMessage())); 246 } 247 } 248 249 251 public ActionForward removeOk( 252 ActionMapping mapping, 253 ActionForm actionForm, 254 HttpServletRequest request, 255 HttpServletResponse response) 256 throws Exception { 257 WebsiteFormEx form = (WebsiteFormEx)actionForm; 258 UserManager umgr = RollerFactory.getRoller().getUserManager(); 259 WebsiteData website = umgr.getWebsite(form.getId()); 260 ActionForward forward = mapping.findForward("removeWebsite.page"); 261 request.setAttribute("model", new WebsitePageModel( 262 "websiteRemove.title", request, response, mapping, website)); 263 try { 264 RollerSession rses = RollerSession.getRollerSession(request); 265 if (rses.isUserAuthorizedToAdmin(website)) { 266 form.copyFrom(website, request.getLocale()); 267 request.setAttribute("website", website); 268 } else { 269 forward = mapping.findForward("access-denied"); 270 } 271 } catch (Exception e) { 272 mLogger.error("ERROR in action",e); 273 throw new ServletException (e); 274 } 275 return forward; 276 } 277 278 public ActionForward remove( 280 ActionMapping mapping, 281 ActionForm actionForm, 282 HttpServletRequest request, 283 HttpServletResponse response) 284 throws Exception { 285 ActionForward forward = mapping.findForward("yourWebsites"); 286 try { 287 UserManager umgr = RollerFactory.getRoller().getUserManager(); 288 WebsiteFormEx form = (WebsiteFormEx)actionForm; 289 WebsiteData website = umgr.getWebsite(form.getId()); 290 291 RollerSession rses = RollerSession.getRollerSession(request); 292 if ( rses.isUserAuthorizedToAdmin(website) ) { 293 294 umgr.removeWebsite(website); 296 RollerFactory.getRoller().flush(); 297 298 CacheManager.invalidate(website); 299 300 actionForm.reset(mapping, request); 301 } else { 302 forward = mapping.findForward("access-denied"); 303 } 304 } catch (RollerException e) { 305 ActionErrors errors = new ActionErrors(); 306 errors.add(null, new ActionError( 307 "error.internationalized",e.getRootCauseMessage())); 308 saveErrors(request, errors); 309 } catch (Exception e) { 310 mLogger.error("ERROR in action",e); 311 throw new ServletException (e); 312 } 313 return forward; 314 } 315 316 public class WebsitePageModel extends BasePageModel { 317 private List permissions = new ArrayList (); 318 private boolean groupBloggingEnabled = false; 319 private boolean emailNotificationEnabled = false; 320 private boolean moderationRequired = false; 321 public WebsitePageModel( 322 String titleKey, 323 HttpServletRequest request, 324 HttpServletResponse response, 325 ActionMapping mapping, 326 WebsiteData website) throws RollerException { 327 super(titleKey, request, response, mapping); 328 this.website = website; 329 Roller roller = RollerFactory.getRoller(); 330 RollerSession rollerSession = RollerSession.getRollerSession(request); 331 UserData user = rollerSession.getAuthenticatedUser(); 332 permissions = roller.getUserManager().getAllPermissions(website); 333 groupBloggingEnabled = 334 RollerConfig.getBooleanProperty("groupblogging.enabled"); 335 emailNotificationEnabled = 336 RollerRuntimeConfig.getBooleanProperty("users.comments.emailnotify"); 337 moderationRequired = 338 RollerRuntimeConfig.getBooleanProperty("users.moderation.required"); 339 } 340 public boolean isGroupBloggingEnabled() { 341 return groupBloggingEnabled; 342 } 343 public boolean isEmailNotificationEnabled() { 344 return emailNotificationEnabled; 345 } 346 public boolean isModerationRequired() { 347 return moderationRequired; 348 } 349 public boolean getHasPagePlugins() { 350 boolean ret = false; 351 try { 352 Roller roller = RollerFactory.getRoller(); 353 PluginManager ppmgr = roller.getPagePluginManager(); 354 ret = ppmgr.hasPagePlugins(); 355 } catch (RollerException e) { 356 mLogger.error(e); 357 } 358 return ret; 359 } 360 361 public List getPagePlugins() { 362 List list = new ArrayList (); 363 try { 364 if (getHasPagePlugins()) { 365 Roller roller = RollerFactory.getRoller(); 366 PluginManager ppmgr = roller.getPagePluginManager(); 367 Map plugins = ppmgr.getWeblogEntryPlugins( 368 getWebsite()); 369 Iterator it = plugins.values().iterator(); 370 while (it.hasNext()) list.add(it.next()); 371 } 372 } catch (Exception e) { 373 mLogger.error(e); 374 } 375 return list; 376 } 377 378 public boolean isGlobalAdminUser() throws RollerException { 379 return getRollerSession().isGlobalAdminUser(); 380 } 381 } 382 } 383 384 | Popular Tags |