From 12112eb67a9697fba603d50c5fdc5e92aa771f05 Mon Sep 17 00:00:00 2001 From: J-N-K Date: Tue, 15 Dec 2020 23:53:45 +0100 Subject: [PATCH] [http] make size of the response buffer configurable (#9391) Signed-off-by: Jan N. Klug --- bundles/org.openhab.binding.http/README.md | 1 + .../binding/http/internal/HttpThingHandler.java | 2 +- .../http/internal/config/HttpThingConfig.java | 3 ++- .../http/internal/http/HttpResponseListener.java | 15 ++++++++++----- .../http/internal/http/RefreshingUrlCache.java | 4 +++- .../main/resources/OH-INF/thing/thing-types.xml | 6 ++++++ 6 files changed, 23 insertions(+), 8 deletions(-) diff --git a/bundles/org.openhab.binding.http/README.md b/bundles/org.openhab.binding.http/README.md index 704b1287ea2..74b5084d4ac 100644 --- a/bundles/org.openhab.binding.http/README.md +++ b/bundles/org.openhab.binding.http/README.md @@ -14,6 +14,7 @@ It can be extended with different channels. | `baseURL` | no | - | The base URL for this thing. Can be extended in channel-configuration. | | `refresh` | no | 30 | Time in seconds between two refresh calls for the channels of this thing. | | `timeout` | no | 3000 | Timeout for HTTP requests in ms. | +| `bufferSize` | no | 2048 | The buffer size for the response data (in kB). | | `username` | yes | - | Username for authentication (advanced parameter). | | `password` | yes | - | Password for authentication (advanced parameter). | | `authMode` | no | BASIC | Authentication mode, `BASIC` or `DIGEST` (advanced parameter). | diff --git a/bundles/org.openhab.binding.http/src/main/java/org/openhab/binding/http/internal/HttpThingHandler.java b/bundles/org.openhab.binding.http/src/main/java/org/openhab/binding/http/internal/HttpThingHandler.java index 173baf41e77..f7ce74a0fad 100644 --- a/bundles/org.openhab.binding.http/src/main/java/org/openhab/binding/http/internal/HttpThingHandler.java +++ b/bundles/org.openhab.binding.http/src/main/java/org/openhab/binding/http/internal/HttpThingHandler.java @@ -331,7 +331,7 @@ public class HttpThingHandler extends BaseThingHandler { } return null; }); - request.send(new HttpResponseListener(f)); + request.send(new HttpResponseListener(f, null, config.bufferSize)); } catch (IllegalArgumentException | URISyntaxException e) { logger.warn("Creating request for '{}' failed: {}", commandUrl, e.getMessage()); } diff --git a/bundles/org.openhab.binding.http/src/main/java/org/openhab/binding/http/internal/config/HttpThingConfig.java b/bundles/org.openhab.binding.http/src/main/java/org/openhab/binding/http/internal/config/HttpThingConfig.java index a6cb7941cc9..19aeae84d04 100644 --- a/bundles/org.openhab.binding.http/src/main/java/org/openhab/binding/http/internal/config/HttpThingConfig.java +++ b/bundles/org.openhab.binding.http/src/main/java/org/openhab/binding/http/internal/config/HttpThingConfig.java @@ -32,9 +32,10 @@ public class HttpThingConfig { public String username = ""; public String password = ""; - public HttpAuthMode authMode = HttpAuthMode.BASIC; + public HttpAuthMode authMode = HttpAuthMode.BASIC; public HttpMethod commandMethod = HttpMethod.GET; + public int bufferSize = 2048; public @Nullable String encoding = null; public @Nullable String contentType = null; diff --git a/bundles/org.openhab.binding.http/src/main/java/org/openhab/binding/http/internal/http/HttpResponseListener.java b/bundles/org.openhab.binding.http/src/main/java/org/openhab/binding/http/internal/http/HttpResponseListener.java index 09348ed14b9..aa8f062ffcd 100644 --- a/bundles/org.openhab.binding.http/src/main/java/org/openhab/binding/http/internal/http/HttpResponseListener.java +++ b/bundles/org.openhab.binding.http/src/main/java/org/openhab/binding/http/internal/http/HttpResponseListener.java @@ -38,11 +38,16 @@ public class HttpResponseListener extends BufferingResponseListener { private final CompletableFuture<@Nullable Content> future; private final String fallbackEncoding; - public HttpResponseListener(CompletableFuture<@Nullable Content> future) { - this(future, null); - } - - public HttpResponseListener(CompletableFuture<@Nullable Content> future, @Nullable String fallbackEncoding) { + /** + * the HttpResponseListener is responsible + * + * @param future Content future to complete with the result of the request + * @param fallbackEncoding a fallback encoding for the content (UTF-8 if null) + * @param bufferSize the buffer size for the content in kB (default 2048 kB) + */ + public HttpResponseListener(CompletableFuture<@Nullable Content> future, @Nullable String fallbackEncoding, + int bufferSize) { + super(bufferSize * 1024); this.future = future; this.fallbackEncoding = fallbackEncoding != null ? fallbackEncoding : StandardCharsets.UTF_8.name(); } diff --git a/bundles/org.openhab.binding.http/src/main/java/org/openhab/binding/http/internal/http/RefreshingUrlCache.java b/bundles/org.openhab.binding.http/src/main/java/org/openhab/binding/http/internal/http/RefreshingUrlCache.java index 8a8d4ef510f..0a0a5a5de20 100644 --- a/bundles/org.openhab.binding.http/src/main/java/org/openhab/binding/http/internal/http/RefreshingUrlCache.java +++ b/bundles/org.openhab.binding.http/src/main/java/org/openhab/binding/http/internal/http/RefreshingUrlCache.java @@ -49,6 +49,7 @@ public class RefreshingUrlCache { private final String url; private final HttpClient httpClient; private final int timeout; + private final int bufferSize; private final @Nullable String fallbackEncoding; private final Set> consumers = ConcurrentHashMap.newKeySet(); private final List headers; @@ -61,6 +62,7 @@ public class RefreshingUrlCache { this.httpClient = httpClient; this.url = url; this.timeout = thingConfig.timeout; + this.bufferSize = thingConfig.bufferSize; this.headers = thingConfig.headers; fallbackEncoding = thingConfig.encoding; @@ -119,7 +121,7 @@ public class RefreshingUrlCache { logger.trace("Sending to '{}': {}", finalUrl, Util.requestToLogString(request)); } - request.send(new HttpResponseListener(response, fallbackEncoding)); + request.send(new HttpResponseListener(response, fallbackEncoding, bufferSize)); } catch (IllegalArgumentException | URISyntaxException e) { logger.warn("Creating request for '{}' failed: {}", url, e.getMessage()); } diff --git a/bundles/org.openhab.binding.http/src/main/resources/OH-INF/thing/thing-types.xml b/bundles/org.openhab.binding.http/src/main/resources/OH-INF/thing/thing-types.xml index e458d796f40..8f1c5e1ec99 100644 --- a/bundles/org.openhab.binding.http/src/main/resources/OH-INF/thing/thing-types.xml +++ b/bundles/org.openhab.binding.http/src/main/resources/OH-INF/thing/thing-types.xml @@ -25,6 +25,12 @@ The timeout in ms for each request 3000 + + + Size of the response buffer (default 2048 kB) + 2048 + true + Basic Authentication username