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.ade.detailpage;
029
030import org.opencms.ade.configuration.CmsADEConfigData;
031import org.opencms.ade.configuration.CmsADEManager;
032import org.opencms.configuration.CmsParameterConfiguration;
033import org.opencms.file.CmsObject;
034import org.opencms.file.CmsResource;
035import org.opencms.main.CmsException;
036import org.opencms.main.OpenCms;
037import org.opencms.site.CmsSite;
038import org.opencms.workplace.CmsWorkplace;
039
040import java.util.ArrayList;
041import java.util.Arrays;
042import java.util.Collection;
043import java.util.List;
044
045/**
046 * Original detail page handler implementing the detail page logic from OpenCms versions up to 11.0.
047 *
048 * <p>Disregards the 'exclude external detail contents' option, allows all combinations of detail page / detail content on the same site.
049 */
050public class CmsPermissiveDetailPageHandler implements I_CmsDetailPageHandler {
051
052    /** The configuration. */
053    private CmsParameterConfiguration m_config = new CmsParameterConfiguration();
054
055    /**
056     * Gets the detail page for a content element.<p>
057     *
058     * @param manager the ADE manager instance.
059     * @param cms the CMS context
060     * @param pageRootPath the element's root path
061     * @param originPath the path in which the the detail page is being requested
062     * @param targetDetailPage the target detail page to use
063     *
064     * @return the detail page for the content element
065     */
066    public static String getDetailPage(
067        CmsADEManager manager,
068        CmsObject cms,
069        String pageRootPath,
070        String originPath,
071        String targetDetailPage) {
072
073        boolean online = cms.getRequestContext().getCurrentProject().isOnlineProject();
074        String resType = manager.getParentFolderType(online, pageRootPath);
075        if (resType == null) {
076            return null;
077        }
078        if ((targetDetailPage != null) && manager.getDetailPages(cms, resType).contains(targetDetailPage)) {
079            return targetDetailPage;
080        }
081
082        String originRootPath = cms.getRequestContext().addSiteRoot(originPath);
083        CmsADEConfigData configData = manager.lookupConfiguration(cms, originRootPath);
084        CmsADEConfigData targetConfigData = manager.lookupConfiguration(cms, pageRootPath);
085        boolean targetFirst = targetConfigData.isPreferDetailPagesForLocalContents();
086        List<CmsADEConfigData> configs = targetFirst
087        ? Arrays.asList(targetConfigData, configData)
088        : Arrays.asList(configData, targetConfigData);
089        for (CmsADEConfigData config : configs) {
090            CmsDetailPageFilter filter = new CmsDetailPageFilter(cms, pageRootPath);
091            List<CmsDetailPageInfo> pageInfo = config.getDetailPagesForType(resType);
092            String uri = filter.filterDetailPages(pageInfo).map(detailPage -> detailPage.getUri()).findFirst().orElse(
093                null);
094            if (uri != null) {
095                return uri;
096            }
097        }
098        return null;
099
100    }
101
102    /**
103     * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#addConfigurationParameter(java.lang.String, java.lang.String)
104     */
105    public void addConfigurationParameter(String paramName, String paramValue) {
106
107        m_config.add(paramName, paramValue);
108
109    }
110
111    /**
112     * @see org.opencms.ade.detailpage.I_CmsDetailPageHandler#getAllDetailPages(org.opencms.file.CmsObject, int)
113     */
114    public Collection<String> getAllDetailPages(CmsObject cms, int resType) throws CmsException {
115
116        if (!OpenCms.getADEManager().isInitialized()) {
117            return new ArrayList<String>();
118        }
119        String typeName = OpenCms.getResourceManager().getResourceType(resType).getTypeName();
120        return OpenCms.getADEManager().getDetailPages(cms, typeName);
121    }
122
123    /**
124     * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#getConfiguration()
125     */
126    public CmsParameterConfiguration getConfiguration() {
127
128        return m_config;
129
130    }
131
132    /**
133     * @see org.opencms.ade.detailpage.I_CmsDetailPageHandler#getDetailPage(org.opencms.file.CmsObject, java.lang.String, java.lang.String, java.lang.String)
134     */
135    public String getDetailPage(CmsObject cms, String rootPath, String linkSource, String targetDetailPage) {
136
137        CmsADEManager manager = OpenCms.getADEManager();
138        if (!manager.isInitialized()) {
139            return null;
140        }
141
142        if (rootPath.endsWith(".jsp") || rootPath.startsWith(CmsWorkplace.VFS_PATH_WORKPLACE)) {
143            // exclude these for performance reasons
144            return null;
145        }
146        String result = getDetailPage(manager, cms, rootPath, linkSource, targetDetailPage);
147        if (result == null) {
148            return null;
149        }
150        if (!CmsResource.isFolder(result)) {
151            result = CmsResource.getFolderPath(result);
152        }
153        return result;
154    }
155
156    /**
157     * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#initConfiguration()
158     */
159    public void initConfiguration() {
160
161        m_config = CmsParameterConfiguration.unmodifiableVersion(m_config);
162
163    }
164
165    /**
166     * @see org.opencms.ade.detailpage.I_CmsDetailPageHandler#initialize(org.opencms.file.CmsObject, org.opencms.file.CmsObject)
167     */
168    public void initialize(CmsObject offlineCms, CmsObject onlineCms) {
169
170        // do nothing
171    }
172
173    /**
174     * Checks whether the given detail page is valid for the given resource.<p>
175     *
176     * @param cms the CMS context
177     * @param page the detail page
178     * @param detailRes the detail resource
179     *
180     * @return true if the given detail page is valid
181     */
182    public boolean isValidDetailPage(CmsObject cms, CmsResource page, CmsResource detailRes) {
183
184        if (OpenCms.getSystemInfo().isRestrictDetailContents()) {
185            // in 'restrict detail contents mode', do not allow detail contents from a real site on a detail page of a different real site
186            CmsSite pageSite = OpenCms.getSiteManager().getSiteForRootPath(page.getRootPath());
187            CmsSite detailSite = OpenCms.getSiteManager().getSiteForRootPath(detailRes.getRootPath());
188            if ((pageSite != null)
189                && (detailSite != null)
190                && !pageSite.getSiteRoot().equals(detailSite.getSiteRoot())) {
191                return false;
192            }
193        }
194        return OpenCms.getADEManager().isDetailPage(cms, page);
195    }
196
197}