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

📄 cmsflexrequest.java

📁 内容管理
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/src/com/opencms/flex/cache/Attic/CmsFlexRequest.java,v $
 * Date   : $Date: 2003/05/13 13:18:20 $
 * Version: $Revision: 1.12.2.1 $
 *
 * This library is part of OpenCms -
 * the Open Source Content Mananagement System
 *
 * Copyright (C) 2002 - 2003 Alkacon Software (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, 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 com.opencms.flex.cache;

import com.opencms.core.A_OpenCms;
import com.opencms.file.CmsObject;
import com.opencms.flex.CmsEvent;
import com.opencms.flex.I_CmsEventListener;

import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

/**
 * Wrapper class for a HttpServletRequest.<p>
 *
 * This class wrapps the standard HttpServletRequest so that it's output can be delivered to
 * the CmsFlexCache.
 *
 * @author Alexander Kandzior (a.kandzior@alkacon.com)
 * @version $Revision: 1.12.2.1 $
 */
public class CmsFlexRequest extends HttpServletRequestWrapper {
    
    /** The wrapped HttpServletRequest */    
    private HttpServletRequest m_req = null;
        
    /** The requested resource (target resource) */    
    private String m_resource = null;
    
    /** The CmsFlexCacheKey for this request */
    private CmsFlexCacheKey m_key = null;
    
    /** The CmsFlexController for this request */
    private CmsFlexController m_controller = null;
    
    /** Flag to decide if this request can be cached or not */
    private boolean m_canCache = false;
    
    /** Flag to check if this request is in the online project or not */
    private boolean m_isOnline = false;
    
    /** Flag to force a JSP recompile */
    private boolean m_doRecompile = false; 
    
    /** Stores the request URI after it was once calculated */
    private String m_requestUri = null;
    
    /** Stores the request URI after it was once calculated */
    private StringBuffer m_requestUrl = null;
        
    /** Set of all include calls (to prevent an endless inclusion loop) */
    private Set m_includeCalls;    
    
    /** Map of parameters from the original request */
    private Map m_parameters = null;
    
    /** Attribute name used for checking if _flex request parameters have already been processed */
    public static String ATTRIBUTE_PROCESSED = "__com.opencms.flex.cache.CmsFlexRequest";
    
    /** Debug flag */
    private static final boolean DEBUG = false;
            
    /**
     * Creates a new CmsFlexRequest wrapper which is most likley the "Top"
     * request wrapper, i.e. the wrapper that is constructed around the
     * first "real" (not wrapped) request.<p>
     *
     * @param req the request to wrap
     * @param controller the controller to use
     */    
    public CmsFlexRequest(HttpServletRequest req, CmsFlexController controller) {
        super(req);
        m_req = req;
        m_controller = controller;
        m_resource = m_controller.getCmsFile().getAbsolutePath();
        CmsObject cms = m_controller.getCmsObject();
        m_includeCalls = Collections.synchronizedSet(new java.util.HashSet(23));
        m_parameters = req.getParameterMap();
        try {
            m_isOnline = cms.getRequestContext().currentProject().isOnlineProject();
        } catch (Exception e) {}        
        String[] paras = req.getParameterValues("_flex");
        boolean nocachepara = false;
        boolean dorecompile = false;
        boolean isAdmin = false;
        if (paras != null) {
            try {
                isAdmin = cms.getRequestContext().isAdmin();
            } catch (Exception e) {}
            if (isAdmin) {                        
                List l = Arrays.asList(paras);
                String context = (String)req.getAttribute(ATTRIBUTE_PROCESSED);
                boolean firstCall = (context == null);
                if (firstCall) req.setAttribute(ATTRIBUTE_PROCESSED, "true");
                nocachepara = l.contains("nocache");            
                dorecompile = l.contains("recompile");
                boolean p_on = l.contains("online");
                boolean p_off = l.contains("offline");                
                if (l.contains("purge") && firstCall) {
                    A_OpenCms.fireCmsEvent(new CmsEvent(cms, I_CmsEventListener.EVENT_FLEX_PURGE_JSP_REPOSITORY, new java.util.HashMap(0)));
                    A_OpenCms.fireCmsEvent(new CmsEvent(cms, I_CmsEventListener.EVENT_FLEX_CACHE_CLEAR, Collections.singletonMap("action", new Integer(CmsFlexCache.C_CLEAR_ENTRIES))));
                    dorecompile = false;
                } else if ((l.contains("clearcache") || dorecompile) && firstCall) {
                    if (! (p_on || p_off)) {
                        A_OpenCms.fireCmsEvent(new CmsEvent(cms, I_CmsEventListener.EVENT_FLEX_CACHE_CLEAR, Collections.singletonMap("action", new Integer(CmsFlexCache.C_CLEAR_ALL))));
                    } else {
                        if (p_on) A_OpenCms.fireCmsEvent(new CmsEvent(cms, I_CmsEventListener.EVENT_FLEX_CACHE_CLEAR, Collections.singletonMap("action", new Integer(CmsFlexCache.C_CLEAR_ONLINE_ALL))));
                        if (p_off) A_OpenCms.fireCmsEvent(new CmsEvent(cms, I_CmsEventListener.EVENT_FLEX_CACHE_CLEAR, Collections.singletonMap("action", new Integer(CmsFlexCache.C_CLEAR_OFFLINE_ALL))));
                    }                    
                } else if (l.contains("clearvariations") && firstCall) {
                    if (! (p_on || p_off)) {
                        A_OpenCms.fireCmsEvent(new CmsEvent(cms, I_CmsEventListener.EVENT_FLEX_CACHE_CLEAR, Collections.singletonMap("action", new Integer(CmsFlexCache.C_CLEAR_ENTRIES))));
                    } else {
                        if (p_on) A_OpenCms.fireCmsEvent(new CmsEvent(cms, I_CmsEventListener.EVENT_FLEX_CACHE_CLEAR, Collections.singletonMap("action", new Integer(CmsFlexCache.C_CLEAR_ONLINE_ENTRIES))));
                        if (p_off)  A_OpenCms.fireCmsEvent(new CmsEvent(cms, I_CmsEventListener.EVENT_FLEX_CACHE_CLEAR, Collections.singletonMap("action", new Integer(CmsFlexCache.C_CLEAR_OFFLINE_ENTRIES))));              
                    }
                }
            }
        }  
        m_canCache = (((m_isOnline || m_controller.getCmsCache().cacheOffline()) && ! nocachepara) || dorecompile);
        m_doRecompile = dorecompile;
        if (DEBUG) System.err.println("[FlexRequest] Constructing new Flex request for resource: " + m_resource);
    }
        
    /** 
     * Constructs a new wrapper layer around a (already wrapped) CmsFlexRequest.<p>
     *
     * @param req the request to be wrapped
     * @param controller the controller to use
     * @param resource the target resource that has been requested
     */    
    CmsFlexRequest(HttpServletRequest req, CmsFlexController controller, String resource) {
        super(req);
        m_req = req;
        m_controller = controller;
        m_resource = toAbsolute(resource);
        // must reset request URI/URL buffer here because m_resource has changed
        m_requestUri = null; 
        m_requestUrl = null;
        m_isOnline = m_controller.getCurrentRequest().isOnline();
        m_canCache = m_controller.getCurrentRequest().isCacheable();
        m_doRecompile = m_controller.getCurrentRequest().isDoRecompile();
        m_includeCalls = m_controller.getCurrentRequest().getCmsIncludeCalls();        
        m_parameters = req.getParameterMap();
        if (DEBUG) System.err.println("[FlexRequest] Re-using Flex request for resource: " + m_resource);
    }
    
    /** 
     * Returns the name of the resource currently processed.<p>
     * 
     * This might be the name of an included resource,
     * not neccesarily the name the resource requested by the user.
     * 
     * @return the name of the resource currently processed
     * @see #getCmsRequestedResource()
     */    
    public String getElementUri() {
        return m_resource;
    }    
    
    /** 
     * Replacement for the standard servlet API getRequestDispatcher() method.<p>
     * 
     * This variation is used if an external file (probably JSP) is dispached to.
     * This external file must have a "mirror" version, i.e. a file in the OpenCms VFS
     * that represents the external file.<p>
     *
     * @param cms_target the OpenCms file that is a "mirror" version of the external file
     * @param ext_target the external file (outside the OpenCms VFS)
     * @return the constructed CmsFlexRequestDispatcher
     */     
    public CmsFlexRequestDispatcher getRequestDispatcherToExternal(String vfs_target, String ext_target) {
        return new CmsFlexRequestDispatcher(m_controller.getTopRequest().getRequestDispatcher(ext_target), toAbsolute(vfs_target), ext_target);
    }

    /** 
     * Overloads the standard servlet API getRequestDispatcher() method,
     * which is the main purpose of this wrapper implementation.<p>
     *
     * @param target the target for the request dispatcher
     * @return the constructed RequestDispatcher
     */    
    public javax.servlet.RequestDispatcher getRequestDispatcher(String target) {
        return (javax.servlet.RequestDispatcher) new CmsFlexRequestDispatcher (m_controller.getTopRequest().getRequestDispatcher(toAbsolute(target)), toAbsolute(target), null);
    }

    /** 
     * Wraps the request URI, overloading the standard API.<p>
     * 
     * This ensures that any wrapped request will use the "faked"
     * target parameters. Remember that for the real request,
     * a mixture of PathInfo and other request information is used to
     * idenify the target.<p>
     *
     * @return a faked URI that will point to the wrapped target in the VFS 
     * @see javax.servlet.http.HttpServletRequest#getRequestURI()
     */      
    public String getRequestURI() {
        if (m_requestUri != null) return m_requestUri;
        StringBuffer buf = new StringBuffer(128);
        buf.append(getContextPath());
        buf.append(getServletPath());
        buf.append(getElementUri());
        m_requestUri = buf.toString();
        return m_requestUri;
    } 
    
    /** 
     * Wraps the request URL, overloading the standard API,
     * the wrapped URL will always point to the currently included VFS resource.<p>
     *
     * @return a faked URL that will point to the included target in the VFS
     * @see javax.servlet.http.HttpServletRequest#getRequestURL()
     */   
    public StringBuffer getRequestURL() {
        if (m_requestUrl != null) return m_requestUrl;
        StringBuffer buf = new StringBuffer(128);
        buf.append(getScheme());
        buf.append("://");
        buf.append(getServerName());
        buf.append(":");
        buf.append(getServerPort());
        buf.append(getRequestURI());  
        m_requestUrl = buf;      
        return m_requestUrl;
    }
        
    /** 
     * Convert (if necessary) and return the absolute URI that represents the
     * resource referenced by this possibly relative URI for this request.<p>
     * 
     * Adjust for resources in the OpenCms VFS by cutting of servlet context
     * and servlet name.
     * If this URI is already absolute, return it unchanged.
     * Return URI also unchanged if it is not well-formed.<p>
     *

⌨️ 快捷键说明

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