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

📄 status.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.StringUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;

/**
 * Outputs information about urlrewritefilter.
 * <p/>
 * todo: add ability to trigger reload conf
 *
 * @author Paul Tuckey
 * @version $Revision: 43 $ $Date: 2006-10-31 17:29:59 +1300 (Tue, 31 Oct 2006) $
 */
public class Status {

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

    private StringBuffer buffer = new StringBuffer();

    private Conf conf;
    private UrlRewriteFilter urlRewriteFilter;

    public Status(Conf conf) {
        this.conf = conf;
    }

    public Status(Conf conf, UrlRewriteFilter urlRewriteFilter) {
        this.conf = conf;
        this.urlRewriteFilter = urlRewriteFilter;
    }

    public void displayStatusInContainer(final HttpServletRequest hsRequest) {
        showHeader();
        showRunningInfo();
        showConf();
        showRequestInfo(hsRequest);
        showFooter();
    }

    public void displayStatusOffline() {
        showHeader();
        showConf();
        showFooter();
    }


    private void showRequestInfo(final HttpServletRequest hsRequest) {
        // other info
        println("<h2>Request Debug Info</h2>");

        println("<h4>General</h4>");
        println("<pre>");

        println("method: " + hsRequest.getMethod());
        if (hsRequest.getAuthType() != null) println("auth-type: " + hsRequest.getAuthType());
        if (hsRequest.getCharacterEncoding() != null)
            println("character-encoding: " + hsRequest.getCharacterEncoding());
        println("context-path: " + hsRequest.getContextPath());
        if (hsRequest.getPathInfo() != null) println("path-info: " + hsRequest.getPathInfo());
        if (hsRequest.getPathTranslated() != null) println("path-translated: " + hsRequest.getPathTranslated());
        println("port: " + hsRequest.getServerPort());
        println("protocol: " + hsRequest.getProtocol());
        if (hsRequest.getQueryString() != null) println("query-string: " + hsRequest.getQueryString());
        println("remote-addr: " + hsRequest.getRemoteAddr());
        println("remote-host: " + hsRequest.getRemoteHost());
        if (hsRequest.getRemoteUser() != null) println("remote-user: " + hsRequest.getRemoteUser());
        if (hsRequest.getRequestedSessionId() != null)
            println("requested-session-id: " + hsRequest.getRequestedSessionId());
        println("request-uri: " + hsRequest.getRequestURI());
        println("request-url: " + hsRequest.getRequestURL());
        println("server-name: " + hsRequest.getServerName());
        println("scheme: " + hsRequest.getScheme());

        println("</pre>");

        HttpSession session = hsRequest.getSession(false);
        if (session != null) {
            println("<h4>Session</h4>");
            println("<br />session-isnew: " + session.isNew());
            Enumeration enumer = session.getAttributeNames();
            while (enumer.hasMoreElements()) {
                String name = (String) enumer.nextElement();
                println("<br />session-attribute " + name + ": " + session.getAttribute(name));
            }
        }

        // show headers from request
        println("<h4>Request Headers</h4>");
        println("<pre>");
        final Enumeration headers = hsRequest.getHeaderNames();
        while (headers.hasMoreElements()) {
            final String headerName = (String) headers.nextElement();
            // ignore cookies as they are handled later
            if ("cookie".equals(headerName)) continue;
            println(headerName + ": " + hsRequest.getHeader(headerName));
        }
        println("</pre>");

        final Cookie[] cookies = hsRequest.getCookies();
        if (cookies != null && cookies.length > 0) {
            println("<h4>Cookies</h4>");
            for (int i = 0; i < cookies.length; i++) {
                println("<h5>Cookie " + i + "</h5>");
                final Cookie cookie = cookies[i];
                if (cookie == null) continue;
                println("<pre>");
                println("    name     : " + cookie.getName());
                println("    value    : " + cookie.getValue());
                println("    path     : " + cookie.getPath());
                println("    domain   : " + cookie.getDomain());
                println("    max age  : " + cookie.getMaxAge());
                println("    is secure: " + cookie.getSecure());
                println("    version  : " + cookie.getVersion());
                println("    comment  : " + cookie.getComment());
                println("</pre>");
            }
        }

        // show headers from request
        println("<h4>Time info</h4>");
        println("<pre>");
        Calendar nowCal = Calendar.getInstance();
        println("time: " + nowCal.getTime().getTime());
        println("year: " + nowCal.get(Calendar.YEAR));
        println("month: " + nowCal.get(Calendar.MONTH));
        println("dayofmonth: " + nowCal.get(Calendar.DAY_OF_MONTH));
        println("dayofweek: " + nowCal.get(Calendar.DAY_OF_WEEK));
        println("ampm: " + nowCal.get(Calendar.AM_PM));
        println("hourofday: " + nowCal.get(Calendar.HOUR_OF_DAY));
        println("minute: " + nowCal.get(Calendar.MINUTE));
        println("second: " + nowCal.get(Calendar.SECOND));
        println("millisecond: " + nowCal.get(Calendar.MILLISECOND));
        println("</pre>");

    }

