📄 cmsrequestcontext.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/src/com/opencms/file/CmsRequestContext.java,v $
* Date : $Date: 2003/03/07 16:15:49 $
* Version: $Revision: 1.67 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (C) 2001 The OpenCms Group
*
* 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 OpenCms, please see the
* OpenCms 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 com.opencms.file;
import com.opencms.boot.I_CmsLogChannels;
import com.opencms.core.A_OpenCms;
import com.opencms.core.CmsException;
import com.opencms.core.CmsExportRequest;
import com.opencms.core.CmsSession;
import com.opencms.core.I_CmsConstants;
import com.opencms.core.I_CmsRequest;
import com.opencms.core.I_CmsResponse;
import com.opencms.core.I_CmsSession;
import com.opencms.flex.util.CmsResourceTranslator;
import com.opencms.template.cache.CmsElementCache;
import com.opencms.workplace.I_CmsWpConstants;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
/**
* This class provides access to the CmsRequestContext.
* <br>
* In the CmsRequestContext class are all methods bundled, which can inform about the
* current request properties, like the url or uri of the request.
* <p>
*
* @author Andreas Schouten
* @author Michael Emmerich
* @author Anders Fugmann
* @author Alexander Lucas
*
* @version $Revision: 1.67 $ $Date: 2003/03/07 16:15:49 $
*
*/
public class CmsRequestContext implements I_CmsConstants {
/** The rb to get access to the OpenCms */
private I_CmsResourceBroker m_rb;
/** The current CmsRequest */
private I_CmsRequest m_req;
/** The current CmsResponse */
private I_CmsResponse m_resp;
/** The current user */
private CmsUser m_user;
/** The current group of the user */
private CmsGroup m_currentGroup;
/** The current project */
private CmsProject m_currentProject;
/** Flag to indicate if this response is streaming or not (legacy, not used by Element or Flex cache) */
private boolean m_streaming = true;
/**
* In export mode the links in pages will be stored in this vector
* for further processing.
*/
private Vector m_links;
/** Flag to indicate that this request is event controlled */
private boolean m_eventControlled = false;
/** Flag to indicate that this context should not update the user session */
private boolean m_updateSession = true;
/**
* In export mode this vector is used to store all dependencies this request
* may have. It is saved to the database and if one of the dependencies changes
* the request will be exported again.
*/
private Vector m_dependencies;
/** Starting point for element cache */
private CmsElementCache m_elementCache = null;
/** Current languages */
private Vector m_language = new Vector();
/** The name of the root, e.g. /site_a/vfs */
private String m_siteRoot = C_DEFAULT_SITE + C_ROOTNAME_VFS;
/** Current encoding */
private String m_encoding = null;
/** The URI for getUri() in case it is "overwritten" */
private String m_uri = null;
/** Directroy name translator */
private CmsResourceTranslator m_directoryTranslator = null;
/** File name translator */
private CmsResourceTranslator m_fileTranslator = null;
/** A map for storing (optional) request context attributes */
private HashMap m_attributeMap = null;
/**
* The default constructor.
*/
public CmsRequestContext() {
super();
}
/**
* Initializes this RequestContext.
*
* @param req the CmsRequest.
* @param resp the CmsResponse.
* @param user the current user for this request.
* @param currentGroup the current group for this request.
* @param currentProjectId the id of the current project for this request.
* @param streaming <code>true</code> if streaming should be enabled for this response, <code>false</code> otherwise.
* @param elementCache Starting point for the element cache or <code>null</code> if the element cache should be disabled.
* @param directoryTranslator Translator for directories (file with full path)
* @param fileTranslator Translator for new file names (without path)
* @throws CmsException if operation was not successful.
*/
void init(
I_CmsResourceBroker rb,
I_CmsRequest req,
I_CmsResponse resp,
String user,
String currentGroup,
int currentProjectId,
boolean streaming,
CmsElementCache elementCache,
CmsResourceTranslator directoryTranslator,
CmsResourceTranslator fileTranslator)
throws CmsException {
m_rb = rb;
m_req = req;
m_resp = resp;
m_links = new Vector();
m_dependencies = new Vector();
try {
m_user = m_rb.readUser(null, null, user);
} catch (CmsException ex) {
}
// if no user found try to read webUser
if (m_user == null) {
m_user = m_rb.readWebUser(null, null, user);
}
// check, if the user is disabled
if (m_user.getDisabled() == true) {
m_user = null;
}
// set current project, group and streaming proerties for this request
try {
setCurrentProject(currentProjectId);
} catch (CmsException exc) {
// there was a problem to set the needed project - using the online one
setCurrentProject(I_CmsConstants.C_PROJECT_ONLINE_ID);
}
m_currentGroup = m_rb.readGroup(m_user, m_currentProject, currentGroup);
m_streaming = streaming;
m_elementCache = elementCache;
m_directoryTranslator = directoryTranslator;
m_fileTranslator = fileTranslator;
// Analyze the user's preferred languages coming with the request
if (req != null) {
try {
HttpServletRequest httpReq =
(HttpServletRequest) req.getOriginalRequest();
String accLangs = null;
if (httpReq != null) {
accLangs = httpReq.getHeader("Accept-Language");
}
if (accLangs != null) {
StringTokenizer toks = new StringTokenizer(accLangs, ",");
while (toks.hasMoreTokens()) {
// Loop through all languages and cut off trailing extensions
String current = toks.nextToken().trim();
if (current.indexOf("-") > -1) {
current =
current.substring(0, current.indexOf("-"));
}
if (current.indexOf(";") > -1) {
current =
current.substring(0, current.indexOf(";"));
}
m_language.addElement(current);
}
}
} catch (UnsupportedOperationException e) {
}
// Initialize encoding
initEncoding();
}
}
/**
* Adds a link for the static export.
*/
public void addLink(String link) {
m_links.add(link);
}
/**
* Returns all links that the template mechanism has registered.
*/
public Vector getLinkVector() {
return m_links;
}
/**
* Adds a dependency.
*
* @param dependency. The rootpath of the resource.
*/
public void addDependency(String rootName) {
m_dependencies.add(rootName);
}
/**
* Returns all dependencies the templatemechanism has registered.
*/
public Vector getDependencies() {
return m_dependencies;
}
/**
* Returns the current folder object.
*
* @return the current folder object.
* @throws CmsException if operation was not successful.
*/
public CmsFolder currentFolder() throws CmsException {
return (
m_rb.readFolder(
currentUser(),
currentProject(),
getSiteRoot(getFolderUri())
));
}
/**
* Returns the current group of the current user.
*
* @return the current group of the current user.
*/
public CmsGroup currentGroup() {
return (m_currentGroup);
}
/**
* 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.
*
* @return the current user object.
*/
public CmsUser currentUser() {
return (m_user);
}
/**
* Gets the name of the requested file without any path-information.
*
* @return the requested filename.
*/
public String getFileUri() {
String uri = m_req.getRequestedResource();
uri = uri.substring(uri.lastIndexOf("/") + 1);
return uri;
}
/**
* Gets the name of the parent folder of the requested file
*
* @return the requested filename.
*/
public String getFolderUri() {
return getUri().substring(0, getUri().lastIndexOf("/") + 1);
}
/**
* Gets the current request, if availaible.
*
* @return the current request, if availaible.
*/
public I_CmsRequest getRequest() {
return (m_req);
}
/**
* Gets the current response, if availaible.
*
* @return the current response, if availaible.
*/
public I_CmsResponse getResponse() {
return (m_resp);
}
/**
* Gets the Session for this request.<p>
*
* This method should be used instead of the originalRequest.getSession() method.
*
* @param value indicates, if a session should be created when a session for the particular client does not already exist.
* @return the CmsSession, or <code>null</code> if no session already exists and value was set to <code>false</code>
*
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -