001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/file/wrapper/A_CmsResourceExtensionWrapper.java,v $
003: * Date : $Date: 2008-02-27 12:05:30 $
004: * Version: $Revision: 1.8 $
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.file.wrapper;
033:
034: import org.opencms.file.CmsFile;
035: import org.opencms.file.CmsObject;
036: import org.opencms.file.CmsResource;
037: import org.opencms.file.CmsResourceFilter;
038: import org.opencms.file.CmsResource.CmsResourceCopyMode;
039: import org.opencms.file.CmsResource.CmsResourceDeleteMode;
040: import org.opencms.lock.CmsLock;
041: import org.opencms.main.CmsException;
042: import org.opencms.main.CmsIllegalArgumentException;
043:
044: import java.util.List;
045:
046: /**
047: * Abstract base class which implements {@link I_CmsResourceWrapper} and
048: * makes it possible to add and remove file extensions to resources.<p>
049: *
050: * @author Peter Bonrad
051: *
052: * @version $Revision: 1.8 $
053: *
054: * @since 6.5.6
055: */
056: public abstract class A_CmsResourceExtensionWrapper extends
057: A_CmsResourceWrapper {
058:
059: /**
060: * @see org.opencms.file.wrapper.A_CmsResourceWrapper#copyResource(org.opencms.file.CmsObject, java.lang.String, java.lang.String, org.opencms.file.CmsResource.CmsResourceCopyMode)
061: */
062: public boolean copyResource(CmsObject cms, String source,
063: String destination, CmsResourceCopyMode siblingMode)
064: throws CmsException, CmsIllegalArgumentException {
065:
066: CmsResource res = getResource(cms, source);
067: if (res != null) {
068:
069: cms.copyResource(CmsResourceWrapperUtils
070: .removeFileExtension(cms, source, getExtension()),
071: CmsResourceWrapperUtils.removeFileExtension(cms,
072: destination, getExtension()), siblingMode);
073: return true;
074: }
075:
076: return false;
077: }
078:
079: /**
080: * @see org.opencms.file.wrapper.A_CmsResourceWrapper#createResource(org.opencms.file.CmsObject, java.lang.String, int, byte[], java.util.List)
081: */
082: public CmsResource createResource(CmsObject cms,
083: String resourcename, int type, byte[] content,
084: List properties) throws CmsException,
085: CmsIllegalArgumentException {
086:
087: if (checkTypeId(type)) {
088:
089: return cms.createResource(CmsResourceWrapperUtils
090: .removeFileExtension(cms, resourcename,
091: getExtension()), type, content, properties);
092: }
093:
094: return null;
095: }
096:
097: /**
098: * @see org.opencms.file.wrapper.A_CmsResourceWrapper#deleteResource(CmsObject, String, org.opencms.file.CmsResource.CmsResourceDeleteMode)
099: */
100: public boolean deleteResource(CmsObject cms, String resourcename,
101: CmsResourceDeleteMode siblingMode) throws CmsException {
102:
103: CmsResource res = getResource(cms, resourcename);
104: if (res != null) {
105:
106: cms.deleteResource(CmsResourceWrapperUtils
107: .removeFileExtension(cms, resourcename,
108: getExtension()), siblingMode);
109: return true;
110: }
111:
112: return false;
113: }
114:
115: /**
116: * @see org.opencms.file.wrapper.A_CmsResourceWrapper#getLock(org.opencms.file.CmsObject, org.opencms.file.CmsResource)
117: */
118: public CmsLock getLock(CmsObject cms, CmsResource resource)
119: throws CmsException {
120:
121: if (checkTypeId(resource.getTypeId())) {
122:
123: CmsWrappedResource wrap = new CmsWrappedResource(resource);
124: wrap.setRootPath(CmsResourceWrapperUtils
125: .removeFileExtension(cms, resource.getRootPath(),
126: getExtension()));
127:
128: return cms.getLock(wrap.getResource());
129: }
130:
131: return null;
132: }
133:
134: /**
135: * @see org.opencms.file.wrapper.I_CmsResourceWrapper#isWrappedResource(CmsObject, CmsResource)
136: */
137: public boolean isWrappedResource(CmsObject cms, CmsResource res) {
138:
139: return checkTypeId(res.getTypeId());
140: }
141:
142: /**
143: * @see org.opencms.file.wrapper.A_CmsResourceWrapper#lockResource(org.opencms.file.CmsObject, java.lang.String)
144: */
145: public boolean lockResource(CmsObject cms, String resourcename)
146: throws CmsException {
147:
148: CmsResource res = getResource(cms, resourcename);
149: if (res != null) {
150:
151: cms.lockResource(cms.getRequestContext().removeSiteRoot(
152: res.getRootPath()));
153: return true;
154: }
155:
156: return false;
157: }
158:
159: /**
160: * @see org.opencms.file.wrapper.A_CmsResourceWrapper#moveResource(org.opencms.file.CmsObject, java.lang.String, java.lang.String)
161: */
162: public boolean moveResource(CmsObject cms, String source,
163: String destination) throws CmsException,
164: CmsIllegalArgumentException {
165:
166: CmsResource res = getResource(cms, source);
167: if (res != null) {
168:
169: // check if destination name is valid
170: if (!destination.endsWith("." + getExtension())) {
171: throw new CmsIllegalArgumentException(Messages.get()
172: .container(
173: Messages.ERR_BAD_RESOURCE_EXTENSION_1,
174: destination));
175: }
176:
177: cms.moveResource(CmsResourceWrapperUtils
178: .removeFileExtension(cms, source, getExtension()),
179: CmsResourceWrapperUtils.removeFileExtension(cms,
180: destination, getExtension()));
181: return true;
182: }
183:
184: return false;
185: }
186:
187: /**
188: * @see org.opencms.file.wrapper.A_CmsResourceWrapper#readFile(CmsObject, String, CmsResourceFilter)
189: */
190: public CmsFile readFile(CmsObject cms, String resourcename,
191: CmsResourceFilter filter) throws CmsException {
192:
193: CmsResource res = getResource(cms, resourcename, filter);
194: if (res != null) {
195:
196: CmsFile file = cms.readFile(res);
197: CmsWrappedResource wrap = new CmsWrappedResource(file);
198: wrap.setRootPath(CmsResourceWrapperUtils.addFileExtension(
199: cms, res.getRootPath(), getExtension()));
200:
201: return wrap.getFile();
202: }
203:
204: return null;
205: }
206:
207: /**
208: * @see org.opencms.file.wrapper.A_CmsResourceWrapper#readResource(CmsObject, String, CmsResourceFilter)
209: */
210: public CmsResource readResource(CmsObject cms, String resourcename,
211: CmsResourceFilter filter) {
212:
213: CmsResource res = getResource(cms, resourcename, filter);
214: if (res != null) {
215:
216: CmsWrappedResource wrap = new CmsWrappedResource(res);
217: wrap.setRootPath(CmsResourceWrapperUtils.addFileExtension(
218: cms, res.getRootPath(), getExtension()));
219:
220: return wrap.getResource();
221: }
222:
223: return null;
224: }
225:
226: /**
227: * @see org.opencms.file.wrapper.A_CmsResourceWrapper#restoreLink(org.opencms.file.CmsObject, java.lang.String)
228: */
229: public String restoreLink(CmsObject cms, String uri) {
230:
231: CmsResource res = getResource(cms, uri);
232: if (res != null) {
233: return res.getRootPath();
234: }
235:
236: return null;
237: }
238:
239: /**
240: * @see org.opencms.file.wrapper.A_CmsResourceWrapper#rewriteLink(CmsObject, CmsResource)
241: */
242: public String rewriteLink(CmsObject cms, CmsResource res) {
243:
244: if (checkTypeId(res.getTypeId())) {
245: return CmsResourceWrapperUtils.addFileExtension(cms, res
246: .getRootPath(), getExtension());
247: }
248:
249: return null;
250: }
251:
252: /**
253: * @see org.opencms.file.wrapper.A_CmsResourceWrapper#unlockResource(org.opencms.file.CmsObject, java.lang.String)
254: */
255: public boolean unlockResource(CmsObject cms, String resourcename)
256: throws CmsException {
257:
258: CmsResource res = getResource(cms, resourcename);
259: if (res != null) {
260: cms.unlockResource(cms.getRequestContext().removeSiteRoot(
261: res.getRootPath()));
262: return true;
263: }
264:
265: return false;
266: }
267:
268: /**
269: * @see org.opencms.file.wrapper.A_CmsResourceWrapper#wrapResource(CmsObject, CmsResource)
270: */
271: public CmsResource wrapResource(CmsObject cms, CmsResource res) {
272:
273: if (checkTypeId(res.getTypeId())) {
274:
275: CmsWrappedResource wrap = new CmsWrappedResource(res);
276: wrap.setRootPath(CmsResourceWrapperUtils.addFileExtension(
277: cms, res.getRootPath(), getExtension()));
278:
279: return wrap.getResource();
280: }
281:
282: return res;
283: }
284:
285: /**
286: * @see org.opencms.file.wrapper.A_CmsResourceWrapper#writeFile(org.opencms.file.CmsObject, org.opencms.file.CmsFile)
287: */
288: public CmsFile writeFile(CmsObject cms, CmsFile resource)
289: throws CmsException {
290:
291: if (checkTypeId(resource.getTypeId())) {
292:
293: CmsWrappedResource wrap = new CmsWrappedResource(resource);
294: wrap.setRootPath(CmsResourceWrapperUtils
295: .removeFileExtension(cms, resource.getRootPath(),
296: getExtension()));
297:
298: return cms.writeFile(wrap.getFile());
299: }
300:
301: return null;
302: }
303:
304: /**
305: * Checks if the type id belongs to this resource wrapper.<p>
306: *
307: * @param typeId the type id to check
308: * @return true if the type id belongs to this wrapper otherwise false
309: */
310: protected abstract boolean checkTypeId(int typeId);
311:
312: /**
313: * Returns the extension to add and/or remove to/from the resource.<p>
314: *
315: * @return the extension to use
316: */
317: protected abstract String getExtension();
318:
319: /**
320: * Trys to read the resourcename after removing the file extension and return the
321: * resource if the type id is correct.<p>
322: *
323: * @param cms the initialized CmsObject
324: * @param resourcename the name of the resource to read
325: *
326: * @return the resource or null if not found
327: */
328: private CmsResource getResource(CmsObject cms, String resourcename) {
329:
330: return getResource(cms, resourcename, CmsResourceFilter.DEFAULT);
331: }
332:
333: /**
334: * Trys to read the resourcename after removing the file extension and return the
335: * resource if the type id is correct.<p>
336: *
337: * @param cms the initialized CmsObject
338: * @param resourcename the name of the resource to read
339: * @param filter the resource filter to use while reading
340: *
341: * @return the resource or null if not found
342: */
343: private CmsResource getResource(CmsObject cms, String resourcename,
344: CmsResourceFilter filter) {
345:
346: CmsResource res = null;
347:
348: try {
349: res = cms.readResource(CmsResourceWrapperUtils
350: .removeFileExtension(cms, resourcename,
351: getExtension()), filter);
352: } catch (CmsException ex) {
353: return null;
354: }
355:
356: if (checkTypeId(res.getTypeId())) {
357: return res;
358: }
359:
360: return null;
361: }
362:
363: }
|