UserAgent 정보 도구 클래스 가 져 오기

8428 단어 SpringJAVA
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cz.mallat.uasparser.OnlineUpdater;
import cz.mallat.uasparser.UASparser;
import cz.mallat.uasparser.UserAgentInfo;

/**
 *	UserAgent        ,jdk1.7         
 */
public class UserAgentUtil {
	private static final Logger LOG = LoggerFactory.getLogger(UserAgentUtil.class);
	private static UASparser uasParser = null;  
    public static String android="Android";    
    public static String iphone="iPhone";    
    public static String ipad="iPad";  
    public static final String noDevice="    ";  
    
    // \b      (     (            )          ),    
   //              ,    "\\b"    
   // \B          (                  )    
   private static String phoneReg = "\\b(ip(hone|od)|android|opera m(ob|in)i"    
           +"|windows (phone|ce)|blackberry"    
           +"|s(ymbian|eries60|amsung)|p(laybook|alm|rofile/midp"    
           +"|laystation portable)|nokia|fennec|htc[-_]"    
           +"|mobile|up.browser|[1-4][0-9]{2}x[1-4][0-9]{2})\\b";    
   private static String tableReg = "\\b(ipad|tablet|(Nexus 7)|up.browser"    
           +"|[1-4][0-9]{2}x[1-4][0-9]{2})\\b";    
      
   //        :   、    
   private static Pattern phonePat = Pattern.compile(phoneReg, Pattern.CASE_INSENSITIVE);    
   private static Pattern tablePat = Pattern.compile(tableReg, Pattern.CASE_INSENSITIVE);  
    
