1 31 32 package org.opencms.importexport; 33 34 import org.opencms.file.CmsDataAccessException; 35 import org.opencms.file.CmsObject; 36 import org.opencms.file.CmsProperty; 37 import org.opencms.file.CmsPropertyDefinition; 38 import org.opencms.file.CmsResource; 39 import org.opencms.file.types.CmsResourceTypeFolder; 40 import org.opencms.file.types.CmsResourceTypePlain; 41 import org.opencms.file.types.CmsResourceTypePointer; 42 import org.opencms.file.types.CmsResourceTypeXmlPage; 43 import org.opencms.file.types.I_CmsResourceType; 44 import org.opencms.i18n.CmsMessageContainer; 45 import org.opencms.main.CmsException; 46 import org.opencms.main.CmsLog; 47 import org.opencms.main.OpenCms; 48 import org.opencms.report.I_CmsReport; 49 import org.opencms.security.CmsRole; 50 import org.opencms.security.I_CmsPasswordHandler; 51 import org.opencms.security.I_CmsPrincipal; 52 import org.opencms.util.CmsUUID; 53 import org.opencms.xml.page.CmsXmlPage; 54 55 import java.io.File ; 56 import java.util.ArrayList ; 57 import java.util.Collections ; 58 import java.util.HashMap ; 59 import java.util.List ; 60 import java.util.Map ; 61 import java.util.zip.ZipFile ; 62 63 import org.apache.commons.logging.Log; 64 65 import org.dom4j.Document; 66 import org.dom4j.Element; 67 68 83 public class CmsImportVersion3 extends A_CmsImport { 84 85 86 private static final int IMPORT_VERSION = 3; 87 88 89 private static final Log LOG = CmsLog.getLog(CmsImportVersion3.class); 90 91 94 public CmsImportVersion3() { 95 96 m_convertToXmlPage = true; 97 } 98 99 103 public int getVersion() { 104 105 return CmsImportVersion3.IMPORT_VERSION; 106 } 107 108 111 public void importResources( 112 CmsObject cms, 113 String importPath, 114 I_CmsReport report, 115 File importResource, 116 ZipFile importZip, 117 Document docXml) throws CmsImportExportException { 118 119 initialize(); 121 m_cms = cms; 122 m_importPath = importPath; 123 m_report = report; 124 m_importResource = importResource; 125 m_importZip = importZip; 126 m_docXml = docXml; 127 m_importingChannelData = false; 128 m_linkStorage = new HashMap (); 129 m_linkPropertyStorage = new HashMap (); 130 try { 131 if (cms.hasRole(CmsRole.ACCOUNT_MANAGER)) { 133 importGroups(); 134 importUsers(); 135 } 136 importAllResources(); 138 convertPointerToSiblings(); 139 } finally { 140 cleanUp(); 141 } 142 } 143 144 160 protected void importUser( 161 String name, 162 String description, 163 String flags, 164 String password, 165 String firstname, 166 String lastname, 167 String email, 168 String address, 169 String type, 170 Map userInfo, 171 List userGroups) throws CmsImportExportException { 172 173 boolean convert = false; 174 175 Map config = OpenCms.getPasswordHandler().getConfiguration(); 176 if (config != null && config.containsKey(I_CmsPasswordHandler.CONVERT_DIGEST_ENCODING)) { 177 convert = Boolean.valueOf((String )config.get(I_CmsPasswordHandler.CONVERT_DIGEST_ENCODING)).booleanValue(); 178 } 179 180 if (convert) { 181 password = convertDigestEncoding(password); 182 } 183 184 super.importUser( 185 name, 186 description, 187 flags, 188 password, 189 firstname, 190 lastname, 191 email, 192 address, 193 type, 194 userInfo, 195 userGroups); 196 } 197 198 203 private void importAllResources() throws CmsImportExportException { 204 205 String source, destination, type, uuidresource, userlastmodified, usercreated, flags, timestamp; 206 long datelastmodified, datecreated; 207 208 List fileNodes, acentryNodes; 209 Element currentElement, currentEntry; 210 List properties = null; 211 212 if (m_importingChannelData) { 213 m_cms.getRequestContext().saveSiteRoot(); 214 m_cms.getRequestContext().setSiteRoot(CmsResource.VFS_FOLDER_CHANNELS); 215 } 216 List deleteProperties = OpenCms.getImportExportManager().getIgnoredProperties(); 218 if (deleteProperties == null) { 219 deleteProperties = new ArrayList (); 220 } 221 List immutableResources = OpenCms.getImportExportManager().getImmutableResources(); 223 if (immutableResources == null) { 224 immutableResources = Collections.EMPTY_LIST; 225 } 226 if (LOG.isDebugEnabled()) { 227 LOG.debug(Messages.get().getBundle().key( 228 Messages.LOG_IMPORTEXPORT_IMMUTABLE_RESOURCES_SIZE_1, 229 Integer.toString(immutableResources.size()))); 230 } 231 m_convertToXmlPage = OpenCms.getImportExportManager().convertToXmlPage(); 233 234 try { 235 fileNodes = m_docXml.selectNodes("//" + CmsImportExportManager.N_FILE); 237 238 int importSize = fileNodes.size(); 239 for (int i = 0; i < fileNodes.size(); i++) { 241 m_report.print(org.opencms.report.Messages.get().container( 242 org.opencms.report.Messages.RPT_SUCCESSION_2, 243 String.valueOf(i + 1), 244 String.valueOf(importSize))); 245 currentElement = (Element)fileNodes.get(i); 246 source = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_SOURCE); 249 destination = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_DESTINATION); 251 type = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_TYPE); 253 uuidresource = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_UUIDRESOURCE); 259 timestamp = CmsImport.getChildElementTextValue( 261 currentElement, 262 CmsImportExportManager.N_DATELASTMODIFIED); 263 if (timestamp != null) { 264 datelastmodified = Long.parseLong(timestamp); 265 } else { 266 datelastmodified = System.currentTimeMillis(); 267 } 268 userlastmodified = CmsImport.getChildElementTextValue( 270 currentElement, 271 CmsImportExportManager.N_USERLASTMODIFIED); 272 timestamp = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_DATECREATED); 274 if (timestamp != null) { 275 datecreated = Long.parseLong(timestamp); 276 } else { 277 datecreated = System.currentTimeMillis(); 278 } 279 usercreated = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_USERCREATED); 281 flags = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_FLAGS); 283 284 String translatedName = m_cms.getRequestContext().addSiteRoot(m_importPath + destination); 285 if (CmsResourceTypeFolder.RESOURCE_TYPE_NAME.equals(type)) { 286 translatedName += "/"; 287 } 288 translatedName = m_cms.getRequestContext().getDirectoryTranslator().translateResource(translatedName); 290 boolean resourceNotImmutable = checkImmutable(translatedName, immutableResources); 292 translatedName = m_cms.getRequestContext().removeSiteRoot(translatedName); 293 if (resourceNotImmutable) { 295 m_report.print(Messages.get().container(Messages.RPT_IMPORTING_0), I_CmsReport.FORMAT_NOTE); 297 m_report.print(org.opencms.report.Messages.get().container( 298 org.opencms.report.Messages.RPT_ARGUMENT_1, 299 translatedName)); 300 properties = readPropertiesFromManifest(currentElement, deleteProperties); 303 304 CmsResource res = importResource( 306 source, 307 destination, 308 type, 309 uuidresource, 310 datelastmodified, 311 userlastmodified, 312 datecreated, 313 usercreated, 314 flags, 315 properties); 316 317 List aceList = new ArrayList (); 318 if (res != null) { 319 320 acentryNodes = currentElement.selectNodes("*/" + CmsImportExportManager.N_ACCESSCONTROL_ENTRY); 322 for (int j = 0; j < acentryNodes.size(); j++) { 324 currentEntry = (Element)acentryNodes.get(j); 325 String id = CmsImport.getChildElementTextValue( 327 currentEntry, 328 CmsImportExportManager.N_ACCESSCONTROL_PRINCIPAL); 329 String acflags = CmsImport.getChildElementTextValue( 330 currentEntry, 331 CmsImportExportManager.N_FLAGS); 332 String allowed = CmsImport.getChildElementTextValue( 333 currentEntry, 334 CmsImportExportManager.N_ACCESSCONTROL_PERMISSIONSET 335 + "/" 336 + CmsImportExportManager.N_ACCESSCONTROL_ALLOWEDPERMISSIONS); 337 String denied = CmsImport.getChildElementTextValue( 338 currentEntry, 339 CmsImportExportManager.N_ACCESSCONTROL_PERMISSIONSET 340 + "/" 341 + CmsImportExportManager.N_ACCESSCONTROL_DENIEDPERMISSIONS); 342 343 try { 345 String principalId = new CmsUUID().toString(); 346 String principal = id.substring(id.indexOf('.') + 1, id.length()); 347 348 if (id.startsWith(I_CmsPrincipal.PRINCIPAL_GROUP)) { 349 principal = OpenCms.getImportExportManager().translateGroup(principal); 350 principalId = m_cms.readGroup(principal).getId().toString(); 351 } else { 352 principal = OpenCms.getImportExportManager().translateUser(principal); 353 principalId = m_cms.readUser(principal).getId().toString(); 354 } 355 356 aceList.add(getImportAccessControlEntry(res, principalId, allowed, denied, acflags)); 358 } catch (CmsDataAccessException e) { 359 } 361 } 362 importAccessControlEntries(res, aceList); 363 364 } else { 365 m_report.print(Messages.get().container(Messages.RPT_SKIPPING_0), I_CmsReport.FORMAT_NOTE); 367 m_report.println(org.opencms.report.Messages.get().container( 368 org.opencms.report.Messages.RPT_ARGUMENT_1, 369 translatedName)); 370 } 371 } else { 372 m_report.print(Messages.get().container(Messages.RPT_SKIPPING_0), I_CmsReport.FORMAT_NOTE); 374 m_report.println(org.opencms.report.Messages.get().container( 375 org.opencms.report.Messages.RPT_ARGUMENT_1, 376 translatedName)); 377 } 378 } 379 380 } catch (Exception e) { 381 382 m_report.println(e); 383 384 CmsMessageContainer message = Messages.get().container( 385 Messages.ERR_IMPORTEXPORT_ERROR_IMPORTING_RESOURCES_0); 386 if (LOG.isDebugEnabled()) { 387 LOG.debug(message.key(), e); 388 } 389 390 throw new CmsImportExportException(message, e); 391 } finally { 392 if (m_importingChannelData) { 393 m_cms.getRequestContext().restoreSiteRoot(); 394 } 395 } 396 397 } 398 399 415 private CmsResource importResource( 416 String source, 417 String destination, 418 String type, 419 String uuidresource, 420 long datelastmodified, 421 String userlastmodified, 422 long datecreated, 423 String usercreated, 424 String flags, 425 List properties) { 426 427 byte[] content = null; 428 CmsResource result = null; 429 430 try { 431 432 if (source != null) { 434 content = getFileBytes(source); 435 } 436 int size = 0; 437 if (content != null) { 438 size = content.length; 439 } 440 441 I_CmsResourceType resType; 443 444 CmsUUID newUserlastmodified; 446 CmsUUID newUsercreated; 447 try { 450 newUserlastmodified = m_cms.readUser(userlastmodified).getId(); 451 } catch (CmsException e) { 452 newUserlastmodified = m_cms.getRequestContext().currentUser().getId(); 453 } 455 456 try { 457 newUsercreated = m_cms.readUser(usercreated).getId(); 458 } catch (CmsException e) { 459 newUsercreated = m_cms.getRequestContext().currentUser().getId(); 460 } 462 463 if (m_convertToXmlPage && (type.equals(RESOURCE_TYPE_NEWPAGE_NAME))) { 465 466 if (content != null) { 467 468 String encoding = null; 470 encoding = CmsProperty.get(CmsPropertyDefinition.PROPERTY_CONTENT_ENCODING, properties).getValue(); 471 if (encoding == null) { 472 encoding = OpenCms.getSystemInfo().getDefaultEncoding(); 473 } 474 475 CmsXmlPage xmlPage = CmsXmlPageConverter.convertToXmlPage(m_cms, content, getLocale( 476 destination, 477 properties), encoding); 478 479 content = xmlPage.marshal(); 480 } 481 resType = OpenCms.getResourceManager().getResourceType(CmsResourceTypeXmlPage.getStaticTypeId()); 482 } else if (type.equals(RESOURCE_TYPE_LINK_NAME)) { 483 resType = OpenCms.getResourceManager().getResourceType(CmsResourceTypePointer.getStaticTypeId()); 484 } else if (type.equals(RESOURCE_TYPE_LEGACY_PAGE_NAME)) { 485 resType = OpenCms.getResourceManager().getResourceType(CmsResourceTypePlain.getStaticTypeId()); 486 } else { 487 resType = OpenCms.getResourceManager().getResourceType(type); 488 } 489 490 CmsUUID newUuidresource = null; 492 if ((uuidresource != null) && (!resType.isFolder())) { 493 newUuidresource = new CmsUUID(uuidresource); 495 } else { 496 newUuidresource = new CmsUUID(); 498 } 499 500 CmsResource resource = new CmsResource( 502 new CmsUUID(), newUuidresource, 504 destination, 505 resType.getTypeId(), 506 resType.isFolder(), 507 new Integer (flags).intValue(), 508 m_cms.getRequestContext().currentProject().getId(), 509 CmsResource.STATE_NEW, 510 datecreated, 511 newUsercreated, 512 datelastmodified, 513 newUserlastmodified, 514 CmsResource.DATE_RELEASED_DEFAULT, 515 CmsResource.DATE_EXPIRED_DEFAULT, 516 1, 517 size); 518 519 if (type.equals(RESOURCE_TYPE_LINK_NAME)) { 520 m_report.print(Messages.get().container(Messages.RPT_STORING_LINK_0), I_CmsReport.FORMAT_NOTE); 522 m_linkStorage.put(m_importPath + destination, new String (content)); 523 m_linkPropertyStorage.put(m_importPath + destination, properties); 524 result = resource; 525 } else { 526 result = m_cms.importResource(destination, resource, content, properties); 528 } 529 530 if (result != null) { 531 m_report.println( 532 org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0), 533 I_CmsReport.FORMAT_OK); 534 } 535 } catch (Exception exc) { 536 m_report.println(exc); 538 try { 539 Thread.sleep(1000); 541 } catch (Exception e) { 542 } 544 } 545 546 return result; 547 } 548 } | Popular Tags |