1 16 package org.roller.presentation.planet; 17 18 import java.io.File ; 19 import java.io.IOException ; 20 21 import javax.servlet.ServletException ; 22 import javax.servlet.http.HttpServletRequest ; 23 import javax.servlet.http.HttpServletResponse ; 24 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 import org.apache.struts.action.ActionError; 28 import org.apache.struts.action.ActionErrors; 29 import org.apache.struts.action.ActionForm; 30 import org.apache.struts.action.ActionForward; 31 import org.apache.struts.action.ActionMapping; 32 import org.apache.struts.action.ActionMessage; 33 import org.apache.struts.action.ActionMessages; 34 import org.apache.struts.actions.DispatchAction; 35 import org.roller.presentation.forms.PlanetConfigForm; 36 import org.roller.config.RollerRuntimeConfig; 37 import org.roller.model.PlanetManager; 38 import org.roller.model.Roller; 39 import org.roller.pojos.PlanetConfigData; 40 import org.roller.pojos.PlanetGroupData; 41 import org.roller.presentation.RollerRequest; 42 import org.roller.presentation.planet.RefreshEntriesTask; 43 import org.roller.presentation.planet.SyncWebsitesTask; 44 45 55 public final class PlanetConfigAction extends DispatchAction 56 { 57 private static Log logger = 58 LogFactory.getFactory().getInstance(PlanetConfigAction.class); 59 60 61 public ActionForward getConfig(ActionMapping mapping, 62 ActionForm actionForm, HttpServletRequest request, 63 HttpServletResponse response) throws IOException , ServletException 64 { 65 ActionForward forward = mapping.findForward("planetConfig.page"); 66 try 67 { 68 RollerRequest rreq = RollerRequest.getRollerRequest(request); 69 if (rreq.isUserAuthorizedToEdit()) 70 { 71 Roller roller = rreq.getRoller(); 72 PlanetManager planet = roller.getPlanetManager(); 73 PlanetConfigData config = planet.getConfiguration(); 74 PlanetConfigForm form = (PlanetConfigForm)actionForm; 75 if (config != null) 76 { 77 form.copyFrom(config, request.getLocale()); 78 } 79 else 80 { 81 form.setTitle("Planet Roller"); 82 form.setAdminEmail(RollerRuntimeConfig.getProperty("site.adminemail")); 83 form.setSiteUrl(RollerRuntimeConfig.getProperty("site.absoluteurl")); 84 form.setCacheDir("/tmp"); 85 } 86 } 87 else 88 { 89 forward = mapping.findForward("access-denied"); 90 } 91 } 92 catch (Exception e) 93 { 94 request.getSession().getServletContext().log("ERROR", e); 95 throw new ServletException (e); 96 } 97 return forward; 98 } 99 100 101 public ActionForward saveConfig(ActionMapping mapping, 102 ActionForm actionForm, HttpServletRequest request, 103 HttpServletResponse response) throws IOException , ServletException 104 { 105 ActionForward forward = mapping.findForward("planetConfig.page"); 106 try 107 { 108 RollerRequest rreq = RollerRequest.getRollerRequest(request); 109 if (rreq.isUserAuthorizedToEdit()) 110 { 111 Roller roller = rreq.getRoller(); 112 PlanetManager planet = roller.getPlanetManager(); 113 PlanetConfigData config = planet.getConfiguration(); 114 if (config == null) 115 { 116 config = new PlanetConfigData(); 117 } 118 PlanetConfigForm form = (PlanetConfigForm) actionForm; 119 ActionErrors errors = validate(form); 120 if (errors.isEmpty()) 121 { 122 form.copyTo(config, request.getLocale()); 123 planet.saveConfiguration(config); 124 if (planet.getGroup("external") == null) 125 { 126 PlanetGroupData group = new PlanetGroupData(); 127 group.setHandle("external"); 128 group.setTitle("external"); 129 planet.saveGroup(group); 130 } 131 roller.commit(); 132 ActionMessages messages = new ActionMessages(); 133 messages.add(null, new ActionMessage("planetConfig.success.saved")); 134 saveMessages(request, messages); 135 } 136 else 137 { 138 saveErrors(request, errors); 139 } 140 } 141 else 142 { 143 forward = mapping.findForward("access-denied"); 144 } 145 } 146 catch (Exception e) 147 { 148 request.getSession().getServletContext().log("ERROR", e); 149 throw new ServletException (e); 150 } 151 return forward; 152 } 153 154 155 public ActionForward refreshEntries(ActionMapping mapping, 156 ActionForm actionForm, HttpServletRequest request, 157 HttpServletResponse response) throws IOException , ServletException 158 { 159 ActionForward forward = mapping.findForward("planetConfig.page"); 160 try 161 { 162 RollerRequest rreq = RollerRequest.getRollerRequest(request); 163 if (rreq.isUserAuthorizedToEdit()) 164 { 165 Roller roller = rreq.getRoller(); 166 RefreshEntriesTask task = new RefreshEntriesTask(); 167 task.init(roller, "dummy"); 168 roller.getThreadManager().executeInBackground(task); 169 170 ActionMessages messages = new ActionMessages(); 171 messages.add(null, 172 new ActionMessage("planetConfig.success.refreshed")); 173 saveMessages(request, messages); 174 } 175 else 176 { 177 forward = mapping.findForward("access-denied"); 178 } 179 } 180 catch (Exception e) 181 { 182 request.getSession().getServletContext().log("ERROR", e); 183 throw new ServletException (e); 184 } 185 return forward; 186 } 187 188 189 public ActionForward syncWebsites(ActionMapping mapping, 190 ActionForm actionForm, HttpServletRequest request, 191 HttpServletResponse response) throws IOException , ServletException 192 { 193 ActionForward forward = mapping.findForward("planetConfig.page"); 194 try 195 { 196 RollerRequest rreq = RollerRequest.getRollerRequest(request); 197 if (rreq.isUserAuthorizedToEdit()) 198 { 199 Roller roller = (Roller)rreq.getRoller(); 200 SyncWebsitesTask task = new SyncWebsitesTask(); 201 task.init(roller, "dummy"); 202 roller.getThreadManager().executeInBackground(task); 203 ActionMessages messages = new ActionMessages(); 204 messages.add(null, 205 new ActionMessage("planetConfig.success.synced")); 206 saveMessages(request, messages); 207 } 208 else 209 { 210 forward = mapping.findForward("access-denied"); 211 } 212 } 213 catch (Exception e) 214 { 215 request.getSession().getServletContext().log("ERROR", e); 216 throw new ServletException (e); 217 } 218 return forward; 219 } 220 221 222 public ActionErrors validate(PlanetConfigForm form) 223 { 224 ActionErrors errors = new ActionErrors(); 225 if (form.getCacheDir()==null || form.getCacheDir().trim().length()==0) 226 { 227 errors.add(null, new ActionError("planetConfig.error.feedUrl")); 228 } 229 else 230 { 231 File file = new File (form.getCacheDir()); 232 if (!file.isDirectory()) 233 { 234 errors.add(null, new ActionError( 235 "planetConfig.error.cacheDirNotFound")); 236 } 237 if (!file.canWrite()) 238 { 239 errors.add(null, new ActionError( 240 "planetConfig.error.cacheDirNotWritable")); 241 } 242 } 243 if (form.getProxyHost()!=null && form.getProxyHost().trim().length()>0) 244 { 245 if (form.getProxyPort()<1) 246 { 247 errors.add(null, new ActionError( 248 "planetConfig.error.badProxyPort")); 249 } 250 } 251 return errors; 252 } 253 } 254 255 | Popular Tags |