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

📄 cmsflexcachekey.java

📁 cms是开源的框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/src/org/opencms/flex/CmsFlexCacheKey.java,v $
 * Date   : $Date: 2006/07/12 08:13:38 $
 * Version: $Revision: 1.27 $
 *
 * 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.flex;

import org.opencms.loader.I_CmsResourceLoader;
import org.opencms.main.CmsLog;
import org.opencms.util.CmsStringUtil;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;

/**
 * Implements the CmsFlexCacheKey,
 * which is a key used to describe the caching behaviour
 * of a specific resource.<p>
 *
 * It has a lot of variables that are directly accessed (which isn't good style, I know)
 * to avoid method calling overhead (a cache is about speed, isn't it :).<p>
 *
 * @author Alexander Kandzior 
 * 
 * @version $Revision: 1.27 $ 
 * 
 * @since 6.0.0 
 */
public class CmsFlexCacheKey {

    /** Flex cache keyword: always. */
    private static final String CACHE_00_ALWAYS = "always";

    /** Flex cache keyword: never. */
    private static final String CACHE_01_NEVER = "never";

    /** Flex cache keyword: uri. */
    private static final String CACHE_02_URI = "uri";

    /** Flex cache keyword: user. */
    private static final String CACHE_03_USER = "user";

    /** Flex cache keyword: params. */
    private static final String CACHE_04_PARAMS = "params";

    /** Flex cache keyword: no-params. */
    private static final String CACHE_05_NO_PARAMS = "no-params";

    /** Flex cache keyword: timeout. */
    private static final String CACHE_06_TIMEOUT = "timeout";

    /** Flex cache keyword: session. */
    private static final String CACHE_07_SESSION = "session";

    /** Flex cache keyword: schemes. */
    private static final String CACHE_08_SCHEMES = "schemes";

    /** Flex cache keyword: ports. */
    private static final String CACHE_09_PORTS = "ports";

    /** Flex cache keyword: false. */
    private static final String CACHE_10_FALSE = CmsStringUtil.FALSE;

    /** Flex cache keyword: parse-error. */
    private static final String CACHE_11_PARSE_ERROR = "parse-error";

    /** Flex cache keyword: true. */
    private static final String CACHE_12_TRUE = CmsStringUtil.TRUE;

    /** Flex cache keyword: ip. */
    private static final String CACHE_13_IP = "ip";

    /** Flex cache keyword: element. */
    private static final String CACHE_14_ELEMENT = "element";

    /** Flex cache keyword: locale. */
    private static final String CACHE_15_LOCALE = "locale";

    /** Flex cache keyword: encoding. */
    private static final String CACHE_16_ENCODING = "encoding";

    /** Flex cache keyword: site. */
    private static final String CACHE_17_SITE = "site";

    /** The list of keywords of the Flex cache language. */
    private static final List CACHE_COMMANDS = Arrays.asList(new String[] {
        CACHE_00_ALWAYS,
        CACHE_01_NEVER,
        CACHE_02_URI,
        CACHE_03_USER,
        CACHE_04_PARAMS,
        CACHE_05_NO_PARAMS,
        CACHE_06_TIMEOUT,
        CACHE_07_SESSION,
        CACHE_08_SCHEMES,
        CACHE_09_PORTS,
        CACHE_10_FALSE,
        CACHE_11_PARSE_ERROR,
        CACHE_12_TRUE,
        CACHE_13_IP,
        CACHE_14_ELEMENT,
        CACHE_15_LOCALE,
        CACHE_16_ENCODING,
        CACHE_17_SITE});

    /** Marker to identify use of certain String key members (uri, ip etc.). */
    private static final String IS_USED = "/ /";

    /** The log object for this class. */
    private static final Log LOG = CmsLog.getLog(CmsFlexCacheKey.class);

    /** Cache key variable: Determines if this resource can be cached alwys, never or under certain conditions. -1 = never, 0=check, 1=always. */
    private int m_always;

    /** Cache key variable: The requested element. */
    private String m_element;

    /** Cache key variable: The requested encoding. */
    private String m_encoding;

    /** Cache key variable: The ip address of the request. */
    private String m_ip;

    /** Cache key variable: The requested locale. */
    private String m_locale;

    /** Cache key variable: List of "blocking" parameters. */
    private Set m_noparams;

    /** Cache key variable: List of parameters. */
    private Set m_params;

    /** Flag raised in case a key parse error occured. */
    private boolean m_parseError;

    /** Cache key variable: The request TCP/IP port. */
    private Set m_ports;

    /** The OpenCms resource that this key is used for. */
    private String m_resource;

    /** Cache key variable: Distinguishes request schemes (http, https etc.). */
    private Set m_schemes;

    /** Cache key variable: List of session variables. */
    private Set m_session;

    /** Cache key variable: The current site root. */
    private String m_site;

    /** Cache key variable: Timeout of the resource. */
    private long m_timeout;

    /** Cache key variable: The uri of the original request. */
    private String m_uri;

    /** Cache key variable: The user id. */
    private String m_user;

    /** The cache behaviour description for the resource. */
    private String m_variation;

    /**
     * This constructor is used when building a cache key from set of cache directives.<p>
     * 
     * These directives are attached to the properties of the requested resource 
     * on a property called "cache". 
     * The value of this poperty that is passed in this constructor as "cacheDirectives" 
     * is parsed to build the keys data structure.<p>
     *
     * In case a parsing error occures, the value of this key is set to "cache=never", 
     * and the hadParseError() flag is set to true. 
     * This is done to ensure that a valid key is always constructed with the constructor.<p>
     * 
     * @param resourcename the full name of the resource including site root
     * @param cacheDirectives the cache directives of the resource (value of the property "cache")
     * @param online must be true for an online resource, false for offline resources
     */
    public CmsFlexCacheKey(String resourcename, String cacheDirectives, boolean online) {

        m_resource = getKeyName(resourcename, online);
        m_variation = "never";
        m_always = -1;
        m_timeout = -1;
        if (cacheDirectives != null) {
            parseFlexKey(cacheDirectives);
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug(Messages.get().getBundle().key(Messages.LOG_FLEXCACHEKEY_GENERATED_1, toString()));
        }
    }

