📄 servletutil.java
字号:
package jodd.servlet;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jodd.util.Base64;
/**
* Miscellaneous servlet utils.
*/
public final class ServletUtil {
/**
* Examines if request is a multipart.
*
* @param request http request
*
* @return <code>true</code> if request is multipart, otherwise <code>false</code>
*/
public static boolean isRequestMultipart(HttpServletRequest request) {
String type = request.getHeader("Content-Type");
if ((type == null) || !type.startsWith("multipart/form-data")) {
return false;
}
return true;
}
// ---------------------------------------------------------------- authorization
/**
* Decodes the "Authorization" header and retrieves the
* user's name from it. Returns null if the header is not present.
*
* @param request
*
* @return user name
*/
public static String getAuthUsername(HttpServletRequest request) {
String header = request.getHeader("Authorization");
if (header == null) {
return null;
}
String encoded = header.substring(header.indexOf(" ") + 1);
String decoded = new String(Base64.decode(encoded));
return decoded.substring(0, decoded.indexOf(":"));
}
/**
* Decodes the "Authorization" header and retrieves the
* password from it. Returns null if the header is not present.
*
* @param request http request
*
* @return password
*/
public static String getAuthPassword(HttpServletRequest request) {
String header = request.getHeader("Authorization");
if (header == null) {
return null;
}
String encoded = header.substring(header.indexOf(" ") + 1);
String decoded = new String(Base64.decode(encoded));
return decoded.substring(decoded.indexOf(":") +1);
}
/**
* Sends correct headers to require basic authentication for the
* given realm.
*
* @param resp
* @param realm
*
* @exception IOException
*/
public static void requireAuthentication(HttpServletResponse resp, String realm) throws IOException {
resp.setHeader("WWW-Authenticate", "Basic realm=\"" + realm + "\"");
resp.sendError(HttpServletResponse.SC_UNAUTHORIZED);
}
// ---------------------------------------------------------------- cookie
/**
* Returns cookie value from client.
*
* @param request request
* @param cookieName name of the cookie
*
* @return cookie value or null if cookie with specified name doesn't exist.
*/
public static Cookie getCookie(HttpServletRequest request, String cookieName) {
Cookie[] cookies = request.getCookies();
if (cookies != null) {
Cookie cookie;
for (int i = 0; i < cookies.length; i++) {
cookie = cookies[i];
if (cookie.getName().equals(cookieName)) {
return cookie;
}
}
}
return null;
}
// ---------------------------------------------------------------- GET parameters
/**
* Iterates the map and creates parameters suffix string. First character is
* not created, so this method may be used in both ways:
* <ol>for creating the complete url params</ol>
* <ol>for creating just one part of the url params</ol>
*
* All values (but not params) are url encoded.
*
* @param map
*
* @return url parameters
*/
public static String makeUrlParams(Map map) {
StringBuffer sb = new StringBuffer();
Iterator i = map.keySet().iterator();
boolean first = true;
while (i.hasNext()) {
if (first == false) {
sb.append('&');
} else {
first = false;
}
String key = (String) i.next();
sb.append(key).append('=');
sb.append(HtmlEncoder.encodeUrl((String) map.get(key)));
}
return sb.toString();
}
/**
* Creates Map from the given URL String with all parameter/value pairs
* extracted from it. Valid URL is expected (i.e. uri?p1=v1&p2=...). Values
* are decoded into regular strings.
*
* @param url url
*
* @return map with param/value pairs extracted from url
*/
public static HashMap getUrlParams(String url) {
HashMap map = new HashMap();
if (url == null) {
return map;
}
int urlLength = url.length();
// skip uri part
int j = 0;
int i = url.indexOf('?');
if (i != -1) {
j = i + 1;
} else {
return map;
}
if (j >= urlLength) {
return map;
}
// find params
while (j < urlLength) {
i = url.indexOf('&', j);
if (i == -1) {
i = urlLength;
}
int e = url.indexOf('=', j);
if (e == -1) {
e = urlLength;
}
if (e < i) {
map.put(url.substring(j, e), HtmlEncoder.decodeUrl(url.substring(e + 1, i)));
} else {
if (j != i) {
map.put(url.substring(j, i), "");
}
}
j = i + 1;
}
return map;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -