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

📄 cmsrequestcontext.java

📁 内容管理
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    public I_CmsSession getSession(boolean value) {
        HttpSession session =
            ((HttpServletRequest) m_req.getOriginalRequest()).getSession(value);
        if (session != null) {
            return (I_CmsSession) new CmsSession(session);
        } else {
            return null;
        }
    }
    
    /**
     * Gets the uri for the requested resource.<p>
     * 
     * For a http request, the name of the resource is extracted as follows:<br>
     * <CODE>http://{servername}/{servletpath}/{path to the cms resource}</CODE><br>
     * In the following example:<br>
     * <CODE>http://my.work.server/servlet/opencms/system/def/explorer</CODE><br>
     * the requested resource is <CODE>/system/def/explorer</CODE>.
     *
     * @return the path to the requested resource.
     */
    public String getUri() {
        if (m_uri != null) return m_uri;
        if( m_req != null ) {
            return( m_req.getRequestedResource() );
        } else {
            return (C_ROOT);
        }
    }

    /**
     * Set the value that is returned by getUri()
     * to the provided String.<p>
     * 
     * This is required in a context where
     * a cascade of included XMLTemplates are combined with JSP or other
     * Templates that use the ResourceLoader interface.
     * You need to fake the URI because the ElementCache always
     * uses cms.getRequestContext().getUri() even if you called
     * CmsXmlLauncher.generateOutput() with a differnt file name.
     *
     * @param value The value to set the Uri to, must be a complete OpenCms path name like /system/workplace/stlye.css
     * @since 5.0 beta 1
     */
    public void setUri(String value) {
        m_uri = value;
    }

    /**
     * Determines if the users is in the admin-group.
     *
     * @return <code>true</code> if the users current group is the admin-group; <code>false</code> otherwise.
     * @throws CmsException if operation was not successful.
     */
    public boolean isAdmin() throws CmsException {
        return (m_rb.isAdmin(m_user, m_currentProject));
    }

    /**
     * Determines if the users current group is the projectmanager-group.<p>
     * 
     * All projectmanagers can create new projects, or close their own projects.
     *
     * @return <code>true</code> if the users current group is the projectleader-group; <code>false</code> otherwise.
     * @throws CmsException if operation was not successful.
     */
    public boolean isProjectManager() throws CmsException {
        return (m_rb.isProjectManager(m_user, m_currentProject));
    }

    /**
     * Sets the current group of the current user.
     *
     * @param groupname the name of the group to be set as current group.
     * @throws CmsException if operation was not successful.
     */
    public void setCurrentGroup(String groupname) throws CmsException {

        // is the user in that group?
        if (m_rb
            .userInGroup(
                m_user,
                m_currentProject,
                m_user.getName(),
                groupname)) {
            // Yes - set it to the current Group.
            m_currentGroup =
                m_rb.readGroup(m_user, m_currentProject, groupname);
        } else {
            // No - throw exception.
            throw new CmsException(
                "[" + this.getClass().getName() + "] " + groupname,
                CmsException.C_NO_ACCESS);
        }
    }

    /**
     * Sets the current project for the user.
     *
     * @param projectId the id of the project to be set as current project.
     * @throws CmsException if operation was not successful.
     */
    public CmsProject setCurrentProject(int projectId) throws CmsException {
        CmsProject newProject =
            m_rb.readProject(m_user, m_currentProject, projectId);
        if (newProject != null) {
            m_currentProject = newProject;
        }
        return (m_currentProject);
    }

    /**
     * Get the current mode for HTTP streaming.
     *
     * @return <code>true</code> if template classes are allowed to stream the
     *    results to the response output stream theirselves, <code>false</code> otherwise.
     */
    public boolean isStreaming() {
        return m_streaming;
    }

    /**
     * Set the current mode for HTTP streaming.<p>
     * 
     * Calling this method is only allowed, if the response output stream was
     * not used before. Otherwise the streaming mode must not be changed.
     *
     * @param b <code>true</code> if template classes are allowed to stream the
     *    results to the response's output stream theirselves, <code>false</code> otherwise.
     * @throws CmsException if the output stream was already used previously.
     */
    public void setStreaming(boolean b) throws CmsException {
        if ((m_streaming != b) && getResponse().isOutputWritten()) {
            throw new CmsException(
                "[CmsRequestContext] Cannot switch streaming mode, if output stream is used previously.",
                CmsException.C_STREAMING_ERROR);
        }
        m_streaming = b;
    }

    /**
     * Get the current mode for element cache.
     *
     * @return <code>true</code> if element cache is active, <code>false</code> otherwise.
     */
    public boolean isElementCacheEnabled() {
        return (m_elementCache != null);
    }

    /**
     * Get the CmsElementCache object. This is the starting point for the element cache area.
     * @return CmsElementCachee
     */
    public CmsElementCache getElementCache() {
        return m_elementCache;
    }

    /**
     * Get a Vector of all accepted languages for this request.
     * Languages are coded in international shortcuts like "en" or "de".
     * If the browser has sent special versions of languages (e.g. "de-ch" for Swiss-German)
     * these extensions will be cut off.
     * @return Vector of Strings with language codes or <code>null</code> if no request object is available.
     */
    public Vector getAcceptedLanguages() {
        return m_language;
    }
    
    /**
     * Returns the name of the current site root, e.g. /default/vfs
     *
     * @param resourcename
     * @return String The resourcename with its site root
     */
    public String getSiteRoot(String resourcename) {
        if (resourcename == null) return null;
        if (resourcename.startsWith("///")) {
            return m_directoryTranslator.translateResource(resourcename.substring(2));
        } else if (resourcename.startsWith("//")) {
            return m_directoryTranslator.translateResource(C_DEFAULT_SITE + resourcename.substring(1));
        } else {
            return m_directoryTranslator.translateResource(m_siteRoot + resourcename);
        }
    }
    
    /**
     * @return The directory name translator this context was initialized with
     */
    public CmsResourceTranslator getDirectoryTranslator() {
        return m_directoryTranslator;
    }
    
    /**
     * @return The file name translator this context was initialized with
     */
    public CmsResourceTranslator getFileTranslator() {
        return m_fileTranslator;
    }    

    /**
     * Returns the site name, e.g. <code>/default</code>
     *
     * @return the site name, e.g. <code>/default</code>
     */
    public String getSiteName() {
        return C_DEFAULT_SITE;
    }
    
    /**
     * Returns the site root, e.g. <code>/default/vfs</code>
     * 
     * @return the site root, e.g. <code>/default/vfs</code>
     */
    public String getSiteRoot() {
        return m_siteRoot;
    }

    /**
     * Sets the name of the current site root
     * of the virtual file system
     */
    public void setContextTo(String name) {
        m_siteRoot = C_DEFAULT_SITE + name;
    }

    /**
     * Detects current content encoding to be used in HTTP response
     * based on requested resource or session state.
     */
    public void initEncoding() {
        try {
            m_encoding = m_rb.readProperty(m_user, m_currentProject, getSiteRoot(m_req.getRequestedResource()), m_siteRoot, C_PROPERTY_CONTENT_ENCODING, true);
        } catch (CmsException e) {
            m_encoding = null;
        }
        if ((m_encoding != null) && ! "".equals(m_encoding)) {
            // encoding was read from resource property
            return;
        } else if ((getUri().startsWith(I_CmsWpConstants.C_VFS_PATH_SYSTEM)) && (! (m_req instanceof CmsExportRequest))) {
            // try to get encoding from session for special system folder only                
            if (A_OpenCms.C_LOGGING && A_OpenCms.isLogging(A_OpenCms.C_OPENCMS_DEBUG)) {                                
                A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_DEBUG,
                    "[" + getClass().getName() + "] can't get encoding property for resource "
                    + m_req.getRequestedResource() + ", trying to get it from session.");
            }                    
            I_CmsSession session = getSession(false);
            if (session != null) {
                m_encoding = (String)session.getValue(I_CmsConstants.C_SESSION_CONTENT_ENCODING);
            }
        }
        if (m_encoding == null || "".equals(m_encoding)) {
            // no encoding found - use default one
            if (A_OpenCms.C_LOGGING && A_OpenCms.isLogging(A_OpenCms.C_OPENCMS_DEBUG)) {                                
                A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_DEBUG,
                    "[" + getClass().getName() + "] no encoding found - using default: " + A_OpenCms.getDefaultEncoding());
            }                  
            m_encoding = A_OpenCms.getDefaultEncoding();
        }
    }

    /**
     * Returns the current content encoding to be used in HTTP response
     */
    public String getEncoding() {
        return m_encoding;
    }

    /**
     * Sets the current content encoding to be used in HTTP response
     */
    public void setEncoding(String encoding) {
        setEncoding(encoding, false);
    }

    /**
     * Sets the current content encoding to be used in HTTP response
     * and store it in session if it is available
     */
    public void setEncoding(String encoding, boolean storeInSession) {
        m_encoding = encoding;
        if (!storeInSession) {
            return;
        }
        I_CmsSession session = getSession(false);
        if (session != null) {
            session.putValue(
                I_CmsConstants.C_SESSION_CONTENT_ENCODING,
                m_encoding);
        }
    }

    /**
     * Mark this request context as event controlled.<p>
     * 
     * @param true if the request is event controlled, false otherwise
     */
    public void setEventControlled(boolean value) {
        m_eventControlled = value;
    }

    /**
     * Check if this request context is event controlled.<p>
     * 
     * @return true if the request context is event controlled, false otherwise
     */
    public boolean isEventControlled() {
        return m_eventControlled;
    }
    
    /**
     * Mark this request context to update the session or not.<p>
     *
     * @param true if this request context will update the session, false otherwise
     */
    public void setUpdateSessionEnabled(boolean value) {
        m_updateSession = value;
    }

    /**
     * Check if this request context will update the session.<p>
     *
     * @return true if this request context will update the session, false otherwise
     */
    public boolean isUpdateSessionEnabled() {
        return m_updateSession;
    }   
    
    /**
     * 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);
    }
    
    /**
     * Sets an attribute in the request context.<p>
     * 
     * @param key the attribute name
     * @param value the attribute value
     */
    public void setAttribute(String key, Object value) {
        if (m_attributeMap == null) m_attributeMap = new HashMap();
        m_attributeMap.put(key, value);
    } 
}

⌨️ 快捷键说明

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