001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: https://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) 081 throws CmsException, CmsIllegalArgumentException { 082 083 if (checkTypeId(type)) { 084 085 return cms.createResource( 086 CmsResourceWrapperUtils.removeFileExtension(cms, resourcename, getExtension()), 087 type, 088 content, 089 properties); 090 } 091 092 return null; 093 } 094 095 /** 096 * @see org.opencms.file.wrapper.A_CmsResourceWrapper#deleteResource(CmsObject, String, org.opencms.file.CmsResource.CmsResourceDeleteMode) 097 */ 098 @Override 099 public boolean deleteResource(CmsObject cms, String resourcename, CmsResourceDeleteMode siblingMode) 100 throws CmsException { 101 102 CmsResource res = getResource(cms, resourcename); 103 if (res != null) { 104 105 cms.deleteResource( 106 CmsResourceWrapperUtils.removeFileExtension(cms, resourcename, getExtension()), 107 siblingMode); 108 return true; 109 } 110 111 return false; 112 } 113 114 /** 115 * @see org.opencms.file.wrapper.A_CmsResourceWrapper#getLock(org.opencms.file.CmsObject, org.opencms.file.CmsResource) 116 */ 117 @Override 118 public CmsLock getLock(CmsObject cms, CmsResource resource) throws CmsException { 119 120 if (checkTypeId(resource.getTypeId())) { 121 122 CmsWrappedResource wrap = new CmsWrappedResource(resource); 123 wrap.setRootPath(CmsResourceWrapperUtils.removeFileExtension(cms, resource.getRootPath(), getExtension())); 124 125 return cms.getLock(wrap.getResource()); 126 } 127 128 return null; 129 } 130 131 /** 132 * @see org.opencms.file.wrapper.I_CmsResourceWrapper#isWrappedResource(CmsObject, CmsResource) 133 */ 134 public boolean isWrappedResource(CmsObject cms, CmsResource res) { 135 136 return checkTypeId(res.getTypeId()); 137 } 138 139 /** 140 * @see org.opencms.file.wrapper.A_CmsResourceWrapper#lockResource(org.opencms.file.CmsObject, java.lang.String, boolean) 141 */ 142 @Override 143 public boolean lockResource(CmsObject cms, String resourcename, boolean temporary) throws CmsException { 144 145 CmsResource res = getResource(cms, resourcename); 146 if (res != null) { 147 String path = cms.getRequestContext().removeSiteRoot(res.getRootPath()); 148 if (temporary) { 149 cms.lockResourceTemporary(path); 150 } else { 151 cms.lockResource(path); 152 } 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 @Override 163 public boolean moveResource(CmsObject cms, String source, String destination) 164 throws CmsException, 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( 172 Messages.get().container(Messages.ERR_BAD_RESOURCE_EXTENSION_1, destination)); 173 } 174 175 cms.moveResource( 176 CmsResourceWrapperUtils.removeFileExtension(cms, source, getExtension()), 177 CmsResourceWrapperUtils.removeFileExtension(cms, destination, getExtension())); 178 return true; 179 } 180 181 return false; 182 } 183 184 /** 185 * @see org.opencms.file.wrapper.A_CmsResourceWrapper#readFile(CmsObject, String, CmsResourceFilter) 186 */ 187 @Override 188 public CmsFile readFile(CmsObject cms, String resourcename, CmsResourceFilter filter) throws CmsException { 189 190 CmsResource res = getResource(cms, resourcename, filter); 191 if (res != null) { 192 193 CmsFile file = cms.readFile(res); 194 CmsWrappedResource wrap = new CmsWrappedResource(file); 195 wrap.setRootPath(CmsResourceWrapperUtils.addFileExtension(cms, res.getRootPath(), getExtension())); 196 197 return wrap.getFile(); 198 } 199 200 return null; 201 } 202 203 /** 204 * @see org.opencms.file.wrapper.A_CmsResourceWrapper#readResource(CmsObject, String, CmsResourceFilter) 205 */ 206 @Override 207 public CmsResource readResource(CmsObject cms, String resourcename, CmsResourceFilter filter) { 208 209 CmsResource res = getResource(cms, resourcename, filter); 210 if (res != null) { 211 212 CmsWrappedResource wrap = new CmsWrappedResource(res); 213 wrap.setRootPath(CmsResourceWrapperUtils.addFileExtension(cms, res.getRootPath(), getExtension())); 214 215 return wrap.getResource(); 216 } 217 218 return null; 219 } 220 221 /** 222 * @see org.opencms.file.wrapper.A_CmsResourceWrapper#restoreLink(org.opencms.file.CmsObject, java.lang.String) 223 */ 224 @Override 225 public String restoreLink(CmsObject cms, String uri) { 226 227 CmsResource res = getResource(cms, uri); 228 if (res != null) { 229 return res.getRootPath(); 230 } 231 232 return null; 233 } 234 235 /** 236 * @see org.opencms.file.wrapper.A_CmsResourceWrapper#rewriteLink(CmsObject, CmsResource) 237 */ 238 @Override 239 public String rewriteLink(CmsObject cms, CmsResource res) { 240 241 if (checkTypeId(res.getTypeId())) { 242 return CmsResourceWrapperUtils.addFileExtension(cms, res.getRootPath(), getExtension()); 243 } 244 245 return null; 246 } 247 248 /** 249 * @see org.opencms.file.wrapper.A_CmsResourceWrapper#unlockResource(org.opencms.file.CmsObject, java.lang.String) 250 */ 251 @Override 252 public boolean unlockResource(CmsObject cms, String resourcename) throws CmsException { 253 254 CmsResource res = getResource(cms, resourcename); 255 if (res != null) { 256 cms.unlockResource(cms.getRequestContext().removeSiteRoot(res.getRootPath())); 257 return true; 258 } 259 260 return false; 261 } 262 263 /** 264 * @see org.opencms.file.wrapper.A_CmsResourceWrapper#wrapResource(CmsObject, CmsResource) 265 */ 266 @Override 267 public CmsResource wrapResource(CmsObject cms, CmsResource res) { 268 269 if (checkTypeId(res.getTypeId())) { 270 271 CmsWrappedResource wrap = new CmsWrappedResource(res); 272 wrap.setRootPath(CmsResourceWrapperUtils.addFileExtension(cms, res.getRootPath(), getExtension())); 273 274 return wrap.getResource(); 275 } 276 277 return res; 278 } 279 280 /** 281 * @see org.opencms.file.wrapper.A_CmsResourceWrapper#writeFile(org.opencms.file.CmsObject, org.opencms.file.CmsFile) 282 */ 283 @Override 284 public CmsFile writeFile(CmsObject cms, CmsFile resource) throws CmsException { 285 286 if (checkTypeId(resource.getTypeId())) { 287 288 CmsWrappedResource wrap = new CmsWrappedResource(resource); 289 wrap.setRootPath(CmsResourceWrapperUtils.removeFileExtension(cms, resource.getRootPath(), getExtension())); 290 291 return cms.writeFile(wrap.getFile()); 292 } 293 294 return null; 295 } 296 297 /** 298 * Checks if the type id belongs to this resource wrapper.<p> 299 * 300 * @param typeId the type id to check 301 * @return true if the type id belongs to this wrapper otherwise false 302 */ 303 protected abstract boolean checkTypeId(int typeId); 304 305 /** 306 * Returns the extension to add and/or remove to/from the resource.<p> 307 * 308 * @return the extension to use 309 */ 310 protected abstract String getExtension(); 311 312 /** 313 * Trys to read the resourcename after removing the file extension and return the 314 * resource if the type id is correct.<p> 315 * 316 * @param cms the initialized CmsObject 317 * @param resourcename the name of the resource to read 318 * 319 * @return the resource or null if not found 320 */ 321 private CmsResource getResource(CmsObject cms, String resourcename) { 322 323 return getResource(cms, resourcename, CmsResourceFilter.DEFAULT); 324 } 325 326 /** 327 * Trys to read the resourcename after removing the file extension and return the 328 * resource if the type id is correct.<p> 329 * 330 * @param cms the initialized CmsObject 331 * @param resourcename the name of the resource to read 332 * @param filter the resource filter to use while reading 333 * 334 * @return the resource or null if not found 335 */ 336 private CmsResource getResource(CmsObject cms, String resourcename, CmsResourceFilter filter) { 337 338 CmsResource res = null; 339 340 try { 341 res = cms.readResource( 342 CmsResourceWrapperUtils.removeFileExtension(cms, resourcename, getExtension()), 343 filter); 344 } catch (CmsException ex) { 345 return null; 346 } 347 348 if (checkTypeId(res.getTypeId())) { 349 return res; 350 } 351 352 return null; 353 } 354 355}