-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserAgent.java
More file actions
43 lines (37 loc) · 1.46 KB
/
Copy pathUserAgent.java
File metadata and controls
43 lines (37 loc) · 1.46 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
package com.mastercard.developer.oauth2.http;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
/**
* Utility class to generate a User-Agent string used in HTTP requests.
*/
public final class UserAgent {
private static final String PRODUCT = "Mastercard-OAuth2-Client";
private static final String UNKNOWN_VERSION = "0.0.0-unknown";
private static final String VERSION = readVersionFile();
private UserAgent() {}
/**
* Builds a stable user-agent string:
* Product/Version (Runtime; OS [OS Version])
* Example:
* Mastercard-OAuth2-Client/1.0.0 (Java/17.0.2; Linux 5.15)
*/
public static String get() {
String javaVer = System.getProperty("java.version", "unknown");
String osName = System.getProperty("os.name", "unknown");
String osVer = System.getProperty("os.version", "").trim();
String runtime = "Java/" + javaVer;
String osPart = osName + (osVer.isEmpty() ? "" : " " + osVer);
return String.format("%s/%s (%s; %s)", PRODUCT, VERSION, runtime, osPart);
}
private static String readVersionFile() {
try (InputStream in = UserAgent.class.getResourceAsStream("/VERSION")) {
if (in != null) {
return new String(in.readAllBytes(), StandardCharsets.UTF_8).trim();
}
} catch (IOException e) {
// Should not happen
}
return UNKNOWN_VERSION;
}
}