KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > authoring > struts > actions > BasePingTargetsAction


1 /*
2  * Copyright 2005-2006 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35 import java.util.List JavaDoc;
36
37
38 /**
39  * Base class for both common and custom ping target operations. The methods
40  * here apply to creating, editing and removing ping targets. Operations for
41  * maintaining automatic ping configurations are handled by
42  * {@link PingSetupAction}.
43  */

44 public abstract class BasePingTargetsAction extends DispatchAction {
45     // These are expected to be defined forwards by the concrete subclass actions.
46
protected static final String JavaDoc VIEW_PAGE = "pingTargets.page";
47     protected static final String JavaDoc PING_TARGET_EDIT_PAGE = "pingTargetEdit.page";
48     protected static final String JavaDoc PING_TARGET_DELETE_PAGE = "pingTargetDeleteOK.page";
49     protected static final String JavaDoc ACCESS_DENIED_PAGE = "access-denied";
50
51     public abstract String JavaDoc getPingTargetsTitle();
52
53     public abstract String JavaDoc getPingTargetEditTitle();
54
55     public abstract String JavaDoc getPingTargetDeleteOKTitle();
56
57     public BasePingTargetsAction() {
58
59     }
60
61     /**
62      * Implements the default action (view) if the method is not specified.
63      *
64      * @param mapping
65      * @param actionForm
66      * @param request
67      * @param response
68      * @return the same result as <code>view()</code>
69      * @throws Exception
70      * @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)
71      */

72     protected ActionForward unspecified(ActionMapping mapping, ActionForm actionForm, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
73         return view(mapping, actionForm, request, response);
74     }
75
76     /**
77      * Display the ping targets.
78      *
79      * @param mapping
80      * @param form
81      * @param req
82      * @param res
83      * @return forward to the ping targets page
84      * @throws Exception
85      */

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

115     public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) throws Exception JavaDoc {
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 JavaDoc 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             // 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.
145
// Save it, commit and return refreshed view of target list.
146
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 JavaDoc e) {
155             getLogger().error("ERROR in action", e);
156             throw new ServletException JavaDoc(e);
157         }
158     }
159
160     /**
161      * Add a new ping target. Loads the edit view blank.
162      *
163      * @param mapping
164      * @param form
165      * @param req
166      * @param res
167      * @return the edit page (blank)
168      * @throws Exception
169      */

170     public ActionForward addNew(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) throws Exception JavaDoc {
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     /**
177      * Edit a ping target (load edit view)
178      *
179      * @param mapping
180      * @param form
181      * @param req
182      * @param res
183      * @return the edit view with the form populated with the ping target specified by the id in the request.
184      * @throws Exception
185      */

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

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

242     public ActionForward deleteConfirmed(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) throws Exception JavaDoc {
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 JavaDoc 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 JavaDoc e) {
259             getLogger().error("ERROR in action", e);
260             throw new ServletException JavaDoc(e);
261         }
262     }
263
264     // TODO: Consider unifying with other RollerRequest methods
265

266     /**
267      * Helper to select the ping target specified by the id in the request.
268      *
269      * @param rreq
270      * @return the ping target specified by the id in the request
271      * @throws RollerException
272      */

273     protected PingTargetData select(RollerRequest rreq) throws RollerException {
274         String JavaDoc 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     /**
288      * Private helper to validate a ping target.
289      *
290      * @param rreq the request
291      * @param pingTarget the ping target to validate
292      * @return an <code>ActionMessages</code> object with
293      * <code>ActionMessage</code> for each error encountered,
294      * empty if no errors were encountered.
295      * @throws RollerException
296      */

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

319     protected abstract boolean hasRequiredRights(RollerRequest rreq, WebsiteData website) throws RollerException;
320
321     /**
322      * Get the logger from the concrete subclass
323      */

324     protected abstract Log getLogger();
325
326     /**
327      * Get the ping targets for the view. This is implemented differently in
328      * the concrete subclasses.
329      */

330     protected abstract List JavaDoc getPingTargets(RollerRequest rreq) throws RollerException;
331
332
333     /**
334      * Create a new ping target (blank). This is implemented differently in
335      * the concrete subclasses.
336      */

337     protected abstract PingTargetData createPingTarget(RollerRequest rreq, PingTargetForm pingTargetForm) throws RollerException;
338 }
339
Popular Tags