]> git.basschouten.com Git - openhab-addons.git/commitdiff
[http] make size of the response buffer configurable (#9391)
authorJ-N-K <J-N-K@users.noreply.github.com>
Tue, 15 Dec 2020 22:53:45 +0000 (23:53 +0100)
committerGitHub <noreply@github.com>
Tue, 15 Dec 2020 22:53:45 +0000 (14:53 -0800)
Signed-off-by: Jan N. Klug <jan.n.klug@rub.de>
bundles/org.openhab.binding.http/README.md
bundles/org.openhab.binding.http/src/main/java/org/openhab/binding/http/internal/HttpThingHandler.java
bundles/org.openhab.binding.http/src/main/java/org/openhab/binding/http/internal/config/HttpThingConfig.java
bundles/org.openhab.binding.http/src/main/java/org/openhab/binding/http/internal/http/HttpResponseListener.java
bundles/org.openhab.binding.http/src/main/java/org/openhab/binding/http/internal/http/RefreshingUrlCache.java
bundles/org.openhab.binding.http/src/main/resources/OH-INF/thing/thing-types.xml

index 704b1287ea233f114c02255fa7791f09a474a316..74b5084d4ac8dc8c5adf54b0f899256215fe96dc 100644 (file)
@@ -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). |
index 173baf41e77b3b3229c58d652fd034f3c2b7ae34..f7ce74a0fad209019c7becdf7473ac76da59806c 100644 (file)
@@ -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());
         }
index a6cb7941cc97147fd27b7af5ffd68977e0350ef2..19aeae84d04eaf9f9ead8ff3cb7a9697ac315387 100644 (file)
@@ -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;
index 09348ed14b9681efbad5cec57eae84c6a1d66576..aa8f062ffcdafd05c57b6c8bbc42f0eb1b0b1101 100644 (file)
@@ -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();
     }
index 8a8d4ef510f6154ed0ec4a8498f079df0661d5cb..0a0a5a5de2063b6a4a012e0ab67d8a5665f834dc 100644 (file)
@@ -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<Consumer<Content>> consumers = ConcurrentHashMap.newKeySet();
     private final List<String> 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());
         }
index e458d796f400a56ea4107fe75bad9696bef65de4..8f1c5e1ec9933b5a3dffa266971a49c4a73c3d88 100644 (file)
                                <description>The timeout in ms for each request</description>
                                <default>3000</default>
                        </parameter>
+                       <parameter name="bufferSize" type="integer" min="0">
+                               <label>Buffer Size</label>
+                               <description>Size of the response buffer (default 2048 kB)</description>
+                               <default>2048</default>
+                               <advanced>true</advanced>
+                       </parameter>
                        <parameter name="username" type="text">
                                <label>Username</label>
                                <description>Basic Authentication username</description>