diff --git a/pom.xml b/pom.xml
index 9b2666a..f2bade1 100644
--- a/pom.xml
+++ b/pom.xml
@@ -2,7 +2,7 @@
4.0.0
de.malkusch.km200
km200
- 2.1.4-SNAPSHOT
+ 3.0.0-SNAPSHOT
KM200
KM200 API
@@ -17,8 +17,8 @@
malkusch/km200
- 17
- 17
+ 21
+ 21
UTF-8
diff --git a/src/main/java/de/malkusch/km200/KM200.java b/src/main/java/de/malkusch/km200/KM200.java
index da9df32..f80fc0b 100644
--- a/src/main/java/de/malkusch/km200/KM200.java
+++ b/src/main/java/de/malkusch/km200/KM200.java
@@ -212,19 +212,15 @@ public String query(String path) throws KM200Exception, IOException, Interrupted
throw new KM200Exception("No response when querying " + path);
}
var decrypted = comm.decodeMessage(device, encrypted);
- if (decrypted == null) {
- throw new KM200Exception("Could not decrypt query " + path);
- }
- if (path.equals("/gateway/firmware")) {
- return decrypted;
- } else {
- if (!decrypted.startsWith("{")) {
- throw new KM200Exception(
- String.format("Could not decrypt query %s. Body was:\n%s\n\n Decrypted was:\n%s", path,
- encrypted, decrypted));
- }
- }
- return decrypted;
+
+ return switch (decrypted) {
+ case String d when path.equals("/gateway/firmware") -> decrypted;
+ case String d when d.startsWith("{") -> decrypted;
+
+ case null -> throw new KM200Exception("Could not decrypt query " + path);
+ case String d -> throw new KM200Exception(String.format(
+ "Could not decrypt query %s. Body was:\n%s\n\n Decrypted was:\n%s", path, encrypted, decrypted));
+ };
}
public double queryDouble(String path) throws KM200Exception, IOException, InterruptedException {
@@ -267,8 +263,13 @@ private static void assertHttpUri(String var) {
assertNotBlank(var, "Wrong uri " + var);
var uri = URI.create(var);
- var scheme = uri.getScheme();
- if (scheme == null || !(scheme.equalsIgnoreCase("http") || scheme.equalsIgnoreCase("https"))) {
+ switch (uri.getScheme()) {
+ case String s when s.equalsIgnoreCase("http"):
+ break;
+ case String s when s.equalsIgnoreCase("https"):
+ break;
+
+ case null, default:
throw new IllegalArgumentException("Wrong uri " + var);
}
}
diff --git a/src/main/java/de/malkusch/km200/KM200Endpoint.java b/src/main/java/de/malkusch/km200/KM200Endpoint.java
index 9d13945..0a9dc81 100644
--- a/src/main/java/de/malkusch/km200/KM200Endpoint.java
+++ b/src/main/java/de/malkusch/km200/KM200Endpoint.java
@@ -12,7 +12,7 @@
import de.malkusch.km200.KM200Exception.Forbidden;
-public abstract class KM200Endpoint {
+public abstract sealed class KM200Endpoint {
private final String path;
private final String type;
@@ -30,7 +30,7 @@ public String toString() {
return String.format("%s [%s]", path, type);
}
- public static class Value extends KM200Endpoint {
+ public static final class Value extends KM200Endpoint {
private final String body;
private final boolean writeable;
private final boolean recordable;
@@ -57,13 +57,13 @@ public String toString() {
}
}
- public static class ForbiddenNode extends KM200Endpoint {
+ public static final class ForbiddenNode extends KM200Endpoint {
ForbiddenNode(String path) {
super(path, "Forbidden");
}
}
- public static class UnknownNode extends KM200Endpoint {
+ public static final class UnknownNode extends KM200Endpoint {
private final String value;
UnknownNode(String path, String type, String value) {
@@ -139,14 +139,11 @@ private static Value value(String path, String type, JsonNode json) {
var writeable = json.path("writeable").asBoolean(false);
var recordable = json.path("recordable").asBoolean(false);
- String value;
- if (json.has("value")) {
- value = json.get("value").asText();
- } else if (json.has("values")) {
- value = json.get("values").toString();
- } else {
- value = json.toString();
- }
+ String value = switch (json) {
+ case JsonNode j when j.has("value") -> json.get("value").asText();
+ case JsonNode j when j.has("values") -> json.get("values").toString();
+ default -> json.toString();
+ };
String allowedValues = null;
if (json.has("allowedValues")) {
diff --git a/src/main/java/de/malkusch/km200/http/Http.java b/src/main/java/de/malkusch/km200/http/Http.java
index 6659b4f..11e3d8c 100644
--- a/src/main/java/de/malkusch/km200/http/Http.java
+++ b/src/main/java/de/malkusch/km200/http/Http.java
@@ -41,19 +41,15 @@ public final Response post(String path, byte[] body) throws KM200Exception, IOEx
protected abstract Response exchange(Request request) throws IOException, InterruptedException, KM200Exception;
static Response assertHttpOk(Request request, Response response) throws KM200Exception {
- var status = response.status();
- if (status >= 200 && status <= 299) {
- return response;
-
- } else {
- throw switch (status) {
- case 400 -> new KM200Exception.BadRequest(request + " was a bad request");
- case 403 -> new KM200Exception.Forbidden(request + " is forbidden");
- case 404 -> new KM200Exception.NotFound(request + " was not found");
- case 423 -> new KM200Exception.Locked(request + " was locked");
- case 500 -> new KM200Exception.ServerError(request + " resulted in a server error");
- default -> new KM200Exception(request + " failed with response code " + status);
- };
- }
+ return switch ((Integer) response.status()) {
+ case Integer status when (status >= 200 && status <= 299) -> response;
+
+ case 400 -> throw new KM200Exception.BadRequest(request + " was a bad request");
+ case 403 -> throw new KM200Exception.Forbidden(request + " is forbidden");
+ case 404 -> throw new KM200Exception.NotFound(request + " was not found");
+ case 423 -> throw new KM200Exception.Locked(request + " was locked");
+ case 500 -> throw new KM200Exception.ServerError(request + " resulted in a server error");
+ default -> throw new KM200Exception(request + " failed with response code " + response.status());
+ };
}
}
diff --git a/src/main/java/de/malkusch/km200/http/RetryHttp.java b/src/main/java/de/malkusch/km200/http/RetryHttp.java
index 9c0a042..b95695f 100644
--- a/src/main/java/de/malkusch/km200/http/RetryHttp.java
+++ b/src/main/java/de/malkusch/km200/http/RetryHttp.java
@@ -34,17 +34,12 @@ public Response exchange(Request request) throws IOException, InterruptedExcepti
return retry.get(() -> http.exchange(request));
} catch (FailsafeException e) {
- if (e.getCause() instanceof IOException cause) {
- throw cause;
-
- } else if (e.getCause() instanceof InterruptedException cause) {
- throw cause;
-
- } else if (e.getCause() instanceof KM200Exception cause) {
- throw cause;
-
- } else {
- throw new KM200Exception("Unexpected retry error for " + request.path(), e);
+ switch (e.getCause()) {
+ case IOException cause -> throw cause;
+ case InterruptedException cause -> throw cause;
+ case KM200Exception cause -> throw cause;
+ case Throwable cause -> throw new KM200Exception("Unexpected retry error for " + request.path(), cause);
+ case null -> throw new KM200Exception("Unexpected retry error for " + request.path(), e);
}
}
}
diff --git a/src/main/java/de/malkusch/km200/http/UrlHttp.java b/src/main/java/de/malkusch/km200/http/UrlHttp.java
index becd95c..fe445fe 100644
--- a/src/main/java/de/malkusch/km200/http/UrlHttp.java
+++ b/src/main/java/de/malkusch/km200/http/UrlHttp.java
@@ -1,6 +1,7 @@
package de.malkusch.km200.http;
import java.io.IOException;
+import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
@@ -45,9 +46,11 @@ protected Response exchange(Request request) throws IOException, InterruptedExce
throw new IOException(request + " received invalid HTTP response");
}
- try (var input = connection.getErrorStream() != null ? connection.getErrorStream()
- : connection.getInputStream()) {
-
+ var input = switch (connection.getErrorStream()) {
+ case InputStream error -> error;
+ case null -> connection.getInputStream();
+ };
+ try (input) {
var body = input.readAllBytes();
var response = new Response(status, body);
return assertHttpOk(request, response);
@@ -63,7 +66,12 @@ protected Response exchange(Request request) throws IOException, InterruptedExce
private HttpURLConnection connect(Request request) throws IOException {
try {
- var connection = (HttpURLConnection) new URL(uri + request.path()).openConnection();
+ var url = new URL(uri + request.path());
+
+ if (!(url.openConnection() instanceof HttpURLConnection connection)) {
+ throw new IllegalStateException(url + " is not a http url");
+ }
+
connection.setConnectTimeout(timeoutMillis);
connection.setReadTimeout(timeoutMillis);
connection.setRequestProperty("User-Agent", userAgent);
@@ -74,7 +82,6 @@ private HttpURLConnection connect(Request request) throws IOException {
}
connection.connect();
-
return connection;
} catch (MalformedURLException e) {