com.yixan.base.web.utils.WebUtil Maven / Gradle / Ivy
/*
* 文 件 名:WebUtil.java
* 版 权:Copyright 2012 JESHING Tech.Co.Ltd.All Rights Reserved.
* 描 述:
* 修 改 人:王波
* 修改时间:2014-4-3
* 修改内容:新增
*/
package com.yixan.base.web.utils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.subject.WebSubject;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.yixan.tools.common.util.FileUtil;
import com.yixan.tools.common.util.StringUtil;
/**
* Web 工具类
*
* @author 王波
* @author 赵卉华 修改为非静态方法, 增加由调用方传入HttpServletRequest的构造方法
* 解决在404和500页面, 通过RequestContextHolder.getRequestAttributes()获取Request报空指针的问题
* @version C01 2014-4-3
* @since v1.0
*/
public class WebUtil {
private final HttpServletRequest request;
/**
* 实例
*/
private static final WebUtil instance = new WebUtil();
/**
* 构造函数
*/
private WebUtil() {
this.request = null;
}
/**
* 构造函数
*/
public WebUtil(HttpServletRequest request) {
this.request = request;
}
/**
* 获取实例
*
* @author 赵卉华
* @return WebUtil实例
*/
public static WebUtil getInstance() {
return instance;
}
/**
* 返回当前请求对象
*
* @return 当前请求对象
*/
public HttpServletRequest getRequest() {
if (request != null) {
return request;
}
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
if (ra != null) {
return ((ServletRequestAttributes) ra).getRequest();
} else {
try {
Subject subject = SecurityUtils.getSubject();
if (subject instanceof WebSubject) {
ServletRequest sr = ((WebSubject) subject).getServletRequest();
if (sr instanceof HttpServletRequest) {
return (HttpServletRequest) sr;
}
}
} catch (Exception ignore) {
}
return null;
}
}
/**
* 获取 session
*
* @return session;session不存在时创建一个;
*/
public HttpSession getSession() {
HttpServletRequest request = getRequest();
return request == null ? null : request.getSession();
}
/**
* 获取 session
*
* @param create SESSION不存在时是否创建
* @return session
*/
public HttpSession getSession(boolean create) {
HttpServletRequest request = getRequest();
return request == null ? null : request.getSession(create);
}
/**
* 获取 session id
*
* @return session
*/
public String getSessionId() {
HttpSession session = getSession(false);
return session == null ? null : session.getId();
}
/**
* 设置SESSION属性
*
* @author 赵卉华
* @param name 属性名称
* @param value 属性值
*/
public void setSessionAttribute(String name, Object value) {
HttpSession session = getSession();
session.setAttribute(name, value);
}
/**
* 获取session 属性
*
* @param name 属性名称
* @return 属性值;session 不存在时返回null;session中没有该熟属性时返回null
*/
public Object getSessionAttribute(String name) {
HttpSession session = getRequest().getSession(false);
return session == null ? null : session.getAttribute(name);
}
/**
* 获取session 属性
*
* @param 返回对象的类型
* @param name 属性名称
* @param type 对象类型
* @return 属性值;session 不存在时返回null;session中没有该熟属性时返回null
*/
@SuppressWarnings("unchecked")
public T getSessionAttribute(String name, Class type) {
return (T) getSessionAttribute(name);
}
/**
* 获取session属性列表
*
* @param 返回对象的类型
* @param name 属性名称
* @param type 对象类型
* @return 属性值列表;session 不存在时返回null;session中没有该熟属性时返回null
*/
@SuppressWarnings("unchecked")
public List getSessionAttributes(String name, Class type) {
return (List) getSessionAttribute(name);
}
/**
* 获取Web工程容器路径
* 如果部署为ROOT将返回空字符串, 否则返回斜杠加工程名; 如工程名为cheetah-site-web, 返回/cheetah-site-web
*
* @return context path
*/
public String getContextPath() {
HttpServletRequest request = getRequest();
return request == null ? null : request.getContextPath();
}
/**
* 获取应用根目录
*
* @return root path
*/
public String getWebRoot() {
HttpServletRequest request = getRequest();
return request == null ? null : request.getServletContext().getRealPath("/");
}
/**
* 获取相对于根目录的路径
*
* @param path 相对路径
* @return root path
*/
public String getWebRoot(String path) {
HttpServletRequest request = getRequest();
return request == null ? null : request.getServletContext().getRealPath(path);
}
/**
* 获取相对于根路径的URL
*
* @return 绝对路径URL
*/
public String getWebUrl() {
// 获取服务器请求信息
HttpServletRequest request = getRequest();
String scheme = request.getScheme();
String host = request.getServerName();
int port = request.getServerPort();
String ctxPath = request.getContextPath();
String prefix = scheme + "://" + host;
if (scheme == "http" && port != 80 || scheme == "https" && port != 443) {
prefix += ":" + port;
}
return FileUtil.concat(prefix, ctxPath);
}
/**
* 获取相对于根路径的URL
*
* @param uri 相对路径, 如果uri以/开头则不追加ContextPath
* @return 绝对路径URL
*/
public String getWebUrl(String uri) {
if (uri == null || StringUtil.isUrl(uri)) {
return uri;
}
// 获取服务器请求信息
HttpServletRequest request = getRequest();
String scheme = request.getScheme();
String host = request.getServerName();
int port = request.getServerPort();
String prefix = scheme + "://" + host;
if (scheme == "http" && port != 80 || scheme == "https" && port != 443) {
prefix += ":" + port;
}
if (uri.startsWith("/")) {
return FileUtil.concat(prefix, uri);
} else {
String ctxPath = request.getContextPath();
return FileUtil.concat(prefix, ctxPath, uri);
}
}
/**
* 根据服务器请求信息替换URL中的占位符
* 如: getServerURL("http://{ServerName}:{ServerPort}/xxx/")
* 返回 http://127.0.0.1:8080/xxx/
*
* @author 赵卉华
* @param url 替换前的URL
* @return 替换后的URL
*/
public String getServerURL(String url) {
if (url == null) {
return url;
}
url = url.trim();
if (url.indexOf("{") < 0 || url.indexOf("}") < 0) {
return url;
}
// 获取服务器请求信息
HttpServletRequest request = getRequest();
String scheme = request.getScheme();
String host = request.getServerName();
String port = String.valueOf(request.getServerPort());
// ContextPath是以/斜杠开头的, substring(1)去掉斜杠
String ctx = request.getContextPath();
if (ctx != null && ctx.length() > 0) {
ctx = request.getContextPath().substring(1);
}
// 占位符正则表达式
Pattern pScheme = Pattern.compile("\\{[Ss]cheme\\}");
Pattern pHost = Pattern.compile("\\{[Ss]erverName\\}");
Pattern pPort = Pattern.compile("\\{[Ss]erverPort\\}");
Pattern pCtx = Pattern.compile("\\{[Cc]ontextPath\\}");
// 替换占位符
url = pScheme.matcher(url).replaceFirst(scheme);
url = pHost.matcher(url).replaceFirst(host);
url = pPort.matcher(url).replaceFirst(port);
url = pCtx.matcher(url).replaceFirst(ctx);
return Pattern.compile("(? getRequestParams() {
HttpServletRequest request = getRequest();
if (request == null) {
return null;
}
Map params = new HashMap<>();
for (Entry entry : request.getParameterMap().entrySet()) {
String name = entry.getKey();
String[] values = entry.getValue();
if (values == null || values.length == 0) {
params.put(name, null);
} else if (values.length == 1) {
params.put(name, values[0]);
} else {
StringBuilder buffer = new StringBuilder();
for (String value : values) {
if (buffer.length() > 0) {
buffer.append(",");
}
buffer.append(value);
}
params.put(name, buffer.toString());
}
}
return params;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy