001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/importexport/CmsImportVersion3.java,v $
003: * Date : $Date: 2008-02-27 12:05:48 $
004: * Version: $Revision: 1.80 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.importexport;
033:
034: import org.opencms.file.CmsDataAccessException;
035: import org.opencms.file.CmsObject;
036: import org.opencms.file.CmsProperty;
037: import org.opencms.file.CmsPropertyDefinition;
038: import org.opencms.file.CmsResource;
039: import org.opencms.file.types.CmsResourceTypeFolder;
040: import org.opencms.file.types.CmsResourceTypePlain;
041: import org.opencms.file.types.CmsResourceTypePointer;
042: import org.opencms.file.types.CmsResourceTypeXmlPage;
043: import org.opencms.file.types.I_CmsResourceType;
044: import org.opencms.i18n.CmsMessageContainer;
045: import org.opencms.main.CmsException;
046: import org.opencms.main.CmsLog;
047: import org.opencms.main.OpenCms;
048: import org.opencms.report.I_CmsReport;
049: import org.opencms.security.CmsRole;
050: import org.opencms.security.I_CmsPasswordHandler;
051: import org.opencms.security.I_CmsPrincipal;
052: import org.opencms.util.CmsUUID;
053: import org.opencms.xml.CmsXmlException;
054: import org.opencms.xml.CmsXmlUtils;
055: import org.opencms.xml.page.CmsXmlPage;
056:
057: import java.io.File;
058: import java.io.IOException;
059: import java.util.ArrayList;
060: import java.util.Collections;
061: import java.util.HashMap;
062: import java.util.List;
063: import java.util.Map;
064: import java.util.zip.ZipFile;
065:
066: import org.apache.commons.logging.Log;
067:
068: import org.dom4j.Document;
069: import org.dom4j.Element;
070:
071: /**
072: * Implementation of the OpenCms Import Interface ({@link org.opencms.importexport.I_CmsImport}) for
073: * the import version 3.<p>
074: *
075: * This import format was used in OpenCms 5.1.2 - 5.1.6.<p>
076: *
077: * @author Michael Emmerich
078: * @author Thomas Weckert
079: *
080: * @version $Revision: 1.80 $
081: *
082: * @since 6.0.0
083: *
084: * @see org.opencms.importexport.A_CmsImport
085: *
086: * @deprecated this import class is no longer in use and should only be used to import old export files
087: */
088: public class CmsImportVersion3 extends A_CmsImport {
089:
090: /** The version number of this import implementation.<p> */
091: private static final int IMPORT_VERSION = 3;
092:
093: /** The log object for this class. */
094: private static final Log LOG = CmsLog
095: .getLog(CmsImportVersion3.class);
096:
097: /**
098: * Creates a new CmsImportVerion3 object.<p>
099: */
100: public CmsImportVersion3() {
101:
102: m_convertToXmlPage = true;
103: }
104:
105: /**
106: * @see org.opencms.importexport.I_CmsImport#getVersion()
107: * @return the version number of this import implementation
108: */
109: public int getVersion() {
110:
111: return CmsImportVersion3.IMPORT_VERSION;
112: }
113:
114: /**
115: * @see org.opencms.importexport.I_CmsImport#importResources(org.opencms.file.CmsObject, java.lang.String, org.opencms.report.I_CmsReport, java.io.File, java.util.zip.ZipFile, org.dom4j.Document)
116: *
117: * @deprecated use {@link #importData(CmsObject, I_CmsReport, CmsImportParameters)} instead
118: */
119: public void importResources(CmsObject cms, String importPath,
120: I_CmsReport report, File importResource, ZipFile importZip,
121: Document docXml) throws CmsImportExportException {
122:
123: CmsImportParameters params = new CmsImportParameters(
124: importResource != null ? importResource
125: .getAbsolutePath() : importZip.getName(),
126: importPath, true);
127:
128: try {
129: importData(cms, report, params);
130: } catch (CmsXmlException e) {
131: throw new CmsImportExportException(e.getMessageContainer(),
132: e);
133: }
134: }
135:
136: /**
137: * @see org.opencms.importexport.I_CmsImport#importData(CmsObject, I_CmsReport, CmsImportParameters)
138: */
139: public void importData(CmsObject cms, I_CmsReport report,
140: CmsImportParameters params)
141: throws CmsImportExportException, CmsXmlException {
142:
143: // initialize the import
144: initialize();
145: m_cms = cms;
146: m_importPath = params.getDestinationPath();
147: m_report = report;
148:
149: m_linkStorage = new HashMap();
150: m_linkPropertyStorage = new HashMap();
151: CmsImportHelper helper = new CmsImportHelper(params);
152: try {
153: helper.openFile();
154: m_importResource = helper.getFolder();
155: m_importZip = helper.getZipFile();
156: m_docXml = CmsXmlUtils
157: .unmarshalHelper(
158: helper
159: .getFileBytes(CmsImportExportManager.EXPORT_MANIFEST),
160: null);
161: // first import the user information
162: if (OpenCms.getRoleManager().hasRole(m_cms,
163: CmsRole.ACCOUNT_MANAGER)) {
164: importGroups();
165: importUsers();
166: }
167: // now import the VFS resources
168: importAllResources();
169: convertPointerToSiblings();
170: } catch (IOException ioe) {
171: CmsMessageContainer msg = Messages.get().container(
172: Messages.ERR_IMPORTEXPORT_ERROR_READING_FILE_1,
173: CmsImportExportManager.EXPORT_MANIFEST);
174: if (LOG.isErrorEnabled()) {
175: LOG.error(msg.key(), ioe);
176: }
177: throw new CmsImportExportException(msg, ioe);
178: } finally {
179: helper.closeFile();
180: cleanUp();
181: }
182: }
183:
184: /**
185: * @see org.opencms.importexport.A_CmsImport#importUser(String, String, String, String, String, String, long, Map, List)
186: */
187: protected void importUser(String name, String flags,
188: String password, String firstname, String lastname,
189: String email, long dateCreated, Map userInfo,
190: List userGroups) throws CmsImportExportException {
191:
192: boolean convert = false;
193:
194: Map config = OpenCms.getPasswordHandler().getConfiguration();
195: if ((config != null)
196: && config
197: .containsKey(I_CmsPasswordHandler.CONVERT_DIGEST_ENCODING)) {
198: convert = Boolean
199: .valueOf(
200: (String) config
201: .get(I_CmsPasswordHandler.CONVERT_DIGEST_ENCODING))
202: .booleanValue();
203: }
204:
205: if (convert) {
206: password = convertDigestEncoding(password);
207: }
208:
209: super .importUser(name, flags, password, firstname, lastname,
210: email, dateCreated, userInfo, userGroups);
211: }
212:
213: /**
214: * Imports the resources and writes them to the cms.<p>
215: *
216: * @throws CmsImportExportException if something goes wrong
217: */
218: private void importAllResources() throws CmsImportExportException {
219:
220: String source, destination, type, uuidresource, userlastmodified, usercreated, flags, timestamp;
221: long datelastmodified, datecreated;
222:
223: List fileNodes, acentryNodes;
224: Element currentElement, currentEntry;
225: List properties = null;
226:
227: // get list of unwanted properties
228: List deleteProperties = OpenCms.getImportExportManager()
229: .getIgnoredProperties();
230: if (deleteProperties == null) {
231: deleteProperties = new ArrayList();
232: }
233: // get list of immutable resources
234: List immutableResources = OpenCms.getImportExportManager()
235: .getImmutableResources();
236: if (immutableResources == null) {
237: immutableResources = Collections.EMPTY_LIST;
238: }
239: if (LOG.isDebugEnabled()) {
240: LOG
241: .debug(Messages
242: .get()
243: .getBundle()
244: .key(
245: Messages.LOG_IMPORTEXPORT_IMMUTABLE_RESOURCES_SIZE_1,
246: Integer.toString(immutableResources
247: .size())));
248: }
249: // get the wanted page type for imported pages
250: m_convertToXmlPage = OpenCms.getImportExportManager()
251: .convertToXmlPage();
252:
253: try {
254: // get all file-nodes
255: fileNodes = m_docXml.selectNodes("//" + A_CmsImport.N_FILE);
256:
257: int importSize = fileNodes.size();
258: // walk through all files in manifest
259: for (int i = 0; i < fileNodes.size(); i++) {
260: m_report
261: .print(org.opencms.report.Messages
262: .get()
263: .container(
264: org.opencms.report.Messages.RPT_SUCCESSION_2,
265: String.valueOf(i + 1),
266: String.valueOf(importSize)));
267: currentElement = (Element) fileNodes.get(i);
268: // get all information for a file-import
269: // <source>
270: source = getChildElementTextValue(currentElement,
271: A_CmsImport.N_SOURCE);
272: // <destintion>
273: destination = getChildElementTextValue(currentElement,
274: A_CmsImport.N_DESTINATION);
275: // <type>
276: type = getChildElementTextValue(currentElement,
277: A_CmsImport.N_TYPE);
278: // <uuidstructure>
279: //uuidstructure = CmsImport.getChildElementTextValue(
280: // currentElement,
281: // CmsImportExportManager.N_UUIDSTRUCTURE);
282: // <uuidresource>
283: uuidresource = getChildElementTextValue(currentElement,
284: A_CmsImport.N_UUIDRESOURCE);
285: // <datelastmodified>
286: timestamp = getChildElementTextValue(currentElement,
287: A_CmsImport.N_DATELASTMODIFIED);
288: if (timestamp != null) {
289: datelastmodified = Long.parseLong(timestamp);
290: } else {
291: datelastmodified = System.currentTimeMillis();
292: }
293: // <userlastmodified>
294: userlastmodified = getChildElementTextValue(
295: currentElement, A_CmsImport.N_USERLASTMODIFIED);
296: // <datecreated>
297: timestamp = getChildElementTextValue(currentElement,
298: A_CmsImport.N_DATECREATED);
299: if (timestamp != null) {
300: datecreated = Long.parseLong(timestamp);
301: } else {
302: datecreated = System.currentTimeMillis();
303: }
304: // <usercreated>
305: usercreated = getChildElementTextValue(currentElement,
306: A_CmsImport.N_USERCREATED);
307: // <flags>
308: flags = getChildElementTextValue(currentElement,
309: A_CmsImport.N_FLAGS);
310:
311: String translatedName = m_cms.getRequestContext()
312: .addSiteRoot(m_importPath + destination);
313: if (CmsResourceTypeFolder.RESOURCE_TYPE_NAME
314: .equals(type)) {
315: translatedName += "/";
316: }
317: // translate the name during import
318: translatedName = m_cms.getRequestContext()
319: .getDirectoryTranslator().translateResource(
320: translatedName);
321: // check if this resource is immutable
322: boolean resourceNotImmutable = checkImmutable(
323: translatedName, immutableResources);
324: translatedName = m_cms.getRequestContext()
325: .removeSiteRoot(translatedName);
326: // if the resource is not immutable and not on the exclude list, import it
327: if (resourceNotImmutable) {
328: // print out the information to the report
329: m_report.print(Messages.get().container(
330: Messages.RPT_IMPORTING_0),
331: I_CmsReport.FORMAT_NOTE);
332: m_report
333: .print(org.opencms.report.Messages
334: .get()
335: .container(
336: org.opencms.report.Messages.RPT_ARGUMENT_1,
337: translatedName));
338: //m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0));
339: // get all properties
340: properties = readPropertiesFromManifest(
341: currentElement, deleteProperties);
342:
343: // import the resource
344: CmsResource res = importResource(source,
345: destination, type, uuidresource,
346: datelastmodified, userlastmodified,
347: datecreated, usercreated, flags, properties);
348:
349: List aceList = new ArrayList();
350: if (res != null) {
351: // write all imported access control entries for this file
352: acentryNodes = currentElement.selectNodes("*/"
353: + A_CmsImport.N_ACCESSCONTROL_ENTRY);
354: // collect all access control entries
355: for (int j = 0; j < acentryNodes.size(); j++) {
356: currentEntry = (Element) acentryNodes
357: .get(j);
358: // get the data of the access control entry
359: String id = getChildElementTextValue(
360: currentEntry,
361: A_CmsImport.N_ACCESSCONTROL_PRINCIPAL);
362: String acflags = getChildElementTextValue(
363: currentEntry, A_CmsImport.N_FLAGS);
364: String allowed = getChildElementTextValue(
365: currentEntry,
366: A_CmsImport.N_ACCESSCONTROL_PERMISSIONSET
367: + "/"
368: + A_CmsImport.N_ACCESSCONTROL_ALLOWEDPERMISSIONS);
369: String denied = getChildElementTextValue(
370: currentEntry,
371: A_CmsImport.N_ACCESSCONTROL_PERMISSIONSET
372: + "/"
373: + A_CmsImport.N_ACCESSCONTROL_DENIEDPERMISSIONS);
374:
375: // get the correct principal
376: try {
377: String principalId = new CmsUUID()
378: .toString();
379: String principal = id.substring(id
380: .indexOf('.') + 1, id.length());
381:
382: if (id
383: .startsWith(I_CmsPrincipal.PRINCIPAL_GROUP)) {
384: principal = OpenCms
385: .getImportExportManager()
386: .translateGroup(principal);
387: principalId = m_cms.readGroup(
388: principal).getId()
389: .toString();
390: } else {
391: principal = OpenCms
392: .getImportExportManager()
393: .translateUser(principal);
394: principalId = m_cms.readUser(
395: principal).getId()
396: .toString();
397: }
398:
399: // add the entry to the list
400: aceList
401: .add(getImportAccessControlEntry(
402: res, principalId,
403: allowed, denied,
404: acflags));
405: } catch (CmsDataAccessException e) {
406: // user or group not found, so do not import the ace
407: }
408: }
409: importAccessControlEntries(res, aceList);
410:
411: } else {
412: // resource import failed, since no CmsResource was created
413: m_report.print(Messages.get().container(
414: Messages.RPT_SKIPPING_0),
415: I_CmsReport.FORMAT_NOTE);
416: m_report
417: .println(org.opencms.report.Messages
418: .get()
419: .container(
420: org.opencms.report.Messages.RPT_ARGUMENT_1,
421: translatedName));
422: }
423: } else {
424: // skip the file import, just print out the information to the report
425: m_report.print(Messages.get().container(
426: Messages.RPT_SKIPPING_0),
427: I_CmsReport.FORMAT_NOTE);
428: m_report
429: .println(org.opencms.report.Messages
430: .get()
431: .container(
432: org.opencms.report.Messages.RPT_ARGUMENT_1,
433: translatedName));
434: }
435: }
436:
437: } catch (Exception e) {
438: m_report.println(e);
439: m_report.addError(e);
440:
441: CmsMessageContainer message = Messages
442: .get()
443: .container(
444: Messages.ERR_IMPORTEXPORT_ERROR_IMPORTING_RESOURCES_0);
445: if (LOG.isDebugEnabled()) {
446: LOG.debug(message.key(), e);
447: }
448: throw new CmsImportExportException(message, e);
449: }
450:
451: }
452:
453: /**
454: * Imports a resource (file or folder) into the cms.<p>
455: *
456: * @param source the path to the source-file
457: * @param destination the path to the destination-file in the cms
458: * @param type the resource-type of the file
459: * @param uuidresource the resource uuid of the resource
460: * @param datelastmodified the last modification date of the resource
461: * @param userlastmodified the user who made the last modifications to the resource
462: * @param datecreated the creation date of the resource
463: * @param usercreated the user who created
464: * @param flags the flags of the resource
465: * @param properties a list with properties for this resource
466: *
467: * @return imported resource
468: */
469: private CmsResource importResource(String source,
470: String destination, String type, String uuidresource,
471: long datelastmodified, String userlastmodified,
472: long datecreated, String usercreated, String flags,
473: List properties) {
474:
475: byte[] content = null;
476: CmsResource result = null;
477:
478: try {
479:
480: // get the file content
481: if (source != null) {
482: content = getFileBytes(source);
483: }
484: int size = 0;
485: if (content != null) {
486: size = content.length;
487: }
488:
489: // get all required information to create a CmsResource
490: I_CmsResourceType resType;
491:
492: // get UUIDs for the user
493: CmsUUID newUserlastmodified;
494: CmsUUID newUsercreated;
495: // check if user created and user lastmodified are valid users in this system.
496: // if not, use the current user
497: try {
498: newUserlastmodified = m_cms.readUser(userlastmodified)
499: .getId();
500: } catch (CmsException e) {
501: newUserlastmodified = m_cms.getRequestContext()
502: .currentUser().getId();
503: // datelastmodified = System.currentTimeMillis();
504: }
505:
506: try {
507: newUsercreated = m_cms.readUser(usercreated).getId();
508: } catch (CmsException e) {
509: newUsercreated = m_cms.getRequestContext()
510: .currentUser().getId();
511: // datecreated = System.currentTimeMillis();
512: }
513:
514: // convert to xml page if wanted
515: if (m_convertToXmlPage
516: && (type.equals(RESOURCE_TYPE_NEWPAGE_NAME))) {
517:
518: if (content != null) {
519:
520: //get the encoding
521: String encoding = null;
522: encoding = CmsProperty
523: .get(
524: CmsPropertyDefinition.PROPERTY_CONTENT_ENCODING,
525: properties).getValue();
526: if (encoding == null) {
527: encoding = OpenCms.getSystemInfo()
528: .getDefaultEncoding();
529: }
530:
531: CmsXmlPage xmlPage = CmsXmlPageConverter
532: .convertToXmlPage(m_cms, content,
533: getLocale(destination, properties),
534: encoding);
535:
536: content = xmlPage.marshal();
537: }
538: resType = OpenCms.getResourceManager().getResourceType(
539: CmsResourceTypeXmlPage.getStaticTypeId());
540: } else if (type.equals(RESOURCE_TYPE_LINK_NAME)) {
541: resType = OpenCms.getResourceManager().getResourceType(
542: CmsResourceTypePointer.getStaticTypeId());
543: } else if (type.equals(RESOURCE_TYPE_LEGACY_PAGE_NAME)) {
544: resType = OpenCms.getResourceManager().getResourceType(
545: CmsResourceTypePlain.getStaticTypeId());
546: } else {
547: resType = OpenCms.getResourceManager().getResourceType(
548: type);
549: }
550:
551: // get UUIDs for the resource and content
552: CmsUUID newUuidresource = null;
553: if ((uuidresource != null) && (!resType.isFolder())) {
554: // create a UUID from the provided string
555: newUuidresource = new CmsUUID(uuidresource);
556: } else {
557: // folders get always a new resource record UUID
558: newUuidresource = new CmsUUID();
559: }
560:
561: // create a new CmsResource
562: CmsResource resource = new CmsResource(
563: new CmsUUID(), // structure ID is always a new UUID
564: newUuidresource, destination, resType.getTypeId(),
565: resType.isFolder(), new Integer(flags).intValue(),
566: m_cms.getRequestContext().currentProject()
567: .getUuid(), CmsResource.STATE_NEW,
568: datecreated, newUsercreated, datelastmodified,
569: newUserlastmodified,
570: CmsResource.DATE_RELEASED_DEFAULT,
571: CmsResource.DATE_EXPIRED_DEFAULT, 1, size, System
572: .currentTimeMillis(), 0);
573:
574: if (type.equals(RESOURCE_TYPE_LINK_NAME)) {
575: // store links for later conversion
576: m_report.print(Messages.get().container(
577: Messages.RPT_STORING_LINK_0),
578: I_CmsReport.FORMAT_NOTE);
579: m_linkStorage.put(m_importPath + destination,
580: new String(content));
581: m_linkPropertyStorage.put(m_importPath + destination,
582: properties);
583: result = resource;
584: } else {
585: // import this resource in the VFS
586: result = m_cms.importResource(destination, resource,
587: content, properties);
588: }
589:
590: if (result != null) {
591: m_report.println(
592: org.opencms.report.Messages.get().container(
593: org.opencms.report.Messages.RPT_OK_0),
594: I_CmsReport.FORMAT_OK);
595: }
596: } catch (Exception exc) {
597: // an error while importing the file
598: m_report.println(exc);
599: m_report.addError(exc);
600:
601: try {
602: // Sleep some time after an error so that the report output has a chance to keep up
603: Thread.sleep(1000);
604: } catch (Exception e) {
605: //
606: }
607: }
608:
609: return result;
610: }
611: }
|