1 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 ; 41 import javax.servlet.http.HttpServletRequest ; 42 import javax.servlet.http.HttpServletResponse ; 43 import java.io.IOException ; 44 import java.net.SocketException ; 45 import java.net.UnknownHostException ; 46 import java.util.*; 47 48 49 57 public class PingSetupAction extends DispatchAction { 58 private static Log mLogger = LogFactory.getFactory().getInstance(PingSetupAction.class); 59 60 private static final String PING_SETUP_PAGE = "pingSetup.page"; 61 62 69 protected ActionForward unspecified(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception { 70 return view(mapping, actionForm, request, response); 71 } 72 73 76 public ActionForward view(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res) throws Exception { 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 allowCustomTargets = new Boolean (!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 Map isEnabled = buildIsEnabledMap(rreq, commonPingTargets, customPingTargets); 100 req.setAttribute("isEnabled", isEnabled); 101 102 return forward; 103 } catch (Exception e) { 104 mLogger.error("ERROR in action", e); 105 throw new ServletException (e); 106 } 107 } 108 109 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 Map isEnabled = new HashMap(); 119 List autopings = autoPingMgr.getAutoPingsByWebsite(website); 120 for (Iterator i = autopings.iterator(); i.hasNext();) { 122 AutoPingData autoPing = (AutoPingData) i.next(); 123 isEnabled.put(autoPing.getPingTarget().getId(), Boolean.TRUE); 124 } 125 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 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 146 public ActionForward enableSelected(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res) throws Exception { 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 e) { 160 mLogger.error("ERROR in action", e); 161 throw new ServletException (e); 162 } 163 } 164 165 168 public ActionForward disableSelected(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res) throws Exception { 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 e) { 181 mLogger.error("ERROR in action", e); 182 throw new ServletException (e); 183 } 184 } 185 186 189 public ActionForward pingSelectedNow(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res) throws Exception { 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 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 ex) { 236 mLogger.error("ERROR in action", ex); 237 throw new ServletException (ex); 238 } 239 } 240 241 private PingTargetData select(RollerRequest rreq) throws RollerException { 244 String 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 ex, ActionMessages errors) { 258 if (ex instanceof UnknownHostException ) { 259 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("ping.unknownHost")); 260 } else if (ex instanceof SocketException ) { 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 |