1 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 ; 33 import javax.servlet.http.HttpServletResponse ; 34 import javax.servlet.ServletException ; 35 import java.util.List ; 36 import java.util.Map ; 37 import java.util.HashMap ; 38 import java.util.Iterator ; 39 import java.util.Collections ; 40 import java.net.UnknownHostException ; 41 import java.net.SocketException ; 42 import java.io.IOException ; 43 44 45 52 public class PingSetupAction extends DispatchAction 53 { 54 private static Log mLogger = 55 LogFactory.getFactory().getInstance(PingSetupAction.class); 56 57 private static final String PING_SETUP_PAGE = "pingSetup.page"; 58 private static final String PING_RESULT_PAGE = "pingResult.page"; 59 60 67 protected ActionForward unspecified(ActionMapping mapping, 68 ActionForm actionForm, 69 HttpServletRequest request, 70 HttpServletResponse response) 71 throws Exception 72 { 73 return view(mapping, actionForm, request, response); 74 } 75 76 79 public ActionForward view(ActionMapping mapping, ActionForm form, 80 HttpServletRequest req, HttpServletResponse res) 81 throws Exception 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 commonPingTargets = pingTargetMgr.getCommonPingTargets(); 95 req.setAttribute("commonPingTargets", commonPingTargets); 96 97 Boolean allowCustomTargets = new Boolean (!PingConfig.getDisallowCustomTargets()); 98 req.setAttribute("allowCustomTargets", allowCustomTargets); 99 100 List customPingTargets = allowCustomTargets.booleanValue() ? 101 pingTargetMgr.getCustomPingTargets(website) : Collections.EMPTY_LIST; 102 req.setAttribute("customPingTargets", customPingTargets); 103 104 Map isEnabled = buildIsEnabledMap(rreq, commonPingTargets, customPingTargets); 106 req.setAttribute("isEnabled", isEnabled); 107 108 return forward; 109 } 110 catch (Exception e) 111 { 112 mLogger.error("ERROR in action", e); 113 throw new ServletException (e); 114 } 115 } 116 117 121 private Map buildIsEnabledMap(RollerRequest rreq, List commonPingTargets, List customPingTargets) 122 throws RollerException 123 { 124 AutoPingManager autoPingMgr = rreq.getRoller().getAutopingManager(); 125 WebsiteData website = rreq.getWebsite(); 126 127 Map isEnabled = new HashMap (); 129 List autopings = autoPingMgr.getAutoPingsByWebsite(website); 130 for (Iterator i = autopings.iterator(); i.hasNext();) 132 { 133 AutoPingData autoPing = (AutoPingData) i.next(); 134 isEnabled.put(autoPing.getPingTarget().getId(), Boolean.TRUE); 135 } 136 for (Iterator 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 for (Iterator 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 161 public ActionForward enableSelected(ActionMapping mapping, ActionForm form, 162 HttpServletRequest req, HttpServletResponse res) 163 throws Exception 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 e) 181 { 182 mLogger.error("ERROR in action", e); 183 throw new ServletException (e); 184 } 185 } 186 187 190 public ActionForward disableSelected(ActionMapping mapping, ActionForm form, 191 HttpServletRequest req, HttpServletResponse res) 192 throws Exception 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 e) 209 { 210 mLogger.error("ERROR in action", e); 211 throw new ServletException (e); 212 } 213 } 214 215 218 public ActionForward pingSelectedNow(ActionMapping mapping, ActionForm form, 219 HttpServletRequest req, HttpServletResponse res) 220 throws Exception 221 { 222 try 223 { 224 RollerRequest rreq = RollerRequest.getRollerRequest(req); 225 String 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 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 ex) 279 { 280 mLogger.error("ERROR in action", ex); 281 throw new ServletException (ex); 282 } 283 } 284 285 private PingTargetData select(RollerRequest rreq) throws RollerException 288 { 289 String 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 ex, ActionMessages errors) 305 { 306 if (ex instanceof UnknownHostException ) 307 { 308 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("ping.unknownHost")); 309 } 310 else if (ex instanceof SocketException ) 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 |