From 4b20d7fdfc0662ea1893ffb6e6a482cd368ce043 Mon Sep 17 00:00:00 2001 From: Trinitus01 Date: Mon, 19 Feb 2024 23:30:40 +0100 Subject: [PATCH 1/3] fixed: missing Content-Length header (BodyPublisher.ofByteArrays JDK 17 issue) Signed-off-by: Tom Blum trinitus01@googlemail.com --- .../NotificationsForFireTVConnection.java | 243 +++++++++--------- 1 file changed, 127 insertions(+), 116 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..43f0d2d784 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 @@ -1,116 +1,127 @@ -/** - * Copyright (c) 2021-2023 Contributors to the SmartHome/J project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.smarthomej.binding.notificationsforfiretv.internal; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.net.HttpURLConnection; -import java.net.URI; -import java.net.http.HttpClient; -import java.net.http.HttpRequest; -import java.net.http.HttpRequest.BodyPublishers; -import java.net.http.HttpResponse; -import java.net.http.HttpResponse.BodyHandlers; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; - -import org.eclipse.jdt.annotation.NonNullByDefault; - -/** - * - * @author Tom Blum - Initial contribution - */ -@NonNullByDefault -public class NotificationsForFireTVConnection { - - private static final String PROTOCOL = "http"; - private static final String LINE = "\r\n"; - private static final String QUOTE = "\""; - - private URI uri; - private String boundary; - private HttpClient httpClient; - private List byteArrays = new ArrayList<>(); - - /** - * This constructor initializes a new HTTP POST request with content - * type is set to multipart/form-data - * - * @param hostname device IP address or a FQDN - * @param port application port - * @throws IOException - */ - public NotificationsForFireTVConnection(String hostname, int port) throws IOException { - uri = URI.create(PROTOCOL + "://" + hostname + ":" + port); - boundary = UUID.randomUUID().toString(); - httpClient = HttpClient.newBuilder().build(); - } - - /** - * Adds a form field to the request - * - * @param name field name - * @param value field value - */ - public void addFormField(String name, String value) { - byteArrays.add( - ("--" + boundary + LINE + "Content-Disposition: form-data; name=").getBytes(StandardCharsets.UTF_8)); - byteArrays.add((QUOTE + name + QUOTE + LINE + LINE + value + LINE).getBytes(StandardCharsets.UTF_8)); - } - - /** - * Adds a upload file section to the request - * - * @param name field name - * @param file file value - * @throws IOException - */ - public void addFilePart(String name, File file) throws IOException { - if (!file.exists()) { - throw new FileNotFoundException("File not found: " + file.getPath()); - } - - byteArrays.add( - ("--" + boundary + LINE + "Content-Disposition: form-data; name=").getBytes(StandardCharsets.UTF_8)); - byteArrays.add((QUOTE + name + QUOTE + "; filename=" + QUOTE + file.toPath().getFileName() + QUOTE + LINE - + "Content-Type: application/octet-stream" + LINE + LINE).getBytes(StandardCharsets.UTF_8)); - byteArrays.add(Files.readAllBytes(file.toPath())); - byteArrays.add(LINE.getBytes(StandardCharsets.UTF_8)); - } - - /** - * Completes the request and receives response from the server. - * - * @return String as response in case the server returned status OK, - * otherwise an exception is thrown. - * @throws IOException - * @throws InterruptedException - */ - public String send() throws IOException, InterruptedException { - byteArrays.add(("--" + boundary + "--").getBytes(StandardCharsets.UTF_8)); - - HttpRequest httpRequest = HttpRequest.newBuilder() - .header("Content-Type", "multipart/form-data;boundary=" + boundary) - .POST(BodyPublishers.ofByteArrays(byteArrays)).uri(uri).build(); - HttpResponse response = httpClient.send(httpRequest, BodyHandlers.ofString()); - if (response.statusCode() == HttpURLConnection.HTTP_OK) { - return response.body(); - } else { - throw new IOException("Unable to connect to server: " + response.statusCode()); - } - } -} +/** + * Copyright (c) 2021-2023 Contributors to the SmartHome/J project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.smarthomej.binding.notificationsforfiretv.internal; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.net.HttpURLConnection; +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; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + * @author Tom Blum - Initial contribution + */ +@NonNullByDefault +public class NotificationsForFireTVConnection { + + private static final String PROTOCOL = "http"; + private static final String LINE = "\r\n"; + private static final String QUOTE = "\""; + + private URI uri; + private String boundary; + private HttpClient httpClient; + private List byteArrays = new ArrayList<>(); + + private final Logger logger = LoggerFactory.getLogger(NotificationsForFireTVConnection.class); + + /** + * This constructor initializes a new HTTP POST request with content + * type is set to multipart/form-data + * + * @param hostname device IP address or a FQDN + * @param port application port + * @throws IOException + */ + public NotificationsForFireTVConnection(String hostname, int port) throws IOException { + uri = URI.create(PROTOCOL + "://" + hostname + ":" + port); + boundary = UUID.randomUUID().toString(); + httpClient = HttpClient.newBuilder().build(); + } + + /** + * Adds a form field to the request + * + * @param name field name + * @param value field value + */ + public void addFormField(String name, String value) { + byteArrays.add( + ("--" + boundary + LINE + "Content-Disposition: form-data; name=").getBytes(StandardCharsets.UTF_8)); + byteArrays.add((QUOTE + name + QUOTE + LINE + LINE + value + LINE).getBytes(StandardCharsets.UTF_8)); + } + + /** + * Adds a upload file section to the request + * + * @param name field name + * @param file file value + * @throws IOException + */ + public void addFilePart(String name, File file) throws IOException { + if (!file.exists()) { + throw new FileNotFoundException("File not found: " + file.getPath()); + } + + byteArrays.add( + ("--" + boundary + LINE + "Content-Disposition: form-data; name=").getBytes(StandardCharsets.UTF_8)); + byteArrays.add((QUOTE + name + QUOTE + "; filename=" + QUOTE + file.toPath().getFileName() + QUOTE + LINE + + "Content-Type: application/octet-stream" + LINE + LINE).getBytes(StandardCharsets.UTF_8)); + byteArrays.add(Files.readAllBytes(file.toPath())); + byteArrays.add(LINE.getBytes(StandardCharsets.UTF_8)); + } + + /** + * Completes the request and receives response from the server. + * + * @return String as response in case the server returned status OK, + * otherwise an exception is thrown. + * @throws IOException + * @throws InterruptedException + */ + 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(bodyPublisher).uri(uri) + .build(); + HttpResponse response = httpClient.send(httpRequest, BodyHandlers.ofString()); + if (response.statusCode() == HttpURLConnection.HTTP_OK) { + return response.body(); + } else { + throw new IOException("Unable to connect to server: " + response.statusCode()); + } + } +} From 92bd0d0adfad1da5049e9a596664227fcdd6eae8 Mon Sep 17 00:00:00 2001 From: Trinitus01 Date: Mon, 19 Feb 2024 23:40:11 +0100 Subject: [PATCH 2/3] fixed: commited line endings Signed-off-by: Tom Blum trinitus01@googlemail.com --- .../NotificationsForFireTVConnection.java | 254 +++++++++--------- 1 file changed, 127 insertions(+), 127 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 43f0d2d784..8d25b36bfc 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 @@ -1,127 +1,127 @@ -/** - * Copyright (c) 2021-2023 Contributors to the SmartHome/J project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.smarthomej.binding.notificationsforfiretv.internal; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.net.HttpURLConnection; -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; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; - -import org.eclipse.jdt.annotation.NonNullByDefault; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * - * @author Tom Blum - Initial contribution - */ -@NonNullByDefault -public class NotificationsForFireTVConnection { - - private static final String PROTOCOL = "http"; - private static final String LINE = "\r\n"; - private static final String QUOTE = "\""; - - private URI uri; - private String boundary; - private HttpClient httpClient; - private List byteArrays = new ArrayList<>(); - - private final Logger logger = LoggerFactory.getLogger(NotificationsForFireTVConnection.class); - - /** - * This constructor initializes a new HTTP POST request with content - * type is set to multipart/form-data - * - * @param hostname device IP address or a FQDN - * @param port application port - * @throws IOException - */ - public NotificationsForFireTVConnection(String hostname, int port) throws IOException { - uri = URI.create(PROTOCOL + "://" + hostname + ":" + port); - boundary = UUID.randomUUID().toString(); - httpClient = HttpClient.newBuilder().build(); - } - - /** - * Adds a form field to the request - * - * @param name field name - * @param value field value - */ - public void addFormField(String name, String value) { - byteArrays.add( - ("--" + boundary + LINE + "Content-Disposition: form-data; name=").getBytes(StandardCharsets.UTF_8)); - byteArrays.add((QUOTE + name + QUOTE + LINE + LINE + value + LINE).getBytes(StandardCharsets.UTF_8)); - } - - /** - * Adds a upload file section to the request - * - * @param name field name - * @param file file value - * @throws IOException - */ - public void addFilePart(String name, File file) throws IOException { - if (!file.exists()) { - throw new FileNotFoundException("File not found: " + file.getPath()); - } - - byteArrays.add( - ("--" + boundary + LINE + "Content-Disposition: form-data; name=").getBytes(StandardCharsets.UTF_8)); - byteArrays.add((QUOTE + name + QUOTE + "; filename=" + QUOTE + file.toPath().getFileName() + QUOTE + LINE - + "Content-Type: application/octet-stream" + LINE + LINE).getBytes(StandardCharsets.UTF_8)); - byteArrays.add(Files.readAllBytes(file.toPath())); - byteArrays.add(LINE.getBytes(StandardCharsets.UTF_8)); - } - - /** - * Completes the request and receives response from the server. - * - * @return String as response in case the server returned status OK, - * otherwise an exception is thrown. - * @throws IOException - * @throws InterruptedException - */ - 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(bodyPublisher).uri(uri) - .build(); - HttpResponse response = httpClient.send(httpRequest, BodyHandlers.ofString()); - if (response.statusCode() == HttpURLConnection.HTTP_OK) { - return response.body(); - } else { - throw new IOException("Unable to connect to server: " + response.statusCode()); - } - } -} +/** + * Copyright (c) 2021-2023 Contributors to the SmartHome/J project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.smarthomej.binding.notificationsforfiretv.internal; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.net.HttpURLConnection; +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; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + * @author Tom Blum - Initial contribution + */ +@NonNullByDefault +public class NotificationsForFireTVConnection { + + private static final String PROTOCOL = "http"; + private static final String LINE = "\r\n"; + private static final String QUOTE = "\""; + + private URI uri; + private String boundary; + private HttpClient httpClient; + private List byteArrays = new ArrayList<>(); + + private final Logger logger = LoggerFactory.getLogger(NotificationsForFireTVConnection.class); + + /** + * This constructor initializes a new HTTP POST request with content + * type is set to multipart/form-data + * + * @param hostname device IP address or a FQDN + * @param port application port + * @throws IOException + */ + public NotificationsForFireTVConnection(String hostname, int port) throws IOException { + uri = URI.create(PROTOCOL + "://" + hostname + ":" + port); + boundary = UUID.randomUUID().toString(); + httpClient = HttpClient.newBuilder().build(); + } + + /** + * Adds a form field to the request + * + * @param name field name + * @param value field value + */ + public void addFormField(String name, String value) { + byteArrays.add( + ("--" + boundary + LINE + "Content-Disposition: form-data; name=").getBytes(StandardCharsets.UTF_8)); + byteArrays.add((QUOTE + name + QUOTE + LINE + LINE + value + LINE).getBytes(StandardCharsets.UTF_8)); + } + + /** + * Adds a upload file section to the request + * + * @param name field name + * @param file file value + * @throws IOException + */ + public void addFilePart(String name, File file) throws IOException { + if (!file.exists()) { + throw new FileNotFoundException("File not found: " + file.getPath()); + } + + byteArrays.add( + ("--" + boundary + LINE + "Content-Disposition: form-data; name=").getBytes(StandardCharsets.UTF_8)); + byteArrays.add((QUOTE + name + QUOTE + "; filename=" + QUOTE + file.toPath().getFileName() + QUOTE + LINE + + "Content-Type: application/octet-stream" + LINE + LINE).getBytes(StandardCharsets.UTF_8)); + byteArrays.add(Files.readAllBytes(file.toPath())); + byteArrays.add(LINE.getBytes(StandardCharsets.UTF_8)); + } + + /** + * Completes the request and receives response from the server. + * + * @return String as response in case the server returned status OK, + * otherwise an exception is thrown. + * @throws IOException + * @throws InterruptedException + */ + 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(bodyPublisher).uri(uri) + .build(); + HttpResponse response = httpClient.send(httpRequest, BodyHandlers.ofString()); + if (response.statusCode() == HttpURLConnection.HTTP_OK) { + return response.body(); + } else { + throw new IOException("Unable to connect to server: " + response.statusCode()); + } + } +} From 55ac4f5c658364f6fa9effd2bba0238a7f42ba39 Mon Sep 17 00:00:00 2001 From: Trinitus01 Date: Mon, 19 Feb 2024 23:42:37 +0100 Subject: [PATCH 3/3] removed: logger Signed-off-by: Tom Blum trinitus01@googlemail.com --- .../internal/NotificationsForFireTVConnection.java | 4 ---- 1 file changed, 4 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 8d25b36bfc..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 @@ -30,8 +30,6 @@ import java.util.UUID; import org.eclipse.jdt.annotation.NonNullByDefault; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * @@ -49,8 +47,6 @@ public class NotificationsForFireTVConnection { private HttpClient httpClient; private List byteArrays = new ArrayList<>(); - private final Logger logger = LoggerFactory.getLogger(NotificationsForFireTVConnection.class); - /** * This constructor initializes a new HTTP POST request with content * type is set to multipart/form-data