KickJava   Java API By Example, From Geeks To Geeks.

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


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.apache.commons.logging.LogFactory;
19 import org.apache.xmlrpc.XmlRpcException;
20 import org.roller.presentation.RollerRequest;
21 import org.roller.presentation.RollerContext;
22 import org.roller.presentation.pings.WeblogUpdatePinger;
23 import org.roller.config.PingConfig;
24 import org.roller.model.PingTargetManager;
25 import org.roller.model.AutoPingManager;
26 import org.roller.pojos.PingTargetData;
27 import org.roller.pojos.WebsiteData;
28 import org.roller.pojos.AutoPingData;
29 import org.roller.RollerException;
30 import org.roller.config.PingConfig;
31
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.http.HttpServletResponse JavaDoc;
34 import javax.servlet.ServletException JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.Map JavaDoc;
37 import java.util.HashMap JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.Collections JavaDoc;
40 import java.net.UnknownHostException JavaDoc;
41 import java.net.SocketException JavaDoc;
42 import java.io.IOException JavaDoc;
43
44
45 /**
46  * Actions for setting up automatic ping configuration for a weblog.
47  *
48  * @struts.action name="pingSetupForm" path="/editor/pingSetup" scope="request" parameter="method"
49  * @struts.action-forward name="pingSetup.page" path="/weblog/Pings.jsp"
50  * @struts.action-forward name="pingResult.page" path="/weblog/PingResult.jsp"
51  */

