summaryrefslogtreecommitdiff
path: root/src/main/java/com/mesalab/ua/analyser/impl/AppleAnalyser.java
blob: 7e1e62fc5c5c4c1bdcce40bef7f1bd5a131ed1da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package com.mesalab.ua.analyser.impl;

import com.mesalab.ua.analyser.enums.UserInfo;
import com.mesalab.ua.dao.QueryMariaDb;
import com.mesalab.ua.dao.impl.QueryMariaDbImpl;
import org.mariadb.jdbc.MariaDbStatement;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * apple类设备分析
 * @author yjy
 * @version 1.0
 * @date 2020/11/15 12:20 下午
 */
public class AppleAnalyser {
    private UserInfo userInfo;
    private MariaDbStatement connStatement;
    private QueryMariaDb queryMariaDb = new QueryMariaDbImpl();

    public AppleAnalyser(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();

        //筛选Apple Ua
        List<String> uaList = selectAppleUa(this.userInfo.getUaList());
        if (uaList.size()==0){
            return this.userInfo;
        }

        for (String ua : uaList) {
            tripleInDatabase = queryMariaDb.getTriple(this.connStatement, ua);
            if ((tripleInDatabase.size() > 0)) {
                if (!(tripleList.contains(tripleInDatabase))) {
                    tripleList.add(tripleInDatabase);
                }
            } else { //不进行重复解析
                tripleInRegex = parseAppleUa(ua);
                if ((tripleInRegex != null) && !(tripleList.contains(tripleInRegex))) {
                    tripleList.add(tripleInRegex);
                }
            }
        }
        this.userInfo.setTerminalList(tripleList);
        this.userInfo.update();

        return this.userInfo;
    }

    public List<String> parseAppleUa(String ua) {
        List<String> appleTerminal = Arrays.asList(new String[3]);

        String macPattern = "Mozilla.*Mac OS";
        String ipadPattern = "server-bag .*iPad";
        String iphoneString = "server-bag .*iPhone";
        Pattern mac = Pattern.compile(macPattern);
        Pattern ipad = Pattern.compile(ipadPattern);
        Pattern iphone1 = Pattern.compile(ipadPattern);

        if (mac.matcher(ua).find()) {
            Pattern mozilla = Pattern.compile("\\(.*;");
            Pattern version = Pattern.compile(";.*\\)");
            Pattern macos = Pattern.compile("Mac OS X\\s.*\\)\\s");
            Matcher matcher1 = mozilla.matcher(ua);
            Matcher matcher2 = version.matcher(ua);

            if (matcher1.find() && matcher2.find()) {
                String tmp = matcher2.group(0);
                Matcher matcher3 = macos.matcher(tmp);
                if(matcher3.find()){
                    String macversion = Arrays.asList(matcher3.group(0).split("\\)")).get(0).trim().replace("_",".");
                    appleTerminal.set(0, macversion);
                    appleTerminal.set(2, "Apple Mac");
                }
            }
        } else if (ipad.matcher(ua).find()) {
            Pattern iPad = Pattern.compile("iPad.*]");
            Matcher matcher = iPad.matcher(ua);
            if (matcher.find()) {
                String tmp = matcher.group();
                appleTerminal.set(0, "iOS " + tmp.replace(',', '.').substring(4, tmp.length() - 1));
                appleTerminal.set(2, "Apple iPad");
            }
        }
        else if (iphone1.matcher(ua).find()) {
            appleTerminal.set(0, "iOS");
            appleTerminal.set(2, "Apple iPhone");
        }

        if (Collections.frequency(appleTerminal, null) == 3) {
            return null;
        }
        return appleTerminal;
    }

    private List<String> selectAppleUa(List<String> uaList){
        List<String>selectedUaList = new ArrayList<>();

        Pattern pattern1 = Pattern.compile("Mozilla.*Mac OS X\\s");
        Pattern pattern2 = Pattern.compile("server-bag .*iPad");
        Pattern pattern3 = Pattern.compile("server-bag .*iPhone");

        for (int i = 0; i < uaList.size(); i++) {
            String ua = uaList.get(i);
            if (pattern1.matcher(ua).find() || pattern2.matcher(ua).find() || pattern3.matcher(ua).find()) {
                selectedUaList.add(ua);
            }
        }
        return selectedUaList;
    }






}