1 31 32 package org.opencms.importexport; 33 34 import org.opencms.configuration.CmsConfigurationException; 35 import org.opencms.file.CmsObject; 36 import org.opencms.i18n.CmsMessageContainer; 37 import org.opencms.main.CmsEvent; 38 import org.opencms.main.CmsException; 39 import org.opencms.main.CmsLog; 40 import org.opencms.main.I_CmsEventListener; 41 import org.opencms.main.OpenCms; 42 import org.opencms.report.I_CmsReport; 43 import org.opencms.security.CmsRole; 44 import org.opencms.security.CmsRoleViolationException; 45 import org.opencms.security.I_CmsPrincipal; 46 import org.opencms.xml.CmsXmlException; 47 48 import java.io.BufferedReader ; 49 import java.io.File ; 50 import java.io.FileReader ; 51 import java.io.InputStream ; 52 import java.io.Reader ; 53 import java.util.ArrayList ; 54 import java.util.Collections ; 55 import java.util.HashMap ; 56 import java.util.List ; 57 import java.util.Map ; 58 import java.util.zip.ZipEntry ; 59 import java.util.zip.ZipFile ; 60 61 import org.apache.commons.logging.Log; 62 63 import org.dom4j.Document; 64 import org.dom4j.io.SAXReader; 65 66 77 public class CmsImportExportManager { 78 79 80 public static final String EXPORT_MANIFEST = "manifest.xml"; 81 82 83 public static final String EXPORT_VERSION = "4"; 84 85 90 public static final String EXPORT_XMLFILENAME = EXPORT_MANIFEST; 91 92 93 public static final String N_ACCESS = "access"; 94 95 96 public static final String N_ACCESSCONTROL_ALLOWEDPERMISSIONS = "allowed"; 97 98 99 public static final String N_ACCESSCONTROL_DENIEDPERMISSIONS = "denied"; 100 101 102 public static final String N_ACCESSCONTROL_ENTRIES = "accesscontrol"; 103 104 105 public static final String N_ACCESSCONTROL_ENTRY = "accessentry"; 106 107 108 public static final String N_ACCESSCONTROL_PERMISSIONSET = "permissionset"; 109 110 111 public static final String N_ACCESSCONTROL_PRINCIPAL = "uuidprincipal"; 112 113 114 public static final String N_CREATOR = "creator"; 115 116 117 public static final String N_DATE = "createdate"; 118 119 120 public static final String N_DATECREATED = "datecreated"; 121 122 123 public static final String N_DATEEXPIRED = "dateexpired"; 124 125 126 public static final String N_DATELASTMODIFIED = "datelastmodified"; 127 128 129 public static final String N_DATERELEASED = "datereleased"; 130 131 132 public static final String N_DEFAULTGROUP = "defaultgroup"; 133 134 135 public static final String N_DESCRIPTION = "description"; 136 137 138 public static final String N_DESTINATION = "destination"; 139 140 141 public static final String N_EMAIL = "email"; 142 143 144 public static final String N_EXPORT = "export"; 145 146 147 public static final String N_FILE = "file"; 148 149 150 public static final String N_FIRSTNAME = "firstname"; 151 152 153 public static final String N_FLAGS = "flags"; 154 155 156 public static final String N_GROUPDATA = "groupdata"; 157 158 159 public static final String N_GROUPNAME = "groupname"; 160 161 162 public static final String N_ID = "id"; 163 164 165 public static final String N_INFO = "info"; 166 167 168 public static final String N_LASTMODIFIED = "lastmodified"; 169 170 171 public static final String N_LASTNAME = "lastname"; 172 173 174 public static final String N_NAME = "name"; 175 176 177 public static final String N_OC_VERSION = "opencms_version"; 178 179 180 public static final String N_PARENTGROUP = "parentgroup"; 181 182 183 public static final String N_PASSWORD = "password"; 184 185 186 public static final String N_PROJECT = "project"; 187 188 189 public static final String N_PROPERTIES = "properties"; 190 191 192 public static final String N_PROPERTY = "property"; 193 194 195 public static final String N_PROPERTY_ATTRIB_TYPE = "type"; 196 197 198 public static final String N_PROPERTY_ATTRIB_TYPE_SHARED = "shared"; 199 200 201 public static final String N_SOURCE = "source"; 202 203 204 public static final String N_TAG_ADDRESS = "address"; 205 206 207 public static final String N_TYPE = "type"; 208 209 210 public static final String N_USER = "user"; 211 212 213 public static final String N_USERCREATED = "usercreated"; 214 215 216 public static final String N_USERDATA = "userdata"; 217 218 219 public static final String N_USERGROUPDATA = "usergroupdata"; 220 221 222 public static final String N_USERGROUPS = "usergroups"; 223 224 225 public static final String N_USERINFO = "userinfo"; 226 227 228 public static final String N_USERLASTMODIFIED = "userlastmodified"; 229 230 231 public static final String N_UUIDRESOURCE = "uuidresource"; 232 233 234 public static final String N_UUIDSTRUCTURE = "uuidstructure"; 235 236 237 public static final String N_VALUE = "value"; 238 239 240 public static final String N_VERSION = "export_version"; 241 242 243 private static final Log LOG = CmsLog.getLog(CmsImportExportManager.class); 244 245 246 private boolean m_convertToXmlPage; 247 248 249 private List m_ignoredProperties; 250 251 252 private List m_immutableResources; 253 254 255 private List m_importExportHandlers; 256 257 258 private Map m_importGroupTranslations; 259 260 261 private Map m_importUserTranslations; 262 263 264 private List m_importVersionClasses; 265 266 267 private boolean m_overwriteCollidingResources; 268 269 270 private String m_webAppUrl; 271 272 275 public CmsImportExportManager() { 276 277 if (LOG.isInfoEnabled()) { 278 LOG.info(Messages.get().getBundle().key(Messages.INIT_IMPORTEXPORT_INITIALIZING_0)); 279 } 280 281 m_importExportHandlers = new ArrayList (); 282 m_immutableResources = new ArrayList (); 283 m_ignoredProperties = new ArrayList (); 284 m_convertToXmlPage = true; 285 m_importGroupTranslations = new HashMap (); 286 m_importUserTranslations = new HashMap (); 287 m_overwriteCollidingResources = true; 288 m_importVersionClasses = new ArrayList (); 289 } 290 291 300 public static Document getManifest(File resource) { 301 302 Document manifest = null; 303 ZipFile zipFile = null; 304 ZipEntry zipFileEntry = null; 305 InputStream input = null; 306 Reader reader = null; 307 SAXReader saxReader = null; 308 File manifestFile = null; 309 310 try { 311 if (resource.isFile()) { 312 if (!resource.getName().toLowerCase().endsWith(".zip")) { 313 return null; 315 } 316 zipFile = new ZipFile (resource); 318 zipFileEntry = zipFile.getEntry(EXPORT_MANIFEST); 319 input = zipFile.getInputStream(zipFileEntry); 320 saxReader = new SAXReader(); 322 manifest = saxReader.read(input); 323 } else if (resource.isDirectory()) { 324 manifestFile = new File (resource, EXPORT_MANIFEST); 326 reader = new BufferedReader (new FileReader (manifestFile)); 327 saxReader = new SAXReader(); 329 manifest = saxReader.read(reader); 330 } 331 } catch (Exception e) { 332 if (LOG.isDebugEnabled()) { 333 LOG.debug( 334 Messages.get().getBundle().key(Messages.LOG_IMPORTEXPORT_ERROR_READING_MANIFEST_1, resource), 335 e); 336 } 337 manifest = null; 338 } finally { 339 try { 340 if (reader != null) { 341 reader.close(); 342 } 343 } catch (Exception e) { 344 } 346 } 347 348 return manifest; 349 } 350 351 356 public void addIgnoredProperty(String propertyName) { 357 358 if (LOG.isDebugEnabled()) { 359 LOG.debug(Messages.get().getBundle().key(Messages.LOG_IMPORTEXPORT_IGNORING_PROPERTY_1, propertyName)); 360 } 361 m_ignoredProperties.add(propertyName); 362 } 363 364 370 public void addImmutableResource(String immutableResource) { 371 372 if (LOG.isDebugEnabled()) { 373 LOG.debug(Messages.get().getBundle().key( 374 Messages.LOG_IMPORTEXPORT_ADDED_IMMUTABLE_RESOURCE_1, 375 immutableResource)); 376 } 377 m_immutableResources.add(immutableResource); 378 } 379 380 385 public void addImportExportHandler(I_CmsImportExportHandler handler) { 386 387 if (LOG.isDebugEnabled()) { 388 LOG.debug(Messages.get().getBundle().key(Messages.LOG_IMPORTEXPORT_ADDED_IMPORTEXPORT_HANDLER_1, handler)); 389 } 390 m_importExportHandlers.add(handler); 391 } 392 393 400 public void addImportPrincipalTranslation(String type, String from, String to) { 401 402 if (LOG.isDebugEnabled()) { 403 LOG.debug(Messages.get().getBundle().key( 404 Messages.LOG_IMPORTEXPORT_ADDED_PRINCIPAL_TRANSLATION_3, 405 type, 406 from, 407 to)); 408 } 409 if (I_CmsPrincipal.PRINCIPAL_GROUP.equalsIgnoreCase(type)) { 410 m_importGroupTranslations.put(from, to); 411 if (LOG.isInfoEnabled()) { 412 LOG.info(Messages.get().getBundle().key(Messages.INIT_IMPORTEXPORT_ADDED_GROUP_TRANSLATION_2, from, to)); 413 } 414 } else if (I_CmsPrincipal.PRINCIPAL_USER.equalsIgnoreCase(type)) { 415 m_importUserTranslations.put(from, to); 416 if (LOG.isInfoEnabled()) { 417 LOG.info(Messages.get().getBundle().key(Messages.INIT_IMPORTEXPORT_ADDED_USER_TRANSLATION_2, from, to)); 418 } 419 } 420 } 421 422 427 public void addImportVersionClass(I_CmsImport importVersionClass) { 428 429 if (LOG.isDebugEnabled()) { 430 LOG.debug(Messages.get().getBundle().key( 431 Messages.LOG_IMPORTEXPORT_ADDED_IMPORT_VERSION_1, 432 importVersionClass)); 433 } 434 m_importVersionClasses.add(importVersionClass); 435 } 436 437 442 public boolean convertToXmlPage() { 443 444 return m_convertToXmlPage; 445 } 446 447 459 public void exportData(CmsObject cms, I_CmsImportExportHandler handler, I_CmsReport report) 460 throws CmsConfigurationException, CmsImportExportException, CmsRoleViolationException { 461 462 cms.checkRole(CmsRole.EXPORT_DATABASE); 463 handler.exportData(cms, report); 464 } 465 466 471 public List getIgnoredProperties() { 472 473 return m_ignoredProperties; 474 } 475 476 486 public List getImmutableResources() { 487 488 return m_immutableResources; 489 } 490 491 499 public I_CmsImportExportHandler getImportExportHandler(String importFile) throws CmsImportExportException { 500 501 Document manifest = null; 502 I_CmsImportExportHandler handler = null; 503 504 File file = new File (importFile); 505 if (!file.exists()) { 506 CmsMessageContainer message = Messages.get().container( 508 Messages.ERR_IMPORTEXPORT_ERROR_IMPORT_FILE_DOES_NOT_EXIST_1, 509 importFile); 510 if (LOG.isDebugEnabled()) { 511 LOG.debug(message.key()); 512 } 513 514 throw new CmsImportExportException(message); 515 } 516 517 manifest = getManifest(file); 518 for (int i = 0; i < m_importExportHandlers.size(); i++) { 519 handler = (I_CmsImportExportHandler)m_importExportHandlers.get(i); 520 if (handler.matches(manifest)) { 521 return handler; 522 } 523 524 handler = null; 525 } 526 527 if (handler == null) { 528 529 CmsMessageContainer message = Messages.get().container( 530 Messages.ERR_IMPORTEXPORT_ERROR_NO_HANDLER_FOUND_1, 531 importFile); 532 if (LOG.isDebugEnabled()) { 533 LOG.debug(message.key()); 534 } 535 536 throw new CmsImportExportException(message); 537 } 538 539 return null; 540 } 541 542 547 public List getImportExportHandlers() { 548 549 return m_importExportHandlers; 550 } 551 552 557 public Map getImportGroupTranslations() { 558 559 return m_importGroupTranslations; 560 } 561 562 567 public Map getImportUserTranslations() { 568 569 return m_importUserTranslations; 570 } 571 572 577 public List getImportVersionClasses() { 578 579 return m_importVersionClasses; 580 } 581 582 590 public String getOldWebAppUrl() { 591 592 return m_webAppUrl; 593 } 594 595 611 public void importData(CmsObject cms, String importFile, String importPath, I_CmsReport report) 612 throws CmsImportExportException, CmsXmlException, CmsRoleViolationException, CmsException { 613 614 cms.checkRole(CmsRole.IMPORT_DATABASE); 616 617 try { 618 OpenCms.fireCmsEvent(new CmsEvent(I_CmsEventListener.EVENT_CLEAR_CACHES, Collections.EMPTY_MAP)); 619 I_CmsImportExportHandler handler = getImportExportHandler(importFile); 620 handler.importData(cms, importFile, importPath, report); 621 } finally { 622 OpenCms.fireCmsEvent(new CmsEvent(I_CmsEventListener.EVENT_CLEAR_CACHES, Collections.EMPTY_MAP)); 623 } 624 } 625 626 632 public boolean overwriteCollidingResources() { 633 634 return m_overwriteCollidingResources; 635 } 636 637 642 public void setConvertToXmlPage(boolean convertToXmlPage) { 643 644 if (LOG.isDebugEnabled()) { 645 LOG.debug(Messages.get().getBundle().key( 646 Messages.LOG_IMPORTEXPORT_SET_CONVERT_PARAMETER_1, 647 Boolean.toString(convertToXmlPage))); 648 } 649 m_convertToXmlPage = convertToXmlPage; 650 } 651 652 657 public void setConvertToXmlPage(String convertToXmlPage) { 658 659 setConvertToXmlPage(Boolean.valueOf(convertToXmlPage).booleanValue()); 660 } 661 662 670 public void setOldWebAppUrl(String webAppUrl) { 671 672 if (LOG.isDebugEnabled()) { 673 LOG.debug(Messages.get().getBundle().key(Messages.LOG_IMPORTEXPORT_SET_OLD_WEBAPP_URL_1, webAppUrl)); 674 } 675 m_webAppUrl = webAppUrl; 676 } 677 678 693 public void setOverwriteCollidingResources(boolean overwriteCollidingResources) { 694 695 if (LOG.isDebugEnabled()) { 696 LOG.debug(Messages.get().getBundle().key( 697 Messages.LOG_IMPORTEXPORT_SET_OVERWRITE_PARAMETER_1, 698 Boolean.toString(overwriteCollidingResources))); 699 } 700 m_overwriteCollidingResources = overwriteCollidingResources; 701 } 702 703 708 public void setOverwriteCollidingResources(String overwriteCollidingResources) { 709 710 setOverwriteCollidingResources(Boolean.valueOf(overwriteCollidingResources).booleanValue()); 711 } 712 713 721 public String translateGroup(String name) { 722 723 if (m_importGroupTranslations == null) { 724 return name; 725 } 726 String match = (String )m_importGroupTranslations.get(name); 727 if (match != null) { 728 return match; 729 } else { 730 return name; 731 } 732 } 733 734 742 public String translateUser(String name) { 743 744 if (m_importUserTranslations == null) { 745 return name; 746 } 747 String match = (String )m_importUserTranslations.get(name); 748 if (match != null) { 749 return match; 750 } else { 751 return name; 752 } 753 } 754 755 758 protected void finalize() throws Throwable { 759 760 try { 761 if (m_immutableResources != null) { 762 m_immutableResources.clear(); 763 } 764 m_immutableResources = null; 765 766 if (m_ignoredProperties != null) { 767 m_ignoredProperties.clear(); 768 } 769 m_ignoredProperties = null; 770 } catch (Throwable t) { 771 } finally { 773 super.finalize(); 774 } 775 } 776 } | Popular Tags |