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, 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.ade.postupload; 029 030import org.opencms.file.CmsObject; 031import org.opencms.file.CmsResource; 032import org.opencms.file.CmsResourceFilter; 033import org.opencms.gwt.I_CmsValidationService; 034import org.opencms.gwt.shared.CmsValidationResult; 035import org.opencms.main.CmsLog; 036import org.opencms.main.OpenCms; 037import org.opencms.security.CmsPermissionViolationException; 038import org.opencms.util.CmsStringUtil; 039import org.opencms.util.CmsUUID; 040 041import java.util.Locale; 042 043import org.apache.commons.logging.Log; 044 045/** 046 * Checks whether an uploaded file's modified name is already used by another file. 047 */ 048public class CmsServerFilenameValidator implements I_CmsValidationService { 049 050 private static final Log LOG = CmsLog.getLog(CmsServerFilenameValidator.class); 051 052 /** 053 * @see org.opencms.gwt.I_CmsValidationService#validate(org.opencms.file.CmsObject, java.lang.String, java.lang.String) 054 */ 055 @Override 056 public CmsValidationResult validate(CmsObject cms, String value, String config) { 057 058 Locale locale = OpenCms.getWorkplaceManager().getWorkplaceLocale(cms); 059 CmsUUID structureId = new CmsUUID(config); 060 try { 061 CmsResource parentFolder = cms.readParentFolder(structureId); 062 String pathToCheck = CmsStringUtil.joinPaths(cms.getSitePath(parentFolder), value); 063 boolean otherFileExists = false; 064 try { 065 CmsResource res = cms.readResource(pathToCheck, CmsResourceFilter.ALL); 066 otherFileExists = !structureId.equals(res.getStructureId()); 067 } catch (CmsPermissionViolationException e) { 068 otherFileExists = true; 069 } catch (Exception e) { 070 otherFileExists = false; 071 } 072 if (otherFileExists) { 073 return new CmsValidationResult( 074 Messages.get().getBundle(locale).key(Messages.GUI_POSTUPLOAD_FILE_EXISTS_0)); 075 } else { 076 return CmsValidationResult.VALIDATION_OK; 077 } 078 } catch (Exception e) { 079 LOG.info(e.getLocalizedMessage(), e); 080 return new CmsValidationResult(e.getLocalizedMessage()); 081 } 082 } 083 084}