Skip to content

Commit

Permalink
[remoteopenhab] Prefer StringContentProvider (openhab#10001)
Browse files Browse the repository at this point in the history
Signed-off-by: Laurent Garnier <lg.hc@free.fr>
Signed-off-by: Luca Calcaterra <calcaterra.luca@gmail.com>
  • Loading branch information
lolodomo authored and lucacalcaterra committed Feb 23, 2021
1 parent 68bf722 commit 7713e37
Showing 1 changed file with 5 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
*/
package org.openhab.binding.remoteopenhab.internal.rest;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
Expand All @@ -39,8 +37,8 @@
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.client.api.Request;
import org.eclipse.jetty.client.api.Response;
import org.eclipse.jetty.client.util.InputStreamContentProvider;
import org.eclipse.jetty.client.util.InputStreamResponseListener;
import org.eclipse.jetty.client.util.StringContentProvider;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.HttpStatus;
import org.openhab.binding.remoteopenhab.internal.data.RemoteopenhabChannelTriggerEvent;
Expand Down Expand Up @@ -183,10 +181,8 @@ public String getRemoteItemState(String itemName) throws RemoteopenhabException
public void sendCommandToRemoteItem(String itemName, Command command) throws RemoteopenhabException {
try {
String url = String.format("%s/%s", getRestApiUrl("items"), itemName);
InputStream stream = new ByteArrayInputStream(command.toFullString().getBytes(StandardCharsets.UTF_8));
executeUrl(HttpMethod.POST, url, "application/json", stream, "text/plain", false);
stream.close();
} catch (RemoteopenhabException | IOException e) {
executeUrl(HttpMethod.POST, url, "application/json", command.toFullString(), "text/plain", false);
} catch (RemoteopenhabException e) {
throw new RemoteopenhabException("Failed to send command to the remote item " + itemName
+ " using the items REST API: " + e.getMessage(), e);
}
Expand Down Expand Up @@ -470,7 +466,7 @@ public String executeGetUrl(String url, String acceptHeader, boolean asyncReadin
return executeUrl(HttpMethod.GET, url, acceptHeader, null, null, asyncReading);
}

public String executeUrl(HttpMethod httpMethod, String url, String acceptHeader, @Nullable InputStream content,
public String executeUrl(HttpMethod httpMethod, String url, String acceptHeader, @Nullable String content,
@Nullable String contentType, boolean asyncReading) throws RemoteopenhabException {
final Request request = httpClient.newRequest(url).method(httpMethod).timeout(REQUEST_TIMEOUT,
TimeUnit.MILLISECONDS);
Expand All @@ -482,10 +478,7 @@ public String executeUrl(HttpMethod httpMethod, String url, String acceptHeader,

if (content != null && (HttpMethod.POST.equals(httpMethod) || HttpMethod.PUT.equals(httpMethod))
&& contentType != null) {
try (final InputStreamContentProvider inputStreamContentProvider = new InputStreamContentProvider(
content)) {
request.content(inputStreamContentProvider, contentType);
}
request.content(new StringContentProvider(content), contentType);
}

try {
Expand Down

0 comments on commit 7713e37

Please sign in to comment.