📄 setattribute.java
字号:
if ( hsResponse instanceof UrlRewriteWrappedResponse ) {
((UrlRewriteWrappedResponse) hsResponse).setOverridenMethod(value);
} else {
log.warn("unable to set request method as request not a UrlRewriteWrappedResponse");
}
} else if (type == SET_TYPE_PARAM) {
log.debug("setting request parameter");
if ( hsResponse instanceof UrlRewriteWrappedResponse ) {
((UrlRewriteWrappedResponse) hsResponse).addOverridenRequestParameter(name, value);
} else {
log.warn("unable to set request parameter as request not a UrlRewriteWrappedResponse");
}
} else if (type == SET_TYPE_SESSION) {
log.debug("setting session attrib");
HttpSession session = hsRequest.getSession(true);
if (session == null) {
log.warn("could not create a new session for a request");
} else {
session.setAttribute(name, value);
}
} else if (type == SET_TYPE_RESPONSE_HEADER) {
log.debug("setting response header");
hsResponse.addHeader(name, value);
} else if (type == SET_TYPE_STAUS) {
log.debug("setting status");
hsResponse.setStatus(numericValue);
} else if (type == SET_TYPE_COOKIE) {
Cookie cookieToAdd = getCookie(name, value);
if ( cookieToAdd != null ) {
log.debug("adding cookie");
hsResponse.addCookie(cookieToAdd);
}
} else if (type == SET_TYPE_CONTENT_TYPE) {
log.debug("setting content type");
hsResponse.setContentType(value);
} else if (type == SET_TYPE_CHARSET) {
log.debug("setting charset");
hsResponse.setCharacterEncoding(value);
} else if (type == SET_TYPE_LOCALE) {
log.debug("setting charset");
hsResponse.setLocale(locale);
} else if (type == SET_TYPE_EXPIRES) {
log.debug("setting expires");
hsResponse.setDateHeader("Expires", System.currentTimeMillis() + expiresValueAdd);
} else {
log.warn("unknown type" + type);
}
}
public boolean initialise() {
initialised = true;
if (value != null) {
if (BackReferenceReplacer.containsBackRef(value)) {
valueContainsBackRef = true;
}
if (VariableReplacer.containsVariable(value)) {
valueContainsVariable = true;
}
if (FunctionReplacer.containsFunction(value)) {
valueContainsFunction = true;
}
}
if (type == SET_TYPE_STAUS) {
initNumericValue();
} else if (type == SET_TYPE_LOCALE) {
// value might be zh-CN-abcdef or zh-CN or zh
locale = null;
if (value == null) {
setError("Locale is not valid because value is null");
} else if (value.matches("[a-zA-Z][a-zA-Z]")) {
locale = new Locale(value);
} else if (value.matches("[a-zA-Z][a-zA-Z]-[a-zA-Z][a-zA-Z]")) {
locale = new Locale(value.substring(1, 2), value.substring(2, 4));
} else if (value.matches("[a-zA-Z][a-zA-Z]-[a-zA-Z][a-zA-Z]-.*")) {
locale = new Locale(value.substring(1, 2), value.substring(4, 5), value.substring(6, value.length()));
} else {
setError("Locale " + value + " is not valid (valid locales are, zh, zh-CN, zh-CN-rural)");
}
} else if (type == SET_TYPE_COOKIE) {
// VAL[:domain[:lifetime[:path]]]
if (value != null && name != null) {
getCookie(name, value);
} else {
setError("cookie must have a name and a value");
}
} else if (type == SET_TYPE_EXPIRES) {
// "access plus 1 month"
if (value != null ) {
expiresValueAdd = parseTimeValue(value);
} else {
setError("expires must have a value");
}
}
if (error == null) {
valid = true;
}
return valid;
}
/**
* takes a string a number expression and converts it to a long.
* syntax: number type
*
* Valid examples: "1 day", "2 days", "1 hour", "1 hour 2 minutes", "34 months"
*
* Any positive number is valid
*
* Valid types are: years, months, weeks, days, hours, minutes, seconds
*
* note, this syntax is a direct copy of mod_expires syntax
* http://httpd.apache.org/docs/2.0/mod/mod_expires.html
*
* note, a year is calculated as 365.25 days and a month as 365.25 days divided by 12.
*/
protected long parseTimeValue(String parsingValue) {
long calculatedMillis = 0;
if ( parsingValue.startsWith("access")) parsingValue = parsingValue.substring("access".length()).trim();
if ( parsingValue.startsWith("plus")) parsingValue = parsingValue.substring("plus".length()).trim();
log.debug("calculating expires ms based on '" + parsingValue + "'");
Matcher matcher = Pattern.compile("([0-9]+)\\s+(\\w+)").matcher(parsingValue);
while ( matcher.find()) {
long num = NumberUtils.stringToInt(matcher.group(1), -1);
if ( num < 0 ) setError("could not calculate numeric value of " + matcher.group(1));
String part = matcher.group(2);
log.debug("adding '"+num+"' '" + part + "'");
long addThisRound = 0;
if ( part.matches("year[s]?") ) addThisRound = num * Math.round(1000 * 60 * 60 * 24 * 365.25);
if ( part.matches("month[s]?") ) addThisRound = num * Math.round( 1000 * 60 * 60 * 24 * (365.25/12) );
if ( part.matches("week[s]?") ) addThisRound = num * ( 1000 * 60 * 60 * 24 * 7 );
if ( part.matches("day[s]?") ) addThisRound = num * ( 1000 * 60 * 60 * 24 );
if ( part.matches("hour[s]?") ) addThisRound = num * ( 1000 * 60 * 60 );
if ( part.matches("minute[s]?") ) addThisRound = num * ( 1000 * 60 );
if ( part.matches("second[s]?") ) addThisRound = num * ( 1000 );
if ( addThisRound == 0 ) {
setError("unkown time unit '" + part + "'");
}
calculatedMillis += addThisRound;
}
if ( calculatedMillis == 0 ) {
setError("could not calculate expires time from '"+parsingValue+"'");
}
return calculatedMillis;
}
private Cookie getCookie(String name, String value) {
if ( log.isDebugEnabled() ) {
log.debug("making cookie for " + name + ", " + value);
}
if ( name == null ) {
log.info("getCookie called with null name");
return null;
}
Cookie cookie;
if (value != null && value.indexOf(":") != -1) {
// we must have extra items
String items[] = value.split(":");
cookie = new Cookie(name, items[0]);
if (items.length > 1) cookie.setDomain(items[1]);
if (items.length > 2) cookie.setMaxAge(NumberUtils.stringToInt(items[2]));
if (items.length > 3) cookie.setPath(items[3]);
} else {
cookie = new Cookie(name, value);
}
return cookie;
}
/**
* Will init a numeric value type ie port.
*/
private void initNumericValue() {
if (numericValue == 0) {
numericValue = NumberUtils.stringToInt(StringUtils.trim(value));
if (numericValue == 0 && !"0".equals(value)) {
setError("Value " + value + " is not a valid number (tried to cast to java type long)");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -