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

136 lines
4.8 KiB
Java
Raw Normal View History

2026-06-01 20:34:01 +02:00
/*
* Copyright (c) 2026 WinniePatGG
*
* Licensed under the MIT License
*/
2026-06-01 18:05:05 +02:00
package de.winniepat.licenselib;
2026-06-01 20:34:01 +02:00
import com.google.gson.*;
2026-06-01 18:05:05 +02:00
import java.net.URI;
2026-06-01 20:34:01 +02:00
import java.net.http.*;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;
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();
2026-06-01 20:34:01 +02:00
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
*/
2026-06-01 20:34:01 +02:00
public static LicenseResult check(String apiUrl, String plugin, String licenseKey, String serverId) {
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
}
/**
* Asynchronously 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 CompletabaleFuture with the LicenseResult from the backend
*/
2026-06-01 20:34:01 +02:00
public static CompletableFuture<LicenseResult> checkAsync(String apiUrl, String plugin, String licenseKey, String serverId) {
JsonObject payload = new JsonObject();
payload.addProperty("plugin", plugin);
payload.addProperty("licenseKey", licenseKey);
if (serverId != null) {
payload.addProperty("serverId", serverId);
}
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();
return HTTP_CLIENT
.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(response -> parseResponse(response.body()))
.exceptionally(ex -> new LicenseResult(
false,
"error",
ex.getMessage()
));
}
2026-06-01 20:34:01 +02:00
/**
* Parses the JSON response from the license server API into a LicenseResult object.
* @param body the JSON response body from the license server API
* @return a LicenseResult object containing the validity, status, and message from the API response
*/
private static LicenseResult parseResponse(String body) {
JsonObject json = GSON.fromJson(body, JsonObject.class);
return new LicenseResult(
json.get("valid").getAsBoolean(),
json.get("status").getAsString(),
json.get("message").getAsString()
);
}
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
*/
2026-06-01 20:34:01 +02:00
public record LicenseResult(boolean isValid, String status, String message) { }
2026-06-01 18:05:05 +02:00
}