1 31 32 package org.opencms.importexport; 33 34 import org.opencms.db.CmsUserSettings; 35 import org.opencms.file.CmsGroup; 36 import org.opencms.file.CmsObject; 37 import org.opencms.file.CmsProperty; 38 import org.opencms.file.CmsPropertyDefinition; 39 import org.opencms.file.CmsResource; 40 import org.opencms.file.types.CmsResourceTypePointer; 41 import org.opencms.i18n.CmsMessageContainer; 42 import org.opencms.i18n.I_CmsMessageBundle; 43 import org.opencms.main.CmsException; 44 import org.opencms.main.CmsLog; 45 import org.opencms.main.OpenCms; 46 import org.opencms.report.I_CmsReport; 47 import org.opencms.security.CmsAccessControlEntry; 48 import org.opencms.util.CmsFileUtil; 49 import org.opencms.util.CmsStringUtil; 50 import org.opencms.util.CmsUUID; 51 52 import java.io.ByteArrayInputStream ; 53 import java.io.File ; 54 import java.io.FileNotFoundException ; 55 import java.io.IOException ; 56 import java.io.InputStream ; 57 import java.io.ObjectInputStream ; 58 import java.util.ArrayList ; 59 import java.util.HashMap ; 60 import java.util.Iterator ; 61 import java.util.List ; 62 import java.util.Locale ; 63 import java.util.Map ; 64 import java.util.Stack ; 65 import java.util.zip.ZipEntry ; 66 import java.util.zip.ZipException ; 67 import java.util.zip.ZipFile ; 68 69 import org.apache.commons.codec.binary.Base64; 70 import org.apache.commons.logging.Log; 71 72 import org.dom4j.Attribute; 73 import org.dom4j.Document; 74 import org.dom4j.Element; 75 76 91 92 public abstract class A_CmsImport implements I_CmsImport { 93 94 95 public static final String RESOURCE_TYPE_LEGACY_PAGE_NAME = "page"; 96 97 98 protected static final int DEBUG = 0; 99 100 101 protected static final int RESOURCE_TYPE_LINK_ID = 1024; 102 103 104 protected static final String RESOURCE_TYPE_LINK_NAME = "link"; 105 106 107 protected static final int RESOURCE_TYPE_NEWPAGE_ID = 9; 108 109 110 protected static final String RESOURCE_TYPE_NEWPAGE_NAME = "newpage"; 111 112 113 private static final Log LOG = CmsLog.getLog(A_CmsImport.class); 114 115 116 protected CmsObject m_cms; 117 118 119 protected boolean m_convertToXmlPage; 120 121 122 protected Document m_docXml; 123 124 125 protected Stack m_groupsToCreate; 126 127 128 protected boolean m_importingChannelData; 129 130 131 protected String m_importPath; 132 133 134 protected File m_importResource; 135 136 137 protected ZipFile m_importZip; 138 139 140 protected Map m_linkPropertyStorage; 141 142 143 protected Map m_linkStorage; 144 145 146 protected I_CmsReport m_report; 147 148 149 protected I_CmsMessageBundle m_userMessages; 150 151 157 public String convertDigestEncoding(String value) { 158 159 byte[] data = new byte[value.length() / 2]; 160 161 for (int i = 0; i < data.length; i++) { 162 data[i] = (byte)(Integer.parseInt(value.substring(i * 2, i * 2 + 2), 16) - 128); 163 } 164 165 return new String (Base64.encodeBase64(data)); 166 } 167 168 175 protected boolean checkImmutable(String translatedName, List immutableResources) { 176 177 boolean resourceNotImmutable = true; 178 if (immutableResources.contains(translatedName)) { 179 if (LOG.isDebugEnabled()) { 180 LOG.debug(Messages.get().getBundle().key( 181 Messages.LOG_IMPORTEXPORT_RESOURCENAME_IMMUTABLE_1, 182 translatedName)); 183 } 184 m_cms.getRequestContext().saveSiteRoot(); 186 try { 187 m_cms.getRequestContext().setSiteRoot("/"); 188 m_cms.readResource(translatedName); 189 resourceNotImmutable = false; 190 if (LOG.isDebugEnabled()) { 191 LOG.debug(Messages.get().getBundle().key( 192 Messages.LOG_IMPORTEXPORT_IMMUTABLE_FLAG_SET_1, 193 translatedName)); 194 } 195 } catch (CmsException e) { 196 if (LOG.isDebugEnabled()) { 198 LOG.debug(Messages.get().getBundle().key( 199 Messages.LOG_IMPORTEXPORT_ERROR_ON_TEST_IMMUTABLE_1, 200 translatedName), e); 201 } 202 } finally { 203 m_cms.getRequestContext().restoreSiteRoot(); 204 } 205 } 206 return resourceNotImmutable; 207 } 208 209 215 protected void cleanUp() { 216 217 m_importResource = null; 218 m_importZip = null; 219 m_report = null; 220 m_linkStorage = null; 221 m_linkPropertyStorage = null; 222 m_groupsToCreate = null; 223 m_cms = null; 224 } 225 226 229 protected void convertPointerToSiblings() { 230 231 Iterator keys = m_linkStorage.keySet().iterator(); 232 int linksSize = m_linkStorage.size(); 233 int i = 0; 234 CmsResource resource = null; 235 String link = null; 236 String key = null; 237 238 try { 239 while (keys.hasNext()) { 241 242 try { 243 key = (String )keys.next(); 244 link = (String )m_linkStorage.get(key); 245 List properties = (List )m_linkPropertyStorage.get(key); 246 CmsProperty.setAutoCreatePropertyDefinitions(properties, true); 247 248 m_report.print(org.opencms.report.Messages.get().container( 249 org.opencms.report.Messages.RPT_SUCCESSION_2, 250 String.valueOf(++i), 251 String.valueOf(linksSize)), I_CmsReport.FORMAT_NOTE); 252 m_report.print(Messages.get().container(Messages.RPT_CONVERT_LINK_0), I_CmsReport.FORMAT_NOTE); 253 m_report.print(org.opencms.report.Messages.get().container( 254 org.opencms.report.Messages.RPT_ARGUMENT_1, 255 key + " ")); 256 m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0)); 257 258 if (link.startsWith("/")) { 260 CmsResource target = m_cms.readResource(link); 262 263 resource = new CmsResource( 265 new CmsUUID(), target.getResourceId(), 267 key, 268 target.getTypeId(), 269 target.isFolder(), 270 0, 271 m_cms.getRequestContext().currentProject().getId(), CmsResource.STATE_NEW, 273 target.getDateCreated(), 274 target.getUserCreated(), 275 target.getDateLastModified(), 276 target.getUserLastModified(), 277 CmsResource.DATE_RELEASED_DEFAULT, 278 CmsResource.DATE_EXPIRED_DEFAULT, 279 1, 280 0); 281 282 m_cms.importResource(key, resource, null, properties); 283 m_report.println(org.opencms.report.Messages.get().container( 284 org.opencms.report.Messages.RPT_OK_0), I_CmsReport.FORMAT_OK); 285 286 if (LOG.isInfoEnabled()) { 287 LOG.info(Messages.get().getBundle().key( 288 Messages.LOG_CONVERT_LINK_DOTS_OK_3, 289 String.valueOf(i), 290 String.valueOf(linksSize), 291 key)); 292 } 293 294 } else { 295 296 m_cms.createResource(key, CmsResourceTypePointer.getStaticTypeId(), link.getBytes(), properties); 297 m_report.println(org.opencms.report.Messages.get().container( 298 org.opencms.report.Messages.RPT_OK_0), I_CmsReport.FORMAT_OK); 299 300 if (LOG.isInfoEnabled()) { 301 LOG.info(Messages.get().getBundle().key( 302 Messages.LOG_CONVERT_LINK_OK_3, 303 String.valueOf(i), 304 String.valueOf(linksSize), 305 key)); 306 } 307 308 } 309 } catch (CmsException e) { 310 m_report.println(); 311 m_report.print( 312 Messages.get().container(Messages.RPT_CONVERT_LINK_NOTFOUND_1, link), 313 I_CmsReport.FORMAT_WARNING); 314 315 if (LOG.isErrorEnabled()) { 316 LOG.error(Messages.get().getBundle().key( 317 Messages.ERR_IMPORTEXPORT_LINK_CONVERSION_FAILED_2, 318 key, 319 link), e); 320 } 321 } 322 } 323 } finally { 324 if (m_linkStorage != null) { 325 m_linkStorage.clear(); 326 } 327 m_linkStorage = null; 328 329 if (m_linkPropertyStorage != null) { 330 m_linkPropertyStorage.clear(); 331 } 332 m_linkPropertyStorage = null; 333 } 334 } 335 336 342 protected byte[] getFileBytes(String filename) { 343 344 try { 345 if (m_importZip != null) { 347 ZipEntry entry = m_importZip.getEntry(filename); 349 350 if (entry == null && filename.startsWith("/")) { 352 entry = m_importZip.getEntry(filename.substring(1)); 353 } 354 if (entry == null) { 355 throw new ZipException (Messages.get().getBundle().key( 356 Messages.LOG_IMPORTEXPORT_FILE_NOT_FOUND_IN_ZIP_1, 357 filename)); 358 } 359 360 InputStream stream = m_importZip.getInputStream(entry); 361 int size = new Long (entry.getSize()).intValue(); 362 return CmsFileUtil.readFully(stream, size); 363 } else { 364 File file = new File (m_importResource, filename); 366 return CmsFileUtil.readFile(file); 367 } 368 } catch (FileNotFoundException fnfe) { 369 if (LOG.isErrorEnabled()) { 370 LOG.error(Messages.get().getBundle().key(Messages.ERR_IMPORTEXPORT_FILE_NOT_FOUND_1, filename), fnfe); 371 } 372 m_report.println(fnfe); 373 } catch (IOException ioe) { 374 if (LOG.isErrorEnabled()) { 375 LOG.error(Messages.get().getBundle().key(Messages.ERR_IMPORTEXPORT_ERROR_READING_FILE_1, filename), ioe); 376 } 377 m_report.println(ioe); 378 } 379 return "".getBytes(); 381 } 382 383 394 protected CmsAccessControlEntry getImportAccessControlEntry( 395 CmsResource res, 396 String id, 397 String allowed, 398 String denied, 399 String flags) { 400 401 return new CmsAccessControlEntry( 402 res.getResourceId(), 403 new CmsUUID(id), 404 Integer.parseInt(allowed), 405 Integer.parseInt(denied), 406 Integer.parseInt(flags)); 407 } 408 409 417 protected Locale getLocale(String destination, List properties) { 418 419 String localeName = CmsProperty.get(CmsPropertyDefinition.PROPERTY_LOCALE, properties).getValue(); 420 421 if (localeName != null) { 422 return (Locale )OpenCms.getLocaleManager().getAvailableLocales(localeName).get(0); 424 } 425 return (Locale )OpenCms.getLocaleManager().getDefaultLocales(m_cms, CmsResource.getParentFolder(destination)).get( 427 0); 428 } 429 430 436 protected void importAccessControlEntries(CmsResource resource, List aceList) { 437 438 if (aceList.size() == 0) { 439 return; 441 } 442 try { 443 m_cms.importAccessControlEntries(resource, aceList); 444 } catch (CmsException exc) { 445 446 m_report.println( 447 Messages.get().container(Messages.RPT_IMPORT_ACL_DATA_FAILED_0), 448 I_CmsReport.FORMAT_WARNING); 449 } 450 } 451 452 462 protected void importGroup(String name, String description, String flags, String parentgroupName) 463 throws CmsImportExportException { 464 465 if (description == null) { 466 description = ""; 467 } 468 469 CmsGroup parentGroup = null; 470 try { 471 if (CmsStringUtil.isNotEmpty(parentgroupName)) { 472 try { 473 parentGroup = m_cms.readGroup(parentgroupName); 474 } catch (CmsException exc) { 475 } 477 } 478 479 if (CmsStringUtil.isNotEmpty(parentgroupName) && (parentGroup == null)) { 480 Map groupData = new HashMap (); 482 groupData.put(CmsImportExportManager.N_NAME, name); 483 groupData.put(CmsImportExportManager.N_DESCRIPTION, description); 484 groupData.put(CmsImportExportManager.N_FLAGS, flags); 485 groupData.put(CmsImportExportManager.N_PARENTGROUP, parentgroupName); 486 m_groupsToCreate.push(groupData); 487 } else { 488 try { 489 m_report.print(Messages.get().container(Messages.RPT_IMPORT_GROUP_0), I_CmsReport.FORMAT_NOTE); 490 m_report.print(org.opencms.report.Messages.get().container( 491 org.opencms.report.Messages.RPT_ARGUMENT_1, 492 name)); 493 m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0)); 494 m_cms.createGroup(name, description, Integer.parseInt(flags), parentgroupName); 495 m_report.println( 496 org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0), 497 I_CmsReport.FORMAT_OK); 498 } catch (CmsException exc) { 499 500 m_report.println(Messages.get().container(Messages.RPT_NOT_CREATED_0), I_CmsReport.FORMAT_OK); 501 } 502 } 503 504 } catch (Exception e) { 505 506 m_report.println(e); 507 508 CmsMessageContainer message = Messages.get().container( 509 Messages.ERR_IMPORTEXPORT_ERROR_IMPORTING_GROUP_1, 510 name); 511 if (LOG.isDebugEnabled()) { 512 LOG.debug(message.key(), e); 513 } 514 throw new CmsImportExportException(message, e); 515 } 516 } 517 518 523 protected void importGroups() throws CmsImportExportException { 524 525 List groupNodes; 526 Element currentElement; 527 String name, description, flags, parentgroup; 528 try { 529 groupNodes = m_docXml.selectNodes("//" + CmsImportExportManager.N_GROUPDATA); 531 for (int i = 0; i < groupNodes.size(); i++) { 533 currentElement = (Element)groupNodes.get(i); 534 name = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_NAME); 535 name = OpenCms.getImportExportManager().translateGroup(name); 536 description = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_DESCRIPTION); 537 flags = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_FLAGS); 538 parentgroup = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_PARENTGROUP); 539 if ((parentgroup != null) && (parentgroup.length() > 0)) { 540 parentgroup = OpenCms.getImportExportManager().translateGroup(parentgroup); 541 } 542 544 importGroup(name, description, flags, parentgroup); 545 } 546 547 while (!m_groupsToCreate.empty()) { 549 Stack tempStack = m_groupsToCreate; 550 m_groupsToCreate = new Stack (); 551 while (tempStack.size() > 0) { 552 Map groupdata = (HashMap )tempStack.pop(); 553 name = (String )groupdata.get(CmsImportExportManager.N_NAME); 554 description = (String )groupdata.get(CmsImportExportManager.N_DESCRIPTION); 555 flags = (String )groupdata.get(CmsImportExportManager.N_FLAGS); 556 parentgroup = (String )groupdata.get(CmsImportExportManager.N_PARENTGROUP); 557 importGroup(name, description, flags, parentgroup); 559 } 560 } 561 } catch (CmsImportExportException e) { 562 563 throw e; 564 } catch (Exception e) { 565 566 m_report.println(e); 567 568 CmsMessageContainer message = Messages.get().container(Messages.ERR_IMPORTEXPORT_ERROR_IMPORTING_GROUPS_0); 569 if (LOG.isDebugEnabled()) { 570 LOG.debug(message.key(), e); 571 } 572 573 throw new CmsImportExportException(message, e); 574 } 575 } 576 577 593 protected void importUser( 594 String name, 595 String description, 596 String flags, 597 String password, 598 String firstname, 599 String lastname, 600 String email, 601 String address, 602 String type, 603 Map userInfo, 604 List userGroups) throws CmsImportExportException { 605 606 String id = new CmsUUID().toString(); 608 try { 609 try { 610 m_report.print(Messages.get().container(Messages.RPT_IMPORT_USER_0), I_CmsReport.FORMAT_NOTE); 611 m_report.print(org.opencms.report.Messages.get().container( 612 org.opencms.report.Messages.RPT_ARGUMENT_1, 613 name)); 614 m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0)); 615 m_cms.importUser( 616 id, 617 name, 618 password, 619 description, 620 firstname, 621 lastname, 622 email, 623 address, 624 Integer.parseInt(flags), 625 Integer.parseInt(type), 626 userInfo); 627 for (int i = 0; i < userGroups.size(); i++) { 629 try { 630 m_cms.addUserToGroup(name, (String )userGroups.get(i)); 631 } catch (CmsException exc) { 632 } 634 } 635 m_report.println( 636 org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0), 637 I_CmsReport.FORMAT_OK); 638 } catch (CmsException exc) { 639 m_report.println(Messages.get().container(Messages.RPT_NOT_CREATED_0), I_CmsReport.FORMAT_OK); 640 } 641 } catch (Exception e) { 642 643 m_report.println(e); 644 645 CmsMessageContainer message = Messages.get().container( 646 Messages.ERR_IMPORTEXPORT_ERROR_IMPORTING_USER_1, 647 name); 648 if (LOG.isDebugEnabled()) { 649 LOG.debug(message.key(), e); 650 } 651 throw new CmsImportExportException(message, e); 652 } 653 } 654 655 660 protected void importUsers() throws CmsImportExportException { 661 662 List userNodes; 663 List groupNodes; 664 List userGroups; 665 Element currentElement, currentGroup; 666 Map userInfo = new HashMap (); 667 String name, description, flags, password, firstname, lastname, email, address, type, pwd, infoNode, defaultGroup; 668 try { 671 userNodes = m_docXml.selectNodes("//" + CmsImportExportManager.N_USERDATA); 673 for (int i = 0; i < userNodes.size(); i++) { 675 currentElement = (Element)userNodes.get(i); 676 name = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_NAME); 677 name = OpenCms.getImportExportManager().translateUser(name); 678 pwd = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_PASSWORD); 680 password = new String (Base64.decodeBase64(pwd.trim().getBytes())); 681 description = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_DESCRIPTION); 682 flags = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_FLAGS); 683 firstname = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_FIRSTNAME); 684 lastname = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_LASTNAME); 685 email = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_EMAIL); 686 address = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_TAG_ADDRESS); 687 type = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_TYPE); 688 defaultGroup = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_DEFAULTGROUP); 689 infoNode = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_USERINFO); 691 try { 692 byte[] value = getFileBytes(infoNode); 694 ByteArrayInputStream bin = new ByteArrayInputStream (value); 696 ObjectInputStream oin = new ObjectInputStream (bin); 697 userInfo = (Map )oin.readObject(); 698 } catch (IOException ioex) { 699 m_report.println(ioex); 700 } 701 702 groupNodes = currentElement.selectNodes("*/" + CmsImportExportManager.N_GROUPNAME); 704 userGroups = new ArrayList (); 705 for (int j = 0; j < groupNodes.size(); j++) { 706 currentGroup = (Element)groupNodes.get(j); 707 String userInGroup = CmsImport.getChildElementTextValue(currentGroup, CmsImportExportManager.N_NAME); 708 userInGroup = OpenCms.getImportExportManager().translateGroup(userInGroup); 709 userGroups.add(userInGroup); 710 } 711 712 if (CmsStringUtil.isNotEmpty(defaultGroup)) { 713 userInfo.put(CmsUserSettings.ADDITIONAL_INFO_DEFAULTGROUP, defaultGroup); 714 } 715 716 importUser( 718 name, 719 description, 720 flags, 721 password, 722 firstname, 723 lastname, 724 email, 725 address, 726 type, 727 userInfo, 728 userGroups); 729 } 730 } catch (CmsImportExportException e) { 731 732 throw e; 733 } catch (Exception e) { 734 735 m_report.println(e); 736 737 CmsMessageContainer message = Messages.get().container(Messages.ERR_IMPORTEXPORT_ERROR_IMPORTING_USERS_0); 738 if (LOG.isDebugEnabled()) { 739 LOG.debug(message.key(), e); 740 } 741 742 throw new CmsImportExportException(message, e); 743 } 744 } 745 746 752 protected void initialize() { 753 754 m_groupsToCreate = new Stack (); 755 } 756 757 765 protected List readPropertiesFromManifest(Element parentElement, List ignoredPropertyKeys) { 766 767 Map properties = new HashMap (); 769 CmsProperty property = null; 770 List propertyElements = parentElement.selectNodes("./" 771 + CmsImportExportManager.N_PROPERTIES 772 + "/" 773 + CmsImportExportManager.N_PROPERTY); 774 Element propertyElement = null; 775 String key = null, value = null; 776 Attribute attrib = null; 777 778 for (int i = 0, n = propertyElements.size(); i < n; i++) { 780 propertyElement = (Element)propertyElements.get(i); 781 key = CmsImport.getChildElementTextValue(propertyElement, CmsImportExportManager.N_NAME); 782 783 if (key == null || ignoredPropertyKeys.contains(key)) { 784 continue; 786 } 787 788 property = (CmsProperty)properties.get(key); 790 if (property == null) { 791 property = new CmsProperty(); 792 property.setName(key); 793 property.setAutoCreatePropertyDefinition(true); 794 properties.put(key, property); 795 } 796 797 value = CmsImport.getChildElementTextValue(propertyElement, CmsImportExportManager.N_VALUE); 798 if (value == null) { 799 value = ""; 800 } 801 802 attrib = propertyElement.attribute(CmsImportExportManager.N_PROPERTY_ATTRIB_TYPE); 803 if ((attrib != null) && attrib.getValue().equals(CmsImportExportManager.N_PROPERTY_ATTRIB_TYPE_SHARED)) { 804 property.setResourceValue(value); 806 } else { 807 property.setStructureValue(value); 809 } 810 } 811 812 return new ArrayList (properties.values()); 813 } 814 } | Popular Tags |