001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH (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.gwt; 029 030import org.opencms.file.CmsObject; 031import org.opencms.file.CmsResource; 032import org.opencms.file.CmsVfsResourceNotFoundException; 033import org.opencms.gwt.shared.CmsValidationResult; 034import org.opencms.main.CmsException; 035import org.opencms.main.CmsRuntimeException; 036import org.opencms.util.CmsStringUtil; 037import org.opencms.util.CmsUUID; 038 039import java.util.Map; 040 041/** 042 * Validation class which both translates a sitemap URL name and checks whether it already exists in a '|'-separated 043 * list of URL names which is passed as a configuration parameter.<p> 044 * 045 * @since 8.0.0 046 */ 047public class CmsUrlNameValidationService implements I_CmsValidationService { 048 049 /** 050 * @see org.opencms.gwt.I_CmsValidationService#validate(org.opencms.file.CmsObject, java.lang.String, java.lang.String) 051 */ 052 public CmsValidationResult validate(CmsObject cms, String value, String config) { 053 054 String name = cms.getRequestContext().getFileTranslator().translateResource(value); 055 name = name.replace('/', '_'); 056 057 Map<String, String> configMap = CmsStringUtil.splitAsMap(config, "|", ":"); 058 String parentPath = configMap.get("parent"); 059 String id = configMap.get("id"); 060 try { 061 CmsResource res = cms.readResource(CmsStringUtil.joinPaths(parentPath, name)); 062 // file already exists 063 if (!CmsUUID.isValidUUID(id) || res.getStructureId().toString().equals(id)) { 064 // no problem, it's the same resource 065 return new CmsValidationResult(null, name); 066 } else { 067 // it's a different resource, so we fail 068 return new CmsValidationResult(org.opencms.gwt.Messages.get().getBundle().key( 069 org.opencms.gwt.Messages.ERR_URL_NAME_ALREADY_EXISTS_1, 070 name)); 071 } 072 } catch (CmsVfsResourceNotFoundException e) { 073 // ok, the resource was not found 074 return new CmsValidationResult(null, name); 075 } catch (CmsException e) { 076 throw new CmsRuntimeException(e.getMessageContainer()); 077 078 } 079 } 080}