diff --git a/src/main/java/de/malkusch/km200/KM200.java b/src/main/java/de/malkusch/km200/KM200.java index 261d479..345747d 100644 --- a/src/main/java/de/malkusch/km200/KM200.java +++ b/src/main/java/de/malkusch/km200/KM200.java @@ -17,6 +17,7 @@ import de.malkusch.km200.http.Http; import de.malkusch.km200.http.JdkHttp; import de.malkusch.km200.http.RetryHttp; +import de.malkusch.km200.http.SerializedHttp; /** * This is an API for Bosch/Buderus/Junkers heaters with a KM200 gateway. @@ -126,9 +127,17 @@ public KM200(String uri, int retries, Duration timeout, String gatewayPassword, this.comm = new KM200Comm(); { - var jdkHttp = new JdkHttp(uri.replaceAll("/*$", ""), USER_AGENT, timeout); - queryHttp = new RetryHttp(jdkHttp, retries, IOException.class, ServerError.class); - updateHttp = new RetryHttp(jdkHttp, retries, ServerError.class); + Http http = new JdkHttp(uri.replaceAll("/*$", ""), USER_AGENT, timeout); + + /* + * The KM200 itself is not thread safe. This proxy serializes all + * requests to protect users from a wrong concurrent usage of this + * API. + */ + http = new SerializedHttp(http); + + queryHttp = new RetryHttp(http, retries, IOException.class, ServerError.class); + updateHttp = new RetryHttp(http, retries, ServerError.class); } query("/system"); diff --git a/src/main/java/de/malkusch/km200/http/JdkHttp.java b/src/main/java/de/malkusch/km200/http/JdkHttp.java index 6180c78..a2189ec 100644 --- a/src/main/java/de/malkusch/km200/http/JdkHttp.java +++ b/src/main/java/de/malkusch/km200/http/JdkHttp.java @@ -45,30 +45,22 @@ public Response post(String path, byte[] body) throws IOException, InterruptedEx return send(request); } - private final Object serializedSendLock = new Object(); - private Response send(HttpRequest request) throws IOException, InterruptedException, KM200Exception { - /* - * The KM200 itself is not thread safe. This lock serializes all - * requests to protect users from a wrong concurrent usage of this API. - */ - synchronized (serializedSendLock) { - var response = client.send(request, BodyHandlers.ofByteArray()); - var status = response.statusCode(); + var response = client.send(request, BodyHandlers.ofByteArray()); + var status = response.statusCode(); - if (status >= 200 && status <= 299) { - return new Response(status, response.body()); + if (status >= 200 && status <= 299) { + return new Response(status, response.body()); - } else { - throw switch (status) { - case 400 -> new KM200Exception.BadRequest("Bad request to " + request.uri()); - case 403 -> new KM200Exception.Forbidden(request.uri() + " is forbidden"); - case 404 -> new KM200Exception.NotFound(request.uri() + " was not found"); - case 423 -> new KM200Exception.Locked(request.uri() + " was locked"); - case 500 -> new KM200Exception.ServerError(request.uri() + " resulted in a server error"); - default -> new KM200Exception(request.uri() + " failed with response code " + status); - }; - } + } else { + throw switch (status) { + case 400 -> new KM200Exception.BadRequest("Bad request to " + request.uri()); + case 403 -> new KM200Exception.Forbidden(request.uri() + " is forbidden"); + case 404 -> new KM200Exception.NotFound(request.uri() + " was not found"); + case 423 -> new KM200Exception.Locked(request.uri() + " was locked"); + case 500 -> new KM200Exception.ServerError(request.uri() + " resulted in a server error"); + default -> new KM200Exception(request.uri() + " failed with response code " + status); + }; } } diff --git a/src/main/java/de/malkusch/km200/http/SerializedHttp.java b/src/main/java/de/malkusch/km200/http/SerializedHttp.java new file mode 100644 index 0000000..99e7555 --- /dev/null +++ b/src/main/java/de/malkusch/km200/http/SerializedHttp.java @@ -0,0 +1,30 @@ +package de.malkusch.km200.http; + +import java.io.IOException; + +import de.malkusch.km200.KM200Exception; + +public final class SerializedHttp implements Http { + + private final Http http; + + public SerializedHttp(Http http) { + this.http = http; + } + + private final Object lock = new Object(); + + @Override + public Response get(String path) throws IOException, InterruptedException, KM200Exception { + synchronized (lock) { + return http.get(path); + } + } + + @Override + public Response post(String path, byte[] body) throws IOException, InterruptedException, KM200Exception { + synchronized (lock) { + return http.post(path, body); + } + } +}