Skip to content

Commit

Permalink
It's Java-21 time
Browse files Browse the repository at this point in the history
  • Loading branch information
malkusch committed Nov 7, 2023
1 parent 6410afb commit aedc1c7
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 60 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>de.malkusch.km200</groupId>
<artifactId>km200</artifactId>
<version>2.1.4-SNAPSHOT</version>
<version>3.0.0-SNAPSHOT</version>
<name>KM200</name>
<description>KM200 API</description>
<parent>
Expand All @@ -17,8 +17,8 @@
</scm>
<properties>
<github.repository>malkusch/km200</github.repository>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
Expand Down
31 changes: 16 additions & 15 deletions src/main/java/de/malkusch/km200/KM200.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
}
Expand Down
21 changes: 9 additions & 12 deletions src/main/java/de/malkusch/km200/KM200Endpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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")) {
Expand Down
24 changes: 10 additions & 14 deletions src/main/java/de/malkusch/km200/http/Http.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
};
}
}
17 changes: 6 additions & 11 deletions src/main/java/de/malkusch/km200/http/RetryHttp.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
17 changes: 12 additions & 5 deletions src/main/java/de/malkusch/km200/http/UrlHttp.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -74,7 +82,6 @@ private HttpURLConnection connect(Request request) throws IOException {
}

connection.connect();

return connection;

} catch (MalformedURLException e) {
Expand Down

0 comments on commit aedc1c7

Please sign in to comment.