1 31 32 package org.opencms.importexport; 33 34 import org.opencms.file.CmsObject; 35 import org.opencms.file.CmsResource; 36 import org.opencms.file.types.I_CmsResourceType; 37 import org.opencms.i18n.CmsMessageContainer; 38 import org.opencms.main.CmsException; 39 import org.opencms.main.CmsLog; 40 import org.opencms.main.OpenCms; 41 import org.opencms.report.I_CmsReport; 42 import org.opencms.security.CmsRole; 43 import org.opencms.security.I_CmsPasswordHandler; 44 import org.opencms.security.I_CmsPrincipal; 45 import org.opencms.util.CmsDateUtil; 46 import org.opencms.util.CmsUUID; 47 48 import java.io.File ; 49 import java.text.ParseException ; 50 import java.util.ArrayList ; 51 import java.util.Collections ; 52 import java.util.HashMap ; 53 import java.util.List ; 54 import java.util.Map ; 55 import java.util.zip.ZipFile ; 56 57 import org.apache.commons.logging.Log; 58 59 import org.dom4j.Document; 60 import org.dom4j.Element; 61 62 77 public class CmsImportVersion4 extends A_CmsImport { 78 79 80 private static final int IMPORT_VERSION = 4; 81 82 83 private static final Log LOG = CmsLog.getLog(CmsImportVersion4.class); 84 85 88 public CmsImportVersion4() { 89 90 m_convertToXmlPage = true; 91 } 92 93 97 public int getVersion() { 98 99 return CmsImportVersion4.IMPORT_VERSION; 100 } 101 102 105 public void importResources( 106 CmsObject cms, 107 String importPath, 108 I_CmsReport report, 109 File importResource, 110 ZipFile importZip, 111 Document docXml) throws CmsImportExportException { 112 113 initialize(); 115 m_cms = cms; 116 m_importPath = importPath; 117 m_report = report; 118 m_importResource = importResource; 119 m_importZip = importZip; 120 m_docXml = docXml; 121 m_importingChannelData = false; 122 m_linkStorage = new HashMap (); 123 m_linkPropertyStorage = new HashMap (); 124 125 try { 126 if (cms.hasRole(CmsRole.ACCOUNT_MANAGER)) { 128 importGroups(); 129 importUsers(); 130 } 131 readResourcesFromManifest(); 133 convertPointerToSiblings(); 134 } finally { 135 cleanUp(); 136 } 137 } 138 139 155 protected void importUser( 156 String name, 157 String description, 158 String flags, 159 String password, 160 String firstname, 161 String lastname, 162 String email, 163 String address, 164 String type, 165 Map userInfo, 166 List userGroups) throws CmsImportExportException { 167 168 boolean convert = false; 169 170 Map config = OpenCms.getPasswordHandler().getConfiguration(); 171 if (config != null && config.containsKey(I_CmsPasswordHandler.CONVERT_DIGEST_ENCODING)) { 172 convert = Boolean.valueOf((String )config.get(I_CmsPasswordHandler.CONVERT_DIGEST_ENCODING)).booleanValue(); 173 } 174 175 if (convert) { 176 password = convertDigestEncoding(password); 177 } 178 179 super.importUser( 180 name, 181 description, 182 flags, 183 password, 184 firstname, 185 lastname, 186 email, 187 address, 188 type, 189 userInfo, 190 userGroups); 191 } 192 193 202 private long convertTimestamp(String timestamp) { 203 204 long value = 0; 205 try { 208 value = Long.parseLong(timestamp); 209 210 } catch (NumberFormatException e) { 211 try { 213 value = CmsDateUtil.parseHeaderDate(timestamp); 214 } catch (ParseException pe) { 215 value = System.currentTimeMillis(); 216 } 217 } 218 return value; 219 } 220 221 239 private CmsResource importResource( 240 String source, 241 String destination, 242 I_CmsResourceType type, 243 String uuidresource, 244 long datelastmodified, 245 String userlastmodified, 246 long datecreated, 247 String usercreated, 248 long datereleased, 249 long dateexpired, 250 String flags, 251 List properties) { 252 253 byte[] content = null; 254 CmsResource result = null; 255 256 try { 257 258 if (source != null) { 260 content = getFileBytes(source); 261 } 262 int size = 0; 263 if (content != null) { 264 size = content.length; 265 } 266 267 CmsUUID newUserlastmodified; 269 CmsUUID newUsercreated; 270 try { 273 newUserlastmodified = m_cms.readUser(userlastmodified).getId(); 274 } catch (CmsException e) { 275 newUserlastmodified = m_cms.getRequestContext().currentUser().getId(); 276 } 278 279 try { 280 newUsercreated = m_cms.readUser(usercreated).getId(); 281 } catch (CmsException e) { 282 newUsercreated = m_cms.getRequestContext().currentUser().getId(); 283 } 285 286 CmsUUID newUuidresource = null; 288 if ((uuidresource != null) && (!type.isFolder())) { 289 newUuidresource = new CmsUUID(uuidresource); 291 } else { 292 newUuidresource = new CmsUUID(); 294 } 295 296 CmsResource resource = new CmsResource( 298 new CmsUUID(), newUuidresource, 300 destination, 301 type.getTypeId(), 302 type.isFolder(), 303 new Integer (flags).intValue(), 304 m_cms.getRequestContext().currentProject().getId(), 305 CmsResource.STATE_NEW, 306 datecreated, 307 newUsercreated, 308 datelastmodified, 309 newUserlastmodified, 310 datereleased, 311 dateexpired, 312 1, 313 size); 314 315 result = m_cms.importResource(destination, resource, content, properties); 317 318 if (result != null) { 319 m_report.println( 320 org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0), 321 I_CmsReport.FORMAT_OK); 322 } 323 } catch (Exception exc) { 324 m_report.println(exc); 326 try { 327 Thread.sleep(1000); 329 } catch (Exception e) { 330 } 332 } 333 return result; 334 } 335 336 342 private void readResourcesFromManifest() throws CmsImportExportException { 343 344 String source = null, destination = null, uuidresource = null, userlastmodified = null, usercreated = null, flags = null, timestamp = null; 345 long datelastmodified = 0, datecreated = 0, datereleased = 0, dateexpired = 0; 346 347 List fileNodes = null, acentryNodes = null; 348 Element currentElement = null, currentEntry = null; 349 List properties = null; 350 351 if (m_importingChannelData) { 352 m_cms.getRequestContext().saveSiteRoot(); 353 m_cms.getRequestContext().setSiteRoot(CmsResource.VFS_FOLDER_CHANNELS); 354 } 355 356 List immutableResources = OpenCms.getImportExportManager().getImmutableResources(); 358 if (immutableResources == null) { 359 immutableResources = Collections.EMPTY_LIST; 360 } 361 if (LOG.isDebugEnabled()) { 362 LOG.debug(Messages.get().getBundle().key( 363 Messages.LOG_IMPORTEXPORT_IMMUTABLE_RESOURCES_SIZE_1, 364 Integer.toString(immutableResources.size()))); 365 } 366 List ignoredProperties = OpenCms.getImportExportManager().getIgnoredProperties(); 368 if (ignoredProperties == null) { 369 ignoredProperties = Collections.EMPTY_LIST; 370 } 371 372 m_convertToXmlPage = OpenCms.getImportExportManager().convertToXmlPage(); 374 375 try { 376 fileNodes = m_docXml.selectNodes("//" + CmsImportExportManager.N_FILE); 378 int importSize = fileNodes.size(); 379 380 for (int i = 0; i < fileNodes.size(); i++) { 382 m_report.print(org.opencms.report.Messages.get().container( 383 org.opencms.report.Messages.RPT_SUCCESSION_2, 384 String.valueOf(i + 1), 385 String.valueOf(importSize)), I_CmsReport.FORMAT_NOTE); 386 currentElement = (Element)fileNodes.get(i); 387 388 source = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_SOURCE); 390 392 destination = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_DESTINATION); 393 394 String typeName = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_TYPE); 396 I_CmsResourceType type = OpenCms.getResourceManager().getResourceType(typeName); 397 398 if (!type.isFolder()) { 399 uuidresource = CmsImport.getChildElementTextValue( 401 currentElement, 402 CmsImportExportManager.N_UUIDRESOURCE); 403 } else { 404 uuidresource = null; 405 } 406 407 timestamp = CmsImport.getChildElementTextValue( 409 currentElement, 410 CmsImportExportManager.N_DATELASTMODIFIED); 411 if (timestamp != null) { 412 datelastmodified = convertTimestamp(timestamp); 413 } else { 414 datelastmodified = System.currentTimeMillis(); 415 } 416 417 userlastmodified = CmsImport.getChildElementTextValue( 419 currentElement, 420 CmsImportExportManager.N_USERLASTMODIFIED); 421 userlastmodified = OpenCms.getImportExportManager().translateUser(userlastmodified); 422 423 timestamp = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_DATECREATED); 425 if (timestamp != null) { 426 datecreated = convertTimestamp(timestamp); 427 } else { 428 datecreated = System.currentTimeMillis(); 429 } 430 431 usercreated = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_USERCREATED); 433 usercreated = OpenCms.getImportExportManager().translateUser(usercreated); 434 435 timestamp = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_DATERELEASED); 437 if (timestamp != null) { 438 datereleased = convertTimestamp(timestamp); 439 } else { 440 datereleased = CmsResource.DATE_RELEASED_DEFAULT; 441 } 442 443 timestamp = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_DATEEXPIRED); 445 if (timestamp != null) { 446 dateexpired = convertTimestamp(timestamp); 447 } else { 448 dateexpired = CmsResource.DATE_EXPIRED_DEFAULT; 449 } 450 451 flags = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_FLAGS); 453 454 String translatedName = m_cms.getRequestContext().addSiteRoot(m_importPath + destination); 456 if (type.isFolder()) { 457 if (!CmsResource.isFolder(translatedName)) { 459 translatedName += "/"; 460 } 461 } 462 463 boolean resourceNotImmutable = checkImmutable(translatedName, immutableResources); 465 translatedName = m_cms.getRequestContext().removeSiteRoot(translatedName); 466 if (resourceNotImmutable) { 468 m_report.print(Messages.get().container(Messages.RPT_IMPORTING_0), I_CmsReport.FORMAT_NOTE); 470 m_report.print(org.opencms.report.Messages.get().container( 471 org.opencms.report.Messages.RPT_ARGUMENT_1, 472 translatedName)); 473 m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0)); 474 properties = readPropertiesFromManifest(currentElement, ignoredProperties); 476 477 CmsResource res = importResource( 479 source, 480 translatedName, 481 type, 482 uuidresource, 483 datelastmodified, 484 userlastmodified, 485 datecreated, 486 usercreated, 487 datereleased, 488 dateexpired, 489 flags, 490 properties); 491 492 if (res != null) { 494 495 List aceList = new ArrayList (); 496 497 acentryNodes = currentElement.selectNodes("*/" + CmsImportExportManager.N_ACCESSCONTROL_ENTRY); 499 500 for (int j = 0; j < acentryNodes.size(); j++) { 502 currentEntry = (Element)acentryNodes.get(j); 503 504 String id = CmsImport.getChildElementTextValue( 506 currentEntry, 507 CmsImportExportManager.N_ACCESSCONTROL_PRINCIPAL); 508 String principalId = new CmsUUID().toString(); 509 String principal = id.substring(id.indexOf('.') + 1, id.length()); 510 511 try { 512 if (id.startsWith(I_CmsPrincipal.PRINCIPAL_GROUP)) { 513 principal = OpenCms.getImportExportManager().translateGroup(principal); 514 principalId = m_cms.readGroup(principal).getId().toString(); 515 } else { 516 principal = OpenCms.getImportExportManager().translateUser(principal); 517 principalId = m_cms.readUser(principal).getId().toString(); 518 } 519 520 String acflags = CmsImport.getChildElementTextValue( 521 currentEntry, 522 CmsImportExportManager.N_FLAGS); 523 524 String allowed = ((Element)currentEntry.selectNodes( 525 "./" 526 + CmsImportExportManager.N_ACCESSCONTROL_PERMISSIONSET 527 + "/" 528 + CmsImportExportManager.N_ACCESSCONTROL_ALLOWEDPERMISSIONS).get(0)).getTextTrim(); 529 530 String denied = ((Element)currentEntry.selectNodes( 531 "./" 532 + CmsImportExportManager.N_ACCESSCONTROL_PERMISSIONSET 533 + "/" 534 + CmsImportExportManager.N_ACCESSCONTROL_DENIEDPERMISSIONS).get(0)).getTextTrim(); 535 536 aceList.add(getImportAccessControlEntry(res, principalId, allowed, denied, acflags)); 538 } catch (CmsException e) { 539 if (LOG.isWarnEnabled()) { 541 LOG.warn(Messages.get().getBundle().key( 542 Messages.LOG_IMPORTEXPORT_ERROR_IMPORTING_ACE_1, 543 translatedName), e); 544 } 545 m_report.println(e); 546 } 547 } 548 549 importAccessControlEntries(res, aceList); 550 551 if (LOG.isInfoEnabled()) { 552 LOG.info(Messages.get().getBundle().key( 553 Messages.LOG_IMPORTING_4, 554 new Object [] { 555 String.valueOf(i + 1), 556 String.valueOf(importSize), 557 translatedName, 558 destination})); 559 } 560 } else { 561 m_report.print(Messages.get().container(Messages.RPT_SKIPPING_0), I_CmsReport.FORMAT_NOTE); 563 m_report.println(org.opencms.report.Messages.get().container( 564 org.opencms.report.Messages.RPT_ARGUMENT_1, 565 translatedName)); 566 567 if (LOG.isInfoEnabled()) { 568 LOG.info(Messages.get().getBundle().key( 569 Messages.LOG_SKIPPING_3, 570 String.valueOf(i + 1), 571 String.valueOf(importSize), 572 translatedName)); 573 } 574 } 575 576 } else { 577 579 m_report.print(Messages.get().container(Messages.RPT_SKIPPING_0), I_CmsReport.FORMAT_NOTE); 580 m_report.println(org.opencms.report.Messages.get().container( 581 org.opencms.report.Messages.RPT_ARGUMENT_1, 582 translatedName)); 583 584 if (LOG.isInfoEnabled()) { 585 LOG.info(Messages.get().getBundle().key( 586 Messages.LOG_SKIPPING_3, 587 String.valueOf(i + 1), 588 String.valueOf(importSize), 589 translatedName)); 590 } 591 } 592 } 593 } catch (Exception e) { 594 595 m_report.println(e); 596 597 CmsMessageContainer message = Messages.get().container( 598 Messages.ERR_IMPORTEXPORT_ERROR_IMPORTING_RESOURCES_0); 599 if (LOG.isDebugEnabled()) { 600 LOG.debug(message.key(), e); 601 } 602 603 throw new CmsImportExportException(message, e); 604 } finally { 605 if (m_importingChannelData) { 606 m_cms.getRequestContext().restoreSiteRoot(); 607 } 608 } 609 } 610 } | Popular Tags |