⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmssitemanager.java

📁 OpenCms 是一个J2EE的产品
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/src/org/opencms/site/CmsSiteManager.java,v $
 * Date   : $Date: 2006/04/28 15:20:52 $
 * Version: $Revision: 1.52 $
 *
 * This library is part of OpenCms -
 * the Open Source Content Mananagement System
 *
 * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * For further information about Alkacon Software GmbH, please see the
 * company website: http://www.alkacon.com
 *
 * For further information about OpenCms, please see the
 * project website: http://www.opencms.org
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.opencms.site;

import org.opencms.configuration.CmsConfigurationException;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsPropertyDefinition;
import org.opencms.file.CmsResource;
import org.opencms.main.CmsException;
import org.opencms.main.CmsLog;
import org.opencms.main.CmsRuntimeException;
import org.opencms.main.OpenCms;
import org.opencms.security.CmsPermissionSet;
import org.opencms.security.CmsRole;
import org.opencms.util.CmsStringUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;

/**
 * Manages all configured sites in OpenCms.<p>
 *
 * @author  Alexander Kandzior 
 *
 * @version $Revision: 1.52 $ 
 * 
 * @since 6.0.0 
 */
public final class CmsSiteManager implements Cloneable {

    /** The static log object for this class. */
    private static final Log LOG = CmsLog.getLog(CmsSiteManager.class);

    /** The site that is configured at the moment, need to be recorded for the case that
     * alias server are added during configuration. */
    private List m_aliases;

    /** The default site root. */
    private CmsSite m_defaultSite;

    /** The default uri. */
    private String m_defaultUri;

    /** Indicates if the configuration is finalized (frozen). */
    private boolean m_frozen;

    /** The set of all configured site root paths (as String). */
    private Set m_siteRoots;

    /** The map of configured sites. */
    private Map m_sites;

    /** The workplace server. */
    private String m_workplaceServer;

    /** The site matcher that matches the workplace site. */
    private CmsSiteMatcher m_workplaceSiteMatcher;

