Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
OpenCms is an enterprise-ready, easy to use website content management system based on Java and XML technology. Offering a complete set of features, OpenCms helps content managers worldwide to create and maintain beautiful websites fast and efficiently.
/*
* This library is part of OpenCms -
* the Open Source Content Management System
*
* Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* For further information about Alkacon Software GmbH & Co. KG, please see the
* company website: http://www.alkacon.com
*
* For further information about OpenCms, please see the
* project website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.opencms.importexport;
import org.opencms.db.CmsDefaultUsers;
import org.opencms.db.CmsResourceState;
import org.opencms.file.CmsFile;
import org.opencms.file.CmsFolder;
import org.opencms.file.CmsGroup;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsProject;
import org.opencms.file.CmsProperty;
import org.opencms.file.CmsResource;
import org.opencms.file.CmsResourceFilter;
import org.opencms.file.CmsUser;
import org.opencms.file.CmsVfsException;
import org.opencms.file.CmsVfsResourceNotFoundException;
import org.opencms.i18n.CmsMessageContainer;
import org.opencms.importexport.CmsImportExportManager.TimestampMode;
import org.opencms.main.CmsEvent;
import org.opencms.main.CmsException;
import org.opencms.main.CmsLog;
import org.opencms.main.I_CmsEventListener;
import org.opencms.main.OpenCms;
import org.opencms.module.CmsModule.ExportMode;
import org.opencms.relations.CmsRelation;
import org.opencms.relations.CmsRelationFilter;
import org.opencms.report.I_CmsReport;
import org.opencms.security.CmsAccessControlEntry;
import org.opencms.security.CmsOrganizationalUnit;
import org.opencms.security.CmsRole;
import org.opencms.security.CmsRoleViolationException;
import org.opencms.util.CmsDataTypeUtil;
import org.opencms.util.CmsDateUtil;
import org.opencms.util.CmsFileUtil;
import org.opencms.util.CmsMacroResolver;
import org.opencms.util.CmsStringUtil;
import org.opencms.util.CmsUUID;
import org.opencms.util.CmsXmlSaxWriter;
import org.opencms.workplace.CmsWorkplace;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.logging.Log;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXWriter;
import org.xml.sax.SAXException;
/**
* Provides the functionality to export files from the OpenCms VFS to a ZIP file.
*
* The ZIP file written will contain a copy of all exported files with their contents.
* It will also contain a manifest.xml file in which all meta-information
* about this files are stored, like permissions etc.
*
* @since 6.0.0
*/
public class CmsExport {
/** The log object for this class. */
private static final Log LOG = CmsLog.getLog(CmsExport.class);
/** The cms context. */
private CmsObject m_cms;
/** Counter for the export. */
private int m_exportCount;
/** Set of all exported files, required for preventing redundant sibling export. */
private Set m_exportedResources;
/** The export writer. */
private CmsExportHelper m_exportWriter;
/** The export parameters. */
private CmsExportParameters m_parameters;
/** The report. */
private I_CmsReport m_report;
/** The top level file node where all resources are appended to. */
private Element m_resourceNode;
/** The SAX writer to write the output to. */
private SAXWriter m_saxWriter;
/** Cache for previously added super folders. */
private List m_superFolders;
/**
* Constructs a new uninitialized export, required for special subclass data export.
*/
public CmsExport() {
// empty constructor
}
/**
* Constructs a new export.
*
* @param cms the cms context
* @param report the report
*
* @throws CmsRoleViolationException if the current user has not the required role
*/
public CmsExport(CmsObject cms, I_CmsReport report)
throws CmsRoleViolationException {
m_cms = cms;
m_report = report;
// check if the user has the required permissions
OpenCms.getRoleManager().checkRole(getCms(), CmsRole.DATABASE_MANAGER);
}
/**
* Export the data.
*
* @param parameters the export parameters
*
* @throws CmsImportExportException if something goes wrong
*/
public void exportData(CmsExportParameters parameters) throws CmsImportExportException {
m_parameters = parameters;
m_exportCount = 0;
// clear all caches
getReport().println(Messages.get().container(Messages.RPT_CLEARCACHE_0), I_CmsReport.FORMAT_NOTE);
OpenCms.fireCmsEvent(new CmsEvent(I_CmsEventListener.EVENT_CLEAR_CACHES, new HashMap(0)));
try {
Element exportNode = openExportFile(parameters.getExportMode());
if (m_parameters.getModuleInfo() != null) {
// add the module element
exportNode.add(m_parameters.getModuleInfo());
// write the XML
digestElement(exportNode, m_parameters.getModuleInfo());
}
// export account data only if selected
if (m_parameters.isExportAccountData()) {
Element accountsElement = exportNode.addElement(CmsImportVersion10.N_ACCOUNTS);
getSaxWriter().writeOpen(accountsElement);
exportOrgUnits(accountsElement);
getSaxWriter().writeClose(accountsElement);
exportNode.remove(accountsElement);
}
// export resource data only if selected
if (m_parameters.isExportResourceData()) {
exportAllResources(exportNode, m_parameters.getResources());
}
// export project data only if selected
if (m_parameters.isExportProjectData()) {
Element projectsElement = exportNode.addElement(CmsImportVersion10.N_PROJECTS);
getSaxWriter().writeOpen(projectsElement);
exportProjects(projectsElement);
getSaxWriter().writeClose(projectsElement);
exportNode.remove(projectsElement);
}
closeExportFile(exportNode);
} catch (SAXException se) {
getReport().println(se);
CmsMessageContainer message = Messages.get().container(
Messages.ERR_IMPORTEXPORT_ERROR_EXPORTING_TO_FILE_1,
getExportFileName());
if (LOG.isDebugEnabled()) {
LOG.debug(message.key(), se);
}
throw new CmsImportExportException(message, se);
} catch (IOException ioe) {
getReport().println(ioe);
CmsMessageContainer message = Messages.get().container(
Messages.ERR_IMPORTEXPORT_ERROR_EXPORTING_TO_FILE_1,
getExportFileName());
if (LOG.isDebugEnabled()) {
LOG.debug(message.key(), ioe);
}
throw new CmsImportExportException(message, ioe);
}
}
/**
* Exports the given folder and all child resources.
*
* @param folderName to complete path to the resource to export
*
* @throws CmsImportExportException if something goes wrong
* @throws SAXException if something goes wrong processing the manifest.xml
* @throws IOException if not all resources could be appended to the ZIP archive
*/
protected void addChildResources(String folderName) throws CmsImportExportException, IOException, SAXException {
try {
// get all subFolders
List subFolders = getCms().getSubFolders(folderName, CmsResourceFilter.IGNORE_EXPIRATION);
// get all files in folder
List subFiles = getCms().getFilesInFolder(folderName, CmsResourceFilter.IGNORE_EXPIRATION);
// walk through all files and export them
for (int i = 0; i < subFiles.size(); i++) {
CmsResource file = subFiles.get(i);
CmsResourceState state = file.getState();
long age = file.getDateLastModified() < file.getDateCreated()
? file.getDateCreated()
: file.getDateLastModified();
if (getCms().getRequestContext().getCurrentProject().isOnlineProject()
|| (m_parameters.isIncludeUnchangedResources())
|| state.isNew()
|| state.isChanged()) {
if (!state.isDeleted()
&& !CmsWorkplace.isTemporaryFile(file)
&& (age >= m_parameters.getContentAge())) {
String export = getCms().getSitePath(file);
if (checkExportResource(export)) {
if (isInExportableProject(file)) {
exportFile(getCms().readFile(export, CmsResourceFilter.IGNORE_EXPIRATION));
}
}
}
}
// release file header memory
subFiles.set(i, null);
}
// all files are exported, release memory
subFiles = null;
// walk through all subfolders and export them
for (int i = 0; i < subFolders.size(); i++) {
CmsResource folder = subFolders.get(i);
if (folder.getState() != CmsResource.STATE_DELETED) {
// check if this is a system-folder and if it should be included.
String export = getCms().getSitePath(folder);
if (checkExportResource(export)) {
long age = folder.getDateLastModified() < folder.getDateCreated()
? folder.getDateCreated()
: folder.getDateLastModified();
// export this folder only if age is above selected age
// default for selected age (if not set by user) is long 0 (i.e. 1970)
if (age >= m_parameters.getContentAge()) {
// only export folder data to manifest.xml if it has changed
appendResourceToManifest(folder, false);
}
// export all sub-resources in this folder
addChildResources(getCms().getSitePath(folder));
}
}
// release folder memory
subFolders.set(i, null);
}
} catch (CmsImportExportException e) {
throw e;
} catch (CmsException e) {
CmsMessageContainer message = Messages.get().container(
Messages.ERR_IMPORTEXPORT_ERROR_ADDING_CHILD_RESOURCES_1,
folderName);
if (LOG.isDebugEnabled()) {
LOG.debug(message.key(), e);
}
throw new CmsImportExportException(message, e);
}
}
/**
* Adds all files in fileNames to the manifest.xml file.
*
* @param fileNames list of path Strings, e.g. /folder/index.html
*
* @throws CmsImportExportException if something goes wrong
* @throws IOException if a file could not be exported
* @throws SAXException if something goes wrong processing the manifest.xml
*/
protected void addFiles(List fileNames) throws CmsImportExportException, IOException, SAXException {
if (fileNames != null) {
for (int i = 0; i < fileNames.size(); i++) {
String fileName = fileNames.get(i);
try {
CmsFile file = getCms().readFile(fileName, CmsResourceFilter.IGNORE_EXPIRATION);
if (!file.getState().isDeleted() && !CmsWorkplace.isTemporaryFile(file)) {
if (checkExportResource(fileName)) {
if (m_parameters.isRecursive()) {
addParentFolders(fileName);
}
if (isInExportableProject(file)) {
exportFile(file);
}
}
}
} catch (CmsImportExportException e) {
throw e;
} catch (CmsException e) {
if (e instanceof CmsVfsException) { // file not found
CmsMessageContainer message = Messages.get().container(
Messages.ERR_IMPORTEXPORT_ERROR_ADDING_FILE_1,
fileName);
if (LOG.isDebugEnabled()) {
LOG.debug(message.key(), e);
}
throw new CmsImportExportException(message, e);
}
}
}
}
}
/**
* Adds the parent folders of the given resource to the config file,
* starting at the top, excluding the root folder.
*
* @param resourceName the name of a resource in the VFS
*
* @throws CmsImportExportException if something goes wrong
* @throws SAXException if something goes wrong processing the manifest.xml
*/
protected void addParentFolders(String resourceName) throws CmsImportExportException, SAXException {
try {
// this is a resource in /system/ folder and option includeSystem is not true
if (!checkExportResource(resourceName)) {
return;
}
// Initialize the "previously added folder cache"
if (m_superFolders == null) {
m_superFolders = new ArrayList();
}
List superFolders = new ArrayList();
String currentSubFolder = resourceName;
// Check, if the path is really a folder
boolean isFolderResource = currentSubFolder.endsWith("/");
while (currentSubFolder.length() > "/".length()) {
currentSubFolder = currentSubFolder.substring(0, currentSubFolder.length() - 1);
currentSubFolder = currentSubFolder.substring(0, currentSubFolder.lastIndexOf("/") + 1);
if (currentSubFolder.length() <= "/".length()) {
break;
}
superFolders.add(currentSubFolder);
}
for (int i = superFolders.size() - 1; i >= 0; i--) {
String addFolder = superFolders.get(i);
if (!m_superFolders.contains(addFolder)) {
// This super folder was NOT added previously. Add it now!
CmsFolder folder = getCms().readFolder(addFolder, CmsResourceFilter.IGNORE_EXPIRATION);
appendResourceToManifest(folder, false, true);
// Remember that this folder was added
m_superFolders.add(addFolder);
}
}
if (isFolderResource) { // add the folder itself
if (!m_superFolders.contains(resourceName)) {
// This super folder was NOT added previously. Add it now!
CmsFolder folder = getCms().readFolder(resourceName, CmsResourceFilter.IGNORE_EXPIRATION);
appendResourceToManifest(folder, false);
// Remember that this folder was added
m_superFolders.add(resourceName);
}
}
} catch (CmsImportExportException e) {
throw e;
} catch (CmsException e) {
CmsMessageContainer message = Messages.get().container(
Messages.ERR_IMPORTEXPORT_ERROR_ADDING_PARENT_FOLDERS_1,
resourceName);
if (LOG.isDebugEnabled()) {
LOG.debug(message.key(), e);
}
throw new CmsImportExportException(message, e);
}
}
/**
* Adds a property node to the manifest.xml.
*
* @param propertiesElement the parent element to append the node to
* @param propertyName the name of the property
* @param propertyValue the value of the property
* @param shared if true, add a shared property attribute to the generated property node
*/
protected void addPropertyNode(
Element propertiesElement,
String propertyName,
String propertyValue,
boolean shared) {
if (propertyValue != null) {
Element propertyElement = propertiesElement.addElement(CmsImportVersion10.N_PROPERTY);
if (shared) {
// add "type" attribute to the property node in case of a shared/resource property value
propertyElement.addAttribute(CmsImportVersion10.A_TYPE, CmsImportVersion10.PROPERTY_ATTRIB_TYPE_SHARED);
}
propertyElement.addElement(CmsImportVersion10.N_NAME).addText(propertyName);
propertyElement.addElement(CmsImportVersion10.N_VALUE).addCDATA(propertyValue);
}
}
/**
* Adds a relation node to the manifest.xml.
*
* @param relationsElement the parent element to append the node to
* @param structureId the structure id of the target relation
* @param sitePath the site path of the target relation
* @param relationType the type of the relation
*/
protected void addRelationNode(Element relationsElement, String structureId, String sitePath, String relationType) {
if ((structureId != null) && (sitePath != null) && (relationType != null)) {
Element relationElement = relationsElement.addElement(CmsImportVersion10.N_RELATION);
relationElement.addElement(CmsImportVersion10.N_ID).addText(structureId);
relationElement.addElement(CmsImportVersion10.N_PATH).addText(sitePath);
relationElement.addElement(CmsImportVersion10.N_TYPE).addText(relationType);
}
}
/** @see #appendResourceToManifest(CmsResource, boolean, boolean)
* @param resource @see #appendResourceToManifest(CmsResource, boolean, boolean)
* @param source @see #appendResourceToManifest(CmsResource, boolean, boolean)
* @throws CmsImportExportException @see #appendResourceToManifest(CmsResource, boolean, boolean)
* @throws SAXException @see #appendResourceToManifest(CmsResource, boolean, boolean)
*/
protected void appendResourceToManifest(CmsResource resource, boolean source)
throws CmsImportExportException, SAXException {
appendResourceToManifest(resource, source, false);
}
/**
* Writes the data for a resource (like access-rights) to the manifest.xml file.
*
* @param resource the resource to get the data from
* @param source flag to show if the source information in the xml file must be written
* @param isSuperFolder flag to indicate that the resource is only a super folder of a module resource.
* This will prevent exporting uuid and creation date in the reduced export mode.
*
* @throws CmsImportExportException if something goes wrong
* @throws SAXException if something goes wrong processing the manifest.xml
*/
protected void appendResourceToManifest(CmsResource resource, boolean source, boolean isSuperFolder)
throws CmsImportExportException, SAXException {
try {
// only write