forked from getsentry/sentry-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.java
More file actions
240 lines (213 loc) · 6.03 KB
/
User.java
File metadata and controls
240 lines (213 loc) · 6.03 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package io.sentry.protocol;
import io.sentry.ILogger;
import io.sentry.JsonDeserializer;
import io.sentry.JsonObjectReader;
import io.sentry.JsonObjectWriter;
import io.sentry.JsonSerializable;
import io.sentry.JsonUnknown;
import io.sentry.util.CollectionUtils;
import io.sentry.vendor.gson.stream.JsonToken;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Information about the user who triggered an event.
*
* <p>```json { "user": { "id": "unique_id", "username": "my_user", "email": "foo@example.com",
* "ip_address": "127.0.0.1", "subscription": "basic" } } ```
*/
public final class User implements JsonUnknown, JsonSerializable {
/** Email address of the user. */
private @Nullable String email;
/** Unique identifier of the user. */
private @Nullable String id;
/** Username of the user. */
private @Nullable String username;
/** Remote IP address of the user. */
private @Nullable String ipAddress;
/**
* Additional arbitrary fields, as stored in the database (and sometimes as sent by clients). All
* data from `self.other` should end up here after store normalization.
*/
private @Nullable Map<String, @NotNull String> other;
/** unknown fields, only internal usage. */
private @Nullable Map<String, @NotNull Object> unknown;
public User() {}
public User(final @NotNull User user) {
this.email = user.email;
this.username = user.username;
this.id = user.id;
this.ipAddress = user.ipAddress;
this.other = CollectionUtils.newConcurrentHashMap(user.other);
this.unknown = CollectionUtils.newConcurrentHashMap(user.unknown);
}
/**
* Gets the e-mail address of the user.
*
* @return the e-mail.
*/
public @Nullable String getEmail() {
return email;
}
/**
* Gets the e-mail address of the user.
*
* @param email the e-mail.
*/
public void setEmail(final @Nullable String email) {
this.email = email;
}
/**
* Gets the id of the user.
*
* @return the id.
*/
public @Nullable String getId() {
return id;
}
/**
* Sets the id of the user.
*
* @param id the user id.
*/
public void setId(final @Nullable String id) {
this.id = id;
}
/**
* Gets the username of the user.
*
* @return the username.
*/
public @Nullable String getUsername() {
return username;
}
/**
* Sets the username of the user.
*
* @param username the username.
*/
public void setUsername(final @Nullable String username) {
this.username = username;
}
/**
* Gets the IP address of the user.
*
* @return the IP address of the user.
*/
public @Nullable String getIpAddress() {
return ipAddress;
}
/**
* Sets the IP address of the user.
*
* @param ipAddress the IP address of the user.
*/
public void setIpAddress(final @Nullable String ipAddress) {
this.ipAddress = ipAddress;
}
/**
* Gets other user related data.
*
* @return the other user data.
*/
public @Nullable Map<String, @NotNull String> getOthers() {
return other;
}
/**
* Sets other user related data.
*
* @param other the other user related data..
*/
public void setOthers(final @Nullable Map<String, @NotNull String> other) {
this.other = CollectionUtils.newConcurrentHashMap(other);
}
// region json
@Nullable
@Override
public Map<String, Object> getUnknown() {
return unknown;
}
@Override
public void setUnknown(@Nullable Map<String, Object> unknown) {
this.unknown = unknown;
}
public static final class JsonKeys {
public static final String EMAIL = "email";
public static final String ID = "id";
public static final String USERNAME = "username";
public static final String IP_ADDRESS = "ip_address";
public static final String OTHER = "other";
}
@Override
public void serialize(@NotNull JsonObjectWriter writer, @NotNull ILogger logger)
throws IOException {
writer.beginObject();
if (email != null) {
writer.name(JsonKeys.EMAIL).value(email);
}
if (id != null) {
writer.name(JsonKeys.ID).value(id);
}
if (username != null) {
writer.name(JsonKeys.USERNAME).value(username);
}
if (ipAddress != null) {
writer.name(JsonKeys.IP_ADDRESS).value(ipAddress);
}
if (other != null) {
writer.name(JsonKeys.OTHER).value(logger, other);
}
if (unknown != null) {
for (String key : unknown.keySet()) {
Object value = unknown.get(key);
writer.name(key);
writer.value(logger, value);
}
}
writer.endObject();
}
public static final class Deserializer implements JsonDeserializer<User> {
@SuppressWarnings("unchecked")
@Override
public @NotNull User deserialize(@NotNull JsonObjectReader reader, @NotNull ILogger logger)
throws Exception {
reader.beginObject();
User user = new User();
Map<String, Object> unknown = null;
while (reader.peek() == JsonToken.NAME) {
final String nextName = reader.nextName();
switch (nextName) {
case JsonKeys.EMAIL:
user.email = reader.nextStringOrNull();
break;
case JsonKeys.ID:
user.id = reader.nextStringOrNull();
break;
case JsonKeys.USERNAME:
user.username = reader.nextStringOrNull();
break;
case JsonKeys.IP_ADDRESS:
user.ipAddress = reader.nextStringOrNull();
break;
case JsonKeys.OTHER:
user.other =
CollectionUtils.newConcurrentHashMap(
(Map<String, String>) reader.nextObjectOrNull());
break;
default:
if (unknown == null) {
unknown = new ConcurrentHashMap<>();
}
reader.nextUnknown(logger, unknown, nextName);
break;
}
}
user.setUnknown(unknown);
reader.endObject();
return user;
}
}
// endregion
}