shikeying
2024-04-03 b77abcbc0f17070a2a970e0c4aa5837e90f28e1f
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
package com.walker.web.agent;
 
import com.walker.web.WebAgentService;
import com.walker.web.WebUserAgent;
import com.walker.web.util.IpUtils;
import org.lionsoul.ip2region.xdb.Searcher;
 
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
 
/**
 * 使用开源组件<code>BrowserCap</code> 实现的解析浏览器代理服务对象。
 * @date 2023-01-05
 */
public class BrowserCapWebAgentService implements WebAgentService {
 
    private Capabilities capabilities = null;
    private UserAgentParser parser = null;
 
    // 搜索地区的本地对象(读取本地xdb数据库),2024-04-02
    private Searcher searcher = null;
 
    public BrowserCapWebAgentService(){
        try {
            this.parser =
                    new UserAgentService().loadParser(Arrays.asList(BrowsCapField.BROWSER, BrowsCapField.BROWSER_TYPE,
                            BrowsCapField.BROWSER_MAJOR_VERSION,
//                            BrowsCapField.DEVICE_TYPE,
                            BrowsCapField.PLATFORM, BrowsCapField.PLATFORM_VERSION
//                            BrowsCapField.RENDERING_ENGINE_VERSION, BrowsCapField.RENDERING_ENGINE_NAME,
//                            BrowsCapField.PLATFORM_MAKER, BrowsCapField.RENDERING_ENGINE_MAKER
                    ));
 
        } catch (Exception e) {
            throw new RuntimeException("创建浏览器解析对象错误:" + e.getMessage(), e);
        }
 
//        this.searcher = Searcher.newWithFileOnly();
//        ClassPathResource resource = new ClassPathResource();
//        resource.getFile();
//        RandomAccessFile randomAccessFile = new RandomAccessFile();
    }
 
    @Override
    public WebUserAgent getWebUserAgent(String userAgentText, HttpServletRequest request) {
        WebUserAgent webUserAgent = new WebUserAgent();
        if(this.parser == null){
            return webUserAgent;
        }
        String ip = null;
        String location = "none";
        if(request != null){
            ip = IpUtils.getIpAddr(request);
            if(this.loadLocation){
                location = IpUtils.getLocationByIP(ip);
            }
        }
        final Capabilities capabilities = parser.parse(userAgentText);
        webUserAgent.setBrowserName(capabilities.getBrowser());
        webUserAgent.setBrowserVersion(capabilities.getBrowserMajorVersion());
        webUserAgent.setDeviceType(capabilities.getDeviceType());
        webUserAgent.setOsName(capabilities.getPlatform());
        webUserAgent.setOsVersion(capabilities.getPlatformVersion());
        webUserAgent.setIp(ip);
        webUserAgent.setLocation(location);
        webUserAgent.setMethod(request.getMethod());
        webUserAgent.setUrl(request.getRequestURI());
        return webUserAgent;
    }
 
 
    /**
     * 设置是否加载ip对应的区域地质,默认:false不加载,因为需要连外网查找。
     * @param loadLocation
     * @date 2023-12-11
     */
    public void setLoadLocation(boolean loadLocation) {
        this.loadLocation = loadLocation;
    }
 
    /**
     * 设置:ip2region数据库文件路径,如:
     * @param xdbPath
     */
    public void setXdbPath(String xdbPath) {
        this.xdbPath = xdbPath;
    }
 
    private String xdbPath;
    private boolean loadLocation = false;
}