Files
license-lib/src/main/java/de/winniepat/licenselib/LicenseClient.java
T

92 lines
2.9 KiB
Java
Raw Normal View History

2026-06-01 18:05:05 +02:00
package de.winniepat.licenselib;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import java.net.URI;
2026-06-01 18:05:05 +02:00
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
2026-06-01 18:05:05 +02:00
/**
* A client for checking plugin licenses against a license server.
*/
public class LicenseClient {
private static final Gson GSON = new Gson();
private static final HttpClient HTTP_CLIENT = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(10))
.build();
2026-06-01 18:05:05 +02:00
/**
* Private constructor to prevent instantiation of this utility class.
*/
private LicenseClient() {
throw new UnsupportedOperationException("Utility class");
}
/**
* Checks the validity of a license key for a given plugin and server ID against the license server API.
* @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 LicenseResult with the data from the backend
*/
public static LicenseResult check(
String apiUrl,
String plugin,
String licenseKey,
String serverId // can be null
2026-06-01 18:05:05 +02:00
) {
try {
JsonObject payload = new JsonObject();
payload.addProperty("plugin", plugin);
payload.addProperty("licenseKey", licenseKey);
2026-06-01 18:05:05 +02:00
if (serverId != null) {
payload.addProperty("serverId", serverId);
}
2026-06-01 18:05:05 +02:00
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(apiUrl))
.timeout(Duration.ofSeconds(10))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(GSON.toJson(payload)))
.build();
2026-06-01 18:05:05 +02:00
HttpResponse<String> response = HTTP_CLIENT.send(
request,
HttpResponse.BodyHandlers.ofString()
);
2026-06-01 18:06:16 +02:00
JsonObject json = GSON.fromJson(response.body(), JsonObject.class);
2026-06-01 18:06:16 +02:00
return new LicenseResult(
json.get("valid").getAsBoolean(),
json.get("status").getAsString(),
json.get("message").getAsString()
);
} catch (Exception e) {
return new LicenseResult(
false,
"error",
e.getMessage()
);
}
2026-06-01 18:06:16 +02:00
}
2026-06-01 18:05:05 +02:00
/**
* Represents the result of a license check.
* @param isValid boolean if the license is valid or not
* @param status license status
* @param message additional message from the backend
*/
public record LicenseResult(
boolean isValid,
String status,
String message
) { }
}