KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * 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. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18
19 package org.apache.roller.ui.authoring.struts.actions;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.apache.roller.RollerException;
24 import org.apache.roller.config.PingConfig;
25 import org.apache.roller.model.AutoPingManager;
26 import org.apache.roller.model.PingTargetManager;
27 import org.apache.roller.model.RollerFactory;
28 import org.apache.roller.pojos.AutoPingData;
29 import org.apache.roller.pojos.PingTargetData;
30 import org.apache.roller.pojos.WebsiteData;
31 import org.apache.roller.ui.core.BasePageModel;
32 import org.apache.roller.ui.core.RequestConstants;
33 import org.apache.roller.ui.core.RollerRequest;
34 import org.apache.roller.ui.core.RollerSession;
35 import org.apache.roller.ui.core.pings.WeblogUpdatePinger;
36 import org.apache.struts.action.*;
37 import org.apache.struts.actions.DispatchAction;
38 import org.apache.xmlrpc.XmlRpcException;
39
40 import javax.servlet.ServletException JavaDoc;
41 import javax.servlet.http.HttpServletRequest JavaDoc;
42 import javax.servlet.http.HttpServletResponse JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.net.SocketException JavaDoc;
45 import java.net.UnknownHostException JavaDoc;
46 import java.util.*;
47
48
49 /**
50  * Actions for setting up automatic ping configuration for a weblog.
51  *
52  * @author <a HREF="mailto:anil@busybuddha.org">Anil Gangolli</a>
53  * @struts.action name="pingSetupForm" path="/roller-ui/authoring/pingSetup" scope="request" parameter="method"
54  * @struts.action-forward name="pingSetup.page" path=".Pings"
55  * @struts.action-forward name="pingResult.page" path=".PingResult"
56  */

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

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

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

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

146     public ActionForward enableSelected(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) throws Exception JavaDoc {
147         RollerRequest rreq = RollerRequest.getRollerRequest(req);
148         AutoPingManager autoPingMgr = RollerFactory.getRoller().getAutopingManager();
149         PingTargetData pingTarget = select(rreq);
150         try {
151             if (!isAuthorized(rreq, rreq.getWebsite())) {
152                 return mapping.findForward("access-denied");
153             }
154             AutoPingData autoPing = new AutoPingData(null, pingTarget, rreq.getWebsite());
155             autoPingMgr.saveAutoPing(autoPing);
156             RollerFactory.getRoller().flush();
157
158             return view(mapping, form, req, res);
159         } catch (Exception JavaDoc e) {
160             mLogger.error("ERROR in action", e);
161             throw new ServletException JavaDoc(e);
162         }
163     }
164
165     /*
166      * Load delete confirmation view.
167      */

168     public ActionForward disableSelected(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) throws Exception JavaDoc {
169         RollerRequest rreq = RollerRequest.getRollerRequest(req);
170         AutoPingManager autoPingMgr = RollerFactory.getRoller().getAutopingManager();
171         PingTargetData pingTarget = select(rreq);
172         try {
173             if (!isAuthorized(rreq, rreq.getWebsite())) {
174                 return mapping.findForward("access-denied");
175             }
176             autoPingMgr.removeAutoPing(pingTarget, rreq.getWebsite());
177             RollerFactory.getRoller().flush();
178
179             return view(mapping, form, req, res);
180         } catch (Exception JavaDoc e) {
181             mLogger.error("ERROR in action", e);
182             throw new ServletException JavaDoc(e);
183         }
184     }
185
186     /*
187      * Ping the selected target now.
188      */

189     public ActionForward pingSelectedNow(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) throws Exception JavaDoc {
190         try {
191             RollerRequest rreq = RollerRequest.getRollerRequest(req);
192             PingTargetData pingTarget = select(rreq);
193             WebsiteData website = rreq.getWebsite();
194             try {
195                 if (!isAuthorized(rreq, website)) {
196                     return mapping.findForward("access-denied");
197                 }
198                 if (PingConfig.getSuspendPingProcessing()) {
199                     if (mLogger.isDebugEnabled()) mLogger.debug("Ping processing is disabled.");
200                     ActionMessages errors = new ActionMessages();
201                     errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("ping.pingProcessingIsSuspended"));
202                     saveErrors(req, errors);
203                 } else {
204                     WeblogUpdatePinger.PingResult pingResult = WeblogUpdatePinger.sendPing(pingTarget, website);
205                     if (pingResult.isError()) {
206                         if (mLogger.isDebugEnabled()) mLogger.debug("Ping Result: " + pingResult);
207                         ActionMessages errors = new ActionMessages();
208                         if (pingResult.getMessage() != null && pingResult.getMessage().trim().length() > 0) {
209                             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("ping.transmittedButError"));
210                             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(pingResult.getMessage()));
211                         } else {
212                             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("ping.transmissionFailed"));
213                         }
214                         saveErrors(req, errors);
215                     } else {
216                         ActionMessages messages = new ActionMessages();
217                         messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("ping.successful"));
218                         saveMessages(req, messages);
219                     }
220                 }
221             } catch (IOException JavaDoc ex) {
222                 mLogger.debug(ex);
223                 ActionMessages errors = new ActionMessages();
224                 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("ping.transmissionFailed"));
225                 addSpecificMessages(ex, errors);
226                 saveErrors(req, errors);
227             } catch (XmlRpcException ex) {
228                 mLogger.debug(ex);
229                 ActionMessages errors = new ActionMessages();
230                 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("ping.transmissionFailed"));
231                 addSpecificMessages(ex, errors);
232                 saveErrors(req, errors);
233             }
234             return view(mapping, form, req, res);
235         } catch (Exception JavaDoc ex) {
236             mLogger.error("ERROR in action", ex);
237             throw new ServletException JavaDoc(ex);
238         }
239     }
240
241     // TODO: Consider unifying with other RollerRequest methods
242
// Private helper to get ping target specified by request
243
private PingTargetData select(RollerRequest rreq) throws RollerException {
244         String JavaDoc pingTargetId = rreq.getRequest().getParameter(RequestConstants.PINGTARGET_ID);
245         PingTargetManager pingTargetMgr = RollerFactory.getRoller().getPingTargetManager();
246         if (pingTargetId == null || pingTargetId.length() == 0) {
247             throw new RollerException("Missing ping target id: " + pingTargetId);
248         }
249
250         PingTargetData pingTarget = pingTargetMgr.getPingTarget(pingTargetId);
251         if (pingTarget == null) {
252             throw new RollerException("No such ping target id: " + pingTargetId);
253         }
254         return pingTarget;
255     }
256
257     private void addSpecificMessages(Exception JavaDoc ex, ActionMessages errors) {
258         if (ex instanceof UnknownHostException JavaDoc) {
259             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("ping.unknownHost"));
260         } else if (ex instanceof SocketException JavaDoc) {
261             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("ping.networkConnectionFailed"));
262         }
263     }
264
265     private boolean isAuthorized(RollerRequest rreq, WebsiteData website) throws RollerException {
266         RollerSession rses = RollerSession.getRollerSession(rreq.getRequest());
267         return rses.isUserAuthorizedToAdmin(website) && !PingConfig.getDisablePingUsage();
268     }
269 }
270
Popular Tags