    /**
     * Creates a new CmsSiteManager.<p>
     *
     */
    public CmsSiteManager() {

        m_sites = new HashMap();
        m_siteRoots = new HashSet();
        m_aliases = new ArrayList();

        if (CmsLog.INIT.isInfoEnabled()) {
            CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_START_SITE_CONFIG_0));
        }
    }

    /**
     * Returns a list of all site available for the current user.<p>
     * 
     * @param cms the current cms context 
     * @param workplaceMode if true, the root and current site is included for the admin user
     * and the view permission is required to see the site root
     * @return a list of all site available for the current user
     */
    public static List getAvailableSites(CmsObject cms, boolean workplaceMode) {

        Map sites = OpenCms.getSiteManager().getSites();
        List siteroots = new ArrayList(sites.size() + 1);
        Map siteServers = new HashMap(sites.size() + 1);
        List result = new ArrayList(sites.size() + 1);

        Iterator i;
        // add site list
        i = sites.keySet().iterator();
        while (i.hasNext()) {
            CmsSite site = (CmsSite)sites.get(i.next());
            String folder = site.getSiteRoot() + "/";
            if (!siteroots.contains(folder)) {
                siteroots.add(folder);
                siteServers.put(folder, site.getSiteMatcher());
            }
        }
        // add default site
        if (workplaceMode && OpenCms.getSiteManager().getDefaultSite() != null) {
            String folder = OpenCms.getSiteManager().getDefaultSite().getSiteRoot() + "/";
            if (!siteroots.contains(folder)) {
                siteroots.add(folder);
            }
        }

        String currentRoot = cms.getRequestContext().getSiteRoot();
        cms.getRequestContext().saveSiteRoot();
        try {
            // for all operations here we need no context
            cms.getRequestContext().setSiteRoot("/");
            if (workplaceMode && cms.hasRole(CmsRole.ROOT_FOLDER_ACCESS)) {
                if (!siteroots.contains("/")) {
                    siteroots.add("/");
                }
                if (!siteroots.contains(currentRoot + "/")) {
                    siteroots.add(currentRoot + "/");
                }
            }
            Collections.sort(siteroots);
            i = siteroots.iterator();
            while (i.hasNext()) {
                String folder = (String)i.next();
                try {
                    CmsResource res = cms.readResource(folder);
                    if (!workplaceMode || cms.hasPermissions(res, CmsPermissionSet.ACCESS_VIEW)) {
                        String title = cms.readPropertyObject(res, CmsPropertyDefinition.PROPERTY_TITLE, false).getValue();
                        if (title == null) {
                            title = folder;
                        }
                        result.add(new CmsSite(
                            folder,
                            res.getStructureId(),
                            title,
                            (CmsSiteMatcher)siteServers.get(folder)));
                    }

                } catch (CmsException e) {
                    // user probably has no read access to the folder, ignore and continue iterating            
                }
            }
        } catch (Throwable t) {
            LOG.error(Messages.get().getBundle().key(Messages.LOG_READ_SITE_PROP_FAILED_0), t);
        } finally {
            // restore the user's current context 
            cms.getRequestContext().restoreSiteRoot();
        }
        return result;
    }

    /**
     * Returns the current site for the provided cms context object.<p>
     * 
     * @param cms the cms context object to check for the site
     * @return the current site for the provided cms context object
     */
    public static CmsSite getCurrentSite(CmsObject cms) {

        String siteRoot = cms.getRequestContext().getSiteRoot();
        CmsSite site = getSite(siteRoot);
        if (site == null) {
            return OpenCms.getSiteManager().getDefaultSite();
        } else {
            return site;
        }
    }

    /**
     * Returns the site with has the provided site root path, 
     * or null if no configured site has that root path.<p>
     * 
     * @param siteRoot the root path to look up the site for
     * @return the site with has the provided site root path, 
     *      or null if no configured site has that root path
     */
    public static CmsSite getSite(String siteRoot) {

        Map sites = OpenCms.getSiteManager().getSites();
        Iterator i = sites.keySet().iterator();
        while (i.hasNext()) {
            CmsSite site = (CmsSite)sites.get(i.next());
            if (siteRoot.equals(site.getSiteRoot())) {
                return site;
            }
        }
        return null;
    }

    /**
     * Returns the site root part of the resources root path, 
     * or null if the path does not match any site root.<p>
     * 
     * @param path the root path of a resource
     * @return the site root part of the resources root path, or null if the path does not match any site root
     */
    public static String getSiteRoot(String path) {

        Set roots = OpenCms.getSiteManager().getSiteRoots();
        // most sites will be subfolders of the "/sites/" folder, 
        int pos = path.indexOf('/', 7);
        if (pos > 0) {
            String candidate = path.substring(0, pos);
            if (roots.contains(candidate)) {
                return candidate;
            }
        }
        // site root not found as subfolder of "/sites/"
        Iterator i = roots.iterator();
        while (i.hasNext()) {
            String siteRoot = (String)i.next();
            if (path.startsWith(siteRoot)) {
                return siteRoot;
            }
        }
        return null;
    }

    /**
     * Adds an alias to the currently configured site.
     * 
     * @param alias the url of the alias server
     */
    public void addAliasToConfigSite(String alias) {

        CmsSiteMatcher siteMatcher = new CmsSiteMatcher(alias);
        m_aliases.add(siteMatcher);
    }

    /**
     * Adds a new CmsSite to the list of configured sites, 
     * this is only allowed during configuration.<p>
     * 
     * If this method is called after the configuration is finished, 
     * a <code>RuntimeException</code> is thrown.<p>
     * 
     * @param server the Server
     * @param uri the vfs path
     * @param secureServer a secure server, can be <code>null</code>
     * @param exclusive if set to <code>true</code>, secure resources will only be available using the configured secure url
     * @param error if exclusive, and set to <code>true</code> will generate a 404 error, 
     *                             if set to <code>false</code> will redirect to secure url
     * 
     * @throws CmsConfigurationException if the site contains a servername, that is already assigned
     */
    public void addSite(String server, String uri, String secureServer, String exclusive, String error)
    throws CmsConfigurationException {

        if (m_frozen) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -