[http] make size of the response buffer configurable (#9391)

Signed-off-by: Jan N. Klug <jan.n.klug@rub.de>
This commit is contained in:
J-N-K 2020-12-15 23:53:45 +01:00 committed by GitHub
parent 1c3022d391
commit 12112eb67a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 23 additions and 8 deletions

View 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). |

View 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());
}

View 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;

View 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();
}

View 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());
}

View File

@ -25,6 +25,12 @@
<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>