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, 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.publish;
029
030import org.opencms.db.CmsPublishList;
031import org.opencms.file.CmsResource;
032import org.opencms.main.CmsException;
033import org.opencms.util.CmsStringUtil;
034import org.opencms.util.CmsUUID;
035
036import java.util.concurrent.ConcurrentHashMap;
037
038/**
039 * Performs some additional checks on publish lists to prevent inconsistent VFS states.<p>
040 */
041public class CmsPublishListVerifier {
042
043    /**
044     * Entry for parent folders in which it is not allowed to publish resources.<p>
045     *
046     * Also contains a field for the reason why publishing is not allowed.
047     */
048    private class ForbiddenFolderEntry {
049
050        /** Reason why publishing is not allowed. */
051        String m_reason;
052
053        /** Root path of the forbidden parent folder. */
054        String m_rootPath;
055
056        /**
057         * Creates a new entry.<p>
058         *
059         * @param rootPath the root path of the forbidden entry
060         * @param reason the reason why publishing is not allowed
061         */
062        public ForbiddenFolderEntry(String rootPath, String reason) {
063
064            m_rootPath = rootPath;
065            m_reason = reason;
066        }
067
068        /**
069         * Returns the reason.<p>
070         *
071         * @return the reason
072         */
073        public String getReason() {
074
075            return m_reason;
076        }
077
078        /**
079         * Returns the rootPath.<p>
080         *
081         * @return the rootPath
082         */
083        public String getRootPath() {
084
085            return m_rootPath;
086        }
087    }
088
089    /** Forbidden parent folders, with UUIDs as keys. */
090    private ConcurrentHashMap<CmsUUID, ForbiddenFolderEntry> m_forbiddenParentFolders = new ConcurrentHashMap<CmsUUID, ForbiddenFolderEntry>();
091
092    /**
093     * Creates a new instance.<p>
094     */
095    public CmsPublishListVerifier() {
096
097        // do nothing
098    }
099
100    /**
101     * Adds a forbidden parent folder.<p>
102     *
103     * @param parentFolder the forbidden parent folder
104     * @param reason the reason why publishing is not allowed
105     * @return an ID which can be used later to remove the forbidden parent folder
106     */
107    public CmsUUID addForbiddenParentFolder(String parentFolder, String reason) {
108
109        CmsUUID id = new CmsUUID();
110        m_forbiddenParentFolders.put(id, new ForbiddenFolderEntry(parentFolder, reason));
111        return id;
112    }
113
114    /**
115     * Checks whether the given publish job is OK, and throws an exception otherwise.<p>
116     *
117     * @param publishList the publish list
118     * @throws CmsException if there's something wrong with the publish job
119     */
120    public void checkPublishList(CmsPublishList publishList) throws CmsException {
121
122        for (CmsResource resource : publishList.getAllResources()) {
123            for (ForbiddenFolderEntry entry : m_forbiddenParentFolders.values()) {
124                if (CmsStringUtil.isPrefixPath(entry.getRootPath(), resource.getRootPath())) {
125                    throw new CmsPublishException(
126                        Messages.get().container(
127                            Messages.ERR_PUBLISH_FORBIDDEN_PARENT_FOLDER_3,
128                            resource.getRootPath(),
129                            entry.getRootPath(),
130                            entry.getReason()));
131                }
132            }
133
134        }
135    }
136
137    /**
138     * Removes the forbidden parent folder using the id obtained while it was added.<p>
139     *
140     * @param id the id to remove the parent folder
141     */
142    public void removeForbiddenParentFolder(CmsUUID id) {
143
144        m_forbiddenParentFolders.remove(id);
145    }
146
147}