From a6655f150cf8e56f6e925d9d170e79570666ee00 Mon Sep 17 00:00:00 2001 From: Ondrej Pecta Date: Mon, 31 Jul 2017 23:50:43 +0200 Subject: [PATCH] Implemented some improvement suggestions from PR code review #3 Signed-off-by: Ondrej Pecta --- .../handler/SomfyTahomaBridgeHandler.java | 23 +++++++------------ 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/addons/binding/org.openhab.binding.somfytahoma/src/main/java/org/openhab/binding/somfytahoma/handler/SomfyTahomaBridgeHandler.java b/addons/binding/org.openhab.binding.somfytahoma/src/main/java/org/openhab/binding/somfytahoma/handler/SomfyTahomaBridgeHandler.java index 51a321801e55c..8e7efdb80d330 100644 --- a/addons/binding/org.openhab.binding.somfytahoma/src/main/java/org/openhab/binding/somfytahoma/handler/SomfyTahomaBridgeHandler.java +++ b/addons/binding/org.openhab.binding.somfytahoma/src/main/java/org/openhab/binding/somfytahoma/handler/SomfyTahomaBridgeHandler.java @@ -214,8 +214,7 @@ private String getGroups() { url = TAHOMA_URL + "getActionGroups"; String urlParameters = ""; - byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8); - InputStream response = sendDataToTahomaWithCookie(url, postData); + InputStream response = sendDataToTahomaWithCookie(url, urlParameters); return readResponse(response); @@ -240,8 +239,7 @@ private void listDevices() { url = TAHOMA_URL + "getSetup"; String urlParameters = ""; - byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8); - InputStream response = sendDataToTahomaWithCookie(url, postData); + InputStream response = sendDataToTahomaWithCookie(url, urlParameters); String line = readResponse(response); @@ -287,9 +285,7 @@ private State getState(SomfyTahomaThingHandler handler, String deviceUrl) { url = TAHOMA_URL + "getStates"; String urlParameters = "[{\"deviceURL\": \"" + deviceUrl + "\", \"states\": [{\"name\": \"" + handler.getStateName() + "\"}]}]"; - byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8); - - InputStream response = sendDataToTahomaWithCookie(url, postData); + InputStream response = sendDataToTahomaWithCookie(url, urlParameters); String line = readResponse(response); SomfyTahomaStatesResponse data = gson.fromJson(line, SomfyTahomaStatesResponse.class); @@ -409,17 +405,17 @@ public InputStream sendToTahomaWithCookie(String url) throws Exception { return connection.getInputStream(); } - public InputStream sendDataToTahomaWithCookie(String url, byte[] postData) throws Exception { + public InputStream sendDataToTahomaWithCookie(String url, String postData) throws Exception { logger.trace("Sending POST to Tahoma to url: {} with data: {}", url, postData); URL cookieUrl = new URL(url); HttpsURLConnection connection = (HttpsURLConnection) cookieUrl.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("POST"); setConnectionDefaults(connection); - connection.setRequestProperty("Content-Length", Integer.toString(postData.length)); + connection.setRequestProperty("Content-Length", Integer.toString(postData.length())); connection.setRequestProperty("Cookie", cookie); try (DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) { - wr.write(postData); + wr.write(postData.getBytes(StandardCharsets.UTF_8)); } return connection.getInputStream(); @@ -453,9 +449,8 @@ public void sendCommand(String io, String command, String params) { String urlParameters = "{\"actions\": [{\"deviceURL\": \"" + io + "\", \"commands\": [{ \"name\": \"" + command + "\", \"parameters\": " + params + "}]}]}"; logger.debug("Sending apply: {}", urlParameters); - byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8); - InputStream response = sendDataToTahomaWithCookie(url, postData); + InputStream response = sendDataToTahomaWithCookie(url, urlParameters); String line = readResponse(response); SomfyTahomaApplyResponse data = gson.fromJson(line, SomfyTahomaApplyResponse.class); @@ -484,11 +479,9 @@ public String getCurrentExecutions(String type) { try { url = TAHOMA_URL + "getCurrentExecutions"; - String urlParameters = ""; - byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8); - InputStream response = sendDataToTahomaWithCookie(url, postData); + InputStream response = sendDataToTahomaWithCookie(url, urlParameters); String line = readResponse(response); SomfyTahomaExecutionsResponse data = gson.fromJson(line, SomfyTahomaExecutionsResponse.class);