cmsrepositorysession.java

来自「找了很久才找到到源代码」· Java 代码 · 共 499 行 · 第 1/2 页

JAVA
499
字号
/*
 * File   : $Source: /usr/local/cvs/opencms/src/org/opencms/repository/CmsRepositorySession.java,v $
 * Date   : $Date: 2007-08-13 16:30:10 $
 * Version: $Revision: 1.7 $
 *
 * This library is part of OpenCms -
 * the Open Source Content Management System
 *
 * Copyright (c) 2002 - 2007 Alkacon Software GmbH (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, 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.repository;

import org.opencms.file.CmsFile;
import org.opencms.file.CmsResource;
import org.opencms.file.CmsResourceFilter;
import org.opencms.file.CmsUser;
import org.opencms.file.CmsVfsResourceAlreadyExistsException;
import org.opencms.file.CmsVfsResourceNotFoundException;
import org.opencms.file.types.CmsResourceTypeFolder;
import org.opencms.file.wrapper.CmsObjectWrapper;
import org.opencms.lock.CmsLock;
import org.opencms.main.CmsException;
import org.opencms.main.CmsLog;
import org.opencms.main.OpenCms;
import org.opencms.security.CmsSecurityException;
import org.opencms.util.CmsFileUtil;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;

/**
 * This is the session class to work with the {@link CmsRepository}.<p> 
 * 
 * You can get an instance of this class by calling 
 * {@link CmsRepository#login(String, String)}.<p>
 *
 * This class provides basic file and folder operations on the resources
 * in the VFS of OpenCms.<p>
 * 
 * @see A_CmsRepositorySession
 * @see I_CmsRepositorySession
 *
 * @author Peter Bonrad
 * 
 * @version $Revision: 1.7 $
 * 
 * @since 6.5.6
 */
public class CmsRepositorySession extends A_CmsRepositorySession {

    /** The log object for this class. */
    private static final Log LOG = CmsLog.getLog(CmsRepositorySession.class);

    /** The initialized {@link CmsObjectWrapper}. */
    private final CmsObjectWrapper m_cms;

    /**
     * Constructor with an initialized {@link CmsObjectWrapper} and a 
     * {@link CmsRepositoryFilter} to use.<p>
     * 
     * @param cms the initialized CmsObject
     * @param filter the repository filter to use
     */
    public CmsRepositorySession(CmsObjectWrapper cms, CmsRepositoryFilter filter) {

        m_cms = cms;
        setFilter(filter);
    }

    /**
     * @see org.opencms.repository.I_CmsRepositorySession#copy(java.lang.String, java.lang.String, boolean)
     */
    public void copy(String src, String dest, boolean overwrite) throws CmsException {

        src = validatePath(src);
        dest = validatePath(dest);

        if (LOG.isDebugEnabled()) {
            LOG.debug(Messages.get().getBundle().key(Messages.LOG_COPY_ITEM_2, src, dest));
        }

        // It is only possible in OpenCms to overwrite files.
        // Folder are not possible to overwrite.
        if (exists(dest)) {

            if (overwrite) {
                CmsResource srcRes = m_cms.readResource(src, CmsResourceFilter.DEFAULT);
                CmsResource destRes = m_cms.readResource(dest, CmsResourceFilter.DEFAULT);

                if ((srcRes.isFile()) && (destRes.isFile())) {

                    if (LOG.isDebugEnabled()) {
                        LOG.debug(Messages.get().getBundle().key(Messages.LOG_DELETE_DEST_0));
                    }

                    // delete existing resource
                    delete(dest);
                } else {

                    if (LOG.isDebugEnabled()) {
                        LOG.debug(Messages.get().getBundle().key(Messages.ERR_OVERWRITE_0));
                    }

                    // internal error (not possible)
                    throw new CmsException(Messages.get().container(Messages.ERR_OVERWRITE_0));
                }
            } else {

                if (LOG.isDebugEnabled()) {
                    LOG.debug(Messages.get().getBundle().key(Messages.ERR_DEST_EXISTS_0));
                }

                throw new CmsVfsResourceAlreadyExistsException(Messages.get().container(Messages.ERR_DEST_EXISTS_0));
            }
        }

        // copy resource
        m_cms.copyResource(src, dest, CmsResource.COPY_PRESERVE_SIBLING);

        // unlock destination resource
        m_cms.unlockResource(dest);
    }

    /**
     * @see org.opencms.repository.I_CmsRepositorySession#create(java.lang.String)
     */
    public void create(String path) throws CmsException {

        path = validatePath(path);

        if (LOG.isDebugEnabled()) {
            LOG.debug(Messages.get().getBundle().key(Messages.LOG_CREATE_ITEM_1, path));
        }

        // create the folder
        CmsResource res = m_cms.createResource(path, CmsResourceTypeFolder.RESOURCE_TYPE_ID);

        // unlock new created folders if lock is not inherited
        if (!m_cms.getLock(res).isInherited()) {
            m_cms.unlockResource(path);
        }
    }

    /**
     * @see org.opencms.repository.I_CmsRepositorySession#delete(java.lang.String)
     */
    public void delete(String path) throws CmsException {

        path = validatePath(path);

        if (LOG.isDebugEnabled()) {
            LOG.debug(Messages.get().getBundle().key(Messages.LOG_DELETE_ITEM_1, path));
        }

        CmsRepositoryLockInfo lock = getLock(path);

        // lock resource
        m_cms.lockResource(path);

        // delete resource
        m_cms.deleteResource(path, CmsResource.DELETE_PRESERVE_SIBLINGS);

        // if deleting items out of a xml page restore lock state after deleting
        try {
            if (lock == null) {
                m_cms.unlockResource(path);
            }
        } catch (CmsException ex) {
            // noop
        }
    }

    /**
     * @see org.opencms.repository.I_CmsRepositorySession#exists(java.lang.String)
     */
    public boolean exists(String path) {

        try {
            path = validatePath(path);
            return m_cms.existsResource(path);
        } catch (CmsException ex) {
            return false;
        }
    }

    /**
     * @see org.opencms.repository.I_CmsRepositorySession#getItem(java.lang.String)
     */
    public I_CmsRepositoryItem getItem(String path) throws CmsException {

        path = validatePath(path);

        if (LOG.isDebugEnabled()) {
            LOG.debug(Messages.get().getBundle().key(Messages.LOG_READ_ITEM_1, path));
        }

        CmsResource res = m_cms.readResource(path, CmsResourceFilter.DEFAULT);

        CmsRepositoryItem item = new CmsRepositoryItem(res, m_cms);
        return item;
    }

    /**
     * @see org.opencms.repository.I_CmsRepositorySession#getLock(java.lang.String)
     */
    public CmsRepositoryLockInfo getLock(String path) {

        try {
            CmsRepositoryLockInfo lockInfo = new CmsRepositoryLockInfo();

            path = validatePath(path);

            CmsResource res = m_cms.readResource(path, CmsResourceFilter.DEFAULT);

            // check user locks
            CmsLock cmsLock = m_cms.getLock(res);
            if (!cmsLock.isUnlocked()) {
                lockInfo.setPath(path);

                CmsUser owner = m_cms.readUser(cmsLock.getUserId());
                if (owner != null) {
                    lockInfo.setUsername(owner.getName());
                    lockInfo.setOwner(owner.getName() + "||" + owner.getEmail());
                }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?