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 GmbH & Co. KG, 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.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.file.CmsResourceFilter;
033import org.opencms.main.CmsException;
034import org.opencms.util.CmsStringUtil;
035import org.opencms.workplace.CmsWorkplace;
036
037import java.util.ArrayList;
038import java.util.List;
039
040/**
041 * Adds the system folder to every root folder of target sites.<p>
042 *
043 * @since 6.5.6
044 */
045public class CmsResourceWrapperSystemFolder extends A_CmsResourceWrapper {
046
047    /**
048     * @see org.opencms.file.wrapper.A_CmsResourceWrapper#addResourcesToFolder(CmsObject, String, CmsResourceFilter)
049     */
050    @Override
051    public List<CmsResource> addResourcesToFolder(CmsObject cms, String resourcename, CmsResourceFilter filter)
052    throws CmsException {
053
054        if (!resourcename.endsWith("/")) {
055            resourcename += "/";
056        }
057
058        // if this is the root folder of a target site, add the system folder
059        if (resourcename.equals("/")) {
060            if (!CmsStringUtil.isEmptyOrWhitespaceOnly(cms.getRequestContext().getSiteRoot())) {
061                List<CmsResource> ret = new ArrayList<CmsResource>();
062                ret.add(readResource(cms, CmsWorkplace.VFS_PATH_SYSTEM, filter));
063                return ret;
064            }
065        }
066
067        return null;
068    }
069
070    /**
071     * @see org.opencms.file.wrapper.I_CmsResourceWrapper#isWrappedResource(CmsObject, CmsResource)
072     */
073    public boolean isWrappedResource(CmsObject cms, CmsResource res) {
074
075        if (res.isFolder()) {
076            if (!cms.getRequestContext().getSiteRoot().equals("/")) {
077
078                String resourcename = cms.getRequestContext().removeSiteRoot(res.getRootPath());
079
080                if (!resourcename.endsWith("/")) {
081                    resourcename += "/";
082                }
083
084                if (resourcename.equals("/")) {
085                    return true;
086                }
087            }
088        }
089
090        return false;
091    }
092
093    /**
094     * @see org.opencms.file.wrapper.A_CmsResourceWrapper#readResource(org.opencms.file.CmsObject, java.lang.String, org.opencms.file.CmsResourceFilter)
095     */
096    @Override
097    public CmsResource readResource(CmsObject cms, String resourcename, CmsResourceFilter filter) throws CmsException {
098
099        // only valid if site root is a target site
100        if (!cms.getRequestContext().getSiteRoot().equals("/")) {
101
102            if (!resourcename.endsWith("/")) {
103                resourcename += "/";
104            }
105
106            // if accessing the system folder switch temporarily to the root site
107            if (resourcename.equals(CmsWorkplace.VFS_PATH_SYSTEM)) {
108
109                // set site root to the root folder
110                String siteRoot = cms.getRequestContext().getSiteRoot();
111                cms.getRequestContext().setSiteRoot("/");
112
113                // read the resource with the correct site root
114                CmsResource res = cms.readResource(resourcename, filter);
115
116                // reset the site root back to the original
117                cms.getRequestContext().setSiteRoot(siteRoot);
118
119                // adjust the root path in the resource
120                CmsWrappedResource wrap = new CmsWrappedResource(res);
121                wrap.setRootPath(cms.getRequestContext().getSiteRoot() + resourcename);
122
123                return wrap.getResource();
124            }
125        }
126
127        return null;
128    }
129}