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

📄 setattribute.java

📁 UrlRewriteFilter 是一个不错的URL转换工具
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * Copyright (c) 2005-2007, Paul Tuckey
 * All rights reserved.
 * ====================================================================
 * Licensed under the BSD License. Text as follows.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   - Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.
 *   - Neither the name tuckey.org nor the names of its contributors
 *     may be used to endorse or promote products derived from this
 *     software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * ====================================================================
 */
package org.tuckey.web.filters.urlrewrite;

import org.tuckey.web.filters.urlrewrite.utils.Log;
import org.tuckey.web.filters.urlrewrite.utils.NumberUtils;
import org.tuckey.web.filters.urlrewrite.utils.StringMatchingMatcher;
import org.tuckey.web.filters.urlrewrite.utils.StringUtils;
import org.tuckey.web.filters.urlrewrite.utils.FunctionReplacer;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Paul Tuckey
 * @version $Revision: 12 $ $Date: 2006-08-20 20:53:09 +1200 (Sun, 20 Aug 2006) $
 */
public class SetAttribute {

    private static Log log = Log.getLog(SetAttribute.class);

    private boolean initialised = false;
    private boolean valid = false;

    /**
     * Error message from the regular expression compilation.
     */
    private String error = null;

    private short type;
    private String name;
    private String value;
    private int numericValue;
    private Locale locale;

    private static final short SET_TYPE_REQUEST = 0;
    private static final short SET_TYPE_SESSION = 1;
    private static final short SET_TYPE_RESPONSE_HEADER = 2;
    private static final short SET_TYPE_COOKIE = 3;
    private static final short SET_TYPE_CONTENT_TYPE = 4;
    private static final short SET_TYPE_CHARSET = 5;
    private static final short SET_TYPE_LOCALE = 6;
    private static final short SET_TYPE_STAUS = 7;
    private static final short SET_TYPE_PARAM = 8;
    private static final short SET_TYPE_EXPIRES = 9;
    private static final short SET_TYPE_METHOD = 10;

    private long expiresValueAdd = 0;
    private boolean valueContainsVariable = false;
    private boolean valueContainsBackRef = false;
    private boolean valueContainsFunction = false;
    private static Pattern replacementVarPattern = Pattern.compile("(?<!\\\\)\\$([0-9])");

    public String getType() {
        if (type == SET_TYPE_RESPONSE_HEADER) return "response-header";
        if (type == SET_TYPE_SESSION) return "session";
        if (type == SET_TYPE_COOKIE) return "cookie";
        if (type == SET_TYPE_CONTENT_TYPE) return "content-type";
        if (type == SET_TYPE_CHARSET) return "charset";
        if (type == SET_TYPE_LOCALE) return "locale";
        if (type == SET_TYPE_STAUS) return "status";
        if (type == SET_TYPE_PARAM) return "parameter";
        if (type == SET_TYPE_EXPIRES) return "expires";
        if (type == SET_TYPE_METHOD) return "method";
        return "request";
    }

    public void setType(String typeStr) {
        if ("response-header".equals(typeStr)) {
            type = SET_TYPE_RESPONSE_HEADER;
        } else if ("session".equals(typeStr)) {
            type = SET_TYPE_SESSION;
        } else if ("cookie".equals(typeStr)) {
            type = SET_TYPE_COOKIE;
        } else if ("content-type".equals(typeStr)) {
            type = SET_TYPE_CONTENT_TYPE;
        } else if ("charset".equals(typeStr)) {
            type = SET_TYPE_CHARSET;
        } else if ("locale".equals(typeStr)) {
            type = SET_TYPE_LOCALE;
        } else if ("status".equals(typeStr)) {
            type = SET_TYPE_STAUS;
        } else if ("parameter".equals(typeStr) || "param".equals(typeStr)) {
            type = SET_TYPE_PARAM;
        } else if ("expires".equals(typeStr)) {
            type = SET_TYPE_EXPIRES;
        } else if ("request".equals(typeStr) || StringUtils.isBlank(typeStr)) {
            type = SET_TYPE_REQUEST;
        } else if ("method".equals(typeStr)) {
            type = SET_TYPE_METHOD;
        } else {
            setError("type (" + typeStr + ") is not valid");
        }
    }

    private void setError(String s) {
        log.error("set " + getDisplayName() + " had error: " + s);
        error = s;
    }

    public String getError() {
        return error;
    }

    public String getDisplayName() {
        return "Set " + getType() + " " + name + " " + value;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public void execute(ConditionMatch lastConditionMatch, StringMatchingMatcher toMatcher,
                        HttpServletRequest hsRequest, HttpServletResponse hsResponse) {

        if (log.isDebugEnabled()) {
            log.debug("set " + getDisplayName() + " called");
        }
        if (!valid) {
            log.debug("not valid, skipping");
            return;
        }
        if (!initialised) {
            log.debug("not initialised, skipping");
            return;
        }

        String value = this.value;
        if (toMatcher != null) {
            Matcher replacementVarMatcher = replacementVarPattern.matcher(value);
            if (replacementVarMatcher.find()) {
                value = toMatcher.replaceAll(value);
            }
        }
        if (valueContainsBackRef) {
            value = BackReferenceReplacer.replace(lastConditionMatch, value);
        }
        if (valueContainsVariable) {
            value = VariableReplacer.replace(value, hsRequest);
        }
        if (valueContainsFunction) {
            value = FunctionReplacer.replace(value);
        }

        if (type == SET_TYPE_REQUEST) {
            log.debug("setting request attrib");
            hsRequest.setAttribute(name, value);

        } else if (type == SET_TYPE_METHOD) {
            log.debug("setting request method");

⌨️ 快捷键说明

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