diff --git a/build.gradle b/build.gradle index 3d7d47f..f4b2198 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { } group = 'de.winniepat' -version = '0.0.8' +version = '0.0.9' java { diff --git a/src/main/java/de/winniepat/licenselib/LicenseClient.java b/src/main/java/de/winniepat/licenselib/LicenseClient.java index 964f63e..a351f3a 100644 --- a/src/main/java/de/winniepat/licenselib/LicenseClient.java +++ b/src/main/java/de/winniepat/licenselib/LicenseClient.java @@ -75,6 +75,8 @@ public class LicenseClient { String licenseKey, String serverId ) { + URI uri = validateUrl(apiUrl); + JsonObject payload = new JsonObject(); payload.addProperty("plugin", plugin); payload.addProperty("licenseKey", licenseKey); @@ -84,7 +86,7 @@ public class LicenseClient { } return HttpRequest.newBuilder() - .uri(URI.create(apiUrl)) + .uri(uri) .timeout(Duration.ofSeconds(10)) .header("Content-Type", "application/json") .POST(HttpRequest.BodyPublishers.ofString(GSON.toJson(payload))) @@ -92,7 +94,13 @@ public class LicenseClient { } private static LicenseResult errorResult(Throwable throwable) { - return new LicenseError(throwable.getMessage()); + String msg = throwable.getMessage(); + + if (msg == null || msg.isBlank()) { + msg = throwable.getClass().getSimpleName(); + } + + return new LicenseError(msg); } private static String getString(JsonObject json, String key, String def) { @@ -121,4 +129,18 @@ public class LicenseClient { getString(json, "checkedAt", null) ); } + + private static URI validateUrl(String apiUrl) { + URI uri = URI.create(apiUrl); + + if (uri.getScheme() == null || (!uri.getScheme().equals("http") && !uri.getScheme().equals("https"))) { + throw new IllegalArgumentException("Invalid URL scheme: " + apiUrl); + } + + if (uri.getHost() == null) { + throw new IllegalArgumentException("Invalid URL (missing host): " + apiUrl); + } + + return uri; + } }