1 12 13 20 package com.openedit.modules.update; 21 22 import java.io.File ; 23 import java.util.ArrayList ; 24 import java.util.Arrays ; 25 import java.util.Collections ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Map ; 30 31 import org.apache.commons.httpclient.HttpClient; 32 import org.apache.commons.httpclient.methods.PostMethod; 33 import org.apache.commons.httpclient.methods.multipart.FilePart; 34 import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; 35 import org.apache.commons.httpclient.methods.multipart.Part; 36 import org.apache.commons.httpclient.methods.multipart.StringPart; 37 import org.apache.commons.logging.Log; 38 import org.apache.commons.logging.LogFactory; 39 import org.dom4j.Element; 40 import org.springframework.beans.factory.config.BeanDefinition; 41 import org.springframework.beans.factory.support.AbstractBeanDefinition; 42 43 import com.openedit.OpenEditException; 44 import com.openedit.OpenEditRuntimeException; 45 import com.openedit.WebPageRequest; 46 import com.openedit.WebServer; 47 import com.openedit.modules.BaseModule; 48 import com.openedit.modules.admin.filemanager.FileUpload; 49 import com.openedit.modules.scriptrunner.ScriptModule; 50 import com.openedit.page.Page; 51 import com.openedit.users.User; 52 import com.openedit.util.XmlUtil; 53 54 59 public class UpdateModule extends BaseModule 60 { 61 protected WebServer fieldWebServer; 62 63 private static final Log log = LogFactory.getLog(UpdateModule.class); 64 65 66 public void updateProject( WebPageRequest inContext ) throws Exception  67 { 68 String project = inContext.getRequestParameter("project-name"); 69 String beanname = inContext.getRequestParameter("spring-name"); 70 String confirm = inContext.getRequestParameter("confirm"); 73 String returnMessage = null; 74 String strUrl = null; 75 loadUpdateModule(inContext, beanname); 76 if ( project != null && project.length() > 0 && confirm != null && confirm.trim().equalsIgnoreCase("true")) 77 { 78 79 83 strUrl = "http://dev.openedit.org/anthill/projects/" + project + "/install.js"; 86 87 String strOutputFile = "/WEB-INF/install.js"; 89 90 String root = getRoot().getAbsolutePath(); 92 if ( root.endsWith("/")) 93 { 94 root = root.substring(0,root.length() - 1); 95 } 96 97 File out = new File ( root, strOutputFile); 99 new Downloader().download(strUrl,out); 100 101 ScriptModule module = (ScriptModule)getModule("Script"); 102 Map variables = new HashMap (); 103 variables.put("context", inContext); 104 List logs = new ArrayList (); 105 logs.add("Downloading latest upgrade script..."); 106 inContext.putPageValue("log", logs); 107 variables.put("log", logs); 108 try 109 { 110 log.info("Upgrading " + project); 111 module.execScript(variables, strOutputFile ); 112 returnMessage = "COMPLETED"; 113 } 114 catch ( OpenEditException ex) 115 { 116 returnMessage = ex.getMessage(); 117 inContext.putPageValue("exception" , ex); 118 log.error(ex); 119 } 120 } 121 else 122 { 123 returnMessage = "CONFIRM"; 124 } 125 inContext.putPageValue("returnMessage" , returnMessage); 126 127 } 130 131 137 public void loadUpdateModule( WebPageRequest inContext, String inBean ) throws Exception  138 { 139 inContext.putPageValue( "updateModule", this ); 140 if (inBean != null) 143 { 144 Bean obj = createBean(inBean); 145 inContext.putPageValue("bean",obj); 146 } 147 148 } 149 150 155 public List listModules(WebPageRequest inReq) throws Exception  156 { 157 List allModules = new ArrayList (); 158 List sortedNames = new ArrayList (); 159 160 String [] names = getWebServer().getBeanFactory().getBeanDefinitionNames(); 161 sortedNames = Arrays.asList(names); 162 163 for (int i = 0; i < sortedNames.size(); i++) { 164 String name = (String )sortedNames.get(i); 165 Bean bean = createBean(name); 166 if( bean != null) 167 { 168 allModules.add(bean); 169 } 170 } 171 172 inReq.putPageValue("allModules",allModules); 174 return allModules; 175 } 176 177 protected Bean createBean(String springId) 178 { 179 if ( getWebServer().getBeanFactory().containsBean(springId)) 180 { 181 BeanDefinition beanDe = getWebServer().getBeanFactory().getBeanDefinition(springId); 182 if ( !(beanDe instanceof AbstractBeanDefinition)) 183 { 184 throw new OpenEditRuntimeException("Spring version not supported yet"); 185 186 } 187 AbstractBeanDefinition beanDef = (AbstractBeanDefinition)beanDe; 188 Bean bean = new Bean(); 189 190 bean.setName(springId); 192 bean.setBeanDefinition(beanDef.getBeanClass()); 193 194 String version = beanDef.getBeanClass().getPackage().getImplementationVersion(); 196 bean.setVersion(version); 197 198 String title = beanDef.getBeanClass().getPackage().getImplementationTitle(); 200 bean.setTitle(title); 201 return bean; 202 } 203 return null; 204 } 205 206 211 public void listPageValues(WebPageRequest inReq) throws Exception  212 { 213 List allModules = new ArrayList (); 214 215 216 List sortedNames = new ArrayList (inReq.getPageMap().keySet() ); 217 Collections.sort(sortedNames); 218 219 for (int i = 0; i < sortedNames.size(); i++) { 220 221 String name = (String )sortedNames.get(i); 222 Bean bean = new Bean(); 223 bean.setName(name); 224 Object obj = inReq.getPageValue(name); 225 226 if ( obj instanceof String ) 227 { 228 bean.setValue((String )obj); 229 } 230 231 bean.setBeanDefinition(obj.getClass()); 232 allModules.add(bean); 233 } 234 235 inReq.putPageValue("allPageValues",allModules); 236 } 237 238 public WebServer getWebServer() 239 { 240 return fieldWebServer; 241 } 242 243 public void setWebServer(WebServer inWebServer) 244 { 245 fieldWebServer = inWebServer; 246 } 247 public List loadSiteList(WebPageRequest inReq) throws Exception  248 { 249 Page sites = getPageManager().getPage("/openedit/update/sites.xml"); 250 Element root = new XmlUtil().getXml( sites.getReader(), sites.getCharacterEncoding() ); 251 List all = new ArrayList (); 252 for (Iterator iter = root.elementIterator("site"); iter.hasNext();) 253 { 254 Element child = (Element) iter.next(); 255 Site site = new Site(); 256 site.setId(child.attributeValue("id")); 257 site.setText(child.attributeValue("text")); 258 site.setHref(child.attributeValue("href")); 259 all.add( site); 260 } 261 262 List dirs = new ArrayList (); 263 dirs.add("/"); 264 List names = getPageManager().getChildrenNames("/"); 265 for (Iterator iterator = names.iterator(); iterator.hasNext();) 266 { 267 String path = (String ) iterator.next(); 268 Page dir = getPageManager().getPage(path); 269 if( dir.isFolder() ) 270 { 271 dirs.add(path); 272 } 273 } 274 inReq.putPageValue("dirs", dirs); 275 276 inReq.putPageValue("sites", all); 277 return all; 278 } 279 public Site selectSite(WebPageRequest inReq) throws Exception  280 { 281 String id = inReq.getRequestParameter("siteid"); 282 if( id == null) 283 { 284 return null; 285 } 286 List sites = loadSiteList(inReq); 287 Site selected = null; 288 for (Iterator iter = sites.iterator(); iter.hasNext();) 289 { 290 Site site = (Site) iter.next(); 291 if ( site.getId().equals(id)) 292 { 293 selected = site; 294 } 295 } 296 if ( selected != null) 298 { 299 String username = inReq.getRequestParameter("username"); 300 String password = inReq.getRequestParameter("password"); 301 selected.setUsername(username); 302 selected.setPassword(password); 303 } 304 inReq.putPageValue("sitetopush", selected); 305 return selected; 306 } 307 public void pushDirectory(WebPageRequest inReq) throws Exception  308 { 309 Site toUpgrade = selectSite( inReq ); 310 inReq.setRequestParameter("name", "pushed to " + toUpgrade.getId()); 311 File backup = backUpDirectory(inReq); 312 313 String url = toUpgrade.getHref(); 314 if (!url.startsWith("http://")) 315 { 316 url = "http://" + url; 317 } 318 319 if ( !url.endsWith("/")) 320 { 321 url += "/"; 322 } 323 url += "openedit/update/receivepush.html"; 324 log.info("posting here:" + url); 325 326 PostMethod postMethod = new PostMethod(url); 327 328 Part[] parts = { 329 new StringPart("username", toUpgrade.getUsername()), 330 new StringPart("password", toUpgrade.getPassword()), 331 new StringPart("savedas", backup.getName()), 332 new FilePart("file", backup) }; 333 334 postMethod.setRequestEntity( new MultipartRequestEntity(parts, postMethod.getParams()) ); 335 336 HttpClient client = new HttpClient(); 337 int statusCode1 = client.executeMethod(postMethod); 339 postMethod.releaseConnection(); 340 if( statusCode1 == 200) 341 { 342 inReq.putPageValue("message", "Push is completed." ); 343 } 344 else 345 { 346 inReq.putPageValue("message", "Status code: <b>" + statusCode1 + "</b><br>" + postMethod.getResponseBodyAsString() ); 347 } 348 } 349 public File backUpDirectory(WebPageRequest inReq) throws Exception  350 { 351 Backup backup = new Backup(); 352 backup.setRoot(getRoot()); 353 String subdir = inReq.getRequiredParameter("directory"); 354 backup.setIncludePath(subdir); 355 backup.setPageManager(getPageManager()); 356 357 String backupName = inReq.getUserName() + subdir; 359 File results = backup.backupCurrentSite(backupName); 360 inReq.putPageValue("result", results); 361 return results; 362 } 363 public List listVersions(WebPageRequest inReq) throws Exception  364 { 365 Backup backup = new Backup(); 366 backup.setRoot(getRoot()); 367 backup.setPageManager(getPageManager()); 368 List versions = backup.listSiteVersions(); 369 inReq.putPageValue("versionlist", versions); 370 return versions; 371 } 372 public void restoreVersion(WebPageRequest inReq) throws Exception  373 { 374 String name = inReq.getRequestParameter("versionid"); 375 if( name != null) 376 { 377 Backup backup = new Backup(); 379 backup.setRoot(getRoot()); 380 backup.setPageManager(getPageManager()); 381 File version = backup.loadVersion( name ); 382 if ( version == null) 383 { 384 log.error("No such backup found " + name); 385 inReq.putPageValue("error", "No such file"); 386 } 387 else 388 { 389 backup.restoreBackup(version); 390 } 391 } 392 else 393 { 394 log.error("No versionid parameter"); 395 } 396 397 } 398 public void receivePush(WebPageRequest inReq) throws Exception  399 { 400 FileUpload command = new FileUpload(); 402 command.setPageManager(getPageManager()); 403 Map properties = command.parseArguments(inReq); 404 if ( properties == null) 405 { 406 log.error("Nothing posted"); 407 return; 408 } 409 String username = (String )properties.get("username"); 410 String password = (String )properties.get("password"); 411 412 User admin = getUserManager().getUser(username); 413 if ( !getUserManager().authenticate(admin, password) ) 414 { 415 throw new OpenEditException("Did not authenticate: " + username); 416 } 417 else 418 { 419 inReq.setUser(admin); 420 } 421 String id = (String )properties.get("savedas"); 422 if ( id != null && id.length() > 0) 423 { 424 String path = "/WEB-INF/versions/" +id; 425 Page page = command.saveFile(properties,path,inReq); 426 if ( page.exists() && page.getContentItem().getLength() > 5000) 427 { 428 inReq.setRequestParameter("versionid",id); 432 restoreVersion(inReq); 433 } 434 else 435 { 436 log.error("Page did not save " + path); 437 } 438 } 439 else 440 { 441 log.error("No ID found"); 442 } 443 } 444 445 } | Popular Tags |