1 package com.dotmarketing.servlets; 2 3 import java.io.BufferedInputStream ; 4 import java.io.BufferedOutputStream ; 5 import java.io.File ; 6 import java.io.FileInputStream ; 7 import java.io.FileNotFoundException ; 8 import java.io.FileOutputStream ; 9 import java.io.FilenameFilter ; 10 import java.io.IOException ; 11 import java.io.InputStream ; 12 import java.io.OutputStream ; 13 import java.io.PrintWriter ; 14 import java.lang.reflect.InvocationTargetException ; 15 import java.util.ArrayList ; 16 import java.util.Date ; 17 import java.util.HashSet ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.Map ; 21 import java.util.Set ; 22 import java.util.zip.ZipEntry ; 23 import java.util.zip.ZipInputStream ; 24 import java.util.zip.ZipOutputStream ; 25 26 import javax.servlet.ServletConfig ; 27 import javax.servlet.ServletException ; 28 import javax.servlet.http.HttpServlet ; 29 import javax.servlet.http.HttpServletRequest ; 30 import javax.servlet.http.HttpServletResponse ; 31 import javax.servlet.http.HttpSession ; 32 33 import org.apache.commons.beanutils.BeanUtils; 34 35 import net.sf.hibernate.HibernateException; 36 import net.sf.hibernate.metadata.ClassMetadata; 37 38 import com.dotmarketing.beans.Clickstream; 39 import com.dotmarketing.beans.ClickstreamRequest; 40 import com.dotmarketing.beans.Inode; 41 import com.dotmarketing.cms.factories.PublicCompanyFactory; 42 import com.dotmarketing.cms.factories.PublicRoleFactory; 43 import com.dotmarketing.cms.factories.PublicUserFactory; 44 import com.dotmarketing.db.DotHibernate; 45 import com.dotmarketing.portlets.categories.model.Category; 46 import com.dotmarketing.util.Config; 47 import com.dotmarketing.util.UtilMethods; 48 import com.liferay.portal.model.Company; 49 import com.liferay.portal.model.Role; 50 import com.liferay.portal.model.User; 51 import com.oreilly.servlet.MultipartRequest; 52 import com.thoughtworks.xstream.XStream; 53 import com.thoughtworks.xstream.io.xml.DomDriver; 54 55 61 62 public class ImportExportXMLServlet extends HttpServlet { 63 66 String backupFilePath = "../backup"; 67 68 71 String backupTempFilePath = "../backup/temp"; 72 73 public void init(ServletConfig config) throws ServletException { 74 File f = new File (Config.CONTEXT.getRealPath(backupFilePath)); 76 f.mkdirs(); 77 f = new File (Config.CONTEXT.getRealPath(backupTempFilePath)); 78 f.mkdirs(); 79 deleteTempFiles(); 80 } 81 82 85 public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 86 87 String action = request.getParameter("action"); 88 HttpSession session = request.getSession(); 89 if (!session.isNew()) { 90 if ("upload".equals(action)) { 91 92 doUpload(request, response); 93 return; 94 } 95 96 99 if ("createZip".equals(action)) { 100 response.getWriter().println("Creating XML Files"); 101 createXMLFiles(); 102 String x = UtilMethods.dateToJDBC(new Date ()).replace(':', '-').replace(' ', '_'); 103 File zipFile = new File (Config.CONTEXT.getRealPath(backupFilePath + "/backup_" + x + "_.zip")); 104 response.getWriter().println("Zipping up to file:" + zipFile.getAbsolutePath()); 105 BufferedOutputStream bout = new BufferedOutputStream (new FileOutputStream (zipFile)); 106 107 zipTempDirectoryToStream(bout); 108 response.getWriter().println("Done."); 109 return; 110 } 111 112 115 if ("wipeOutDotCMSDatabase".equals(action)) { 116 response.getWriter().println("Deleting everything"); 117 118 } 119 120 123 if ("downloadZip".equals(action)) { 124 125 String x = UtilMethods.dateToJDBC(new Date ()).replace(':', '-').replace(' ', '_'); 126 File zipFile = new File (Config.CONTEXT.getRealPath(backupFilePath + "/backup_" + x + "_.zip")); 127 128 response.setHeader("Content-type", ""); 129 response.setHeader("Content-Disposition", "attachment; filename=" + zipFile.getName()); 130 131 createXMLFiles(); 132 133 zipTempDirectoryToStream(response.getOutputStream()); 134 return; 135 } 136 137 } 138 139 response.setContentType("text/html"); 140 PrintWriter out = response.getWriter(); 141 out.println("<html>"); 142 out.println("<head>"); 143 out.println("<title>"); 144 out.println("Import/Export dotCMS Content"); 145 out.println("</title>"); 146 out.println("<style type=\"text/css\">"); 147 out.println("@import \"/css/global.css\";"); 148 out.println("</style>"); 149 out.println("</head>"); 150 out.println("<body>"); 151 out.println("<h1>Import/Export dotCMS Content</h1><hr size='1'>"); 152 out.println("<ul>"); 153 out.println("<li>"); 154 out.println("<a HREF='?action=createZip'>Backup to Zip File</a>"); 155 out.println("</li>"); 156 out.println("<li>"); 157 out.println("<a HREF='?action=wipeOutDotCMSDatabase'>Delete the dotCMS database</a>"); 158 out.println("</li>"); 159 out.println("<li>"); 160 out.println("<a HREF='?action=downloadZip'>Download Zip File</a>"); 161 out.println("</li>"); 162 out.println("</ul>"); 163 out.println("<form method='post' action='?action=upload' enctype='multipart/form-data'>"); 164 out.println("<table cellpadding='4'><tr><td style='font-size:12px;'>file to import:</td><td><input type='file' name='fileUpload'></td></tr>"); 165 out.println("<tr><td></td><td>"); 166 out.println("<input type='submit' class=\"core-button\" value='upload xml or zip file'></td></tr></table></form>"); 167 out.println("</body>"); 168 out.println("</html>"); 169 } 170 171 179 private void doUpload(HttpServletRequest request, HttpServletResponse response) throws IOException { 180 deleteTempFiles(); 181 String tempdir = Config.CONTEXT.getRealPath(backupTempFilePath); 182 183 MultipartRequest mpr; 184 try { 185 mpr = new MultipartRequest(request, tempdir, 1000000000); 186 File importFile = mpr.getFile("fileUpload"); 187 188 191 if (importFile != null && importFile.getName().toLowerCase().endsWith(".zip")) { 192 193 InputStream in = new BufferedInputStream (new FileInputStream (importFile)); 194 ZipInputStream zin = new ZipInputStream (in); 195 ZipEntry e; 196 197 while ((e = zin.getNextEntry()) != null) { 198 unzip(zin, e.getName()); 199 } 200 zin.close(); 201 importFile.delete(); 202 } 203 204 } catch (IOException e) { 205 e.printStackTrace(); 207 } 208 File f = new File (Config.CONTEXT.getRealPath(backupTempFilePath)); 209 String [] _tempFiles = f.list(new XMLFileNameFilter()); 210 PrintWriter out = response.getWriter(); 211 out.println("<pre>Found " + _tempFiles.length + " files to import"); 212 for (int i = 0; i < _tempFiles.length; i++) { 213 File _importFile = new File (Config.CONTEXT.getRealPath(backupTempFilePath + "/" + _tempFiles[i])); 214 System.gc(); 215 doXMLFileImport(_importFile, out); 216 out.flush(); 217 } 218 out.println("Done Importing"); 219 220 } 221 222 232 233 private void doXMLFileImport(File f, PrintWriter out) { 234 BufferedInputStream _bin = null; 235 try { 236 XStream _xstream = null; 237 String _className = null; 238 Class _importClass = null; 239 DotHibernate _dh = null; 240 241 _className = f.getName().substring(0, f.getName().lastIndexOf(".")); 242 _xstream = new XStream(new DomDriver()); 243 _importClass = Class.forName(_className); 244 out.println("Importing:\t" + _className); 245 if (_importClass.equals(User.class)) { 246 247 } else if (_importClass.equals(Company.class)) { 248 249 } else if (_importClass.equals(Role.class)) { 250 251 } else { 252 253 _dh = new DotHibernate(_importClass); 254 _bin = new BufferedInputStream (new FileInputStream (f)); 255 List l = (List ) _xstream.fromXML(_bin); 256 out.println("Found :\t" + l.size() + " " + _className + "(s)"); 257 String id = _dh.getSession().getSessionFactory().getClassMetadata(_importClass).getIdentifierPropertyName(); 258 for (int j = 0; j < l.size(); j++) { 259 Object obj = l.get(j); 260 if (UtilMethods.isSet(id)) { 261 String prop = BeanUtils.getProperty(obj, id); 262 263 try { 264 Long myId = new Long (Long.parseLong(prop)); 265 _dh.saveWithPrimaryKey(obj, myId); 266 } catch (Exception e) { 267 _dh.saveWithPrimaryKey(obj, prop); 268 } 269 270 } else { 271 _dh.save(obj); 272 } 273 } 274 275 } 276 } catch (FileNotFoundException e) { 277 e.printStackTrace(); 279 } catch (ClassNotFoundException e1) { 280 e1.printStackTrace(); 281 } catch (HibernateException e) { 282 e.printStackTrace(); 284 } catch (IllegalAccessException e) { 285 e.printStackTrace(); 287 } catch (InvocationTargetException e) { 288 e.printStackTrace(); 290 } catch (NoSuchMethodException e) { 291 e.printStackTrace(); 293 } finally { 294 try { 295 if (_bin != null) { 296 _bin.close(); 297 } 298 } catch (IOException e) { 299 e.printStackTrace(); 301 } 302 } 303 304 } 305 306 314 private void createXMLFiles() throws ServletException , IOException { 315 316 deleteTempFiles(); 317 318 Set <Class > _tablesToDump = new HashSet <Class >(); 319 try { 320 321 322 Map map = DotHibernate.getSession().getSessionFactory().getAllClassMetadata(); 323 Iterator it = map.entrySet().iterator(); 324 while (it.hasNext()) { 325 Map.Entry pairs = (Map.Entry ) it.next(); 326 Class x = (Class ) pairs.getKey(); 327 if (!x.equals(Inode.class) && !x.equals(Clickstream.class) && !x.equals(ClickstreamRequest.class)) 328 _tablesToDump.add(x); 329 330 } 331 XStream _xstream = null; 332 DotHibernate _dh = null; 333 List _list = null; 334 File _writing = null; 335 BufferedOutputStream _bout = null; 336 337 for (Class clazz : _tablesToDump) { 338 _xstream = new XStream(new DomDriver()); 339 340 345 346 _writing = new File (Config.CONTEXT.getRealPath(backupTempFilePath + "/" + clazz.getName() + ".xml")); 347 _bout = new BufferedOutputStream (new FileOutputStream (_writing)); 348 _dh = new DotHibernate(clazz); 349 _dh.setQuery("from " + clazz.getName()); 350 351 _list = _dh.list(); 352 System.out.println("writing : " + _list.size() + " records to " + clazz.getName()); 353 _xstream.toXML(_list, _bout); 354 355 _bout.close(); 356 _list = null; 357 _dh = null; 358 _bout = null; 359 System.gc(); 360 } 361 362 363 364 _list = PublicCompanyFactory.getCompanies(); 365 _xstream = new XStream(new DomDriver()); 366 _writing = new File (Config.CONTEXT.getRealPath(backupTempFilePath + "/" + Company.class.getName() + ".xml")); 367 _bout = new BufferedOutputStream (new FileOutputStream (_writing)); 368 _xstream.toXML(_list, _bout); 369 _bout.close(); 370 _list = null; 371 _bout = null; 372 373 374 _list = PublicUserFactory.getAllUsers(); 375 _xstream = new XStream(new DomDriver()); 376 _writing = new File (Config.CONTEXT.getRealPath(backupTempFilePath + "/" + User.class.getName() + ".xml")); 377 _bout = new BufferedOutputStream (new FileOutputStream (_writing)); 378 _xstream.toXML(_list, _bout); 379 _bout.close(); 380 _list = null; 381 _bout = null; 382 383 384 _list = PublicRoleFactory.getAllRoles(); 385 _xstream = new XStream(new DomDriver()); 386 _writing = new File (Config.CONTEXT.getRealPath(backupTempFilePath + "/" + Role.class.getName() + ".xml")); 387 _bout = new BufferedOutputStream (new FileOutputStream (_writing)); 388 _xstream.toXML(_list, _bout); 389 _bout.close(); 390 _list = null; 391 _bout = null; 392 393 394 395 } catch (HibernateException e) { 396 397 e.printStackTrace(); 398 } 399 400 } 401 402 410 private void zipTempDirectoryToStream(OutputStream out) throws IOException { 411 412 byte b[] = new byte[512]; 413 ZipOutputStream zout = new ZipOutputStream (out); 414 File f = new File (Config.CONTEXT.getRealPath(backupTempFilePath)); 415 String [] s = f.list(); 416 for (int i = 0; i < s.length; i++) { 417 InputStream in = new BufferedInputStream (new FileInputStream (f = new File (Config.CONTEXT.getRealPath(backupTempFilePath + "/" + s[i])))); 418 ZipEntry e = new ZipEntry (s[i].replace(File.separatorChar, '/')); 419 zout.putNextEntry(e); 420 int len = 0; 421 while ((len = in.read(b)) != -1) { 422 zout.write(b, 0, len); 423 } 424 zout.closeEntry(); 425 in.close(); 426 } 427 zout.close(); 428 out.close(); 429 } 430 431 435 private void deleteTempFiles() { 436 File f = new File (Config.CONTEXT.getRealPath(backupTempFilePath)); 437 String [] _tempFiles = f.list(); 438 for (int i = 0; i < _tempFiles.length; i++) { 439 f = new File (Config.CONTEXT.getRealPath(backupTempFilePath + "/" + _tempFiles[i])); 440 f.delete(); 441 } 442 443 } 444 445 455 private void unzip(ZipInputStream zin, String s) throws IOException { 456 System.out.println("unzipping " + s); 457 File f = new File (Config.CONTEXT.getRealPath(backupTempFilePath + "/" + s)); 458 BufferedOutputStream out = new BufferedOutputStream (new FileOutputStream (f)); 459 byte[] b = new byte[512]; 460 int len = 0; 461 while ((len = zin.read(b)) != -1) { 462 out.write(b, 0, len); 463 } 464 out.close(); 465 } 466 467 473 private class XMLFileNameFilter implements FilenameFilter { 474 475 public boolean accept(File f, String s) { 476 if (s.toLowerCase().endsWith(".xml")) { 477 return true; 478 } else { 479 return false; 480 } 481 } 482 483 } 484 485 489 private void deleteDotCMS() { 490 try { 491 492 Set <Class > _tablesToDump = new HashSet <Class >(); 493 Map map; 494 495 map = DotHibernate.getSession().getSessionFactory().getAllClassMetadata(); 496 497 Iterator it = map.entrySet().iterator(); 498 while (it.hasNext()) { 499 Map.Entry pairs = (Map.Entry ) it.next(); 500 ClassMetadata cmd = (ClassMetadata) pairs.getValue(); 501 502 } 503 } catch (HibernateException e) { 504 e.printStackTrace(); 506 } 507 508 } 509 } 510 | Popular Tags |