1 16 17 package org.apache.roller.ui.authoring.struts.actions; 18 19 import org.apache.commons.logging.Log; 20 import org.apache.roller.RollerException; 21 import org.apache.roller.model.PingTargetManager; 22 import org.apache.roller.model.RollerFactory; 23 import org.apache.roller.pojos.PingTargetData; 24 import org.apache.roller.pojos.WebsiteData; 25 import org.apache.roller.ui.authoring.struts.forms.PingTargetForm; 26 import org.apache.roller.ui.core.BasePageModel; 27 import org.apache.roller.ui.core.RequestConstants; 28 import org.apache.roller.ui.core.RollerRequest; 29 import org.apache.struts.action.*; 30 import org.apache.struts.actions.DispatchAction; 31 32 import javax.servlet.ServletException ; 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.http.HttpServletResponse ; 35 import java.util.List ; 36 37 38 44 public abstract class BasePingTargetsAction extends DispatchAction { 45 protected static final String VIEW_PAGE = "pingTargets.page"; 47 protected static final String PING_TARGET_EDIT_PAGE = "pingTargetEdit.page"; 48 protected static final String PING_TARGET_DELETE_PAGE = "pingTargetDeleteOK.page"; 49 protected static final String ACCESS_DENIED_PAGE = "access-denied"; 50 51 public abstract String getPingTargetsTitle(); 52 53 public abstract String getPingTargetEditTitle(); 54 55 public abstract String getPingTargetDeleteOKTitle(); 56 57 public BasePingTargetsAction() { 58 59 } 60 61 72 protected ActionForward unspecified(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception { 73 return view(mapping, actionForm, request, response); 74 } 75 76 86 public ActionForward view(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res) throws Exception { 87 ActionForward forward = mapping.findForward(VIEW_PAGE); 88 RollerRequest rreq = RollerRequest.getRollerRequest(req); 89 try { 90 BasePageModel pageModel = new BasePageModel(getPingTargetsTitle(), req, res, mapping); 91 req.setAttribute("model", pageModel); 92 if (!hasRequiredRights(rreq, rreq.getWebsite())) { 93 return mapping.findForward(ACCESS_DENIED_PAGE); 94 } 95 96 List pingTargets = getPingTargets(rreq); 97 req.setAttribute("pingTargets", pingTargets); 98 return forward; 99 } catch (Exception e) { 100 getLogger().error("ERROR in action", e); 101 throw new ServletException (e); 102 } 103 } 104 105 115 public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res) throws Exception { 116 RollerRequest rreq = RollerRequest.getRollerRequest(req); 117 PingTargetManager pingTargetMgr = RollerFactory.getRoller().getPingTargetManager(); 118 PingTargetForm pingTargetForm = (PingTargetForm) form; 119 try { 120 BasePageModel pageModel = new BasePageModel(getPingTargetEditTitle(), req, res, mapping); 121 req.setAttribute("model", pageModel); 122 if (!hasRequiredRights(rreq, rreq.getWebsite())) { 123 return mapping.findForward(ACCESS_DENIED_PAGE); 124 } 125 126 PingTargetData pingTarget = null; 127 String pingTargetId = pingTargetForm.getId(); 128 if (pingTargetId != null && pingTargetId.length() > 0) { 129 pingTarget = pingTargetMgr.getPingTarget(pingTargetForm.getId()); 130 if (pingTarget == null) throw new RollerException("No such ping target id: " + pingTargetId); 131 pingTargetForm.copyTo(pingTarget, req.getLocale()); 132 } else { 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 pingTargetMgr.savePingTarget(pingTarget); 147 RollerFactory.getRoller().flush(); 148 149 ActionMessages msgs = new ActionMessages(); 150 msgs.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("pingTarget.saved")); 151 saveMessages(req, msgs); 152 153 return view(mapping, form, req, res); 154 } catch (Exception e) { 155 getLogger().error("ERROR in action", e); 156 throw new ServletException (e); 157 } 158 } 159 160 170 public ActionForward addNew(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res) throws Exception { 171 BasePageModel pageModel = new BasePageModel(getPingTargetEditTitle(), req, res, mapping); 172 req.setAttribute("model", pageModel); 173 return mapping.findForward(PING_TARGET_EDIT_PAGE); 174 } 175 176 186 public ActionForward editSelected(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res) throws Exception { 187 ActionForward forward = mapping.findForward(PING_TARGET_EDIT_PAGE); 188 RollerRequest rreq = RollerRequest.getRollerRequest(req); 189 try { 190 BasePageModel pageModel = new BasePageModel(getPingTargetEditTitle(), req, res, mapping); 191 req.setAttribute("model", pageModel); 192 if (!hasRequiredRights(rreq, rreq.getWebsite())) { 193 return mapping.findForward(ACCESS_DENIED_PAGE); 194 } 195 PingTargetData pingTarget = select(rreq); 196 ((PingTargetForm) form).copyFrom(pingTarget, req.getLocale()); 197 return forward; 198 } catch (Exception e) { 199 getLogger().error("ERROR in action", e); 200 throw new ServletException (e); 201 } 202 } 203 204 214 public ActionForward deleteSelected(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res) throws Exception { 215 ActionForward forward = mapping.findForward(PING_TARGET_DELETE_PAGE); 216 RollerRequest rreq = RollerRequest.getRollerRequest(req); 217 try { 218 BasePageModel pageModel = new BasePageModel(getPingTargetDeleteOKTitle(), req, res, mapping); 219 req.setAttribute("model", pageModel); 220 if (!hasRequiredRights(rreq, rreq.getWebsite())) { 221 return mapping.findForward(ACCESS_DENIED_PAGE); 222 } 223 PingTargetData pingTarget = select(rreq); 224 ((PingTargetForm) form).copyFrom(pingTarget, req.getLocale()); 225 return forward; 226 } catch (Exception e) { 227 getLogger().error("ERROR in action", e); 228 throw new ServletException (e); 229 } 230 } 231 232 242 public ActionForward deleteConfirmed(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res) throws Exception { 243 RollerRequest rreq = RollerRequest.getRollerRequest(req); 244 PingTargetForm pingTargetForm = (PingTargetForm) form; 245 PingTargetManager pingTargetMgr = RollerFactory.getRoller().getPingTargetManager(); 246 try { 247 if (!hasRequiredRights(rreq, rreq.getWebsite())) { 248 return mapping.findForward(ACCESS_DENIED_PAGE); 249 } 250 String pingTargetId = pingTargetForm.getId(); 251 if (pingTargetId == null || pingTargetId.length() == 0) { 252 throw new RollerException("Missing ping target id."); 253 } 254 PingTargetData ping = pingTargetMgr.getPingTarget(pingTargetId); 255 pingTargetMgr.removePingTarget(ping); 256 RollerFactory.getRoller().flush(); 257 return view(mapping, form, req, res); 258 } catch (Exception e) { 259 getLogger().error("ERROR in action", e); 260 throw new ServletException (e); 261 } 262 } 263 264 266 273 protected PingTargetData select(RollerRequest rreq) throws RollerException { 274 String pingTargetId = rreq.getRequest().getParameter(RequestConstants.PINGTARGET_ID); 275 PingTargetManager pingTargetMgr = RollerFactory.getRoller().getPingTargetManager(); 276 if (pingTargetId == null || pingTargetId.length() == 0) { 277 throw new RollerException("Missing ping target id: " + pingTargetId); 278 } 279 280 PingTargetData pingTarget = pingTargetMgr.getPingTarget(pingTargetId); 281 if (pingTarget == null) { 282 throw new RollerException("No such ping target id: " + pingTargetId); 283 } 284 return pingTarget; 285 } 286 287 297 private ActionMessages validate(RollerRequest rreq, PingTargetData pingTarget) throws RollerException { 298 ActionMessages errors = new ActionMessages(); 299 300 PingTargetManager pingTargetMgr = RollerFactory.getRoller().getPingTargetManager(); 301 if (!pingTargetMgr.isNameUnique(pingTarget)) { 302 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("pingTarget.nameNotUnique")); 303 } 304 if (!pingTargetMgr.isUrlWellFormed(pingTarget)) { 305 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("pingTarget.malformedUrl")); 306 } else if (!pingTargetMgr.isHostnameKnown(pingTarget)) { 307 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("pingTarget.unknownHost")); 308 } 309 return errors; 310 } 311 312 313 319 protected abstract boolean hasRequiredRights(RollerRequest rreq, WebsiteData website) throws RollerException; 320 321 324 protected abstract Log getLogger(); 325 326 330 protected abstract List getPingTargets(RollerRequest rreq) throws RollerException; 331 332 333 337 protected abstract PingTargetData createPingTarget(RollerRequest rreq, PingTargetForm pingTargetForm) throws RollerException; 338 } 339 | Popular Tags |