1 18 19 package org.apache.roller.ui.authoring.struts.actions; 20 21 import java.io.IOException ; 22 import java.util.Date ; 23 import java.util.List ; 24 import java.util.ResourceBundle ; 25 26 import javax.servlet.ServletException ; 27 import javax.servlet.http.HttpServletRequest ; 28 import javax.servlet.http.HttpServletResponse ; 29 30 import org.apache.commons.lang.StringUtils; 31 32 import org.apache.commons.logging.Log; 33 import org.apache.commons.logging.LogFactory; 34 import org.apache.roller.RollerException; 35 import org.apache.roller.RollerPermissionsException; 36 import org.apache.roller.model.RollerFactory; 37 import org.apache.roller.model.UserManager; 38 import org.apache.roller.pojos.UserData; 39 import org.apache.roller.pojos.WeblogTemplate; 40 import org.apache.roller.pojos.WebsiteData; 41 import org.apache.roller.ui.authoring.struts.forms.WeblogTemplateForm; 42 import org.apache.roller.ui.core.BasePageModel; 43 import org.apache.roller.ui.core.RollerRequest; 44 import org.apache.roller.ui.core.RollerSession; 45 import org.apache.roller.util.Utilities; 46 import org.apache.roller.util.cache.CacheManager; 47 import org.apache.struts.action.ActionError; 48 import org.apache.struts.action.ActionErrors; 49 import org.apache.struts.action.ActionForm; 50 import org.apache.struts.action.ActionForward; 51 import org.apache.struts.action.ActionMapping; 52 import org.apache.struts.action.ActionMessage; 53 import org.apache.struts.action.ActionMessages; 54 import org.apache.struts.actions.DispatchAction; 55 56 66 public final class WeblogTemplateFormAction extends DispatchAction { 67 protected static ResourceBundle bundle = 68 ResourceBundle.getBundle("ApplicationResources"); 69 private static Log mLogger = 70 LogFactory.getFactory().getInstance(WeblogTemplateFormAction.class); 71 72 public ActionForward add( 73 ActionMapping mapping, 74 ActionForm actionForm, 75 HttpServletRequest request, 76 HttpServletResponse response) 77 throws IOException , ServletException { 78 ActionForward forward = mapping.findForward("editPages.page"); 79 try { 80 request.setAttribute("model", new BasePageModel( 81 "pagesForm.title", request, response, mapping)); 82 RollerRequest rreq = RollerRequest.getRollerRequest(request); 83 RollerSession rses = RollerSession.getRollerSession(request); 84 WebsiteData website = rreq.getWebsite(); 85 if ( rses.isUserAuthorizedToAdmin(website) ) { 86 WeblogTemplateForm form = (WeblogTemplateForm)actionForm; 87 WeblogTemplate data = new WeblogTemplate(); 88 form.copyTo(data, request.getLocale()); 89 data.setWebsite(website); 90 data.setLastModified( new Date () ); 91 data.setDescription(data.getName()); 92 data.setContents(bundle.getString("pageForm.newTemplateContent")); 93 validateLink( data ); 94 95 data.setTemplateLanguage("velocity"); 97 98 if(!"_decorator".equals(data.getName())) { 100 data.setDecoratorName("_decorator"); 101 } 102 103 UserManager mgr = RollerFactory.getRoller().getUserManager(); 104 mgr.savePage( data ); 105 RollerFactory.getRoller().flush(); 106 107 ActionMessages uiMessages = new ActionMessages(); 108 uiMessages.add(ActionMessages.GLOBAL_MESSAGE, 109 new ActionMessage("pagesForm.addNewPage.success", 110 data.getName())); 111 saveMessages(request, uiMessages); 112 113 actionForm.reset(mapping,request); 114 115 addModelObjects(request, response, mapping, website, data); 116 } else { 117 forward = mapping.findForward("access-denied"); 118 } 119 } catch (Exception e) { 120 mLogger.error("ERROR in action",e); 121 throw new ServletException (e); 122 } 123 return forward; 124 } 125 126 public ActionForward edit( 128 ActionMapping mapping, 129 ActionForm actionForm, 130 HttpServletRequest request, 131 HttpServletResponse response) 132 throws IOException , ServletException { 133 ActionForward forward = mapping.findForward("editPage.page"); 134 try { 135 RollerRequest rreq = RollerRequest.getRollerRequest(request); 136 WeblogTemplate pd = (WeblogTemplate)rreq.getPage(); 137 138 RollerSession rses = RollerSession.getRollerSession(request); 139 if ( rses.isUserAuthorizedToAdmin(pd.getWebsite()) ) { 140 BasePageModel pageModel = new BasePageModel( 141 "pageForm.title", request, response, mapping); 142 pageModel.setWebsite(pd.getWebsite()); 143 request.setAttribute("model", pageModel); 144 145 WeblogTemplateForm pf = (WeblogTemplateForm)actionForm; 146 pf.copyFrom(pd, request.getLocale()); 147 148 addModelObjects(request, response, mapping, pd.getWebsite(), pd); 149 } else { 150 forward = mapping.findForward("access-denied"); 151 } 152 } catch (Exception e) { 153 mLogger.error("ERROR in action",e); 154 throw new ServletException (e); 155 } 156 return forward; 157 } 158 159 public ActionForward editPages( 161 ActionMapping mapping, 162 ActionForm actionForm, 163 HttpServletRequest request, 164 HttpServletResponse response) 165 throws IOException , ServletException { 166 ActionForward forward = mapping.findForward("editPages.page"); 167 try { 168 WeblogTemplateForm form = (WeblogTemplateForm)actionForm; 169 RollerRequest rreq = RollerRequest.getRollerRequest(request); 170 RollerSession rses = RollerSession.getRollerSession(request); 171 request.setAttribute("model", new BasePageModel( 172 "pagesForm.title", request, response, mapping)); 173 174 WebsiteData website = rreq.getWebsite(); 175 if (website == null && form.getId()!=null) { 176 UserManager mgr = RollerFactory.getRoller().getUserManager(); 177 WeblogTemplate template = mgr.getPage(form.getId()); 178 website = template.getWebsite(); 179 } 180 181 if ( rses.isUserAuthorizedToAdmin(website)) { 182 addModelObjects(request, response, mapping, website, null); 183 } else { 184 forward = mapping.findForward("access-denied"); 185 } 186 } catch (Exception e) { 187 mLogger.error("ERROR in action",e); 188 throw new ServletException (e); 189 } 190 return forward; 191 } 192 193 public ActionForward remove( 195 ActionMapping mapping, 196 ActionForm actionForm, 197 HttpServletRequest request, 198 HttpServletResponse response) 199 throws IOException , ServletException { 200 ActionForward forward = mapping.findForward("editPages"); 201 request.setAttribute("model", new BasePageModel( 202 "pagesForm.title", request, response, mapping)); 203 try { 204 UserManager mgr = RollerFactory.getRoller().getUserManager(); 205 WeblogTemplateForm form = (WeblogTemplateForm)actionForm; 206 WeblogTemplate template = mgr.getPage(form.getId()); 207 WebsiteData website = template.getWebsite(); 208 209 RollerSession rses = RollerSession.getRollerSession(request); 210 if ( rses.isUserAuthorizedToAdmin(website) ) { 211 if(!template.isRequired()) { 212 213 mgr.removePage(template); 214 RollerFactory.getRoller().flush(); 215 216 CacheManager.invalidate(template); 218 } else { 219 220 throw new RollerException("Cannot remove required page"); 222 } 223 224 addModelObjects( 225 request, response, mapping, template.getWebsite(), template); 226 actionForm.reset(mapping, request); 227 } else { 228 forward = mapping.findForward("access-denied"); 229 } 230 } catch (RollerException e) { 231 ActionErrors errors = new ActionErrors(); 232 errors.add(null, new ActionError( 233 "error.internationalized",e.getRootCauseMessage())); 234 saveErrors(request, errors); 235 } catch (Exception e) { 236 mLogger.error("ERROR in action",e); 237 throw new ServletException (e); 238 } 239 return forward; 240 } 241 242 244 public ActionForward removeOk( 245 ActionMapping mapping, 246 ActionForm actionForm, 247 HttpServletRequest request, 248 HttpServletResponse response) 249 throws IOException , ServletException { 250 ActionForward forward = mapping.findForward("removePage.page"); 251 try { 252 RollerSession rses = RollerSession.getRollerSession(request); 253 RollerRequest rreq = RollerRequest.getRollerRequest(request); 254 WeblogTemplate page = (WeblogTemplate) rreq.getPage(); 255 WebsiteData website = page.getWebsite(); 256 if ( rses.isUserAuthorizedToAdmin(website) ) { 257 WeblogTemplateForm form = (WeblogTemplateForm)actionForm; 258 form.copyFrom(page, request.getLocale()); 259 260 addModelObjects(request, response, mapping, page.getWebsite(), page); 261 262 BasePageModel pageModel = new BasePageModel( 263 "editPages.title.removeOK", request, response, mapping); 264 pageModel.setWebsite(website); 265 request.setAttribute("model", pageModel); 266 267 UserData ud = rses.getAuthenticatedUser(); 268 request.setAttribute("user",ud); 269 } else { 270 forward = mapping.findForward("access-denied"); 271 } 272 } catch (Exception e) { 273 mLogger.error("ERROR in action",e); 274 throw new ServletException (e); 275 } 276 return forward; 277 } 278 279 public ActionForward update( 281 ActionMapping mapping, 282 ActionForm actionForm, 283 HttpServletRequest request, 284 HttpServletResponse response) 285 throws IOException , ServletException { 286 ActionForward forward = mapping.findForward("editPage.page"); 287 try { 288 RollerRequest rreq = RollerRequest.getRollerRequest(request); 289 WeblogTemplateForm form = (WeblogTemplateForm)actionForm; 290 UserManager mgr = RollerFactory.getRoller().getUserManager(); 291 WeblogTemplate data = mgr.getPage(form.getId()); 292 WebsiteData website = data.getWebsite(); 293 294 RollerSession rses = RollerSession.getRollerSession(request); 295 if (rses.isUserAuthorizedToAdmin(website)) { 296 form.copyTo(data, request.getLocale()); 297 data.setLastModified( new Date () ); 298 299 validateLink( data ); 300 301 mgr.savePage( data ); 302 RollerFactory.getRoller().flush(); 303 304 ((WeblogTemplateForm)actionForm).setLink( data.getLink() ); 306 307 ActionMessages uiMessages = new ActionMessages(); 308 uiMessages.add(ActionMessages.GLOBAL_MESSAGE, 309 new ActionMessage("pageForm.save.success", 310 data.getName())); 311 saveMessages(request, uiMessages); 312 313 CacheManager.invalidate(data); 314 315 addModelObjects(request, response, mapping, data.getWebsite(), data); 316 317 BasePageModel pageModel = new BasePageModel( 318 "pageForm.title", request, response, mapping); 319 pageModel.setWebsite(website); 320 request.setAttribute("model", pageModel); 321 322 } else { 323 forward = mapping.findForward("access-denied"); 324 } 325 326 } catch (RollerPermissionsException e) { 329 ActionErrors errors = new ActionErrors(); 330 errors.add(null, new ActionError("error.permissions.deniedSave")); 331 saveErrors(request, errors); 332 forward = mapping.findForward("access-denied"); 333 } catch (Exception e) { 334 mLogger.error("ERROR in action",e); 335 throw new ServletException (e); 336 } 337 return forward; 338 } 339 340 349 private void validateLink( WeblogTemplate data ) { 350 if ( StringUtils.isEmpty( data.getLink() ) ) { 353 data.setLink( data.getName() ); 354 } 355 356 if ( !StringUtils.isAlphanumeric( data.getLink() ) ) { 361 String link = Utilities.removeHTML( data.getLink() ); 362 link = Utilities.removeNonAlphanumeric( link ); 363 data.setLink( link ); 364 } 365 } 366 367 public ActionForward cancel( 369 ActionMapping mapping, 370 ActionForm actionForm, 371 HttpServletRequest request, 372 HttpServletResponse response) 373 throws IOException , ServletException { 374 request.setAttribute("model", new BasePageModel( 375 "pagesForm.title", request, response, mapping)); 376 return (mapping.findForward("editPages")); 377 } 378 379 private void addModelObjects( 381 HttpServletRequest request, 382 HttpServletResponse response, 383 ActionMapping mapping, 384 WebsiteData website, 385 WeblogTemplate page) 386 throws RollerException { 387 UserManager mgr = RollerFactory.getRoller().getUserManager(); 388 RollerSession rses = RollerSession.getRollerSession(request); 389 RollerRequest rreq = RollerRequest.getRollerRequest(request); 390 391 UserData user = rses.getAuthenticatedUser(); 392 request.setAttribute("user", user); 393 394 WebsiteData wd = rreq.getWebsite(); 395 request.setAttribute("website", website); 396 397 List pages = mgr.getPages(website); 398 request.setAttribute("pages", pages); 399 400 if (page != null) request.setAttribute("page", page); 401 } 402 } 403 404 | Popular Tags |