    /**
     * Calculates the cache key name that is used as key in 
     * the first level of the FlexCache.<p>
     * 
     * @param resourcename the full name of the resource including site root
     * @param online must be true for an online resource, false for offline resources
     *
     * @return fhe FlexCache key name
     */
    public static String getKeyName(String resourcename, boolean online) {

        return resourcename.concat(online ? CmsFlexCache.CACHE_ONLINESUFFIX : CmsFlexCache.CACHE_OFFLINESUFFIX);
    }

    /**
     * Appends a flex cache key value to the given buffer.<p> 
     *  
     * @param str the buffer to append to
     * @param key the key to append
     * @param value the value to append
     */
    private static void appendKeyValue(StringBuffer str, String key, String value) {

        str.append(key);
        if (value == IS_USED) {
            str.append(";");
        } else {
            str.append("=(");
            str.append(value);
            str.append(");");
        }
    }

    /**
     * This flag is used to indicate that a parse error had
     * occured, which can happen if the cache directives String
     * passed to the constructor using the response is
     * not build according to the Flex cache language syntax.<p>
     * 
     * @return true if a parse error did occur, false otherwise
     */
    public boolean hadParseError() {

        return m_parseError;
    }

    /**
     * Compares this key to the other key passed as parameter,
     * from comparing the two keys, a variation String is constructed.<p>
     * 
     * This method is the "heart" of the key matching process.<p>
     *
     * The assumtion is that this key should be the one constructed for the response, 
     * while the parameter key should have been constructed from the request.<p>
     *
     * A short example how this works:
     * If the cache key is "cache=user" and the request is done from a guest user
     * the constructed variation will be "user=(guest)".<p>
     * 
     * @param key the key to match this key with
     * @return null if not cachable, or the Variation String if cachable
     */
    public String matchRequestKey(CmsFlexRequestKey key) {

        StringBuffer str = new StringBuffer(100);
        if (m_always < 0) {
            if (LOG.isDebugEnabled()) {
                LOG.debug(Messages.get().getBundle().key(Messages.LOG_FLEXCACHEKEY_KEYMATCH_CACHE_NEVER_0));
            }
            return null;
        }

        if (LOG.isDebugEnabled()) {
            LOG.debug(Messages.get().getBundle().key(Messages.LOG_FLEXCACHEKEY_KEYMATCH_CHECK_NO_PARAMS_0));
        }
        if ((m_noparams != null) && (key.getParams() != null)) {
            if ((m_noparams.size() == 0) && (key.getParams().size() > 0)) {
                return null;
            }
            Iterator i = key.getParams().keySet().iterator();
            while (i.hasNext()) {
                if (m_noparams.contains(i.next())) {
                    return null;
                }
            }
        }

        if (m_always > 0) {
            if (LOG.isDebugEnabled()) {
                LOG.debug(Messages.get().getBundle().key(Messages.LOG_FLEXCACHEKEY_KEYMATCH_CACHE_ALWAYS_0));
            }
            str.append(CACHE_00_ALWAYS);
            return str.toString();
        }

        if (m_uri != null) {
            appendKeyValue(str, CACHE_02_URI, key.getUri());
        }

        if (m_site != null) {
            appendKeyValue(str, CACHE_17_SITE, key.getSite());
        }

        if (m_element != null) {
            appendKeyValue(str, CACHE_14_ELEMENT, key.getElement());
        }

        if (m_locale != null) {
            appendKeyValue(str, CACHE_15_LOCALE, key.getLocale());
        }

        if (m_encoding != null) {
            appendKeyValue(str, CACHE_16_ENCODING, key.getEncoding());
        }

        if (m_ip != null) {
            appendKeyValue(str, CACHE_13_IP, key.getIp());
        }

        if (m_user != null) {
            appendKeyValue(str, CACHE_03_USER, key.getUser());
        }

        if (m_params != null) {
            str.append(CACHE_04_PARAMS);
            str.append("=(");
            Map keyParams = key.getParams();
            if (keyParams != null) {
                if (m_params.size() > 0) {
                    // match only params listed in cache directives
                    Iterator i = m_params.iterator();
                    while (i.hasNext()) {
                        Object o = i.next();
                        if (keyParams.containsKey(o)) {
                            str.append(o);
                            str.append("=");
                            // TODO: handle multiple occurances of the same parameter value
                            String[] values = (String[])keyParams.get(o);
                            str.append(values[0]);
                            if (i.hasNext()) {
                                str.append(",");
                            }
                        }
                    }
                } else {
                    // match all request params
                    Iterator i = keyParams.keySet().iterator();
                    while (i.hasNext()) {
                        Object o = i.next();
                        str.append(o);
                        str.append("=");
                        // TODO: handle multiple occurances of the same parameter value
                        String[] values = (String[])keyParams.get(o);
                        str.append(values[0]);
                        if (i.hasNext()) {
                            str.append(",");
                        }
                    }
                }
            }
            str.append(");");
        }

        if (m_session != null) {
            StringBuffer buf = new StringBuffer(32);
            boolean found = false;
            buf.append(CACHE_07_SESSION);
            buf.append("=(");
            HttpSession keySession = key.getSession();
            if (keySession != null) {

⌨️ 快捷键说明

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