diff --git a/src/main/java/de/malkusch/km200/KM200.java b/src/main/java/de/malkusch/km200/KM200.java index da9df32..06cd064 100644 --- a/src/main/java/de/malkusch/km200/KM200.java +++ b/src/main/java/de/malkusch/km200/KM200.java @@ -267,8 +267,12 @@ 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/RetryHttp.java b/src/main/java/de/malkusch/km200/http/RetryHttp.java index 9c0a042..56c36f2 100644 --- a/src/main/java/de/malkusch/km200/http/RetryHttp.java +++ b/src/main/java/de/malkusch/km200/http/RetryHttp.java @@ -34,17 +34,11 @@ 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; + default -> 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..65654d9 100644 --- a/src/main/java/de/malkusch/km200/http/UrlHttp.java +++ b/src/main/java/de/malkusch/km200/http/UrlHttp.java @@ -63,7 +63,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 +79,6 @@ private HttpURLConnection connect(Request request) throws IOException { } connection.connect(); - return connection; } catch (MalformedURLException e) {