]> git.basschouten.com Git - openhab-addons.git/commitdiff
[bindings d-e] Fix exception handling (Jetty HTTP client) (#10476)
authorlolodomo <lg.hc@free.fr>
Tue, 6 Apr 2021 15:37:19 +0000 (17:37 +0200)
committerGitHub <noreply@github.com>
Tue, 6 Apr 2021 15:37:19 +0000 (17:37 +0200)
Fixes #10474

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
20 files changed:
bundles/org.openhab.binding.daikin/src/main/java/org/openhab/binding/daikin/internal/DaikinWebTargets.java
bundles/org.openhab.binding.daikin/src/main/java/org/openhab/binding/daikin/internal/api/BasicInfo.java
bundles/org.openhab.binding.daikin/src/main/java/org/openhab/binding/daikin/internal/api/EnergyInfoDayAndWeek.java
bundles/org.openhab.binding.daikin/src/main/java/org/openhab/binding/daikin/internal/api/EnergyInfoYear.java
bundles/org.openhab.binding.daikin/src/main/java/org/openhab/binding/daikin/internal/api/airbase/AirbaseBasicInfo.java
bundles/org.openhab.binding.daikin/src/main/java/org/openhab/binding/daikin/internal/handler/DaikinAcUnitHandler.java
bundles/org.openhab.binding.darksky/src/main/java/org/openhab/binding/darksky/internal/config/DarkSkyChannelConfiguration.java
bundles/org.openhab.binding.darksky/src/main/java/org/openhab/binding/darksky/internal/connection/DarkSkyConnection.java
bundles/org.openhab.binding.denonmarantz/src/main/java/org/openhab/binding/denonmarantz/internal/connector/telnet/DenonMarantzTelnetClientThread.java
bundles/org.openhab.binding.denonmarantz/src/main/java/org/openhab/binding/denonmarantz/internal/connector/telnet/DenonMarantzTelnetConnector.java
bundles/org.openhab.binding.denonmarantz/src/main/java/org/openhab/binding/denonmarantz/internal/handler/DenonMarantzHandler.java
bundles/org.openhab.binding.dlinksmarthome/src/main/java/org/openhab/binding/dlinksmarthome/internal/DLinkHNAPCommunication.java
bundles/org.openhab.binding.dlinksmarthome/src/main/java/org/openhab/binding/dlinksmarthome/internal/motionsensor/DLinkMotionSensorCommunication.java
bundles/org.openhab.binding.ecobee/README.md
bundles/org.openhab.binding.ecobee/src/main/java/org/openhab/binding/ecobee/internal/api/EcobeeAuth.java
bundles/org.openhab.binding.enturno/src/main/java/org/openhab/binding/enturno/internal/connection/EnturNoConnection.java
bundles/org.openhab.binding.etherrain/src/main/java/org/openhab/binding/etherrain/internal/api/EtherRainCommunication.java
bundles/org.openhab.binding.evohome/src/main/java/org/openhab/binding/evohome/internal/api/ApiAccess.java
bundles/org.openhab.binding.evohome/src/main/java/org/openhab/binding/evohome/internal/api/EvohomeApiClient.java
bundles/org.openhab.binding.evohome/src/main/java/org/openhab/binding/evohome/internal/handler/EvohomeAccountBridgeHandler.java

index 3ebe999a07bac2b6b06b177cd6417a3950ddac4b..3df15c12cc5f71508587012f1a4a35b1c3d5b71f 100644 (file)
@@ -15,7 +15,9 @@ package org.openhab.binding.daikin.internal;
 import java.io.IOException;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
 import java.util.stream.Collectors;
 
 import org.eclipse.jdt.annotation.NonNullByDefault;
@@ -234,8 +236,11 @@ public class DaikinWebTargets {
             return response.getContentAsString();
         } catch (DaikinCommunicationException e) {
             throw e;
-        } catch (Exception e) {
+        } catch (ExecutionException | TimeoutException e) {
             throw new DaikinCommunicationException("Daikin HTTP error", e);
+        } catch (InterruptedException e) {
+            Thread.currentThread().interrupt();
+            throw new DaikinCommunicationException("Daikin HTTP interrupted", e);
         }
     }
 
index c37fba2788a31aee4bdd689d9cd2f00d6a696027..be87721cceba0c52d449ca51db8820e51732c27d 100644 (file)
@@ -28,7 +28,7 @@ import org.slf4j.LoggerFactory;
  */
 @NonNullByDefault
 public class BasicInfo {
-    private static final Logger logger = LoggerFactory.getLogger(BasicInfo.class);
+    private static final Logger LOGGER = LoggerFactory.getLogger(BasicInfo.class);
 
     public String mac = "";
     public String ret = "";
@@ -38,7 +38,7 @@ public class BasicInfo {
     }
 
     public static BasicInfo parse(String response) {
-        logger.debug("Parsing string: \"{}\"", response);
+        LOGGER.debug("Parsing string: \"{}\"", response);
 
         Map<String, String> responseMap = InfoParser.parse(response);
 
index 88995529858d3dafed8682921efd266f5b5c94a4..ddcbab2d9511f698fd301da59f2e76263c1c57b2 100644 (file)
@@ -36,7 +36,7 @@ public class EnergyInfoDayAndWeek {
     public Optional<Double> energyCoolingThisWeek = Optional.empty();
     public Optional<Double> energyCoolingLastWeek = Optional.empty();
 
-    private static final Logger logger = LoggerFactory.getLogger(EnergyInfoDayAndWeek.class);
+    private static final Logger LOGGER = LoggerFactory.getLogger(EnergyInfoDayAndWeek.class);
 
     private EnergyInfoDayAndWeek() {
     }
@@ -44,7 +44,7 @@ public class EnergyInfoDayAndWeek {
     public static EnergyInfoDayAndWeek parse(String response) {
         EnergyInfoDayAndWeek info = new EnergyInfoDayAndWeek();
 
-        logger.trace("Parsing string: \"{}\"", response);
+        LOGGER.trace("Parsing string: \"{}\"", response);
 
         // /aircon/get_week_power_ex
         // ret=OK,s_dayw=0,week_heat=1/1/1/1/1/5/2/1/1/1/1/2/1/1,week_cool=0/0/0/0/0/0/0/0/0/0/0/0/0/0
@@ -94,7 +94,7 @@ public class EnergyInfoDayAndWeek {
                 info.energyCoolingLastWeek = Optional.of(previousWeekEnergy / 10);
             }
         } else {
-            logger.debug("did not receive 'ret=OK' from adapter");
+            LOGGER.debug("did not receive 'ret=OK' from adapter");
         }
         return info;
     }
index 2d4b126381f0c7fbdd2bca11ac9d7bffb33ca8bd..de3ca69ac934ba8fb27921c8144e3ea104930d07 100644 (file)
@@ -37,7 +37,6 @@ public class EnergyInfoYear {
     }
 
     public static EnergyInfoYear parse(String response) {
-
         LOGGER.trace("Parsing string: \"{}\"", response);
 
         Map<String, String> responseMap = InfoParser.parse(response);
index 235da931e8f149aa7d31ac3195b1ee305e039076..4b15fc1e78c0e94c50768025e1b8f096544858d2 100644 (file)
@@ -29,7 +29,7 @@ import org.slf4j.LoggerFactory;
  */
 @NonNullByDefault
 public class AirbaseBasicInfo {
-    private static final Logger logger = LoggerFactory.getLogger(AirbaseBasicInfo.class);
+    private static final Logger LOGGER = LoggerFactory.getLogger(AirbaseBasicInfo.class);
 
     public String mac = "";
     public String ret = "";
@@ -39,7 +39,7 @@ public class AirbaseBasicInfo {
     }
 
     public static AirbaseBasicInfo parse(String response) {
-        logger.debug("Parsing string: \"{}\"", response);
+        LOGGER.debug("Parsing string: \"{}\"", response);
 
         Map<String, String> responseMap = InfoParser.parse(response);
 
index 6a88283df4d7800561d3c4eda67c680f2e13d2cc..ce46d611cc5ca1b3dfb27c90868045b78f50359a 100644 (file)
@@ -54,7 +54,7 @@ import org.slf4j.LoggerFactory;
  * @author Paul Smedley - Modifications to support Airbase Controllers
  * @author Lukas Agethen - Added support for Energy Year reading, compressor frequency and powerful mode
  * @author Wouter Denayer - Added to support for weekly & daily energy reading
- * 
+ *
  */
 @NonNullByDefault
 public class DaikinAcUnitHandler extends DaikinBaseHandler {
@@ -281,7 +281,7 @@ public class DaikinAcUnitHandler extends DaikinBaseHandler {
     }
 
     /**
-     * 
+     *
      * @param channel
      * @param maybePower
      */
@@ -304,7 +304,7 @@ public class DaikinAcUnitHandler extends DaikinBaseHandler {
                 return;
             }
             webTargets.registerUuid(key);
-        } catch (Exception e) {
+        } catch (DaikinCommunicationException e) {
             // suppress exceptions
             logger.debug("registerUuid({}) error: {}", key, e.getMessage());
         }
index 976b0cf7211e1fdb5e40c0275fc98030e405e6c9..ac2c6d9acd24e247904f64dacd3aad8b805c7596 100644 (file)
@@ -14,6 +14,7 @@ package org.openhab.binding.darksky.internal.config;
 
 import java.time.Duration;
 import java.util.regex.Pattern;
+import java.util.regex.PatternSyntaxException;
 
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
@@ -85,11 +86,14 @@ public class DarkSkyChannelConfiguration {
                     throw new NumberFormatException();
                 } else {
                     String[] splittedConfigTime = time.split(TIME_SEPARATOR);
+                    if (splittedConfigTime.length < 2) {
+                        throw new NumberFormatException();
+                    }
                     int hour = Integer.parseInt(splittedConfigTime[0]);
                     int minutes = Integer.parseInt(splittedConfigTime[1]);
                     return Duration.ofMinutes(minutes).plusHours(hour).toMinutes();
                 }
-            } catch (Exception ex) {
+            } catch (PatternSyntaxException | NumberFormatException | ArithmeticException ex) {
                 logger.warn("Cannot parse channel configuration '{}' to hour and minutes, use pattern hh:mm, ignoring!",
                         time);
             }
index 6b4fb5345c9d3e50799f1474762eaba0c8e23674..aae205c62e97cddd98ab2ccf630d38703f75191c 100644 (file)
@@ -211,9 +211,13 @@ public class DarkSkyConnection {
             } else {
                 throw new DarkSkyCommunicationException(errorMessage, e.getCause());
             }
-        } catch (InterruptedException | TimeoutException e) {
+        } catch (TimeoutException e) {
             logger.debug("Exception occurred during execution: {}", e.getLocalizedMessage(), e);
             throw new DarkSkyCommunicationException(e.getLocalizedMessage(), e.getCause());
+        } catch (InterruptedException e) {
+            logger.debug("Execution interrupted: {}", e.getLocalizedMessage(), e);
+            Thread.currentThread().interrupt();
+            throw new DarkSkyCommunicationException(e.getLocalizedMessage(), e.getCause());
         }
     }
 
index 6062c1ea7d59db83365a2178311b4c9b2bd871fd..3a258052faa5e4f6e86446b1f9692aafd3299a8b 100644 (file)
@@ -145,6 +145,7 @@ public class DenonMarantzTelnetClientThread extends Thread {
                 listener.telnetClientConnected(false);
             } catch (InterruptedException e) {
                 logger.debug("Interrupted while connecting to {}", config.getHost(), e);
+                Thread.currentThread().interrupt();
             }
             delay = RECONNECT_DELAY;
         }
index fbdb8d666f9782e6b764469484c60a2af6cbcc93..df158222666be22387ab111b03a387c83890571d 100644 (file)
@@ -127,6 +127,7 @@ public class DenonMarantzTelnetConnector extends DenonMarantzConnector implement
                     Thread.sleep(300);
                 } catch (InterruptedException e) {
                     logger.trace("requestStateOverTelnet() - Interrupted while requesting state.");
+                    Thread.currentThread().interrupt();
                 }
             }
         });
