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

📄 cmsrequestcontext.java

📁 cms是开源的框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/src/org/opencms/file/CmsRequestContext.java,v $
 * Date   : $Date: 2006/03/27 14:52:41 $
 * Version: $Revision: 1.29 $
 *
 * 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.file;

import org.opencms.main.CmsRuntimeException;
import org.opencms.util.CmsResourceTranslator;
import org.opencms.workplace.CmsWorkplace;

import java.util.Hashtable;
import java.util.Locale;
import java.util.Map;

/**
 * Stores the information about the current users OpenCms context,
 * for example the requested URI, the current project, the selected site and more.<p>  
 *
 * @author Alexander Kandzior 
 * @author Michael Emmerich 
 *
 * @version $Revision: 1.29 $
 * 
 * @since 6.0.0 
 */
public final class CmsRequestContext {

    /** Request context attribute for indicating that an editor is currently open. */
    public static final String ATTRIBUTE_EDITOR = "org.opencms.file.CmsRequestContext.ATTRIBUTE_EDITOR";

    /** A map for storing (optional) request context attributes. */
    private Map m_attributeMap;

    /** The current project. */
    private CmsProject m_currentProject;

    /** Directory name translator. */
    private CmsResourceTranslator m_directoryTranslator;

    /** Current encoding. */
    private String m_encoding;

    /** File name translator. */
    private CmsResourceTranslator m_fileTranslator;

    /** The locale for this request. */
    private Locale m_locale;

    /** The remote ip address. */
    private String m_remoteAddr;

    /** The current request time. */
    private long m_requestTime;

    /** Used to save / restore a site root .*/
    private String m_savedSiteRoot;

    /** The name of the root, e.g. /site_a/vfs. */
    private String m_siteRoot;

    /** Flag to indicate that this context should not update the user session. */
    private boolean m_updateSession;

    /** The URI for getUri() in case it is "overwritten".  */
    private String m_uri;

    /** The current user. */
    private CmsUser m_user;

    /**
     * Constructs a new request context.<p>
     * 
     * @param user the current user
     * @param project the current project
     * @param requestedUri the requested OpenCms VFS URI
     * @param siteRoot the users current site root
     * @param locale the users current locale 
     * @param encoding the encoding to use for this request
     * @param remoteAddr the remote IP address of the user
     * @param directoryTranslator the directory translator
     * @param fileTranslator the file translator
     */
    public CmsRequestContext(
        CmsUser user,
        CmsProject project,
        String requestedUri,
        String siteRoot,
        Locale locale,
        String encoding,
        String remoteAddr,
        CmsResourceTranslator directoryTranslator,
        CmsResourceTranslator fileTranslator) {

        m_updateSession = true;
        m_user = user;
        m_currentProject = project;
        m_uri = requestedUri;
        setSiteRoot(siteRoot);
        m_locale = locale;
        m_encoding = encoding;
        m_remoteAddr = remoteAddr;
        m_directoryTranslator = directoryTranslator;
        m_fileTranslator = fileTranslator;
        m_requestTime = System.currentTimeMillis();
    }

    /**
     * Returns the adjusted site root for a resoure using the provided site root as a base.<p>
     * 
     * Usually, this would be the site root for the current site.
     * However, if a resource from the <code>/system/</code> folder is requested,
     * this will be the empty String.<p>
     * 
     * @param siteRoot the site root to use
     * @param resourcename the resource name to get the adjusted site root for
     * @return the adjusted site root for the resoure
     */
    public static String getAdjustedSiteRoot(String siteRoot, String resourcename) {

        if (resourcename.startsWith(CmsWorkplace.VFS_PATH_SYSTEM)) {
            return "";
        } else {
            return siteRoot;
        }
    }

    /**
     * Adds the current site root of this context to the given resource name,
     * and also translates the resource name with the configured the directory translator.<p>
     * 
     * @param resourcename the resource name
     * @return the translated resource name including site root
     * @see #addSiteRoot(String, String)
     */
    public String addSiteRoot(String resourcename) {

        return addSiteRoot(m_siteRoot, resourcename);
    }

    /**
     * Adds the given site root of this context to the given resource name,
     * taking into account special folders like "/system" where no site root must be added,
     * and also translates the resource name with the configured the directory translator.<p>
     * 
     * @param siteRoot the site root to add
     * @param resourcename the resource name
     * @return the translated resource name including site root
     */
    public String addSiteRoot(String siteRoot, String resourcename) {

        if ((resourcename == null) || (siteRoot == null)) {
            return null;
        }
        siteRoot = getAdjustedSiteRoot(siteRoot, resourcename);
        StringBuffer result = new StringBuffer(128);
        result.append(siteRoot);
        if (((siteRoot.length() == 0) || (siteRoot.charAt(siteRoot.length() - 1) != '/'))
            && ((resourcename.length() == 0) || (resourcename.charAt(0) != '/'))) {
            // add slash between site root and resource if required
            result.append('/');
        }
        result.append(resourcename);
        return m_directoryTranslator.translateResource(result.toString());
    }

    /**
     * Returns the current project of the current user.
     *
     * @return the current project of the current user
     */
    public CmsProject currentProject() {

        return m_currentProject;
    }

    /**
     * Returns the current user object.<p>
     *
     * @return the current user object
     */
    public CmsUser currentUser() {

        return m_user;
    }

    /**
     * Returns the adjusted site root for a resoure this context current site root.<p>
     * 
     * @param resourcename the resource name to get the adjusted site root for
     * @return the adjusted site root for the resoure
     * @see #getAdjustedSiteRoot(String, String)
     */
    public String getAdjustedSiteRoot(String resourcename) {

        return getAdjustedSiteRoot(m_siteRoot, resourcename);
    }

    /**
     * Gets the value of an attribute from the OpenCms request context attribute list.<p>
     * 
     * @param attributeName the attribute name
     * @return Object the attribute value, or <code>null</code> if the attribute was not found
     */
    public Object getAttribute(String attributeName) {

        if (m_attributeMap == null) {
            return null;
        }
        return m_attributeMap.get(attributeName);
    }

    /**
     * Returns the directory name translator this context was initialized with.<p>
     * 
     * The directory translator is used to translate old VFS path information 
     * to a new location. Example: <code>/bodys/index.html --> /system/bodies/</code>.<p>
     * 
     * @return the directory name translator this context was initialized with
     */
    public CmsResourceTranslator getDirectoryTranslator() {

        return m_directoryTranslator;
    }

    /**
     * Returns the current content encoding to be used in HTTP response.<p>
     * 
     * @return the encoding
     */
    public String getEncoding() {

        return m_encoding;
    }

⌨️ 快捷键说明

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