com.rt.web.util.WebUtil Maven / Gradle / Ivy
The newest version!
package com.rt.web.util;
import com.rt.core.constant.RTConst;
import com.rt.core.log.Log;
import com.rt.core.util.RTUtil;
import com.rt.logic.util.LogicUtil;
import com.rt.web.logic.WebLogic;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.io.Serializable;
/**
* web帮助类
*/
public class WebUtil extends LogicUtil {
private static Log log = Log.getLog(WebUtil.class);
private static final String UNKNOWN_IP_STR = "unknown";
/**
* 30分 1800秒
*/
public final static int TIME_1800s = 1800;
/**
* 24小时 86400秒
*/
public final static int TIME_86400s = 86400;
public final static String DOT_DO = ".do";
public final static String SECURITY_SESSION_ID = "sessionid";
/**
* 获取sessionid
*
* @param request request
* @return String
*/
public static String getSessionId(HttpServletRequest request) {
return request.getSession().getId();
}
/**
* 删除session对象
*
* @param request request
* @param name name
*/
public static void deleteSessionObject(HttpServletRequest request, String name) {
request.getSession().removeAttribute(name);
}
/**
* 写入session对象
*
* @param request request
* @param name name
* @param object object
*/
public static void putSessionObject(HttpServletRequest request, String name, Serializable object) {
request.getSession().setAttribute(name, object);
}
/**
* 获取session对象
*
* @param request request
* @param name name
* @param object
* @return T object
*/
public static T getSessionObject(HttpServletRequest request, String name) {
return (T) request.getSession().getAttribute(name);
}
/**
* 删除cookie对象
*
* @param request request
* @param response response
* @param name name
* @param uri uri
*/
public static void deleteCookieObject(HttpServletRequest request,
HttpServletResponse response, String name, String uri) {
Cookie[] cookieArray = request.getCookies();
if (cookieArray == null) {
return;
}
for (Cookie cookie : cookieArray) {
if (RTUtil.equals(name, cookie.getName())) {
cookie.setMaxAge(0);
if (RTUtil.isNotEmpty(uri)) {
cookie.setPath(uri);
} else {
cookie.setPath(WebConst.ROOT_PATH);
}
response.addCookie(cookie);
}
}
}
/**
* 获取cookie对象
*
* @param request request
* @param name name
* @return String
*/
public static String getCookieObject(HttpServletRequest request, String name) {
Cookie[] cookieArray = request.getCookies();
if (cookieArray == null) {
return null;
}
for (Cookie cookie : cookieArray) {
if (RTUtil.equals(name, cookie.getName())) {
return RTUtil.urlEncoderToString(cookie.getValue());
}
}
return null;
}
/**
* 添加新对象到cookie
*
* @param request request
* @param response response
* @param name name
* @param value value
* @param uri uri
* @param maxAge maxAge
*/
public static void setCookieObject(HttpServletRequest request,
HttpServletResponse response, String name, String value,
String uri, int maxAge) {
// 先删除旧的
deleteCookieObject(request, response, name, uri);
// 创建cookie
Cookie cookie = new Cookie(name, RTUtil.toURLEncoder(value));
// 单位是分
cookie.setMaxAge(maxAge);
if (RTUtil.isNotEmpty(uri)) {
cookie.setPath(uri);
} else {
cookie.setPath(WebConst.ROOT_PATH);
}
response.addCookie(cookie);
}
/**
* 获取sessionId
*
* @param request request
* @param response response
* @return String
*/
public static String initCookieId(HttpServletRequest request,
HttpServletResponse response) {
String sessionId = getCookieId(request);
if (RTUtil.isEmpty(sessionId)) {
sessionId = RTUtil.getUUID();
setCookieId(request, response, sessionId, TIME_1800s);
}
return sessionId;
}
/**
* 获取sessionId
*
* @param request request
* @return String
*/
public static String getCookieId(HttpServletRequest request) {
return getCookieObject(request, SECURITY_SESSION_ID);
}
/**
* 添加新sessionId到cookie
*
* @param request request
* @param response response
* @param sessionId sessionId
* @param maxAge maxAge
*/
public static void setCookieId(HttpServletRequest request,
HttpServletResponse response, String sessionId, int maxAge) {
setCookieObject(request, response, SECURITY_SESSION_ID, sessionId,
null, maxAge);
}
/**
* 获取客户端的真实IP
*
* @param request request
* @return String
*/
public static String getClientRealIp(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if (StringUtils.isBlank(ip) || UNKNOWN_IP_STR.equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (StringUtils.isBlank(ip) || UNKNOWN_IP_STR.equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (StringUtils.isBlank(ip) || UNKNOWN_IP_STR.equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
// 多个IP按照','分割,对于通过多个代理的情况,第一个IP为客户端真实IP
// '***.***.***.***'.length() = 15
if (null != ip && ip.length() > 15) {
if (ip.indexOf(",") > 0) {
ip = ip.substring(0, ip.indexOf(","));
}
}
return ip;
}
/**
* 添加缓存对象
* 只有生产环境,且启用缓存的情况下才有效.
*
* @param key key
* @param value value
*/
public static void putCacheItem(String key, Serializable value) {
WebLogic.putCacheItem(key, value);
}
/**
* 添加缓存对象
* 只有生产环境,且启用缓存的情况下才有效.
*
* @param key key
* @param value value
* @param millisDelay number of millisDelay after which an item must be discarded.
*/
public static void putCacheItem(String key, Serializable value, long millisDelay) {
WebLogic.putCacheItem(key, value, millisDelay);
}
/**
* 删除缓存对象
*
* @param key key
*/
public static void deleteCacheItem(String key) {
WebLogic.delCacheItem(key);
}
/**
* 获取缓存对象
*
* @param key key
* @param object
* @return T object
*/
public static T getCacheItem(String key) {
return WebLogic.getCacheItem(key);
}
}