1 8 9 package org.roller.presentation.weblog.actions; 10 11 import org.apache.struts.actions.DispatchAction; 12 import org.apache.struts.action.ActionForward; 13 import org.apache.struts.action.ActionMapping; 14 import org.apache.struts.action.ActionForm; 15 import org.apache.struts.action.ActionMessages; 16 import org.apache.struts.action.ActionMessage; 17 import org.apache.commons.logging.Log; 18 import org.roller.presentation.RollerRequest; 19 import org.roller.presentation.forms.PingTargetForm; 20 import org.roller.model.PingTargetManager; 21 import org.roller.pojos.PingTargetData; 22 import org.roller.RollerException; 23 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpServletResponse ; 26 import javax.servlet.ServletException ; 27 import java.util.List ; 28 29 34 public abstract class BasePingTargetsAction extends DispatchAction 35 { 36 protected static final String VIEW_PAGE = "pingTargets.page"; 38 protected static final String PING_TARGET_EDIT_PAGE = "pingTargetEdit.page"; 39 protected static final String PING_TARGET_DELETE_PAGE = "pingTargetDeleteOK.page"; 40 protected static final String ACCESS_DENIED_PAGE = "access-denied"; 41 42 public BasePingTargetsAction() { 43 44 } 45 46 56 protected ActionForward unspecified(ActionMapping mapping, 57 ActionForm actionForm, 58 HttpServletRequest request, 59 HttpServletResponse response) 60 throws Exception 61 { 62 return view(mapping, actionForm, request, response); 63 } 64 65 75 public ActionForward view(ActionMapping mapping, ActionForm form, 76 HttpServletRequest req, HttpServletResponse res) 77 throws Exception 78 { 79 ActionForward forward = mapping.findForward(VIEW_PAGE); 80 RollerRequest rreq = RollerRequest.getRollerRequest(req); 81 try 82 { 83 if (!hasRequiredRights(rreq)) 84 { 85 return mapping.findForward(ACCESS_DENIED_PAGE); 86 } 87 88 List pingTargets = getPingTargets(rreq); 89 req.setAttribute("pingTargets", pingTargets); 90 return forward; 91 } 92 catch (Exception e) 93 { 94 getLogger().error("ERROR in action", e); 95 throw new ServletException (e); 96 } 97 } 98 99 109 public ActionForward save(ActionMapping mapping, ActionForm form, 110 HttpServletRequest req, HttpServletResponse res) 111 throws Exception 112 { 113 RollerRequest rreq = RollerRequest.getRollerRequest(req); 114 PingTargetManager pingTargetMgr = rreq.getRoller().getPingTargetManager(); 115 PingTargetForm pingTargetForm = (PingTargetForm) form; 116 try 117 { 118 if (!hasRequiredRights(rreq)) 119 { 120 return mapping.findForward(ACCESS_DENIED_PAGE); 121 } 122 123 PingTargetData pingTarget = null; 124 String pingTargetId = pingTargetForm.getId(); 125 if (pingTargetId != null && pingTargetId.length() > 0) 126 { 127 pingTarget = pingTargetMgr.retrievePingTarget(pingTargetForm.getId()); 128 if (pingTarget == null) throw new RollerException("No such ping target id: " + pingTargetId); 129 pingTargetForm.copyTo(pingTarget, req.getLocale()); 130 } 131 else 132 { 133 pingTarget = createPingTarget(rreq, pingTargetForm); 134 } 135 136 ActionMessages errors = validate(rreq, pingTarget); 139 if (!errors.isEmpty()) { 140 saveErrors(rreq.getRequest(), errors); 141 return mapping.findForward(PING_TARGET_EDIT_PAGE); 142 } 143 144 pingTarget.save(); 146 rreq.getRoller().commit(); 147 return view(mapping, form, req, res); 148 } 149 catch (Exception e) 150 { 151 getLogger().error("ERROR in action", e); 152 throw new ServletException (e); 153 } 154 } 155 156 166 public ActionForward addNew(ActionMapping mapping, ActionForm form, 167 HttpServletRequest req, HttpServletResponse res) 168 throws Exception 169 { 170 return mapping.findForward(PING_TARGET_EDIT_PAGE); 171 } 172 173 183 public ActionForward editSelected(ActionMapping mapping, ActionForm form, 184 HttpServletRequest req, HttpServletResponse res) 185 throws Exception 186 { 187 ActionForward forward = mapping.findForward(PING_TARGET_EDIT_PAGE); 188 RollerRequest rreq = RollerRequest.getRollerRequest(req); 189 try 190 { 191 if (!hasRequiredRights(rreq)) 192 { 193 return mapping.findForward(ACCESS_DENIED_PAGE); 194 } 195 PingTargetData pingTarget = select(rreq); 196 ((PingTargetForm) form).copyFrom(pingTarget, req.getLocale()); 197 return forward; 198 } 199 catch (Exception e) 200 { 201 getLogger().error("ERROR in action", e); 202 throw new ServletException (e); 203 } 204 } 205 206 216 public ActionForward deleteSelected(ActionMapping mapping, ActionForm form, 217 HttpServletRequest req, HttpServletResponse res) 218 throws Exception 219 { 220 ActionForward forward = mapping.findForward(PING_TARGET_DELETE_PAGE); 221 RollerRequest rreq = RollerRequest.getRollerRequest(req); 222 try 223 { 224 if (!hasRequiredRights(rreq)) 225 { 226 return mapping.findForward(ACCESS_DENIED_PAGE); 227 } 228 PingTargetData pingTarget = select(rreq); 229 ((PingTargetForm) form).copyFrom(pingTarget, req.getLocale()); 230 return forward; 231 } 232 catch (Exception e) 233 { 234 getLogger().error("ERROR in action", e); 235 throw new ServletException (e); 236 } 237 } 238 239 249 public ActionForward deleteConfirmed(ActionMapping mapping, ActionForm form, 250 HttpServletRequest req, HttpServletResponse res) 251 throws Exception 252 { 253 RollerRequest rreq = RollerRequest.getRollerRequest(req); 254 PingTargetForm pingTargetForm = (PingTargetForm) form; 255 PingTargetManager pingTargetMgr = rreq.getRoller().getPingTargetManager(); 256 try 257 { 258 if (!hasRequiredRights(rreq)) 259 { 260 return mapping.findForward(ACCESS_DENIED_PAGE); 261 } 262 String pingTargetId = pingTargetForm.getId(); 263 if (pingTargetId == null || pingTargetId.length() == 0) 264 { 265 throw new RollerException("Missing ping target id."); 266 } 267 pingTargetMgr.removePingTarget(pingTargetId); 268 rreq.getRoller().commit(); 269 return view(mapping, form, req, res); 270 } 271 catch (Exception e) 272 { 273 getLogger().error("ERROR in action", e); 274 throw new ServletException (e); 275 } 276 } 277 278 285 private PingTargetData select(RollerRequest rreq) throws RollerException 286 { 287 String pingTargetId = rreq.getRequest().getParameter(RollerRequest.PINGTARGETID_KEY); 288 PingTargetManager pingTargetMgr = rreq.getRoller().getPingTargetManager(); 289 if (pingTargetId == null || pingTargetId.length() == 0) 290 { 291 throw new RollerException("Missing ping target id: " + pingTargetId); 292 } 293 294 PingTargetData pingTarget = pingTargetMgr.retrievePingTarget(pingTargetId); 295 if (pingTarget == null) 296 { 297 throw new RollerException("No such ping target id: " + pingTargetId); 298 } 299 return pingTarget; 300 } 301 302 310 private ActionMessages validate(RollerRequest rreq, PingTargetData pingTarget) throws RollerException 311 { 312 ActionMessages errors = new ActionMessages(); 313 314 PingTargetManager pingTargetMgr = rreq.getRoller().getPingTargetManager(); 315 if (!pingTargetMgr.isNameUnique(pingTarget)) 316 { 317 errors.add(ActionMessages.GLOBAL_MESSAGE, 318 new ActionMessage("pingTarget.nameNotUnique")); 319 } 320 if (!pingTargetMgr.isUrlWellFormed(pingTarget)) 321 { 322 errors.add(ActionMessages.GLOBAL_MESSAGE, 323 new ActionMessage("pingTarget.malformedUrl")); 324 } else if (!pingTargetMgr.isHostnameKnown(pingTarget)) 325 { 326 errors.add(ActionMessages.GLOBAL_MESSAGE, 327 new ActionMessage("pingTarget.unknownHost")); 328 } 329 return errors; 330 } 331 332 333 338 protected abstract boolean hasRequiredRights(RollerRequest rreq) throws RollerException; 339 340 343 protected abstract Log getLogger(); 344 345 348 protected abstract List getPingTargets(RollerRequest rreq) throws RollerException; 349 350 351 354 protected abstract PingTargetData createPingTarget(RollerRequest rreq, PingTargetForm pingTargetForm) throws RollerException; 355 } 356 | Popular Tags |