-
-
Notifications
You must be signed in to change notification settings - Fork 467
Expand file tree
/
Copy pathDsn.java
More file actions
94 lines (85 loc) · 2.7 KB
/
Dsn.java
File metadata and controls
94 lines (85 loc) · 2.7 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
package io.sentry;
import io.sentry.util.Objects;
import java.net.URI;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
final class Dsn {
private final @NotNull String projectId;
private final @Nullable String path;
private final @Nullable String secretKey;
private final @NotNull String publicKey;
private final @NotNull URI sentryUri;
/*
/ The project ID which the authenticated user is bound to.
*/
public @NotNull String getProjectId() {
return projectId;
}
/*
/ An optional path of which Sentry is hosted
*/
public @Nullable String getPath() {
return path;
}
/*
/ The optional secret key to authenticate the SDK.
*/
public @Nullable String getSecretKey() {
return secretKey;
}
/*
/ The required public key to authenticate the SDK.
*/
public @NotNull String getPublicKey() {
return publicKey;
}
/*
/ The URI used to communicate with Sentry
*/
@NotNull
URI getSentryUri() {
return sentryUri;
}
Dsn(@Nullable String dsn) throws IllegalArgumentException {
try {
final String dsnString = Objects.requireNonNull(dsn, "The DSN is required.").trim();
if (dsnString.isEmpty()) {
throw new IllegalArgumentException("The DSN is empty.");
}
final URI uri = new URI(dsnString).normalize();
final String scheme = uri.getScheme();
if (!("http".equalsIgnoreCase(scheme) || "https".equalsIgnoreCase(scheme))) {
throw new IllegalArgumentException("Invalid DSN scheme: " + scheme);
}
String userInfo = uri.getUserInfo();
if (userInfo == null || userInfo.isEmpty()) {
throw new IllegalArgumentException("Invalid DSN: No public key provided.");
}
String[] keys = userInfo.split(":", -1);
publicKey = keys[0];
if (publicKey == null || publicKey.isEmpty()) {
throw new IllegalArgumentException("Invalid DSN: No public key provided.");
}
secretKey = keys.length > 1 ? keys[1] : null;
String uriPath = uri.getPath();
if (uriPath.endsWith("/")) {
uriPath = uriPath.substring(0, uriPath.length() - 1);
}
int projectIdStart = uriPath.lastIndexOf("/") + 1;
String path = uriPath.substring(0, projectIdStart);
if (!path.endsWith("/")) {
path += "/";
}
this.path = path;
projectId = uriPath.substring(projectIdStart);
if (projectId.isEmpty()) {
throw new IllegalArgumentException("Invalid DSN: A Project Id is required.");
}
sentryUri =
new URI(
scheme, null, uri.getHost(), uri.getPort(), path + "api/" + projectId, null, null);
} catch (Throwable e) {
throw new IllegalArgumentException(e);
}
}
}