001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software GmbH & Co. KG, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.file.wrapper; 029 030import org.opencms.file.CmsFile; 031import org.opencms.file.CmsObject; 032import org.opencms.file.CmsProperty; 033import org.opencms.file.CmsResource; 034import org.opencms.file.CmsResource.CmsResourceCopyMode; 035import org.opencms.file.CmsResource.CmsResourceDeleteMode; 036import org.opencms.file.CmsResourceFilter; 037import org.opencms.lock.CmsLock; 038import org.opencms.main.CmsException; 039import org.opencms.main.CmsIllegalArgumentException; 040 041import java.util.List; 042 043/** 044 * Abstract base class which implements {@link I_CmsResourceWrapper} and 045 * makes it possible to add and remove file extensions to resources.<p> 046 * 047 * @since 6.5.6 048 */ 049public abstract class A_CmsResourceExtensionWrapper extends A_CmsResourceWrapper { 050 051 /** 052 * @see org.opencms.file.wrapper.A_CmsResourceWrapper#copyResource(org.opencms.file.CmsObject, java.lang.String, java.lang.String, org.opencms.file.CmsResource.CmsResourceCopyMode) 053 */ 054 @Override 055 public boolean copyResource(CmsObject cms, String source, String destination, CmsResourceCopyMode siblingMode) 056 throws CmsException, CmsIllegalArgumentException { 057 058 CmsResource res = getResource(cms, source); 059 if (res != null) { 060 061 cms.copyResource( 062 CmsResourceWrapperUtils.removeFileExtension(cms, source, getExtension()), 063 CmsResourceWrapperUtils.removeFileExtension(cms, destination, getExtension()), 064 siblingMode); 065 return true; 066 } 067 068 return false; 069 } 070 071 /** 072 * @see org.opencms.file.wrapper.A_CmsResourceWrapper#createResource(org.opencms.file.CmsObject, java.lang.String, int, byte[], java.util.List) 073 */ 074 @Override 075 public CmsResource createResource( 076 CmsObject cms, 077 String resourcename, 078 int type, 079 byte[] content, 080 List<CmsProperty> properties) throws CmsException, CmsIllegalArgumentException { 081 082 if (checkTypeId(type)) { 083 084 return cms.createResource( 085 CmsResourceWrapperUtils.removeFileExtension(cms, resourcename, getExtension()), 086 type, 087 content, 088 properties); 089 } 090 091 return null; 092 } 093 094 /** 095 * @see org.opencms.file.wrapper.A_CmsResourceWrapper#deleteResource(CmsObject, String, org.opencms.file.CmsResource.CmsResourceDeleteMode) 096 */ 097 @Override 098 public boolean deleteResource(CmsObject cms, String resourcename, CmsResourceDeleteMode siblingMode) 099 throws CmsException { 100 101 CmsResource res = getResource(cms, resourcename); 102 if (res != null) { 103 104 cms.deleteResource( 105 CmsResourceWrapperUtils.removeFileExtension(cms, resourcename, getExtension()), 106 siblingMode); 107 return true; 108 } 109 110 return false; 111 } 112 113 /** 114 * @see org.opencms.file.wrapper.A_CmsResourceWrapper#getLock(org.opencms.file.CmsObject, org.opencms.file.CmsResource) 115 */ 116 @Override 117 public CmsLock getLock(CmsObject cms, CmsResource resource) throws CmsException { 118 119 if (checkTypeId(resource.getTypeId())) { 120 121 CmsWrappedResource wrap = new CmsWrappedResource(resource); 122 wrap.setRootPath(CmsResourceWrapperUtils.removeFileExtension(cms, resource.getRootPath(), getExtension())); 123 124 return cms.getLock(wrap.getResource()); 125 } 126 127 return null; 128 } 129 130 /** 131 * @see org.opencms.file.wrapper.I_CmsResourceWrapper#isWrappedResource(CmsObject, CmsResource) 132 */ 133 public boolean isWrappedResource(CmsObject cms, CmsResource res) { 134 135 return checkTypeId(res.getTypeId()); 136 } 137 138 /** 139 * @see org.opencms.file.wrapper.A_CmsResourceWrapper#lockResource(org.opencms.file.CmsObject, java.lang.String, boolean) 140 */ 141 @Override 142 public boolean lockResource(CmsObject cms, String resourcename, boolean temporary) throws CmsException { 143 144 CmsResource res = getResource(cms, resourcename); 145 if (res != null) { 146 String path = cms.getRequestContext().removeSiteRoot(res.getRootPath()); 147 if (temporary) { 148 cms.lockResourceTemporary(path); 149 } else { 150 cms.lockResource(path); 151 } 152 return true; 153 } 154 155 return false; 156 } 157 158 /** 159 * @see org.opencms.file.wrapper.A_CmsResourceWrapper#moveResource(org.opencms.file.CmsObject, java.lang.String, java.lang.String) 160 */ 161 @Override 162 public boolean moveResource(CmsObject cms, String source, String destination) 163 throws CmsException, CmsIllegalArgumentException { 164 165 CmsResource res = getResource(cms, source); 166 if (res != null) { 167 168 // check if destination name is valid 169 if (!destination.endsWith("." + getExtension())) { 170 throw new CmsIllegalArgumentException( 171 Messages.get().container(Messages.ERR_BAD_RESOURCE_EXTENSION_1, destination)); 172 } 173 174 cms.moveResource( 175 CmsResourceWrapperUtils.removeFileExtension(cms, source, getExtension()), 176 CmsResourceWrapperUtils.removeFileExtension(cms, destination, getExtension())); 177 return true; 178 } 179 180 return false; 181 } 182 183 /** 184 * @see org.opencms.file.wrapper.A_CmsResourceWrapper#readFile(CmsObject, String, CmsResourceFilter) 185 */ 186 @Override 187 public CmsFile readFile(CmsObject cms, String resourcename, CmsResourceFilter filter) throws CmsException { 188 189 CmsResource res = getResource(cms, resourcename, filter); 190 if (res != null) { 191 192 CmsFile file = cms.readFile(res); 193 CmsWrappedResource wrap = new CmsWrappedResource(file); 194 wrap.setRootPath(CmsResourceWrapperUtils.addFileExtension(cms, res.getRootPath(), getExtension())); 195 196 return wrap.getFile(); 197 } 198 199 return null; 200 } 201 202 /** 203 * @see org.opencms.file.wrapper.A_CmsResourceWrapper#readResource(CmsObject, String, CmsResourceFilter) 204 */ 205 @Override 206 public CmsResource readResource(CmsObject cms, String resourcename, CmsResourceFilter filter) { 207 208 CmsResource res = getResource(cms, resourcename, filter); 209 if (res != null) { 210 211 CmsWrappedResource wrap = new CmsWrappedResource(res); 212 wrap.setRootPath(CmsResourceWrapperUtils.addFileExtension(cms, res.getRootPath(), getExtension())); 213 214 return wrap.getResource(); 215 } 216 217 return null; 218 } 219 220 /** 221 * @see org.opencms.file.wrapper.A_CmsResourceWrapper#restoreLink(org.opencms.file.CmsObject, java.lang.String) 222 */ 223 @Override 224 public String restoreLink(CmsObject cms, String uri) { 225 226 CmsResource res = getResource(cms, uri); 227 if (res != null) { 228 return res.getRootPath(); 229 } 230 231 return null; 232 } 233 234 /** 235 * @see org.opencms.file.wrapper.A_CmsResourceWrapper#rewriteLink(CmsObject, CmsResource) 236 */ 237 @Override 238 public String rewriteLink(CmsObject cms, CmsResource res) { 239 240 if (checkTypeId(res.getTypeId())) { 241 return CmsResourceWrapperUtils.addFileExtension(cms, res.getRootPath(), getExtension()); 242 } 243 244 return null; 245 } 246 247 /** 248 * @see org.opencms.file.wrapper.A_CmsResourceWrapper#unlockResource(org.opencms.file.CmsObject, java.lang.String) 249 */ 250 @Override 251 public boolean unlockResource(CmsObject cms, String resourcename) throws CmsException { 252 253 CmsResource res = getResource(cms, resourcename); 254 if (res != null) { 255 cms.unlockResource(cms.getRequestContext().removeSiteRoot(res.getRootPath())); 256 return true; 257 } 258 259 return false; 260 } 261 262 /** 263 * @see org.opencms.file.wrapper.A_CmsResourceWrapper#wrapResource(CmsObject, CmsResource) 264 */ 265 @Override 266 public CmsResource wrapResource(CmsObject cms, CmsResource res) { 267 268 if (checkTypeId(res.getTypeId())) { 269 270 CmsWrappedResource wrap = new CmsWrappedResource(res); 271 wrap.setRootPath(CmsResourceWrapperUtils.addFileExtension(cms, res.getRootPath(), getExtension())); 272 273 return wrap.getResource(); 274 } 275 276 return res; 277 } 278 279 /** 280 * @see org.opencms.file.wrapper.A_CmsResourceWrapper#writeFile(org.opencms.file.CmsObject, org.opencms.file.CmsFile) 281 */ 282 @Override 283 public CmsFile writeFile(CmsObject cms, CmsFile resource) throws CmsException { 284 285 if (checkTypeId(resource.getTypeId())) { 286 287 CmsWrappedResource wrap = new CmsWrappedResource(resource); 288 wrap.setRootPath(CmsResourceWrapperUtils.removeFileExtension(cms, resource.getRootPath(), getExtension())); 289 290 return cms.writeFile(wrap.getFile()); 291 } 292 293 return null; 294 } 295 296 /** 297 * Checks if the type id belongs to this resource wrapper.<p> 298 * 299 * @param typeId the type id to check 300 * @return true if the type id belongs to this wrapper otherwise false 301 */ 302 protected abstract boolean checkTypeId(int typeId); 303 304 /** 305 * Returns the extension to add and/or remove to/from the resource.<p> 306 * 307 * @return the extension to use 308 */ 309 protected abstract String getExtension(); 310 311 /** 312 * Trys to read the resourcename after removing the file extension and return the 313 * resource if the type id is correct.<p> 314 * 315 * @param cms the initialized CmsObject 316 * @param resourcename the name of the resource to read 317 * 318 * @return the resource or null if not found 319 */ 320 private CmsResource getResource(CmsObject cms, String resourcename) { 321 322 return getResource(cms, resourcename, CmsResourceFilter.DEFAULT); 323 } 324 325 /** 326 * Trys to read the resourcename after removing the file extension and return the 327 * resource if the type id is correct.<p> 328 * 329 * @param cms the initialized CmsObject 330 * @param resourcename the name of the resource to read 331 * @param filter the resource filter to use while reading 332 * 333 * @return the resource or null if not found 334 */ 335 private CmsResource getResource(CmsObject cms, String resourcename, CmsResourceFilter filter) { 336 337 CmsResource res = null; 338 339 try { 340 res = cms.readResource( 341 CmsResourceWrapperUtils.removeFileExtension(cms, resourcename, getExtension()), 342 filter); 343 } catch (CmsException ex) { 344 return null; 345 } 346 347 if (checkTypeId(res.getTypeId())) { 348 return res; 349 } 350 351 return null; 352 } 353 354}