KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > weblog > actions > BasePingTargetsAction


1 /*
2  * Copyright (c) 2005
3  * Anil R. Gangolli. All rights reserved.
4  *
5  * Distributed with the Roller Weblogger Project under the terms of the Roller Software
6  * License
7  */

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 JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26 import javax.servlet.ServletException JavaDoc;
27 import java.util.List JavaDoc;
28
29 /**
30  * Base class for both common and custom ping target operations. The methods here apply to
31  * creating, editing and removing ping targets. Operations for maintaining automatic ping
32  * configurations are handled by {@link PingSetupAction}.
33  */

34 public abstract class BasePingTargetsAction extends DispatchAction
35 {
36     // These are expected to be defined forwards by the concrete subclass actions.
37
protected static final String JavaDoc VIEW_PAGE = "pingTargets.page";
38     protected static final String JavaDoc PING_TARGET_EDIT_PAGE = "pingTargetEdit.page";
39     protected static final String JavaDoc PING_TARGET_DELETE_PAGE = "pingTargetDeleteOK.page";
40     protected static final String JavaDoc ACCESS_DENIED_PAGE = "access-denied";
41
42     public BasePingTargetsAction() {
43
44     }
45
46     /**
47      * Implements the default action (view) if the method is not specified.
48      * @param mapping
49      * @param actionForm
50      * @param request
51      * @param response
52      * @return the same result as <code>view()</code>
53      * @throws Exception
54      * @see org.apache.struts.actions.DispatchAction#unspecified(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
55      */

56     protected ActionForward unspecified(ActionMapping mapping,
57                                         ActionForm actionForm,
58                                         HttpServletRequest JavaDoc request,
59                                         HttpServletResponse JavaDoc response)
60         throws Exception JavaDoc
61     {
62         return view(mapping, actionForm, request, response);
63     }
64
65     /**
66      * Display the ping targets.
67      *
68      * @param mapping
69      * @param form
70      * @param req
71      * @param res
72      * @return forward to the ping targets page
73      * @throws Exception
74      */

75     public ActionForward view(ActionMapping mapping, ActionForm form,
76                               HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
77         throws Exception JavaDoc
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 JavaDoc pingTargets = getPingTargets(rreq);
89             req.setAttribute("pingTargets", pingTargets);
90             return forward;
91         }
92         catch (Exception JavaDoc e)
93         {
94             getLogger().error("ERROR in action", e);
95             throw new ServletException JavaDoc(e);
96         }
97     }
98
99     /**
100      * Save a ping target, new or existing (depending on whether the id is non-empty).
101      *
102      * @param mapping
103      * @param form
104      * @param req
105      * @param res
106      * @return the result of <code>view()</code> after the target is saved.
107      * @throws Exception
108      */

109     public ActionForward save(ActionMapping mapping, ActionForm form,
110                               HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
111         throws Exception JavaDoc
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 JavaDoc 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             // Call private helper to validate ping target
137
// If there are errors, go back to the target edit page.
138
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             // Appears to be ok. Save it, commit and return refreshed view of target list.
145
pingTarget.save();
146             rreq.getRoller().commit();
147             return view(mapping, form, req, res);
148         }
149         catch (Exception JavaDoc e)
150         {
151             getLogger().error("ERROR in action", e);
152             throw new ServletException JavaDoc(e);
153         }
154     }
155
156     /**
157      * Add a new ping target. Loads the edit view blank.
158      *
159      * @param mapping
160      * @param form
161      * @param req
162      * @param res
163      * @return the edit page (blank)
164      * @throws Exception
165      */

166     public ActionForward addNew(ActionMapping mapping, ActionForm form,
167                                 HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
168         throws Exception JavaDoc
169     {
170         return mapping.findForward(PING_TARGET_EDIT_PAGE);
171     }
172
173     /**
174      * Edit a ping target (load edit view)
175      *
176      * @param mapping
177      * @param form
178      * @param req
179      * @param res
180      * @return the edit view with the form populated with the ping target specified by the id in the request.
181      * @throws Exception
182      */

183     public ActionForward editSelected(ActionMapping mapping, ActionForm form,
184                                       HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
185         throws Exception JavaDoc
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 JavaDoc e)
200         {
201             getLogger().error("ERROR in action", e);
202             throw new ServletException JavaDoc(e);
203         }
204     }
205
206     /**
207      * Delete a ping target (load delete confirmation view).
208      *
209      * @param mapping
210      * @param form
211      * @param req
212      * @param res
213      * @return the delete confirmation view with the form populated with the ping target specified by the id in the request.
214      * @throws Exception
215      */

216     public ActionForward deleteSelected(ActionMapping mapping, ActionForm form,
217                                         HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
218         throws Exception JavaDoc
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 JavaDoc e)
233         {
234             getLogger().error("ERROR in action", e);
235             throw new ServletException JavaDoc(e);
236         }
237     }
238
239     /**
240      * Delete a ping target (already confirmed). This performs the actual deletion.
241      *
242      * @param mapping
243      * @param form
244      * @param req
245      * @param res
246      * @return the result of <code>view()</code> after the deletion
247      * @throws Exception
248      */

249     public ActionForward deleteConfirmed(ActionMapping mapping, ActionForm form,
250                                          HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
251         throws Exception JavaDoc
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 JavaDoc 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 JavaDoc e)
272         {
273             getLogger().error("ERROR in action", e);
274             throw new ServletException JavaDoc(e);
275         }
276     }
277
278     // TODO: Consider unifying with other RollerRequest methods
279
/*
280      * Helper to select the ping target specified by the id in the request.
281      * @param rreq
282      * @return the ping target specified by the id in the request
283      * @throws RollerException
284      */

285     private PingTargetData select(RollerRequest rreq) throws RollerException
286     {
287         String JavaDoc 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     /*
303      * Private helper to validate a ping target.
304      * @param rreq the request
305      * @param pingTarget the ping target to validate
306      * @return an <code>ActionMessages</code> object with <code>ActionMessage</code> for each error encountered, empty if no
307      * errors were encountered.
308      * @throws RollerException
309      */

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     /*
334      * Helper defined by the subclass to determine if user has adequate rights for the action.
335      * This and the {@link org.roller.pojos.PingTargetData#canSave()} method determine the access
336      * control for the action.
337      */

338     protected abstract boolean hasRequiredRights(RollerRequest rreq) throws RollerException;
339
340     /*
341      * Get the logger from the concrete subclass
342      */

343     protected abstract Log getLogger();
344
345     /*
346      * Get the ping targets for the view. This is implemented differently in the concrete subclasses.
347      */

348     protected abstract List JavaDoc getPingTargets(RollerRequest rreq) throws RollerException;
349
350
351     /*
352      * Create a new ping target (blank). This is implemented differently in the concrete subclasses.
353      */

354     protected abstract PingTargetData createPingTarget(RollerRequest rreq, PingTargetForm pingTargetForm) throws RollerException;
355 }
356
Popular Tags