📄 condition.java
字号:
/**
* Copyright (c) 2005, Paul Tuckey
* All rights reserved.
*
* Each copy or derived work must preserve the copyright notice and this
* notice unmodified.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
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.StringUtils;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
/**
* Conditions must be met when the filter is processing a url.
*
* @author Paul Tuckey
* @version $Revision: 1.16 $ $Date: 2005/12/07 10:27:02 $
*/
public final 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 Pattern 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;
/**
* Will check and see if the condition matches the request.
*
* @param hsRequest
* @return true on match
* @deprecated use
*/
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;
if (hsRequest.getSession() != null && name != null) {
sessionAttributeValue = hsRequest.getSession().getAttribute(name);
}
return evaluateAttributeCondition(sessionAttributeValue);
case TYPE_SESSION_IS_NEW:
boolean sessionNew = false;
HttpSession session = hsRequest.getSession();
if (session != null) {
sessionNew = session.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));
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();
}
return evaluateStringCondition(attribValue);
}
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 null;
}
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);
}
Matcher 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;
}
private ConditionMatch evaluateBoolCondition(Matcher matcher, boolean outcome) {
ConditionMatch conditionMatch = evaluateBoolCondition(outcome);
if ( conditionMatch != null ) {
conditionMatch.setMatcher(matcher);
}
return conditionMatch;
}
private ConditionMatch evaluateHeaderCondition(final HttpServletRequest hsRequest) {
String headerValue = null;
if (name != null) {
headerValue = hsRequest.getHeader(name);
}
return evaluateStringCondition(headerValue);
}
/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -