📄 requestutil.java
字号:
}
}
// if there were non-metacharacters, copy them all as a block
if (laPos > strPos) {
dec.append(str.substring(strPos,laPos));
strPos = laPos;
}
// shortcut out of here if we're at the end of the string
if (strPos >= strLen) {
break;
}
// process next metacharacter
char metaChar = str.charAt(strPos);
if (metaChar == '+') {
dec.append(' ');
strPos++;
continue;
} else if (metaChar == '%') {
char c = (char) Integer.parseInt(str.substring(strPos + 1, strPos + 3), 16);
if(c == '/' || c == '%' || c=='.' || c == '\\' || c == '\0')
dec.append(str.substring(strPos, strPos+3));
else
dec.append(c);
strPos += 3;
}
}
return dec.toString();
}
public static String unUrlDecode(String data) {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < data.length(); i++) {
char c = data.charAt(i);
switch (c) {
case '+':
buf.append(' ');
break;
case '%':
// XXX XXX
try {
buf.append((char) Integer.parseInt(data.substring(i+1,
i+3), 16));
i += 2;
} catch (NumberFormatException e) {
String msg = "Decode error ";
// XXX no need to add sm just for that
// sm.getString("serverRequest.urlDecode.nfe", data);
throw new IllegalArgumentException(msg);
} catch (StringIndexOutOfBoundsException e) {
String rest = data.substring(i);
buf.append(rest);
if (rest.length()==2)
i++;
}
break;
default:
buf.append(c);
break;
}
}
return buf.toString();
}
// Basically return everything after ";charset="
// If no charset specified, use the HTTP default (ASCII) character set.
public static String getCharsetFromContentType(String type) {
if (type == null) {
return null;
}
int semi = type.indexOf(";");
if (semi == -1) {
return null;
}
String afterSemi = type.substring(semi + 1);
int charsetLocation = afterSemi.indexOf("charset=");
if (charsetLocation == -1) {
return null;
}
String afterCharset = afterSemi.substring(charsetLocation + 8);
// The charset value in a Content-Type header is allowed to be quoted
// and charset values can't contain quotes. Just convert any quote
// chars into spaces and let trim clean things up.
afterCharset = afterCharset.replace('"', ' ');
String encoding = afterCharset.trim();
return encoding;
}
public static Locale getLocale(Request req) {
String acceptLanguage = req.getHeader("Accept-Language");
if( acceptLanguage == null ) return Locale.getDefault();
Hashtable languages = new Hashtable();
Vector quality=new Vector();
processAcceptLanguage(acceptLanguage, languages,quality);
if (languages.size() == 0) return Locale.getDefault();
Vector l = new Vector();
extractLocales( languages,quality, l);
return (Locale)l.elementAt(0);
}
public static Enumeration getLocales(HttpServletRequest req) {
String acceptLanguage = req.getHeader("Accept-Language");
// Short circuit with an empty enumeration if null header
if (acceptLanguage == null) {
Vector v = new Vector();
v.addElement(Locale.getDefault());
return v.elements();
}
Hashtable languages = new Hashtable();
Vector quality=new Vector();
processAcceptLanguage(acceptLanguage, languages , quality);
if (languages.size() == 0) {
Vector v = new Vector();
v.addElement(Locale.getDefault());
return v.elements();
}
Vector l = new Vector();
extractLocales( languages, quality , l);
return l.elements();
}
public static void processAcceptLanguage( String acceptLanguage,
Hashtable languages, Vector q)
{
StringTokenizer languageTokenizer =
new StringTokenizer(acceptLanguage, ",");
while (languageTokenizer.hasMoreTokens()) {
String language = languageTokenizer.nextToken().trim();
int qValueIndex = language.indexOf(';');
int qIndex = language.indexOf('q');
int equalIndex = language.indexOf('=');
Double qValue = new Double(1);
if (qValueIndex > -1 &&
qValueIndex < qIndex &&
qIndex < equalIndex) {
String qValueStr = language.substring(qValueIndex + 1);
language = language.substring(0, qValueIndex);
qValueStr = qValueStr.trim().toLowerCase();
qValueIndex = qValueStr.indexOf('=');
qValue = new Double(0);
if (qValueStr.startsWith("q") &&
qValueIndex > -1) {
qValueStr = qValueStr.substring(qValueIndex + 1);
try {
qValue = new Double(qValueStr.trim());
} catch (NumberFormatException nfe) {
}
}
}
// XXX
// may need to handle "*" at some point in time
if (! language.equals("*")) {
String key = qValue.toString();
Vector v;
if (languages.containsKey(key)) {
v = (Vector)languages.get(key) ;
} else {
v= new Vector();
q.addElement(qValue);
}
v.addElement(language);
languages.put(key, v);
}
}
}
public static void extractLocales(Hashtable languages, Vector q,Vector l)
{
// XXX We will need to order by q value Vector in the Future ?
Enumeration e = q.elements();
while (e.hasMoreElements()) {
Vector v =
(Vector)languages.get(((Double)e.nextElement()).toString());
Enumeration le = v.elements();
while (le.hasMoreElements()) {
String language = (String)le.nextElement();
String country = "";
int countryIndex = language.indexOf("-");
if (countryIndex > -1) {
country = language.substring(countryIndex + 1).trim();
language = language.substring(0, countryIndex).trim();
}
l.addElement(new Locale(language, country));
}
}
}
/**
* Filter the specified message string for characters that are sensitive
* in HTML. This avoids potential attacks caused by including JavaScript
* codes in the request URL that is often reported in error messages.
*
* @param message The message string to be filtered
*/
public static String filter(String message) {
if (message == null)
return (null);
char content[] = new char[message.length()];
message.getChars(0, message.length(), content, 0);
StringBuffer result = new StringBuffer(content.length + 50);
for (int i = 0; i < content.length; i++) {
switch (content[i]) {
case '<':
result.append("<");
break;
case '>':
result.append(">");
break;
case '&':
result.append("&");
break;
case '"':
result.append(""");
break;
default:
result.append(content[i]);
}
}
return (result.toString());
}
/* -------------------- From HttpDate -------------------- */
// Parse date - XXX This code is _very_ slow ( 3 parsers, GregorianCalendar,
// etc ). It was moved out to avoid creating 1 Calendar instance ( and
// a associated parsing ) per header ( the Calendar was created in HttpDate
// which was created for each HeaderField ).
// This also avoid passing HttpHeaders - which was required to access
// HttpHeaderFiled to access HttpDate to access the parsing code.
// we force our locale here as all http dates are in english
private final static Locale loc = Locale.US;
// all http dates are expressed as time at GMT
private final static TimeZone zone = TimeZone.getTimeZone("GMT");
// format for RFC 1123 date string -- "Sun, 06 Nov 1994 08:49:37 GMT"
private final static String rfc1123Pattern ="EEE, dd MMM yyyyy HH:mm:ss z";
// format for RFC 1036 date string -- "Sunday, 06-Nov-94 08:49:37 GMT"
private final static String rfc1036Pattern ="EEEEEEEEE, dd-MMM-yy HH:mm:ss z";
// format for C asctime() date string -- "Sun Nov 6 08:49:37 1994"
private final static String asctimePattern ="EEE MMM d HH:mm:ss yyyyy";
private final static SimpleDateFormat rfc1123Format =
new SimpleDateFormat(rfc1123Pattern, loc);
private final static SimpleDateFormat rfc1036Format =
new SimpleDateFormat(rfc1036Pattern, loc);
private final static SimpleDateFormat asctimeFormat =
new SimpleDateFormat(asctimePattern, loc);
public static long toDate( String dateString ) {
// XXX
Date date=null;
try {
date = rfc1123Format.parse(dateString);
} catch (ParseException e) { }
catch (StringIndexOutOfBoundsException e) { }
if( date==null)
try {
date = rfc1036Format.parse(dateString);
} catch (ParseException e) { }
catch (StringIndexOutOfBoundsException e) { }
if( date==null)
try {
date = asctimeFormat.parse(dateString);
} catch (ParseException pe) {
}
catch (StringIndexOutOfBoundsException e) { }
if(date==null) {
return -1;
}
// Original code was:
// Calendar calendar = new GregorianCalendar(zone, loc);
//calendar.setTime(date);
// calendar.getTime().getTime();
return date.getTime();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -