opencms整站开发之五--- 调用opencms源码创建文件

在opencms中存两种基本的文件类型,一种是 File,另一种是 folder. 通常的JSP,HTML 就属于File类型。而文件夹只有一种,不用多说。这个两种文件都继承于 org.opencms.file.CmsResource 这个基类。CmsResource是opencms中最基本类型。创建它,也就是创建了File 或是 folder了。

要创建文件,我们必须了解opencms的核心工具类 org.opencms.file.CmsObject ,先摘录其源码的注释:

/* * File : $Source: /usr/local/cvs/opencms/src/org/opencms/file/CmsObject.java,v $ * Date : $Date: 2008-04-21 08:35:04 $ * Version: $Revision: 1.163 $ * * This library is part of OpenCms - * the Open Source Content Management System * * Copyright (c) 2002 - 2008 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.file; import org.opencms.db.CmsDbEntryNotFoundException; import org.opencms.db.CmsPublishList; import org.opencms.db.CmsResourceState; import org.opencms.db.CmsSecurityManager; import org.opencms.db.CmsUserSettings; import org.opencms.file.history.CmsHistoryPrincipal; import org.opencms.file.history.CmsHistoryProject; import org.opencms.file.history.I_CmsHistoryResource; import org.opencms.file.types.I_CmsResourceType; import org.opencms.lock.CmsLock; import org.opencms.lock.CmsLockFilter; import org.opencms.lock.CmsLockType; import org.opencms.main.CmsException; import org.opencms.main.CmsIllegalArgumentException; import org.opencms.main.I_CmsEventListener; import org.opencms.main.OpenCms; import org.opencms.relations.CmsRelationFilter; import org.opencms.relations.CmsRelationType; import org.opencms.report.CmsShellReport; import org.opencms.report.I_CmsReport; import org.opencms.security.CmsAccessControlEntry; import org.opencms.security.CmsAccessControlList; import org.opencms.security.CmsOrganizationalUnit; import org.opencms.security.CmsPermissionSet; import org.opencms.security.CmsPrincipal; import org.opencms.security.CmsRole; import org.opencms.security.CmsRoleViolationException; import org.opencms.security.CmsSecurityException; import org.opencms.security.I_CmsPermissionHandler; import org.opencms.security.I_CmsPrincipal; import org.opencms.util.CmsUUID; import org.opencms.workplace.CmsWorkplace; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; /** * This pivotal class provides all authorized access to the OpenCms VFS resources.<p> * * It encapsulates user identification and permissions. * Think of it as an initialized "shell" to access the OpenCms VFS. * Every call to a method here will be checked for user permissions * according to the <code>{@link org.opencms.file.CmsRequestContext}</code> this CmsObject instance was created with.<p> * * From a JSP page running in OpenCms, use <code>{@link org.opencms.jsp.CmsJspBean#getCmsObject()}</code> to gain * access to the current users CmsObject. Usually this is done with a <code>{@link org.opencms.jsp.CmsJspActionElement}</code>.<p> * * To generate a new instance of this class in your application, use * <code>{@link org.opencms.main.OpenCms#initCmsObject(String)}</code>. The argument String should be * the name of the guest user, usually "Guest" and more formally obtained by <code>{@link org.opencms.db.CmsDefaultUsers#getUserGuest()}</code>. * This will give you an initialized context with guest user permissions. * Then use <code>{@link CmsObject#loginUser(String, String)}</code> to log in the user you want. * Obviously you need the password for the new user. * You should never try to create an instance of this class using the constructor, * this is reserved for internal operation only.<p> * * @author Alexander Kandzior * @author Thomas Weckert * @author Carsten Weinholz * @author Andreas Zahner * @author Michael Moossen * * @version $Revision: 1.163 $ * * @since 6.0.0 */ public final class CmsObject { /** The request context. */ protected CmsRequestContext m_context; /** The security manager to access the cms. */ protected CmsSecurityManager m_securityManager;

从第一句不难看出,些类提供了对 opencms VFS(虚拟文件系统)合法,授权访问。

他提供了很多创建,删除,修改VFS的方法。

/** * Creates a new resource of the given resource type * with the provided content and properties.<p> * * @param resourcename the name of the resource to create (full current site relative path) * @param type the type of the resource to create * @param content the contents for the new resource * @param properties the properties for the new resource * * @return the created resource * * @throws CmsException if something goes wrong * @throws CmsIllegalArgumentException if the <code>resourcename</code> argument is null or of length 0 */ public CmsResource createResource(String resourcename, int type, byte[] content, List properties) throws CmsException, CmsIllegalArgumentException { return getResourceType(type).createResource(this, m_securityManager, resourcename, content, properties); }

上面的代码片断,就是其创建 CmsResource 方法。当然,这只是一个高度抽象的API接口,内部层层调用了很多工厂提供的方法来创建资源。最底层是利用JDBC 创建了一条记录插入数据库。

要调用CmsObject 所提供的方法来进行自己的操作.我们必须先得到CmsObject 实例.你可在opencms正常执行的某一环节进来获取CmsObject. 或可以在自己创建. 下面是在 jsp 中的创建方法:

org.opencms.jsp.CmsJspActionElement cms = new org.opencms.jsp.CmsJspActionElement( pageContext, request, response); CmsObject cobject = cms.getCmsObject();

得到了CmsObject实例对象,你可以进行自己的操作了.

下面是我写的一个工具类的方法:

/** * * 创建一个CmsResource * * @param cms * CmsObject * @param fullResourceName * 文件的相对路径 比如从根目录开始: module1/news1/news_9999.html * @param content * 二进度形式的字节文件 可以为空 * @param fileType * 要创建的文件类型 比如 "jsp" 最后会转换成类型对应的ID 如我以前写的文章里所定义的 7800 * @param properties * 相关属性 你可以在list加入一些CmsProperty来为些文件设置相关属性 null * @return 创建的CmsResource * */ public static CmsResource createFile(CmsObject cms, String fullResourceName, byte[] content, String fileType, List<CmsProperty> properties) { CmsResource cmsResource = null; try { /** * 类型 */ I_CmsResourceType resType = OpenCms.getResourceManager() .getResourceType(fileType); cmsResource = cms.createResource(fullResourceName, resType .getTypeId(), content, properties); } catch (CmsException e) { LOG.error("CmsException", e); } catch (Exception e) { LOG.error("Exception", e); } return cmsResource; }

CmsProperty可以用下面的方法创建:

/** * 创建一个属性对象 * * @param name * @param value */ public static CmsProperty generatePropertie(String name, String value) { CmsProperty prop = new CmsProperty(); prop.setAutoCreatePropertyDefinition(true); prop.setName(name); prop.setValue(value, CmsProperty.TYPE_SHARED); return prop; }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值