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.file.wrapper;
029
030import org.opencms.db.Messages;
031import org.opencms.file.CmsObject;
032import org.opencms.file.CmsProperty;
033import org.opencms.file.CmsResource;
034import org.opencms.main.CmsException;
035import org.opencms.main.CmsIllegalArgumentException;
036import org.opencms.security.CmsPermissionViolationException;
037import org.opencms.util.CmsStringUtil;
038
039import java.util.HashSet;
040import java.util.List;
041import java.util.Set;
042
043/**
044 * Resource wrapper class which is used to prevent resources with a certain name from being created.<p>
045 *
046 * This can be used to prevent file browsers accessing the repository from creating thumbnail files.
047 * This class does not distinguish between upper case and lower case letters in file names.
048 */
049public class CmsResourceWrapperPreventCreateNameCI extends A_CmsResourceWrapper {
050
051    /** File name separator. */
052    public static final String NAME_SEPARATOR = "|";
053
054    /**
055     * The set of illegal file names.<p>
056     */
057    private Set<String> m_disabledNames = new HashSet<String>();
058
059    /**
060     * @see org.opencms.file.wrapper.A_CmsResourceWrapper#configure(java.lang.String)
061     */
062    @Override
063    public void configure(String configString) {
064
065        List<String> tokens = CmsStringUtil.splitAsList(configString, NAME_SEPARATOR);
066        for (String token : tokens) {
067            m_disabledNames.add(token.trim().toLowerCase());
068        }
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 resourcepath,
078        int type,
079        byte[] content,
080        List<CmsProperty> properties) throws CmsIllegalArgumentException {
081
082        String name = CmsResource.getName(resourcepath);
083        if (m_disabledNames.contains(name.toLowerCase())) {
084            throw new CmsSilentWrapperException(
085                new CmsPermissionViolationException(
086                    Messages.get().container(Messages.ERR_PERM_DENIED_2, resourcepath, "+c")));
087        } else {
088            return null;
089        }
090    }
091
092    /**
093     * @see org.opencms.file.wrapper.I_CmsResourceWrapper#isWrappedResource(org.opencms.file.CmsObject, org.opencms.file.CmsResource)
094     */
095    public boolean isWrappedResource(CmsObject cms, CmsResource res) {
096
097        return false;
098    }
099
100    /**
101     * @see org.opencms.file.wrapper.A_CmsResourceWrapper#moveResource(org.opencms.file.CmsObject, java.lang.String, java.lang.String)
102     */
103    @Override
104    public boolean moveResource(CmsObject cms, String source, String destination)
105    throws CmsException, CmsIllegalArgumentException {
106
107        String name = CmsResource.getName(destination);
108
109        if (m_disabledNames.contains(name)) {
110            throw new CmsPermissionViolationException(
111                Messages.get().container(Messages.ERR_PERM_DENIED_2, destination, "+c"));
112        } else {
113            return false;
114        }
115    }
116
117}