1 2 package org.roller.presentation.website.actions; 3 4 import java.io.IOException ; 5 import java.util.List ; 6 7 import javax.servlet.ServletException ; 8 import javax.servlet.http.HttpServletRequest ; 9 import javax.servlet.http.HttpServletResponse ; 10 11 import org.apache.commons.logging.Log; 12 import org.apache.commons.logging.LogFactory; 13 import org.apache.struts.action.ActionError; 14 import org.apache.struts.action.ActionErrors; 15 import org.apache.struts.action.ActionForm; 16 import org.apache.struts.action.ActionForward; 17 import org.apache.struts.action.ActionMapping; 18 import org.apache.struts.action.ActionMessage; 19 import org.apache.struts.action.ActionMessages; 20 import org.apache.struts.actions.DispatchAction; 21 import org.roller.RollerException; 22 import org.roller.RollerPermissionsException; 23 import org.roller.model.UserManager; 24 import org.roller.pojos.PageData; 25 import org.roller.pojos.UserData; 26 import org.roller.pojos.WebsiteData; 27 import org.roller.presentation.RollerRequest; 28 import org.roller.presentation.forms.PageForm; 29 import org.roller.presentation.pagecache.PageCacheFilter; 30 import org.roller.util.StringUtils; 31 import org.roller.util.Utilities; 32 33 34 44 public final class PageFormAction extends DispatchAction 45 { 46 private static Log mLogger = 47 LogFactory.getFactory().getInstance(PageFormAction.class); 48 49 public ActionForward add( 50 ActionMapping mapping, 51 ActionForm actionForm, 52 HttpServletRequest request, 53 HttpServletResponse response) 54 throws IOException , ServletException 55 { 56 ActionForward forward = mapping.findForward("editPages.page"); 57 try 58 { 59 RollerRequest rreq = RollerRequest.getRollerRequest(request); 60 if ( rreq.isUserAuthorizedToEdit() ) 61 { 62 PageForm form = (PageForm)actionForm; 63 PageData data = new PageData(); 64 form.copyTo(data, request.getLocale()); 65 WebsiteData hd = rreq.getWebsite(); 66 67 data.setWebsite( hd ); 68 data.setUpdateTime( new java.util.Date () ); 69 data.setDescription(""); 70 data.setTemplate(""); 71 validateLink( data ); 72 73 UserManager mgr = rreq.getRoller().getUserManager(); 74 mgr.storePage( data ); 75 rreq.getRoller().commit(); 76 77 ActionMessages uiMessages = new ActionMessages(); 78 uiMessages.add(ActionMessages.GLOBAL_MESSAGE, 79 new ActionMessage("pagesForm.addNewPage.success", 80 data.getName())); 81 saveMessages(request, uiMessages); 82 83 UserData user = rreq.getUser(); 84 PageCacheFilter.removeFromCache( request, user ); 85 86 actionForm.reset(mapping,request); 87 88 addModelObjects(rreq); 89 } 90 else 91 { 92 forward = mapping.findForward("access-denied"); 93 } 94 } 95 catch (Exception e) 96 { 97 mLogger.error("ERROR in action",e); 98 throw new ServletException (e); 99 } 100 return forward; 101 } 102 103 public ActionForward edit( 105 ActionMapping mapping, 106 ActionForm actionForm, 107 HttpServletRequest request, 108 HttpServletResponse response) 109 throws IOException , ServletException 110 { 111 ActionForward forward = mapping.findForward("editPage.page"); 112 try 113 { 114 RollerRequest rreq = RollerRequest.getRollerRequest(request); 115 if ( rreq.isUserAuthorizedToEdit() ) 116 { 117 UserData ud = rreq.getUser(); 118 PageData pd = rreq.getPage(); 119 PageForm pf = (PageForm)actionForm; 120 pf.copyFrom(pd, request.getLocale()); 121 122 PageCacheFilter.removeFromCache( request,ud ); 123 124 addModelObjects(rreq); 125 } 126 else 127 { 128 forward = mapping.findForward("access-denied"); 129 } 130 } 131 catch (Exception e) 132 { 133 mLogger.error("ERROR in action",e); 134 throw new ServletException (e); 135 } 136 return forward; 137 } 138 139 public ActionForward editPages( 141 ActionMapping mapping, 142 ActionForm actionForm, 143 HttpServletRequest request, 144 HttpServletResponse response) 145 throws IOException , ServletException 146 { 147 ActionForward forward = mapping.findForward("editPages.page"); 148 try 149 { 150 RollerRequest rreq = RollerRequest.getRollerRequest(request); 151 if ( rreq.isUserAuthorizedToEdit() ) 152 { 153 addModelObjects(rreq); 154 } 155 else 156 { 157 forward = mapping.findForward("access-denied"); 158 } 159 } 160 catch (Exception e) 161 { 162 mLogger.error("ERROR in action",e); 163 throw new ServletException (e); 164 } 165 return forward; 166 } 167 168 public ActionForward remove( 170 ActionMapping mapping, 171 ActionForm actionForm, 172 HttpServletRequest request, 173 HttpServletResponse response) 174 throws IOException , ServletException 175 { 176 ActionForward forward = mapping.findForward("editPages"); 177 try 178 { 179 RollerRequest rreq = RollerRequest.getRollerRequest(request); 180 if ( rreq.isUserAuthorizedToEdit() ) 181 { 182 PageForm form = (PageForm)actionForm; 183 PageData data = new PageData(); 184 form.copyTo(data, request.getLocale()); 185 186 UserManager mgr = rreq.getRoller().getUserManager(); 187 mgr.removePageSafely( data.getId() ); 188 rreq.getRoller().commit(); 189 190 UserData user = rreq.getUser(); 191 PageCacheFilter.removeFromCache( request,user ); 192 193 addModelObjects(rreq); 194 195 actionForm.reset(mapping,request); 196 } 197 else 198 { 199 forward = mapping.findForward("access-denied"); 200 } 201 } 202 catch (RollerException e) 203 { 204 ActionErrors errors = new ActionErrors(); 205 errors.add(null, new ActionError( 206 "error.internationalized",e.getRootCauseMessage())); 207 saveErrors(request, errors); 208 } 209 catch (Exception e) 210 { 211 mLogger.error("ERROR in action",e); 212 throw new ServletException (e); 213 } 214 return forward; 215 } 216 217 public ActionForward removeOk( 219 ActionMapping mapping, 220 ActionForm actionForm, 221 HttpServletRequest request, 222 HttpServletResponse response) 223 throws IOException , ServletException 224 { 225 ActionForward forward = mapping.findForward("removePage.page"); 226 try 227 { 228 RollerRequest rreq = RollerRequest.getRollerRequest(request); 229 if ( rreq.isUserAuthorizedToEdit() ) 230 { 231 PageData cd = rreq.getPage(); 232 PageForm pf = (PageForm)actionForm; 233 pf.copyFrom(cd, request.getLocale()); 234 235 UserData ud = rreq.getUser(); 236 request.setAttribute("user",ud); 237 } 238 else 239 { 240 forward = mapping.findForward("access-denied"); 241 } 242 } 243 catch (Exception e) 244 { 245 mLogger.error("ERROR in action",e); 246 throw new ServletException (e); 247 } 248 return forward; 249 } 250 251 public ActionForward update( 253 ActionMapping mapping, 254 ActionForm actionForm, 255 HttpServletRequest request, 256 HttpServletResponse response) 257 throws IOException , ServletException 258 { 259 ActionForward forward = mapping.findForward("editPage.page"); 260 try 261 { 262 RollerRequest rreq = RollerRequest.getRollerRequest(request); 263 if ( rreq.isUserAuthorizedToEdit() ) 264 { 265 PageForm form = (PageForm)actionForm; 266 UserManager mgr = rreq.getRoller().getUserManager(); 267 PageData data = mgr.retrievePage(form.getId()); 268 data.save(); form.copyTo(data, request.getLocale()); 270 data.setUpdateTime( new java.util.Date () ); 271 data.setWebsite( rreq.getWebsite() ); 272 273 validateLink( data ); 274 275 mgr.storePage( data ); 276 rreq.getRoller().commit(); 277 278 ((PageForm)actionForm).setLink( data.getLink() ); 280 281 ActionMessages uiMessages = new ActionMessages(); 282 uiMessages.add(ActionMessages.GLOBAL_MESSAGE, 283 new ActionMessage("pageForm.save.success", 284 data.getName())); 285 saveMessages(request, uiMessages); 286 287 UserData user = rreq.getUser(); 288 PageCacheFilter.removeFromCache( request,user ); 289 } 290 else 291 { 292 forward = mapping.findForward("access-denied"); 293 } 294 295 } 298 catch (RollerPermissionsException e) 299 { 300 ActionErrors errors = new ActionErrors(); 301 errors.add(null, new ActionError("error.permissions.deniedSave")); 302 saveErrors(request, errors); 303 forward = mapping.findForward("access-denied"); 304 } 305 catch (Exception e) 306 { 307 mLogger.error("ERROR in action",e); 308 throw new ServletException (e); 309 } 310 return forward; 311 } 312 313 322 private void validateLink( PageData data ) 323 { 324 if ( StringUtils.isEmpty( data.getLink() ) ) 327 { 328 data.setLink( data.getName() ); 329 } 330 331 if ( !StringUtils.isAlphanumeric( data.getLink() ) ) 336 { 337 String link = Utilities.removeHTML( data.getLink() ); 338 link = Utilities.removeNonAlphanumeric( link ); 339 data.setLink( link ); 340 } 341 } 342 343 public ActionForward cancel( 345 ActionMapping mapping, 346 ActionForm actionForm, 347 HttpServletRequest request, 348 HttpServletResponse response) 349 throws IOException , ServletException 350 { 351 return (mapping.findForward("editPages")); 352 } 353 354 private void addModelObjects( RollerRequest rreq ) 356 throws RollerException { 357 358 HttpServletRequest request = rreq.getRequest(); 359 360 UserManager mgr = rreq.getRoller().getUserManager(); 361 362 UserData user = rreq.getUser(); 363 request.setAttribute("user",user); 364 365 WebsiteData wd = rreq.getWebsite(); 366 request.setAttribute("website", wd); 367 368 List pages = mgr.getPages(wd); 369 request.setAttribute("pages",pages); 370 } 371 } 372 373 | Popular Tags |