forked from auth0/auth0-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTelemetry.java
More file actions
107 lines (89 loc) · 2.91 KB
/
Telemetry.java
File metadata and controls
107 lines (89 loc) · 2.91 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
package com.auth0.net;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.codec.binary.Base64;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* Representation of the client information sent by this SDK on every request.
* <p>
* This class is thread-safe.
*
* @see TelemetryInterceptor
*/
@SuppressWarnings("WeakerAccess")
public class Telemetry {
static final String HEADER_NAME = "Auth0-Client";
private static final String JAVA_SPECIFICATION_VERSION = "java.specification.version";
private static final String NAME_KEY = "name";
private static final String VERSION_KEY = "version";
private static final String LIBRARY_VERSION_KEY = "auth0-java";
private static final String ENV_KEY = "env";
private static final String JAVA_KEY = "java";
private final String name;
private final String version;
private final String libraryVersion;
private final Map<String, String> env;
private final String value;
public Telemetry(String name, String version) {
this(name, version, null);
}
public Telemetry(String name, String version, String libraryVersion) {
this.name = name;
this.version = version;
this.libraryVersion = libraryVersion;
if (name == null) {
env = Collections.emptyMap();
value = null;
return;
}
Map<String, Object> values = new HashMap<>();
values.put(NAME_KEY, name);
if (version != null) {
values.put(VERSION_KEY, version);
}
HashMap<String, String> tmpEnv = new HashMap<>();
tmpEnv.put(JAVA_KEY, getJDKVersion());
if (libraryVersion != null) {
tmpEnv.put(LIBRARY_VERSION_KEY, libraryVersion);
}
this.env = Collections.unmodifiableMap(tmpEnv);
values.put(ENV_KEY, env);
String tmpValue;
try {
String json = new ObjectMapper().writeValueAsString(values);
tmpValue = Base64.encodeBase64URLSafeString(json.getBytes());
} catch (JsonProcessingException e) {
tmpValue = null;
e.printStackTrace();
}
value = tmpValue;
}
public String getName() {
return name;
}
public String getVersion() {
return version;
}
//Visible for testing
String getLibraryVersion() {
return libraryVersion;
}
//Visible for testing
Map<String, String> getEnvironment() {
return env;
}
public String getValue() {
return value;
}
private String getJDKVersion() {
String version;
try {
version = System.getProperty(JAVA_SPECIFICATION_VERSION);
} catch (Exception ignored) {
version = Runtime.class.getPackage().getSpecificationVersion();
}
return version;
}
}