This commit is contained in:
Patrick
2026-06-05 23:58:33 +02:00
parent 8e5aae7aa2
commit dd54ff1944
2 changed files with 36 additions and 0 deletions
+1
View File
@@ -3,6 +3,7 @@
build
gradle
run
pierre.md
# VERY IMPORTANT
gradle.properties
@@ -74,6 +74,14 @@ public class LicenseClient {
*/
public sealed interface LicenseResult permits LicenseSuccess, LicenseError {}
/**
* Creates an HttpRequest that will be used for sync and async checking
* @param apiUrl Server API url
* @param plugin Plugin id matching the one on the backend api
* @param licenseKey License key issued by the api
* @param serverId Server id matching the one on the backend api
* @return HttpRequest ready to be sent to the backend
*/
private static HttpRequest createRequest(
String apiUrl,
String plugin,
@@ -98,17 +106,34 @@ public class LicenseClient {
.build();
}
/**
* Builds a LicenseError from a Throwable
* @param throwable The Throwable to extract the error message from
* @return LicenseError containing the error message extracted from the Throwable
*/
private static LicenseResult errorResult(Throwable throwable) {
String message = buildErrorMessage(throwable);
return new LicenseError(message);
}
/**
* Extracts a String from a Json Object passed in with a key and a fallback
* @param json The JsonObject to extract the value from
* @param key The key to look for in the JsonObject
* @param def The default value to return if the key is not present or is null in the JsonObject
* @return The String value associated with the key in the JsonObject, or the default value if the key is not present or is null
*/
private static String getString(JsonObject json, String key, String def) {
return json.has(key) && !json.get(key).isJsonNull()
? json.get(key).getAsString()
: def;
}
/**
* Parses the HttpResponse from the license server API and constructs a LicenseResult based on the response status code and body content.
* @param response The HttpResponse received from the license server API after sending the license check request
* @return LicenseResult representing either a successful license check with details or an error if the response indicates a failure or cannot be parsed correctly
*/
private static LicenseResult fromHttpResponse(HttpResponse<String> response) {
if (response.statusCode() != 200) {
return errorResult(new RuntimeException(
@@ -130,6 +155,11 @@ public class LicenseClient {
);
}
/**
* Validates if a url is valid with the http or https protocol
* @param apiUrl The url to validate
* @return URI object if the url is valid
*/
private static URI validateUrl(String apiUrl) {
URI uri = URI.create(apiUrl);
@@ -144,6 +174,11 @@ public class LicenseClient {
return uri;
}
/**
* Helper method to build an error message from a Throwable
* @param t Throwable to get the cause from
* @return A string containing the class name and message of the root cause of the Throwable, or just the class name if the message is null or blank
*/
private static String buildErrorMessage(Throwable t) {
Throwable root = t;