102 lines
3.1 KiB
Java
102 lines
3.1 KiB
Java
package com.example.ua.analyser.impl;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
|
|
import com.example.ua.analyser.enums.RegexFeatureEnum;
|
|
import com.example.ua.analyser.enums.UserInfo;
|
|
import com.example.ua.dao.QueryMariaDb;
|
|
import com.example.ua.dao.impl.QueryMariaDbImpl;
|
|
import org.mariadb.jdbc.MariaDbStatement;
|
|
|
|
/**
|
|
* android类设备分析
|
|
* @author yjy
|
|
* @version 1.0
|
|
* @date 2020/11/15 12:20 下午
|
|
*/
|
|
public class AndroidAnalyser {
|
|
private UserInfo userInfo;
|
|
private MariaDbStatement connStatement;
|
|
private QueryMariaDb queryMariaDb = new QueryMariaDbImpl();
|
|
|
|
public AndroidAnalyser(MariaDbStatement connStatement, UserInfo userInfo){
|
|
this.userInfo = userInfo;
|
|
this.connStatement = connStatement;
|
|
}
|
|
|
|
public UserInfo getTriples(){
|
|
List<String> tripleInDatabase;
|
|
List<String> tripleInRegex;
|
|
List<List<String>> tripleList = this.userInfo.getTerminalList();
|
|
|
|
//筛选androidUA
|
|
List<String> uaList = selectAndroidUa(this.userInfo.getUaList());
|
|
if (uaList.size()==0){
|
|
return null;
|
|
}
|
|
|
|
for (String ua : uaList) {
|
|
tripleInDatabase = queryMariaDb.getTriple(this.connStatement, ua);
|
|
if (tripleInDatabase.size() > 0) {
|
|
if (!(tripleList.contains(tripleInDatabase))){
|
|
tripleList.add(tripleInDatabase);
|
|
}
|
|
} else { //不进行重复解析
|
|
tripleInRegex = parseAndroidUa(ua);
|
|
if ((tripleInRegex != null) && !(tripleList.contains(tripleInRegex))){
|
|
tripleList.add(tripleInRegex);
|
|
}
|
|
}
|
|
}
|
|
this.userInfo.setTerminalList(tripleList);
|
|
this.userInfo.update();
|
|
|
|
return this.userInfo;
|
|
}
|
|
|
|
private List<String> selectAndroidUa(List<String> uaList){
|
|
List<String>selectedUaList = new ArrayList<>();
|
|
|
|
Pattern android = Pattern.compile(RegexFeatureEnum.TERMINAL_CAT_ANDROID_FEATURE.getCode());
|
|
|
|
for (String ua : uaList) {
|
|
if (android.matcher(ua).find()){
|
|
selectedUaList.add(ua);
|
|
}
|
|
}
|
|
return selectedUaList;
|
|
}
|
|
|
|
private List<String> parseAndroidUa(String ua){
|
|
List<String> androidTerminal = Arrays.asList(new String[3]);
|
|
String dePattern = "; [a-zA-Z0-9\\s]+/";
|
|
String versionPattern = "; U; [a-zA-Z0-9\\s\\.]+;";
|
|
Pattern de = Pattern.compile(dePattern);
|
|
Pattern version = Pattern.compile(versionPattern);
|
|
Matcher matcher1 = de.matcher(ua);
|
|
Matcher matcher2 = version.matcher(ua);
|
|
|
|
if (matcher1.find() && matcher2.find()) {
|
|
// os
|
|
String tmp = matcher2.group(0);
|
|
androidTerminal.set(0, tmp.substring(5, tmp.length()-1));
|
|
// TODO browser
|
|
androidTerminal.set(1, null);
|
|
// device
|
|
tmp = matcher1.group(0);
|
|
androidTerminal.set(2, tmp.substring(2, tmp.length() - 1));
|
|
} else {
|
|
return null;
|
|
}
|
|
return androidTerminal;
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|