0.0.4 | Removed dependency on winnie-lib for being more lightweight ;)
This commit is contained in:
+2
-5
@@ -4,7 +4,7 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
group = 'de.winniepat'
|
group = 'de.winniepat'
|
||||||
version = '0.0.3'
|
version = '0.0.4'
|
||||||
|
|
||||||
|
|
||||||
java {
|
java {
|
||||||
@@ -17,7 +17,7 @@ java {
|
|||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
maven { url = "https://maven.winniepat.de/repository/maven-releases/" }
|
maven {url = "https://repo.papermc.io/repository/maven-public/" }
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
@@ -28,9 +28,6 @@ dependencies {
|
|||||||
testImplementation 'org.junit.jupiter:junit-jupiter'
|
testImplementation 'org.junit.jupiter:junit-jupiter'
|
||||||
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
|
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
|
||||||
implementation 'com.google.code.gson:gson:2.13.1'
|
implementation 'com.google.code.gson:gson:2.13.1'
|
||||||
|
|
||||||
|
|
||||||
implementation("de.winniepat:winnie-lib:+")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks.named('test') {
|
tasks.named('test') {
|
||||||
|
|||||||
@@ -2,10 +2,12 @@ package de.winniepat.licenselib;
|
|||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import de.winniepat.winnielib.http.Http;
|
|
||||||
import de.winniepat.winnielib.http.HttpResponse;
|
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
import java.net.http.HttpClient;
|
import java.net.http.HttpClient;
|
||||||
|
import java.net.http.HttpRequest;
|
||||||
|
import java.net.http.HttpResponse;
|
||||||
|
import java.time.Duration;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A client for checking plugin licenses against a license server.
|
* A client for checking plugin licenses against a license server.
|
||||||
@@ -13,7 +15,9 @@ import java.net.http.HttpClient;
|
|||||||
public class LicenseClient {
|
public class LicenseClient {
|
||||||
|
|
||||||
private static final Gson GSON = new Gson();
|
private static final Gson GSON = new Gson();
|
||||||
private static final HttpClient HTTP_CLIENT = HttpClient.newHttpClient();
|
private static final HttpClient HTTP_CLIENT = HttpClient.newBuilder()
|
||||||
|
.connectTimeout(Duration.ofSeconds(10))
|
||||||
|
.build();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Private constructor to prevent instantiation of this utility class.
|
* Private constructor to prevent instantiation of this utility class.
|
||||||
@@ -36,25 +40,41 @@ public class LicenseClient {
|
|||||||
String licenseKey,
|
String licenseKey,
|
||||||
String serverId // can be null
|
String serverId // can be null
|
||||||
) {
|
) {
|
||||||
|
try {
|
||||||
|
JsonObject payload = new JsonObject();
|
||||||
|
payload.addProperty("plugin", plugin);
|
||||||
|
payload.addProperty("licenseKey", licenseKey);
|
||||||
|
|
||||||
Http http = new Http();
|
if (serverId != null) {
|
||||||
|
payload.addProperty("serverId", serverId);
|
||||||
|
}
|
||||||
|
|
||||||
JsonObject payload = new JsonObject();
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
payload.addProperty("plugin", plugin);
|
.uri(URI.create(apiUrl))
|
||||||
payload.addProperty("licenseKey", licenseKey);
|
.timeout(Duration.ofSeconds(10))
|
||||||
|
.header("Content-Type", "application/json")
|
||||||
|
.POST(HttpRequest.BodyPublishers.ofString(GSON.toJson(payload)))
|
||||||
|
.build();
|
||||||
|
|
||||||
if (serverId != null) {
|
HttpResponse<String> response = HTTP_CLIENT.send(
|
||||||
payload.addProperty("serverId", serverId);
|
request,
|
||||||
|
HttpResponse.BodyHandlers.ofString()
|
||||||
|
);
|
||||||
|
|
||||||
|
JsonObject json = GSON.fromJson(response.body(), JsonObject.class);
|
||||||
|
|
||||||
|
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()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpResponse response = http.post(apiUrl, GSON.toJson(payload));
|
|
||||||
JsonObject json = GSON.fromJson(response.body(), JsonObject.class);
|
|
||||||
|
|
||||||
return new LicenseResult(
|
|
||||||
json.get("valid").getAsBoolean(),
|
|
||||||
json.get("status").getAsString(),
|
|
||||||
json.get("message").getAsString()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user