cmsrepositorysession.java

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

JAVA
499
字号
                return lockInfo;
            }

            return null;
        } catch (CmsException ex) {

            // error occured while finding locks
            // return null (no lock found)
            return null;
        }
    }

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

        List ret = new ArrayList();

        path = validatePath(path);

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

        List resources = m_cms.getResourcesInFolder(path, CmsResourceFilter.DEFAULT);
        Iterator iter = resources.iterator();
        while (iter.hasNext()) {
            CmsResource res = (CmsResource)iter.next();

            if (!isFiltered(m_cms.getRequestContext().removeSiteRoot(res.getRootPath()))) {

                // open the original resource (for virtual files this is the resource in the VFS
                // which the virtual resource is based on)
                // this filters e.g. property files for resources that are filtered out and thus
                // should not be displayed
                CmsResource org = m_cms.readResource(res.getStructureId(), CmsResourceFilter.DEFAULT);
                if (!isFiltered(m_cms.getRequestContext().removeSiteRoot(org.getRootPath()))) {
                    ret.add(new CmsRepositoryItem(res, m_cms));
                }
            }
        }

        if (LOG.isDebugEnabled()) {
            LOG.debug(Messages.get().getBundle().key(Messages.LOG_LIST_ITEMS_SUCESS_1, new Integer(ret.size())));
        }

        return ret;
    }

    /**
     * @see org.opencms.repository.I_CmsRepositorySession#lock(java.lang.String, org.opencms.repository.CmsRepositoryLockInfo)
     */
    public boolean lock(String path, CmsRepositoryLockInfo lock) throws CmsException {

        path = validatePath(path);

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

        m_cms.lockResource(path);
        return true;
    }

    /**
     * @see org.opencms.repository.I_CmsRepositorySession#move(java.lang.String, java.lang.String, boolean)
     */
    public void move(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_MOVE_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));
                    }

                    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));
            }
        }

        // lock source resource
        m_cms.lockResource(src);

        // moving
        m_cms.moveResource(src, dest);

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

    /**
     * @see org.opencms.repository.I_CmsRepositorySession#save(java.lang.String, java.io.InputStream, boolean)
     */
    public void save(String path, InputStream inputStream, boolean overwrite) throws CmsException, IOException {

        path = validatePath(path);
        byte[] content = CmsFileUtil.readFully(inputStream);

        try {
            CmsFile file = m_cms.readFile(path, CmsResourceFilter.DEFAULT);

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

            if (overwrite) {

                file.setContents(content);

                CmsLock lock = m_cms.getLock(file);

                // lock resource
                if (!lock.isInherited()) {
                    m_cms.lockResource(path);
                }

                // write file
                m_cms.writeFile(file);

                if (lock.isNullLock()) {
                    m_cms.unlockResource(path);
                }
            } 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));
            }
        } catch (CmsVfsResourceNotFoundException ex) {

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

            int type = OpenCms.getResourceManager().getDefaultTypeForName(path).getTypeId();

            // create the file
            CmsResource res = m_cms.createResource(path, type, content, null);

            // unlock file after creation if lock is not inherited
            if (!m_cms.getLock(res).isInherited()) {
                m_cms.unlockResource(path);
            }
        }

    }

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

        try {
            path = validatePath(path);

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

            m_cms.unlockResource(path);
        } catch (CmsException ex) {

            if (LOG.isErrorEnabled()) {
                LOG.error(Messages.get().getBundle().key(Messages.ERR_UNLOCK_FAILED_0), ex);
            }
        }
    }

    /**
     * Adds the site root to the path name and checks then if the path
     * is filtered.<p>
     * 
     * @see org.opencms.repository.A_CmsRepositorySession#isFiltered(java.lang.String)
     */
    protected boolean isFiltered(String name) {

        boolean ret = super.isFiltered(m_cms.getRequestContext().addSiteRoot(name));
        if (ret) {

            if (LOG.isDebugEnabled()) {
                LOG.debug(Messages.get().getBundle().key(Messages.ERR_ITEM_FILTERED_1, name));
            }

        }

        return ret;
    }

    /**
     * Validates (translates) the given path and checks if it is filtered out.<p>
     * 
     * @param path the path to validate
     * 
     * @return the validated path
     * 
     * @throws CmsSecurityException if the path is filtered out
     */
    private String validatePath(String path) throws CmsSecurityException {

        // Problems with spaces in new folders (default: "Neuer Ordner")
        // Solution: translate this to a correct name.
        String ret = m_cms.getRequestContext().getFileTranslator().translateResource(path);

        // add site root only works correct if system folder ends with a slash
        if (CmsResource.VFS_FOLDER_SYSTEM.equals(ret)) {
            ret = ret.concat("/");
        }

        // filter path
        if (isFiltered(ret)) {
            throw new CmsSecurityException(Messages.get().container(Messages.ERR_ITEM_FILTERED_1, path));
        }

        return ret;
    }
}

⌨️ 快捷键说明

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