package com.base.util;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.util.WebUtils;
public class WebUtil extends WebUtils{
public final static String AJAX_HEADER = "x-requested-with";
public final static String XMLHTTPREQUEST = "XMLHttpRequest";
public static boolean isAjaxRequest() {
return isAjaxRequest(getHttpServletRequest());
}
public static boolean isAjaxRequest(HttpServletRequest request) {
String requestType = request.getHeader(AJAX_HEADER);
return XMLHTTPREQUEST.equalsIgnoreCase(requestType);
}
public static String getQueryString(HttpServletRequest request) {
@SuppressWarnings("unchecked")
Map<String, String[]> params = request.getParameterMap();
StringBuffer buffer = new StringBuffer();
for (String key : params.keySet()) {
String[] values = params.get(key);
int i = 0;
int countValues = values.length;
for (; i < countValues; i++) {
buffer.append(key);
buffer.append("=");
buffer.append(StringUtil.trimToEmpty(values[i]));
buffer.append("&");
}
}
String queryString = buffer.toString();
if (queryString.equals("")) {
return queryString;
}
else {
return queryString.substring(0, queryString.length() - 1);
}
}
public static Map<String, List<String>> getRequestQueryParameterMap(String result) {
Map<String, List<String>> map = new HashMap<String, List<String>>();
if(StringUtil.isEmpty(result)) {
return map;
}
String[] paramStrArray = result.split("&");
for(String paramStr : paramStrArray) {
if(paramStr.indexOf("=") == -1) {
continue;
}
String[] entry = new String[2];
entry[0] = StringUtil.trimAllWhitespace(paramStr.substring(0, paramStr.indexOf("=")));
entry[1] = StringUtil.trimAllWhitespace(paramStr.substring(paramStr.indexOf("=") + 1, paramStr.length()));
if(map.containsKey(entry[0])) {
map.get(entry[0]).add(entry[1]);
} else {
List<String> values = new ArrayList<String>();
values.add(entry[1]);
map.put(entry[0], values);
}
}
return map;
}
@SuppressWarnings("unchecked")
public static Map<String, String[]> getParameterMap(ServletRequest request){
Map<String, String[]> srcMap = request.getParameterMap();
Map<String, String[]> destMap = new HashMap<String, String[]>();
DataUtil.copyMap(srcMap, destMap);
Enumeration<String> enums = request.getAttributeNames();
if (DataUtil.isEmpty(enums)) {
return null;
}
while (enums.hasMoreElements()) {
String object = (String) enums.nextElement();
if(object.startsWith("log.")){
destMap.put(object, (String[])request.getAttribute(object));
}
}
return destMap;
}
public static String getRemoteAddr(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if(StringUtil.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("X-Real-IP");
}
if(StringUtil.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
public static HttpSession getHttpSession() {
return getHttpServletRequest().getSession();
}
public static HttpSession getHttpSession(HttpServletRequest request) {
return request.getSession();
}
public static HttpServletRequest getHttpServletRequest() {
return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
}
public static String[] getParameterValues(String parameterName) {
return getHttpServletRequest().getParameterValues(parameterName);
}
public static String getParameterValue(String parameterName) {
return getHttpServletRequest().getParameter(parameterName);
}
public static void setAttribute(String attributeName, Object obj) {
getHttpServletRequest().setAttribute(attributeName, obj);
}
public static Object getAttribute(String attributeName) {
return getHttpServletRequest().getAttribute(attributeName);
}
public static void setAttribute(HttpServletRequest request, String attributeName, Object obj) {
request.setAttribute(attributeName, obj);
}
public static Object getAttribute(HttpServletRequest request, String attributeName) {
return request.getAttribute(attributeName);
}
public static Object getObjectInSession(String key) {
return getHttpSession().getAttribute(key);
}
public static Object getObjectInSession(HttpServletRequest request, String key) {
return getHttpSession(request).getAttribute(key);
}
public static String getMsgFromStackTrace(Exception e){
ByteArrayOutputStream buf = new java.io.ByteArrayOutputStream();
e.printStackTrace(new java.io.PrintWriter(buf, true));
String expMessage = buf.toString();
return expMessage;
}
}
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.util.WebUtils;
public class WebUtil extends WebUtils{
public final static String AJAX_HEADER = "x-requested-with";
public final static String XMLHTTPREQUEST = "XMLHttpRequest";
public static boolean isAjaxRequest() {
return isAjaxRequest(getHttpServletRequest());
}
public static boolean isAjaxRequest(HttpServletRequest request) {
String requestType = request.getHeader(AJAX_HEADER);
return XMLHTTPREQUEST.equalsIgnoreCase(requestType);
}
public static String getQueryString(HttpServletRequest request) {
@SuppressWarnings("unchecked")
Map<String, String[]> params = request.getParameterMap();
StringBuffer buffer = new StringBuffer();
for (String key : params.keySet()) {
String[] values = params.get(key);
int i = 0;
int countValues = values.length;
for (; i < countValues; i++) {
buffer.append(key);
buffer.append("=");
buffer.append(StringUtil.trimToEmpty(values[i]));
buffer.append("&");
}
}
String queryString = buffer.toString();
if (queryString.equals("")) {
return queryString;
}
else {
return queryString.substring(0, queryString.length() - 1);
}
}
public static Map<String, List<String>> getRequestQueryParameterMap(String result) {
Map<String, List<String>> map = new HashMap<String, List<String>>();
if(StringUtil.isEmpty(result)) {
return map;
}
String[] paramStrArray = result.split("&");
for(String paramStr : paramStrArray) {
if(paramStr.indexOf("=") == -1) {
continue;
}
String[] entry = new String[2];
entry[0] = StringUtil.trimAllWhitespace(paramStr.substring(0, paramStr.indexOf("=")));
entry[1] = StringUtil.trimAllWhitespace(paramStr.substring(paramStr.indexOf("=") + 1, paramStr.length()));
if(map.containsKey(entry[0])) {
map.get(entry[0]).add(entry[1]);
} else {
List<String> values = new ArrayList<String>();
values.add(entry[1]);
map.put(entry[0], values);
}
}
return map;
}
@SuppressWarnings("unchecked")
public static Map<String, String[]> getParameterMap(ServletRequest request){
Map<String, String[]> srcMap = request.getParameterMap();
Map<String, String[]> destMap = new HashMap<String, String[]>();
DataUtil.copyMap(srcMap, destMap);
Enumeration<String> enums = request.getAttributeNames();
if (DataUtil.isEmpty(enums)) {
return null;
}
while (enums.hasMoreElements()) {
String object = (String) enums.nextElement();
if(object.startsWith("log.")){
destMap.put(object, (String[])request.getAttribute(object));
}
}
return destMap;
}
public static String getRemoteAddr(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if(StringUtil.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("X-Real-IP");
}
if(StringUtil.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
public static HttpSession getHttpSession() {
return getHttpServletRequest().getSession();
}
public static HttpSession getHttpSession(HttpServletRequest request) {
return request.getSession();
}
public static HttpServletRequest getHttpServletRequest() {
return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
}
public static String[] getParameterValues(String parameterName) {
return getHttpServletRequest().getParameterValues(parameterName);
}
public static String getParameterValue(String parameterName) {
return getHttpServletRequest().getParameter(parameterName);
}
public static void setAttribute(String attributeName, Object obj) {
getHttpServletRequest().setAttribute(attributeName, obj);
}
public static Object getAttribute(String attributeName) {
return getHttpServletRequest().getAttribute(attributeName);
}
public static void setAttribute(HttpServletRequest request, String attributeName, Object obj) {
request.setAttribute(attributeName, obj);
}
public static Object getAttribute(HttpServletRequest request, String attributeName) {
return request.getAttribute(attributeName);
}
public static Object getObjectInSession(String key) {
return getHttpSession().getAttribute(key);
}
public static Object getObjectInSession(HttpServletRequest request, String key) {
return getHttpSession(request).getAttribute(key);
}
public static String getMsgFromStackTrace(Exception e){
ByteArrayOutputStream buf = new java.io.ByteArrayOutputStream();
e.printStackTrace(new java.io.PrintWriter(buf, true));
String expMessage = buf.toString();
return expMessage;
}
}