-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathUserAgent.java
143 lines (125 loc) · 3.83 KB
/
UserAgent.java
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package io.ipgeolocation.api;
import java.util.Objects;
import org.json.JSONObject;
/**
* Represents information about a user agent.
* <p>It contains information about the user agent string, the name, type, version, version major,
* device, engine, and operating system.
*
* <p>The UserAgent class provides methods to retrieve information about a user agent.
* For example, you can use the {@link #getOperatingSystem()} method to retrieve the operating system of the user
*/
public class UserAgent {
private final String userAgentString;
private final String name;
private final String type;
private final String version;
private final String versionMajor;
public UserAgentDevice device;
private UserAgentEngine engine;
private UserAgentOperatingSystem operatingSystem;
private final JSONObject json;
/**
* Constructs a new UserAgent object with the provided JSON data.
*
* @param json The JSON object containing user agent information.
* @throws IllegalArgumentException If the provided JSON object is null or empty.
*/
UserAgent(JSONObject json) {
if (Objects.isNull(json)) {
throw new IllegalArgumentException("'json' must not be null");
}
if (json.isEmpty()) {
throw new IllegalArgumentException("'json' must not be empty");
}
this.userAgentString = json.getString("userAgentString");
this.name = json.getString("name");
this.type = json.getString("type");
this.version = json.getString("version");
this.versionMajor = json.getString("versionMajor");
if (json.has("device")) {
this.device = new UserAgentDevice(json.getJSONObject("device"));
}
if (json.has("engine")) {
this.engine = new UserAgentEngine(json.getJSONObject("engine"));
}
if (json.has("operatingSystem")) {
this.operatingSystem = new UserAgentOperatingSystem(json.getJSONObject("operatingSystem"));
}
this.json = json;
}
/**
* Returns the user agent string passed along with the query.
*
* @return The user agent string.
*/
public String getUserAgentString() {
return userAgentString;
}
/**
* Returns the name of the user agent.
*
* @return The name of the user agent.
*/
public String getName() {
return name;
}
/**
* Returns the class of the user agent.
*
* @return The class of the user agent.
*/
public String getType() {
return type;
}
/**
* Returns the version of the user agent.
*
* @return The version of the user agent.
*/
public String getVersion() {
return version;
}
/**
* Returns the major version of the user agent.
*
* @return The major version of the user agent.
*/
public String getVersionMajor() {
return versionMajor;
}
/**
* Returns information about the device associated with the user agent.
*
* @return Information about the device as {@link UserAgentDevice} object, or null if no device associated with the user agent.
*/
public UserAgentDevice getDevice() {
return device;
}
/**
* Returns information about the engine associated with the user agent.
*
* @return Information about the engine as {@link UserAgentEngine} object, or null if no engine associated with the user agent.
*/
public UserAgentEngine getEngine() {
return engine;
}
/**
* Returns information about the operating system associated with the user agent.
*
* @return Information about the operating system as {@link UserAgentOperatingSystem} object, or null if no
* operating system associated with the user agent.
*/
public UserAgentOperatingSystem getOperatingSystem() {
return operatingSystem;
}
/**
* Returns a JSON representation of the user agent data.
*
* @return The JSON representation of the user agent data as a string.
*/
@Override
public String toString() {
return json.toString(2);
}
}