    static {  
        try {  
            uasParser = new UASparser(OnlineUpdater.getVendoredInputStream());  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
	
    /** 
     *             
     *  
     * @Title: check 
     * @param userAgent       
     * @return true:      ,false:pc    
     */  
    public static boolean checkMobileOrPC(HttpServletRequest request){   
    	String userAgent=request.getHeader("user-agent").toLowerCase();
    	System.out.println("==========================      ========================================");
        System.out.println("    :"+userAgent);
        //       
        Matcher matcherPhone = phonePat.matcher(userAgent);    
        Matcher matcherTable = tablePat.matcher(userAgent); 
        boolean result=false;
        if(matcherPhone.find() || matcherTable.find()){    
        	result = true;    
        }
        System.out.println("===========================      =======================================");
        return result;
    }  
    
    /** 
     *             
     *  
     * @Title: check 
     * @param userAgent       
     * @return true:      ,false:pc    
     */  
    public static boolean checkMobileOrPC(String userAgent){   
        System.out.println("    :"+userAgent);
        //       
        Matcher matcherPhone = phonePat.matcher(userAgent);    
        Matcher matcherTable = tablePat.matcher(userAgent);    
        if(matcherPhone.find() || matcherTable.find()){    
            return true;    
        } else {    
            return false;    
        }    
    }  
    
	/**
	 *          Smartphone/Personal computer
	 * @param userAgent
	 * @return
	 * @throws IOException 
	 */
	public static String getDeviceType(HttpServletRequest request) throws IOException{
		String userAgent=request.getHeader("user-agent").toLowerCase();
		UserAgentInfo userAgentInfo = UserAgentUtil.uasParser.parse(userAgent);
		LOG.debug("    :" + userAgentInfo.getDeviceType());  
		return userAgentInfo.getDeviceType();
	}	
    
	/**
	 *           Android/Windows/iOS
	 * @param userAgent
	 * @return
	 * @throws IOException 
	 */
	public static String getOsName(HttpServletRequest request) throws IOException{
		String userAgent=request.getHeader("user-agent").toLowerCase();
		UserAgentInfo userAgentInfo = UserAgentUtil.uasParser.parse(userAgent);
		LOG.debug("        :" + userAgentInfo.getOsName());  
		return userAgentInfo.getOsName();
	}	
	
	/**
	 *               Chrome Mobile 53.0.2785.146/Chrome 63.0.3239.84/Android browser 4.0
	 * @param userAgent
	 * @return
	 * @throws IOException 
	 */
	public static String getUaName(HttpServletRequest request) throws IOException{
		String userAgent=request.getHeader("user-agent").toLowerCase();
		UserAgentInfo userAgentInfo = UserAgentUtil.uasParser.parse(userAgent);
		LOG.debug("        :" + userAgentInfo.getUaName());  
		return userAgentInfo.getUaName();
	}		
     
	/**
	 *            
	 * @param request
	 * @return
	 */
	public static boolean isWeChatBrowser(HttpServletRequest request){
		String userAgent=request.getHeader("user-agent").toLowerCase();
		return userAgent.toLowerCase().indexOf("micromessenger")>-1?true:false;
	}
	
	/**
	 *            ios ipad       
	 * @param request
	 * @return
	 */
	public static String getMobileModel(HttpServletRequest request){
		String userAgent=request.getHeader("user-agent").toLowerCase();
		//        **************************************************************************
		//   Android    
		Pattern pattern = Pattern.compile(";\\s?(\\S*?\\s?\\S*?)\\s?(Build)?/");
		Matcher matcher = pattern.matcher(userAgent);
		String mobileModel = null;
		if (matcher.find()) {
			mobileModel = matcher.group(1).trim();
		}

		//   iphone ipad
		if (userAgent.indexOf("iphone") > 0) {
			mobileModel = "iphone";
		}
		if (userAgent.indexOf("ipad") > 0) {
			mobileModel = "ipad";
		}
		//        **************************************************************************
		return mobileModel;
	}
	
	/**
	 *             Mobile Browser/Browser
	 * @param userAgent
	 * @return
	 * @throws IOException 
	 */
	public static String getBrowerType(HttpServletRequest request) throws IOException{
		String userAgent=request.getHeader("user-agent").toLowerCase();
		UserAgentInfo userAgentInfo = UserAgentUtil.uasParser.parse(userAgent);
		LOG.debug("  :" + userAgentInfo.getType());  
		return userAgentInfo.getType();
	}	    
	
	/**
	 *        Chrome Mobile 53.0.2785.146/Chrome 63.0.3239.84
	 * @param userAgent
	 * @return
	 * @throws IOException 
	 */
	public static String getBrowserVersionInfo(HttpServletRequest request) throws IOException{
		String userAgent=request.getHeader("user-agent").toLowerCase();
		UserAgentInfo userAgentInfo = UserAgentUtil.uasParser.parse(userAgent);
		LOG.debug("     :" + userAgentInfo.getBrowserVersionInfo());  
		return userAgentInfo.getBrowserVersionInfo();
	}		
	
	/**
	 *                
	 * @param userAgent
	 * @return
	 */
    public static String getMobileOS(HttpServletRequest request){    
    	String userAgent=request.getHeader("user-agent").toLowerCase();
        if (userAgent.contains(android)) {    
            return android;    
        }else if (userAgent.contains(iphone)){    
            return iphone;    
        }else if (userAgent.contains(ipad)){    
            return ipad;    
        }else {    
            return "others";  
        }    
    }    

	/**
	 *       
	 * @param userAgent
	 * @return
	 * @throws IOException 
	 */
	public static String getOsFamily(HttpServletRequest request) throws IOException{
		String userAgent=request.getHeader("user-agent").toLowerCase();
		UserAgentInfo userAgentInfo = UserAgentUtil.uasParser.parse(userAgent);
		LOG.debug("      :" + userAgentInfo.getOsFamily());  
		return userAgentInfo.getOsFamily();
	}
	
	/**
	 *      
	 * @param userAgent
	 * @return
	 * @throws IOException 
	 */
	public static String getUaFamily(HttpServletRequest request) throws IOException{
		String userAgent=request.getHeader("user-agent").toLowerCase();
		UserAgentInfo userAgentInfo = UserAgentUtil.uasParser.parse(userAgent);
		LOG.debug("     :" + userAgentInfo.getUaFamily());  
		return userAgentInfo.getUaFamily();
	}	
	
}

필요 maven:

	cz.mallat.uasparser
	uasparser
	0.6.2


	net.sourceforge.jregex
	jregex
	1.2_01

좋은 웹페이지 즐겨찾기