package com.qxueyou.scc.base.util;
|
|
import java.io.IOException;
|
import java.net.InetAddress;
|
import java.net.NetworkInterface;
|
import java.util.Enumeration;
|
|
import javax.servlet.http.Cookie;
|
import javax.servlet.http.HttpServletRequest;
|
|
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.Logger;
|
|
import ua_parser.Client;
|
import ua_parser.Parser;
|
|
|
|
public final class RequestClientUtils {
|
|
private static Logger log = LogManager.getLogger(RequestClientUtils.class);
|
|
private static final String[][] agentArrs = new String[][]{
|
{"micromessenger","Weixin"},
|
{"android","Android"},
|
{"mac os","Mac"},
|
{"windows phone","Windows"}};
|
|
|
/**
|
* »ñÈ¡¿Í»§¶ËipµØÖ·
|
*
|
* @param request
|
* @return
|
*/
|
public static String getRemoteIP(HttpServletRequest request) {
|
|
if (request.getHeader("x-real-ip") == null) {
|
return request.getRemoteAddr();
|
}
|
return request.getHeader("x-real-ip");
|
|
}
|
|
/**
|
* »ñÈ¡·þÎñÆ÷±¾µØIP
|
* @return
|
*/
|
public static String getLocalIP() {
|
InetAddress ip = null;
|
try {
|
Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface.getNetworkInterfaces();
|
while (netInterfaces.hasMoreElements()) {
|
NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
|
Enumeration<InetAddress> ips = ni.getInetAddresses();
|
while (ips.hasMoreElements()) {
|
ip = (InetAddress) ips.nextElement();
|
// 127.¿ªÍ·µÄ¶¼ÊÇlookbackµØÖ·
|
if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {
|
return ip.getHostAddress();
|
}
|
}
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
/**
|
* »ñÈ¡COOKIE : KEY
|
* @param httpRequest
|
* @return
|
*/
|
public static Cookie getKeyCookie(HttpServletRequest httpRequest) {
|
Cookie[] cookies = httpRequest.getCookies();
|
Cookie cookieKey = null;
|
|
if(cookies==null){
|
return null;
|
}
|
|
for(Cookie cookie:cookies){
|
if("KEY".equals(cookie.getName())){
|
cookieKey = cookie;
|
break;
|
}
|
}
|
return cookieKey;
|
}
|
|
/**
|
* ½âÎö¿Í»§¶Ë
|
* @param userAgent userAgent
|
* @return
|
*/
|
public static String parseClient(String userAgent){
|
|
if(userAgent==null){
|
return null;
|
}
|
|
int leftBracketStartIdx = userAgent.indexOf('(');
|
int rightBracketStartIdx = userAgent.indexOf(')');
|
|
|
if(leftBracketStartIdx<0 || rightBracketStartIdx<0){
|
return userAgent;
|
}
|
|
return userAgent.substring(leftBracketStartIdx+1, rightBracketStartIdx);
|
}
|
|
|
/**
|
* ÅжÏÊÇPC¶Ë»¹ÊÇÒÆ¶¯¶Ë
|
*
|
* @return
|
*/
|
public static boolean isMobileDevice(String requestHeader){
|
/**
|
* android : ËùÓÐandroidÉ豸
|
* iphone : iphone
|
* windows phone:NokiaµÈwindowsϵͳµÄÊÖ»ú
|
* ipad £ºipad
|
*/
|
String lowerRequestHeader = requestHeader;
|
String[] deviceArray = new String[]{"android","iphone","windows phone","ipad"};
|
if(lowerRequestHeader == null) {
|
return false;
|
}
|
lowerRequestHeader = lowerRequestHeader.toLowerCase();
|
for(int i=0;i<deviceArray.length;i++){
|
if(lowerRequestHeader.indexOf(deviceArray[i])>0){
|
return true;
|
}
|
}
|
|
return false;
|
}
|
|
/**
|
* ÅжÏÊÇPC¶Ë»¹ÊÇÒÆ¶¯¶Ë
|
*
|
* @return
|
*/
|
public static String getAgentDevice(String requestHeader){
|
|
for(String[] agent:agentArrs){
|
if(requestHeader.toLowerCase().indexOf(agent[0])>0){
|
return agent[1];
|
}
|
}
|
return "δ֪";
|
}
|
|
/**
|
* ÅжÏÊÇ·ñÔÚ΢ÐÅä¯ÀÀÆ÷´ò¿ª
|
*
|
* @return
|
*/
|
public static boolean isWeixin(String userAgent){
|
|
return getAgentDevice(userAgent).equals("Weixin");
|
}
|
|
/**
|
* »ñȡͷÐÅÏ¢
|
*
|
* @param userAgent
|
* @return
|
*/
|
public static Client userAgent(String userAgent){
|
Parser uaParser;
|
try {
|
uaParser = new Parser();
|
return uaParser.parse(userAgent);
|
} catch (IOException e) {
|
log.error("½âÎöÍ·ÐÅϢʧ°Ü", e);
|
}
|
return null;
|
}
|
}
|