index 614bf55ca41b5f35b0a0659526d29a02ce78e199..0be9348c12d9cd95f8e17fbe156d288a1da9b63e 100644 (file)
@@ -200,7 +200,7 @@ public class DenonMarantzHandler extends BaseThingHandler implements DenonMarant
      * Try to auto configure the connection type (Telnet or HTTP)
      * for Things not added through Paper UI.
      */
-    private void autoConfigure() {
+    private void autoConfigure() throws InterruptedException {
         /*
          * The isTelnet parameter has no default.
          * When not set we will try to auto-detect the correct values
@@ -223,7 +223,7 @@ public class DenonMarantzHandler extends BaseThingHandler implements DenonMarant
                     telnetEnable = false;
                     httpApiUsable = true;
                 }
-            } catch (InterruptedException | TimeoutException | ExecutionException e) {
+            } catch (TimeoutException | ExecutionException e) {
                 logger.debug("Error when trying to access AVR using HTTP on port 80, reverting to Telnet mode.", e);
             }
 
@@ -239,7 +239,7 @@ public class DenonMarantzHandler extends BaseThingHandler implements DenonMarant
                         httpPort = 8080;
                         httpApiUsable = true;
                     }
-                } catch (InterruptedException | TimeoutException | ExecutionException e) {
+                } catch (TimeoutException | ExecutionException e) {
                     logger.debug("Additionally tried to connect to port 8080, this also failed", e);
                 }
             }
@@ -255,7 +255,7 @@ public class DenonMarantzHandler extends BaseThingHandler implements DenonMarant
                     response = httpClient.newRequest("http://" + host + ":" + httpPort + "/goform/Deviceinfo.xml")
                             .timeout(3, TimeUnit.SECONDS).send();
                     status = response.getStatus();
-                } catch (InterruptedException | TimeoutException | ExecutionException e) {
+                } catch (TimeoutException | ExecutionException e) {
                     logger.debug("Failed in fetching the Deviceinfo.xml to determine zone count", e);
                 }
 
@@ -303,7 +303,12 @@ public class DenonMarantzHandler extends BaseThingHandler implements DenonMarant
 
         // Configure Connection type (Telnet/HTTP) and number of zones
         // Note: this only happens for discovered Things
-        autoConfigure();
+        try {
+            autoConfigure();
+        } catch (InterruptedException e) {
+            Thread.currentThread().interrupt();
+            return;
+        }
 
         if (!checkConfiguration()) {
             return;
index f18a1dbced342e4e046f337a7e4ea358263a83c9..bcac91e1691c9b74ef4a15aebc01c1657f0b5e3f 100644 (file)
@@ -363,6 +363,9 @@ public abstract class DLinkHNAPCommunication {
             } catch (final NoSuchAlgorithmException e) {
                 logger.debug("login - Internal error", e);
                 status = HNAPStatus.INTERNAL_ERROR;
+            } catch (final InterruptedException e) {
+                status = HNAPStatus.COMMUNICATION_ERROR;
+                Thread.currentThread().interrupt();
             } catch (final Exception e) {
                 // Assume there has been some problem trying to send one of the messages
                 if (status != HNAPStatus.COMMUNICATION_ERROR) {
index 936803a9e0469abf75260bb7c1ad81bc6049f6a5..39b78008b950dff862f8f11eb8e85a213e04bd9b 100644 (file)
@@ -278,6 +278,9 @@ public class DLinkMotionSensorCommunication extends DLinkHNAPCommunication {
                 } else {
                     unexpectedResult("getLastDetection - Unexpected response", soapResponse);
                 }
+            } catch (final InterruptedException e) {
+                status = DeviceStatus.COMMUNICATION_ERROR;
+                Thread.currentThread().interrupt();
             } catch (final Exception e) {
                 // Assume there has been some problem trying to send one of the messages
                 if (status != DeviceStatus.COMMUNICATION_ERROR) {
index d7a450b1b4f2dcbd246703f552f0f3456e8414e6..1ba91fa6b9543e8108940cd2120db41ad18202c0 100644 (file)
@@ -47,6 +47,7 @@ Once logged in, select the **Developer** option from the menu.
 If you don't see the **Developer** option on the menu, then something went wrong with the previous step.
 
 4. Finally, create a new application.
+
 Give the application a name and fill in the application summary.
 Select the **Ecobee PIN** Authorization Method, then press **Create**.
 You now should see the **API key** for the application you just created.
index 06958b89d12cfad3336e1808f7132a1536e10dcc..cafea05ca28907b43d302734bc287ebea38ed78a 100644 (file)
@@ -262,6 +262,7 @@ public class EcobeeAuth {
             }
         } catch (InterruptedException e) {
             logger.debug("InterruptedException on call to Ecobee authorization API: {}", e.getMessage());
+            Thread.currentThread().interrupt();
         }
         return null;
     }
index 5dc92dfd19bee2902e281ff8000fbcac259774f8..70ecd71b23cda7c637dca011bd2e982556b3ce1f 100644 (file)
@@ -160,9 +160,13 @@ public class EnturNoConnection {
             String errorMessage = e.getLocalizedMessage();
             logger.debug("Exception occurred during execution: {}", errorMessage, e);
             throw new EnturCommunicationException(errorMessage, e);
-        } catch (InterruptedException | TimeoutException | IOException e) {
+        } catch (TimeoutException | IOException e) {
             logger.debug("Exception occurred during execution: {}", e.getLocalizedMessage(), e);
             throw new EnturCommunicationException(e.getLocalizedMessage(), e);
+        } catch (InterruptedException e) {
+            logger.debug("Execution interrupted: {}", e.getLocalizedMessage(), e);
+            Thread.currentThread().interrupt();
+            throw new EnturCommunicationException(e.getLocalizedMessage(), e);
         }
     }
 
index 67589fa9af11b3f1218a5155a4ed5722f66fa7be..a10417d93395b31b589441260bcfedef0cae9e3c 100644 (file)
@@ -191,9 +191,13 @@ public class EtherRainCommunication {
                 logger.warn("Etherrain return status other than HTTP_OK : {}", response.getStatus());
                 return Collections.emptyList();
             }
-        } catch (InterruptedException | TimeoutException | ExecutionException e) {
+        } catch (TimeoutException | ExecutionException e) {
             logger.warn("Could not connect to Etherrain with exception: {}", e.getMessage());
             return Collections.emptyList();
+        } catch (InterruptedException e) {
+            logger.warn("Connect to Etherrain interrupted: {}", e.getMessage());
+            Thread.currentThread().interrupt();
+            return Collections.emptyList();
         }
 
         return new BufferedReader(new StringReader(response.getContentAsString())).lines().collect(Collectors.toList());
index a53d0d963f89aa622bf64e8b2b9f4655b2c47662..2d49c2bddef6810fe0e83d080df2e812ffa49af1 100644 (file)
@@ -119,8 +119,11 @@ public class ApiAccess {
                     retVal = new Gson().fromJson(reply, outClass);
                 }
             }
-        } catch (InterruptedException | ExecutionException e) {
+        } catch (ExecutionException e) {
             logger.debug("Error in handling request: ", e);
+        } catch (InterruptedException e) {
+            logger.debug("Handling request interrupted: ", e);
+            Thread.currentThread().interrupt();
         }
 
         return retVal;
index 48939b1819d04b01239a906fe2f1d4303836fbe7..af9caf58b78d9ef621446efddd7e4be2fc950572 100644 (file)
@@ -48,7 +48,6 @@ public class EvohomeApiClient {
     private static final String CLIENT_SECRET = "1a15cdb8-42de-407b-add0-059f92c530cb";
 
     private final Logger logger = LoggerFactory.getLogger(EvohomeApiClient.class);
-    private final HttpClient httpClient;
     private final EvohomeAccountConfiguration configuration;
     private final ApiAccess apiAccess;
 
@@ -60,19 +59,9 @@ public class EvohomeApiClient {
      * Creates a new API client based on the V2 API interface
      *
      * @param configuration The configuration of the account to use
-     * @throws Exception
      */
-    public EvohomeApiClient(EvohomeAccountConfiguration configuration, HttpClient httpClient) throws Exception {
+    public EvohomeApiClient(EvohomeAccountConfiguration configuration, HttpClient httpClient) {
         this.configuration = configuration;
-        this.httpClient = httpClient;
-
-        try {
-            httpClient.start();
-        } catch (Exception e) {
-            logger.error("Could not start http client", e);
-            throw new EvohomeApiClientException("Could not start http client", e);
-        }
-
         apiAccess = new ApiAccess(httpClient);
         apiAccess.setApplicationId(APPLICATION_ID);
     }
@@ -85,14 +74,6 @@ public class EvohomeApiClient {
         useraccount = null;
         locations = null;
         locationsStatus = null;
-
-        if (httpClient.isStarted()) {
-            try {
-                httpClient.stop();
-            } catch (Exception e) {
-                logger.debug("Could not stop http client.", e);
-            }
-        }
     }
 
     public boolean login() {
index 5c64f717afa7c479ff1149c4936f1c17b27818c8..069e0ca1b4b828945f9968cfcd1f4945afba2077 100644 (file)
@@ -74,30 +74,22 @@ public class EvohomeAccountBridgeHandler extends BaseBridgeHandler {
         configuration = getConfigAs(EvohomeAccountConfiguration.class);
 
         if (checkConfig()) {
-            try {
-                apiClient = new EvohomeApiClient(configuration, this.httpClient);
-            } catch (Exception e) {
-                logger.error("Could not start API client", e);
-                updateAccountStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
-                        "Could not create evohome API client");
-            }
+            apiClient = new EvohomeApiClient(configuration, this.httpClient);
 
-            if (apiClient != null) {
-                // Initialization can take a while, so kick it off on a separate thread
-                scheduler.schedule(() -> {
-                    if (apiClient.login()) {
-                        if (checkInstallationInfoHasDuplicateIds(apiClient.getInstallationInfo())) {
-                            startRefreshTask();
-                        } else {
-                            updateAccountStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
-                                    "System Information Sanity Check failed");
-                        }
+            // Initialization can take a while, so kick it off on a separate thread
+            scheduler.schedule(() -> {
+                if (apiClient.login()) {
+                    if (checkInstallationInfoHasDuplicateIds(apiClient.getInstallationInfo())) {
+                        startRefreshTask();
                     } else {
                         updateAccountStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
-                                "Authentication failed");
+                                "System Information Sanity Check failed");
                     }
-                }, 0, TimeUnit.SECONDS);
-            }
+                } else {
+                    updateAccountStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
+                            "Authentication failed");
+                }
+            }, 0, TimeUnit.SECONDS);
         }
     }