52 public class PingSetupAction extends DispatchAction
53 {
54     private static Log mLogger =
55         LogFactory.getFactory().getInstance(PingSetupAction.class);
56
57     private static final String JavaDoc PING_SETUP_PAGE = "pingSetup.page";
58     private static final String JavaDoc PING_RESULT_PAGE = "pingResult.page";
59
60     /* (non-Javadoc)
61      * @see org.apache.struts.actions.DispatchAction#unspecified(
62      * org.apache.struts.action.ActionMapping,
63      * org.apache.struts.action.ActionForm,
64      * javax.servlet.http.HttpServletRequest,
65      * javax.servlet.http.HttpServletResponse)
66      */

67     protected ActionForward unspecified(ActionMapping mapping,
68                                         ActionForm actionForm,
69                                         HttpServletRequest JavaDoc request,
70                                         HttpServletResponse JavaDoc response)
71         throws Exception JavaDoc
72     {
73         return view(mapping, actionForm, request, response);
74     }
75
76     /*
77      * Display the common ping targets with page
78      */

79     public ActionForward view(ActionMapping mapping, ActionForm form,
80                               HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
81         throws Exception JavaDoc
82     {
83         ActionForward forward = mapping.findForward(PING_SETUP_PAGE);
84         RollerRequest rreq = RollerRequest.getRollerRequest(req);
85         PingTargetManager pingTargetMgr = rreq.getRoller().getPingTargetManager();
86         WebsiteData website = rreq.getWebsite();
87         try
88         {
89             if (!isAuthorized(rreq))
90             {
91                 return mapping.findForward("access-denied");
92             }
93
94             List JavaDoc commonPingTargets = pingTargetMgr.getCommonPingTargets();
95             req.setAttribute("commonPingTargets", commonPingTargets);
96
97             Boolean JavaDoc allowCustomTargets = new Boolean JavaDoc(!PingConfig.getDisallowCustomTargets());
98             req.setAttribute("allowCustomTargets", allowCustomTargets);
99
100             List JavaDoc customPingTargets = allowCustomTargets.booleanValue() ?
101                 pingTargetMgr.getCustomPingTargets(website) : Collections.EMPTY_LIST;
102             req.setAttribute("customPingTargets", customPingTargets);
103
104             // Build isEnabled map (keyed by ping target id and values Boolean.TRUE/Boolean.FALSE)
105
Map JavaDoc isEnabled = buildIsEnabledMap(rreq, commonPingTargets, customPingTargets);
106             req.setAttribute("isEnabled", isEnabled);
107
108             return forward;
109         }
110         catch (Exception JavaDoc e)
111         {
112             mLogger.error("ERROR in action", e);
113             throw new ServletException JavaDoc(e);
114         }
115     }
116
117     /*
118      * Private helper to build a map indexed by ping target id with values Boolean.TRUE and Boolean.FALSE
119      * based on whether the ping target is enabled (has a corresponding auto ping configuration).
120      */

121     private Map JavaDoc buildIsEnabledMap(RollerRequest rreq, List JavaDoc commonPingTargets, List JavaDoc customPingTargets)
122         throws RollerException
123     {
124         AutoPingManager autoPingMgr = rreq.getRoller().getAutopingManager();
125         WebsiteData website = rreq.getWebsite();
126
127         // Build isEnabled map (keyed by ping target id and values Boolean.TRUE/Boolean.FALSE)
128
Map JavaDoc isEnabled = new HashMap JavaDoc();
129         List JavaDoc autopings = autoPingMgr.getAutoPingsByWebsite(website);
130         // Add the enabled auto ping configs with TRUE
131
for (Iterator JavaDoc i = autopings.iterator(); i.hasNext();)
132         {
133             AutoPingData autoPing = (AutoPingData) i.next();
134             isEnabled.put(autoPing.getPingTarget().getId(), Boolean.TRUE);
135         }
136         // Somewhat awkward, but the two loops save building a separate combined list.
137
// Add disabled common ones with FALSE
138
for (Iterator JavaDoc i = commonPingTargets.iterator(); i.hasNext();)
139         {
140             PingTargetData pingTarget = (PingTargetData) i.next();
141             if (isEnabled.get(pingTarget.getId()) == null)
142             {
143                 isEnabled.put(pingTarget.getId(), Boolean.FALSE);
144             }
145         }
146         // Add disabled custom ones with FALSE
147
for (Iterator JavaDoc i = customPingTargets.iterator(); i.hasNext();)
148         {
149             PingTargetData pingTarget = (PingTargetData) i.next();
150             if (isEnabled.get(pingTarget.getId()) == null)
151             {
152                 isEnabled.put(pingTarget.getId(), Boolean.FALSE);
153             }
154         }
155         return isEnabled;
156     }
157
158     /*
159      * Enable a ping target.
160      */

161     public ActionForward enableSelected(ActionMapping mapping, ActionForm form,
162                                         HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
163         throws Exception JavaDoc
164     {
165         RollerRequest rreq = RollerRequest.getRollerRequest(req);
166         AutoPingManager autoPingMgr = rreq.getRoller().getAutopingManager();
167         try
168         {
169             if (!isAuthorized(rreq))
170             {
171                 return mapping.findForward("access-denied");
172             }
173             PingTargetData pingTarget = select(rreq);
174             AutoPingData autoPing = autoPingMgr.createAutoPing(pingTarget, rreq.getWebsite());
175             autoPingMgr.storeAutoPing(autoPing);
176             rreq.getRoller().commit();
177
178             return view(mapping, form, req, res);
179         }
180         catch (Exception JavaDoc e)
181         {
182             mLogger.error("ERROR in action", e);
183             throw new ServletException JavaDoc(e);
184         }
185     }
186
187     /*
188      * Load delete confirmation view.
189      */

190     public ActionForward disableSelected(ActionMapping mapping, ActionForm form,
191                                          HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
192         throws Exception JavaDoc
193     {
194         RollerRequest rreq = RollerRequest.getRollerRequest(req);
195         AutoPingManager autoPingMgr = rreq.getRoller().getAutopingManager();
196         try
197         {
198             if (!isAuthorized(rreq))
199             {
200                 return mapping.findForward("access-denied");
201             }
202             PingTargetData pingTarget = select(rreq);
203             autoPingMgr.removeAutoPing(pingTarget, rreq.getWebsite());
204             rreq.getRoller().commit();
205
206             return view(mapping, form, req, res);
207         }
208         catch (Exception JavaDoc e)
209         {
210             mLogger.error("ERROR in action", e);
211             throw new ServletException JavaDoc(e);
212         }
213     }
214
215     /*
216      * Ping the selected target now.
217      */

218     public ActionForward pingSelectedNow(ActionMapping mapping, ActionForm form,
219                                          HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
220         throws Exception JavaDoc
221     {
222         try
223         {
224             RollerRequest rreq = RollerRequest.getRollerRequest(req);
225             String JavaDoc absoluteUrl = RollerContext.getRollerContext(req).getAbsoluteContextUrl(req);
226             PingTargetData pingTarget = select(rreq);
227             WebsiteData website = rreq.getWebsite();
228             try
229             {
230                 if (!isAuthorized(rreq))
231                 {
232                     return mapping.findForward("access-denied");
233                 }
234                 if (PingConfig.getSuspendPingProcessing())
235                 {
236                     if (mLogger.isDebugEnabled()) mLogger.debug("Ping processing is disabled.");
237                     ActionMessages errors = new ActionMessages();
238                     errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("ping.pingProcessingIsSuspended"));
239                     saveErrors(req, errors);
240                 }
241                 else
242                 {
243                     WeblogUpdatePinger.PingResult pingResult = WeblogUpdatePinger.sendPing(absoluteUrl, pingTarget, website);
244                     if (pingResult.isError())
245                     {
246                         if (mLogger.isDebugEnabled()) mLogger.debug("Ping Result: " + pingResult);
247                         ActionMessages errors = new ActionMessages();
248                         errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("ping.transmittedButErrorReturned"));
249                         errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(pingResult.getMessage()));
250                         saveErrors(req, errors);
251                     }
252                     else
253                     {
254                         ActionMessages messages = new ActionMessages();
255                         messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("ping.successful"));
256                         saveMessages(req, messages);
257                     }
258                 }
259             }
260             catch (IOException JavaDoc ex)
261             {
262                 mLogger.debug(ex);
263                 ActionMessages errors = new ActionMessages();
264                 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("ping.transmissionFailed"));
265                 addSpecificMessages(ex, errors);
266                 saveErrors(req, errors);
267             }
268             catch (XmlRpcException ex)
269             {
270                 mLogger.debug(ex);
271                 ActionMessages errors = new ActionMessages();
272                 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("ping.transmissionFailed"));
273                 addSpecificMessages(ex, errors);
274                 saveErrors(req, errors);
275             }
276             return mapping.findForward(PING_RESULT_PAGE);
277         }
278         catch (Exception JavaDoc ex)
279         {
280             mLogger.error("ERROR in action", ex);
281             throw new ServletException JavaDoc(ex);
282         }
283     }
284
285     // TODO: Consider unifying with other RollerRequest methods
286
// Private helper to get ping target specified by request
287
private PingTargetData select(RollerRequest rreq) throws RollerException
288     {
289         String JavaDoc pingTargetId = rreq.getRequest().getParameter(RollerRequest.PINGTARGETID_KEY);
290         PingTargetManager pingTargetMgr = rreq.getRoller().getPingTargetManager();
291         if (pingTargetId == null || pingTargetId.length() == 0)
292         {
293             throw new RollerException("Missing ping target id: " + pingTargetId);
294         }
295
296         PingTargetData pingTarget = pingTargetMgr.retrievePingTarget(pingTargetId);
297         if (pingTarget == null)
298         {
299             throw new RollerException("No such ping target id: " + pingTargetId);
300         }
301         return pingTarget;
302     }
303
304     private void addSpecificMessages(Exception JavaDoc ex, ActionMessages errors)
305     {
306         if (ex instanceof UnknownHostException JavaDoc)
307         {
308             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("ping.unknownHost"));
309         }
310         else if (ex instanceof SocketException JavaDoc)
311         {
312             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("ping.networkConnectionFailed"));
313         }
314     }
315
316     private boolean isAuthorized(RollerRequest rreq) throws RollerException
317     {
318         return rreq.isUserAuthorizedToEdit() && !PingConfig.getDisablePingUsage();
319     }
320 }
321
Popular Tags