    private void showConf() {
        if (conf == null) return;

        println("<h2>Summary");
        if (conf.isLoadedFromFile()) println(" of " + conf.getFileName());
        println("</h2>");

        if (!conf.isOk()) {
            final List errors = conf.getErrors();
            println("<h4 class=\"err\">Errors During Load of " + conf.getFileName() + "</h4>");
            println("<ul>");
            if (errors.size() > 0) {
                for (int i = 0; i < errors.size(); i++) {
                    final String error = (String) errors.get(i);
                    println("<li class=\"err\">" + error + "</li>");
                }
            }
            displayRuleErrors(conf.getRules());
            displayRuleErrors(conf.getOutboundRules());
            displayCatchErrors(conf.getCatchElems());
            println("</ul>");
        }

        int conditionsCount = 0;
        final List rules = conf.getRules();
        for (int i = 0; i < rules.size(); i++) {
            final Rule rule = (Rule) rules.get(i);
            if (rule instanceof NormalRule) {
                conditionsCount += ((NormalRule) rule).getConditions().size();
            }
        }
        final List outboundRules = conf.getOutboundRules();
        for (int i = 0; i < outboundRules.size(); i++) {
            final OutboundRule rule = (OutboundRule) outboundRules.get(i);
            conditionsCount += rule.getConditions().size();
        }
        println("<p>In total there " +
                (rules.size() == 1 ? "is 1 rule" : "are " + rules.size() + " rules") + ", " +
                (outboundRules.size() == 1 ? "1 outbound rule" : outboundRules.size() + " outbound rules") +
                (conditionsCount > 0 ? " and " : "") +
                (conditionsCount == 1 ? conditionsCount + " condtion" : "") +
                (conditionsCount > 1 ? conditionsCount + " condtions" : "") +
                " in the configuration file.</p>");

        showRules(rules);
        showOutboundRules(outboundRules);
        println("<hr />");
    }

    private void showRules(List rules) {
        for (int i = 0; i < rules.size(); i++) {
            final Rule rule = (Rule) rules.get(i);
            if (rule instanceof NormalRule) {
                NormalRule normalRule = (NormalRule) rule;
                println("<h3>" + normalRule.getDisplayName() +
                        (normalRule.isEnabled() ? "" : " **DISABLED**") + "</h3>");
                if (!StringUtils.isBlank(normalRule.getNote())) {
                    println("<dl><dd><p>" + StringUtils.nl2br(normalRule.getNote()) + "</p></dd></dl>");
                }

                print("<p>URL's matching <code>" + normalRule.getFrom() + "</code>");
                if (normalRule.isFilter()) {
                    print(" (filter)");
                }
                if (!StringUtils.isBlank(normalRule.getTo())) {
                    print(" will ");
                    if ("forward".equals(normalRule.getToType()))
                        print("be <code>forwarded</code> to");
                    else if ("include".equals(normalRule.getToType()))
                        print("<code>include</code>");
                    else if ("redirect".equals(normalRule.getToType()))
                        print("be <code>redirected</code> to");
                    else
                        print("<code>" + normalRule.getToType() + "</code> to");
                    print(" <code>" + normalRule.getTo() + "</code>");
                }
                println(".</p>");
                print("<p>This rule and it's conditions will use the <code>" + normalRule.getMatchType() + "</code> matching engine.</p>");
                showConditions(normalRule);
                showSets(normalRule);
                showRuns(normalRule);

                if (!rule.isLast()) {
                    println("<p>Note, other rules will be processed after this rule.</p>");
                }
            }
            if (rule instanceof ClassRule) {
                ClassRule classRule = (ClassRule) rule;
                println("<h3>" + classRule.getDisplayName() +
                        (classRule.isEnabled() ? "" : " **DISABLED**") + "</h3>");
            }
            println();
            println();
        }
    }

    private void showOutboundRules(List outboundRules) {
        for (int i = 0; i < outboundRules.size(); i++) {
            final OutboundRule rule = (OutboundRule) outboundRules.get(i);

⌨️ 快捷键说明

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