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