📄 condition.java
字号:
/**
* 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.RegexPattern;
import org.tuckey.web.filters.urlrewrite.utils.StringMatchingMatcher;
import org.tuckey.web.filters.urlrewrite.utils.StringMatchingPattern;
import org.tuckey.web.filters.urlrewrite.utils.StringMatchingPatternSyntaxException;
import org.tuckey.web.filters.urlrewrite.utils.StringUtils;
import org.tuckey.web.filters.urlrewrite.utils.WildcardPattern;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Calendar;
/**
* Conditions must be met when the filter is processing a url.
*
* @author Paul Tuckey
* @version $Revision: 48 $ $Date: 2006-11-27 16:53:31 +1300 (Mon, 27 Nov 2006) $
*/
public class Condition extends TypeConverter {
private static Log log = Log.getLog(Condition.class);
/**
* Should this expression be matched case sensitively.
*/
private boolean caseSensitive = false;
/**
* Used to identify the condition.
*/
private int id = 0;
/**
* Used to cache the pattern for faster execution.
*/
private StringMatchingPattern pattern;
/**
* The name of the header etc.
*/
private String name;
/**
* The operator that should be used to evaluate the condition.
*/
private short operator;
/**
* Value of this condition if the type is header.
*/
private String strValue;
/**
* Value of this condition if the type is port.
*/
private long numericValue = 0;
/**
* What to do with the next rule, this will indicate "or" otherwise do "and".
*/
private boolean processNextOr = false;
private boolean valid = false;
private boolean initialised = false;
// Operators
private static final short OPERATOR_EQUAL = 1;
private static final short OPERATOR_NOT_EQUAL = 2;
private static final short OPERATOR_GREATER_THAN = 3;
private static final short OPERATOR_LESS_THAN = 4;
private static final short OPERATOR_GREATER_THAN_OR_EQUAL = 5;
private static final short OPERATOR_LESS_THAN_OR_EQUAL = 6;
private static final short OPERATOR_INSTANCEOF = 7;
// if we are doing an instanceof test the class we want to test against
Class instanceOfClass = null;
// the rule that "owns" this condition.
private RuleBase rule;
/**
* Will check and see if the condition matches the request.
*
* @param hsRequest
* @return true on match
* @deprecated use getConditionMatch(HttpServletRequest hsRequest)
*/
public boolean matches(final HttpServletRequest hsRequest) {
return getConditionMatch(hsRequest) != null;
}
/**
* Will check and see if the condition matches the request.
*
* @param hsRequest
* @return true on match
*/
public ConditionMatch getConditionMatch(final HttpServletRequest hsRequest) {
if (!initialised) {
log.debug("condition not initialised skipping");
// error initialising do not process
return null;
}
if (!valid) {
log.debug("condition not valid skipping");
return null;
}
switch (type) {
case TYPE_TIME:
return evaluateNumericCondition(System.currentTimeMillis());
case TYPE_TIME_YEAR:
return evaluateCalendarCondition(Calendar.YEAR);
case TYPE_TIME_MONTH:
return evaluateCalendarCondition(Calendar.MONTH);
case TYPE_TIME_DAY_OF_MONTH:
return evaluateCalendarCondition(Calendar.DAY_OF_MONTH);
case TYPE_TIME_DAY_OF_WEEK:
return evaluateCalendarCondition(Calendar.DAY_OF_WEEK);
case TYPE_TIME_AMPM:
return evaluateCalendarCondition(Calendar.AM_PM);
case TYPE_TIME_HOUR_OF_DAY:
return evaluateCalendarCondition(Calendar.HOUR_OF_DAY);
case TYPE_TIME_MINUTE:
return evaluateCalendarCondition(Calendar.MINUTE);
case TYPE_TIME_SECOND:
return evaluateCalendarCondition(Calendar.SECOND);
case TYPE_TIME_MILLISECOND:
return evaluateCalendarCondition(Calendar.MILLISECOND);
case TYPE_ATTRIBUTE:
return evaluateAttributeCondition(name == null ? null : hsRequest.getAttribute(name));
case TYPE_AUTH_TYPE:
return evaluateStringCondition(hsRequest.getAuthType());
case TYPE_CHARACTER_ENCODING:
return evaluateStringCondition(hsRequest.getCharacterEncoding());
case TYPE_CONTENT_LENGTH:
return evaluateNumericCondition(hsRequest.getContentLength());
case TYPE_CONTENT_TYPE:
return evaluateStringCondition(hsRequest.getContentType());
case TYPE_CONTEXT_PATH:
return evaluateStringCondition(hsRequest.getContextPath());
case TYPE_COOKIE:
return evaluateCookieCondition(hsRequest.getCookies(), name);
case TYPE_LOCAL_PORT:
return evaluateNumericCondition(hsRequest.getLocalPort());
case TYPE_METHOD:
return evaluateStringCondition(hsRequest.getMethod());
case TYPE_PARAMETER:
return evaluateStringCondition(name == null ? null : hsRequest.getParameter(name));
case TYPE_PATH_INFO:
return evaluateStringCondition(hsRequest.getPathInfo());
case TYPE_PATH_TRANSLATED:
return evaluateStringCondition(hsRequest.getPathTranslated());
case TYPE_PROTOCOL:
return evaluateStringCondition(hsRequest.getProtocol());
case TYPE_QUERY_STRING:
return evaluateStringCondition(hsRequest.getQueryString());
case TYPE_REMOTE_ADDR:
return evaluateStringCondition(hsRequest.getRemoteAddr());
case TYPE_REMOTE_HOST:
return evaluateStringCondition(hsRequest.getRemoteHost());
case TYPE_REMOTE_USER:
return evaluateStringCondition(hsRequest.getRemoteUser());
case TYPE_REQUESTED_SESSION_ID:
return evaluateStringCondition(hsRequest.getRequestedSessionId());
case TYPE_REQUEST_URI:
return evaluateStringCondition(hsRequest.getRequestURI());
case TYPE_REQUEST_URL:
StringBuffer requestUrlBuff = hsRequest.getRequestURL();
String requestUrlStr = null;
if (requestUrlBuff != null) {
requestUrlStr = requestUrlBuff.toString();
}
return evaluateStringCondition(requestUrlStr);
case TYPE_SESSION_ATTRIBUTE:
Object sessionAttributeValue = null;
final HttpSession session = hsRequest.getSession(false);
if (session != null && name != null) {
sessionAttributeValue = session.getAttribute(name);
}
return evaluateAttributeCondition(sessionAttributeValue);
case TYPE_SESSION_IS_NEW:
boolean sessionNew = false;
final HttpSession sessionIsNew = hsRequest.getSession(false);
if (sessionIsNew != null) {
sessionNew = sessionIsNew.isNew();
}
return evaluateBoolCondition(sessionNew);
case TYPE_SERVER_PORT:
return evaluateNumericCondition(hsRequest.getServerPort());
case TYPE_SERVER_NAME:
return evaluateStringCondition(hsRequest.getServerName());
case TYPE_SCHEME:
return evaluateStringCondition(hsRequest.getScheme());
case TYPE_USER_IN_ROLE:
log.debug("is user in role " + name + " op " + operator);
return evaluateBoolCondition(hsRequest.isUserInRole(name));
case TYPE_EXCEPTION:
String eName = null;
Exception e = (Exception) hsRequest.getAttribute("javax.servlet.error.exception");
if (OPERATOR_INSTANCEOF == operator) {
return evaluateInstanceOfCondition(e);
} else {
if (e != null && e.getClass() != null) eName = e.getClass().getName();
return evaluateStringCondition(eName);
}
default:
return evaluateHeaderCondition(hsRequest);
}
}
private ConditionMatch evaluateAttributeCondition(Object attribObject) {
String attribValue = null;
if (attribObject == null) {
if (log.isDebugEnabled()) {
log.debug(name + " doesn't exist");
}
} else {
attribValue = attribObject.toString();
}
if (OPERATOR_INSTANCEOF == operator) {
return evaluateInstanceOfCondition(attribObject);
} else {
return evaluateStringCondition(attribValue);
}
}
private ConditionMatch evaluateInstanceOfCondition(Object obj) {
// only test for instanceof if object is not null
if (obj == null) return null;
if (log.isDebugEnabled()) {
log.debug("is " + obj.getClass() + " an instanceof " + instanceOfClass);
}
if (instanceOfClass == null) {
log.error("this condition may have failed to initialise correctly, instanceof class is null");
return null;
}
if (instanceOfClass.isInstance(obj)) {
log.debug("yes");
return new ConditionMatch();
}
log.debug("no");
return null;
}
private ConditionMatch evaluateCookieCondition(Cookie[] cookies, String name) {
if (cookies == null) {
// we will have to do an exists check
return evaluateBoolCondition(false);
}
if (name == null) {
return evaluateBoolCondition(false);
}
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
if (cookie == null) {
continue;
}
if (name.equals(cookie.getName())) {
return evaluateStringCondition(cookie.getValue());
}
}
return evaluateBoolCondition(false);
}
private ConditionMatch evaluateStringCondition(String value) {
if (pattern == null && value == null) {
log.debug("value is empty and pattern is also, condition false");
return evaluateBoolCondition(false);
} else if (pattern == null) {
log.debug("value isn't empty but pattern is, assuming checking for existence, condition true");
return evaluateBoolCondition(true);
}
if (value == null) {
// value is null make value ""
value = "";
}
if (log.isDebugEnabled()) {
log.debug("evaluating \"" + value + "\" against " + strValue);
}
StringMatchingMatcher matcher = pattern.matcher(value);
return evaluateBoolCondition(matcher, matcher.find());
}
/**
* Evaluate taking into account the operator, not only boolean operators considered.
*/
private ConditionMatch evaluateBoolCondition(boolean outcome) {
if (log.isTraceEnabled()) {
log.trace("outcome " + outcome);
}
if (operator == OPERATOR_NOT_EQUAL) {
log.debug("not equal operator in use");
return !outcome ? new ConditionMatch() : null;
}
return outcome ? new ConditionMatch() : null;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -