package com.izkml.mlyun.core.tool.util;
import com.zkml.api.internal.util.StringUtils;
import org.apache.commons.codec.Charsets;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.util.WebUtils;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.Enumeration;
import java.util.function.Predicate;
/**
* @author zkml
*/
public class WebUtil extends WebUtils {
public static final String USER_AGENT_HEADER = "user-agent";
private static final String[] IP_HEADER_NAMES = new String[]{
"x-forwarded-for",
"Proxy-Client-IP",
"WL-Proxy-Client-IP",
"HTTP_CLIENT_IP",
"HTTP_X_FORWARDED_FOR"
};
private static final Predicate<String> IP_PREDICATE = (ip) -> StrUtils.isBlank(ip) || StrUtils.UNKNOWN.equalsIgnoreCase(ip);
public static HttpServletRequest getRequest() {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
return (requestAttributes == null) ? null : ((ServletRequestAttributes) requestAttributes).getRequest();
}
public static String getIP(HttpServletRequest request) {
if (request == null) {
return StrUtils.EMPTY;
}
String ip = null;
for (String ipHeader : IP_HEADER_NAMES) {
ip = request.getHeader(ipHeader);
if (!IP_PREDICATE.test(ip)) {
break;
}
}
if (IP_PREDICATE.test(ip)) {
ip = request.getRemoteAddr();
}
return StrUtils.isBlank(ip) ? null : StrUtils.splitTrim(ip, StrUtils.COMMA).get(0);
}
public static String getPath(String uriStr){
URI uri;
try {
uri = new URI(uriStr);
}catch (URISyntaxException e){
throw new RuntimeException(e);
}
return uri.getPath();
}
/**
* 获取 request 请求体
*
* @param servletInputStream servletInputStream
* @return body
*/
public static String getRequestBody(ServletInputStream servletInputStream) {
StringBuilder sb = new StringBuilder();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(servletInputStream, StandardCharsets.UTF_8));
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (servletInputStream != null) {
try {
servletInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}
/**
* 获取 request 请求内容
*
* @param request request
* @return String
*/
public static String getRequestContent(HttpServletRequest request) {
try {
String queryString = request.getQueryString();
if (StrUtils.isNotBlank(queryString)) {
return new String(queryString.getBytes(Charsets.ISO_8859_1), Charsets.UTF_8).replaceAll("&", "&").replaceAll("%22", """);
}
String charEncoding = request.getCharacterEncoding();
if (charEncoding == null) {
charEncoding = StrUtils.UTF_8;
}
byte[] buffer = getRequestBody(request.getInputStream()).getBytes();
String str = new String(buffer, charEncoding).trim();
if (StrUtils.isBlank(str)) {
StringBuilder sb = new StringBuilder();
Enumeration<String> parameterNames = request.getParameterNames();
while (parameterNames.hasMoreElements()) {
String key = parameterNames.nextElement();
String value = request.getParameter(key);
StrUtils.appendBuilder(sb, key, "=", value, "&");
}
str = StrUtils.removeSuffix(sb.toString(), "&");
}
return str.replaceAll("&", "&");
} catch (Exception ex) {
ex.printStackTrace();
return StrUtils.EMPTY;
}
}
}