cmsrequestcontext.java
来自「找了很久才找到到源代码」· Java 代码 · 共 613 行 · 第 1/2 页
JAVA
613 行
/*
* File : $Source: /usr/local/cvs/opencms/src/org/opencms/file/CmsRequestContext.java,v $
* Date : $Date: 2007-08-31 16:08:14 $
* Version: $Revision: 1.34 $
*
* This library is part of OpenCms -
* the Open Source Content Management System
*
* Copyright (c) 2002 - 2007 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.CmsIllegalArgumentException;
import org.opencms.main.CmsRuntimeException;
import org.opencms.security.CmsOrganizationalUnit;
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.34 $
*
* @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 = CmsRequestContext.class.getName() + ".ATTRIBUTE_EDITOR";
/** Request context attribute for indicating we want full links generated for HTML fields. */
public static final String ATTRIBUTE_FULLLINKS = CmsRequestContext.class.getName() + ".ATTRIBUTE_FULLLINKS";
/** Request context attribute for indicating the model file for a create resource operation. */
public static final String ATTRIBUTE_MODEL = CmsRequestContext.class.getName() + ".ATTRIBUTE_MODEL";
/** 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 fully qualified name of the organizational unit for this request. */
private String m_ouFqn;
/** 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 requestTime the time of the request (used for resource publication / expiration date)
* @param directoryTranslator the directory translator
* @param fileTranslator the file translator
* @param ouFqn the fully qualified name of the organizational unit
*/
public CmsRequestContext(
CmsUser user,
CmsProject project,
String requestedUri,
String siteRoot,
Locale locale,
String encoding,
String remoteAddr,
long requestTime,
CmsResourceTranslator directoryTranslator,
CmsResourceTranslator fileTranslator,
String ouFqn) {
m_updateSession = true;
m_user = user;
m_currentProject = project;
m_uri = requestedUri;
setSiteRoot(siteRoot);
m_locale = locale;
m_encoding = encoding;
m_remoteAddr = remoteAddr;
m_requestTime = requestTime;
m_directoryTranslator = directoryTranslator;
m_fileTranslator = fileTranslator;
setOuFqn(ouFqn);
}
/**
* 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 of the current site
* @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;
}
/**
* Returns the file name translator this context was initialized with.<p>
*
* The file name translator is used to translate filenames from uploaded files
* to valid OpenCms filenames. Example: <code>W黶te W鰎ter.doc --> Wueste_Woerter.doc</code>.<p>
*
* @return the file name translator this context was initialized with
*/
public CmsResourceTranslator getFileTranslator() {
return m_fileTranslator;
}
/**
* Gets the name of the parent folder of the requested file.<p>
*
* @return the name of the parent folder of the requested file
*/
public String getFolderUri() {
return CmsResource.getFolderPath(m_uri);
}
/**
* Returns the name of the requested locale within this context.<p>
*
* @return the name of the locale
*/
public Locale getLocale() {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?