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;
037
038import java.util.List;
039import java.util.regex.Pattern;
040
041/**
042 * Resource wrapper class which is used to prevent resources with names matching a given regex from being created.<p>
043 */
044public class CmsResourceWrapperPreventCreateNameRegex extends A_CmsResourceWrapper {
045
046    /** The pattern for file names which should be prevented from being created. */
047    private Pattern m_pattern;
048
049    /**
050     * @see org.opencms.file.wrapper.A_CmsResourceWrapper#configure(java.lang.String)
051     */
052    @Override
053    public void configure(String configString) {
054
055        m_pattern = Pattern.compile(configString);
056    }
057
058    /**
059     * @see org.opencms.file.wrapper.A_CmsResourceWrapper#createResource(org.opencms.file.CmsObject, java.lang.String, int, byte[], java.util.List)
060     */
061    @Override
062    public CmsResource createResource(
063        CmsObject cms,
064        String resourcepath,
065        int type,
066        byte[] content,
067        List<CmsProperty> properties)
068    throws CmsIllegalArgumentException {
069
070        String name = CmsResource.getName(resourcepath);
071        if (m_pattern.matcher(name).matches()) {
072            throw new CmsSilentWrapperException(
073                new CmsPermissionViolationException(
074                    Messages.get().container(Messages.ERR_PERM_DENIED_2, resourcepath, "+c")));
075        } else {
076            return null;
077        }
078    }
079
080    /**
081     * @see org.opencms.file.wrapper.I_CmsResourceWrapper#isWrappedResource(org.opencms.file.CmsObject, org.opencms.file.CmsResource)
082     */
083    public boolean isWrappedResource(CmsObject cms, CmsResource res) {
084
085        return false;
086    }
087
088    /**
089     * @see org.opencms.file.wrapper.A_CmsResourceWrapper#moveResource(org.opencms.file.CmsObject, java.lang.String, java.lang.String)
090     */
091    @Override
092    public boolean moveResource(CmsObject cms, String source, String destination)
093    throws CmsException, CmsIllegalArgumentException {
094
095        String name = CmsResource.getName(destination);
096
097        if (m_pattern.matcher(name).matches()) {
098            throw new CmsPermissionViolationException(
099                Messages.get().container(Messages.ERR_PERM_DENIED_2, destination, "+c"));
100        } else {
101            return false;
102        }
103    }
104
105}