From 448f17b0cecf28acf647c3f2572ab96fc10c9052 Mon Sep 17 00:00:00 2001 From: "Jan N. Klug" Date: Sat, 23 Mar 2024 16:00:09 +0100 Subject: [PATCH] [notificationsforfiretv] Missing Content-Length (smarthomej#521) (#569) * fixed: missing Content-Length header (BodyPublisher.ofByteArrays JDK 17 issue) Signed-off-by: Tom Blum trinitus01@googlemail.com (cherry picked from commit 574630205d00c14f985aa9d7c7b2dce1bf9d7eec) Signed-off-by: Jan N. Klug --- .../internal/NotificationsForFireTVConnection.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/bundles/org.smarthomej.binding.notificationsforfiretv/src/main/java/org/smarthomej/binding/notificationsforfiretv/internal/NotificationsForFireTVConnection.java b/bundles/org.smarthomej.binding.notificationsforfiretv/src/main/java/org/smarthomej/binding/notificationsforfiretv/internal/NotificationsForFireTVConnection.java index 2bede8f999..970b8f5f6d 100644 --- a/bundles/org.smarthomej.binding.notificationsforfiretv/src/main/java/org/smarthomej/binding/notificationsforfiretv/internal/NotificationsForFireTVConnection.java +++ b/bundles/org.smarthomej.binding.notificationsforfiretv/src/main/java/org/smarthomej/binding/notificationsforfiretv/internal/NotificationsForFireTVConnection.java @@ -19,6 +19,7 @@ import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; +import java.net.http.HttpRequest.BodyPublisher; import java.net.http.HttpRequest.BodyPublishers; import java.net.http.HttpResponse; import java.net.http.HttpResponse.BodyHandlers; @@ -103,9 +104,15 @@ public void addFilePart(String name, File file) throws IOException { public String send() throws IOException, InterruptedException { byteArrays.add(("--" + boundary + "--").getBytes(StandardCharsets.UTF_8)); + int length = 0; + for (byte[] bytes : byteArrays) { + length += bytes.length; + } + + BodyPublisher bodyPublisher = BodyPublishers.fromPublisher(BodyPublishers.ofByteArrays(byteArrays), length); HttpRequest httpRequest = HttpRequest.newBuilder() - .header("Content-Type", "multipart/form-data;boundary=" + boundary) - .POST(BodyPublishers.ofByteArrays(byteArrays)).uri(uri).build(); + .header("Content-Type", "multipart/form-data;boundary=" + boundary).POST(bodyPublisher).uri(uri) + .build(); HttpResponse response = httpClient.send(httpRequest, BodyHandlers.ofString()); if (response.statusCode() == HttpURLConnection.HTTP_OK) { return response.body();