]> git.basschouten.com Git - openhab-addons.git/commitdiff
[roku] Improve exception handling for failed http connections (#15932)
authormlobstein <michael.lobstein@gmail.com>
Wed, 29 Nov 2023 07:12:27 +0000 (01:12 -0600)
committerGitHub <noreply@github.com>
Wed, 29 Nov 2023 07:12:27 +0000 (08:12 +0100)
Signed-off-by: Michael Lobstein <michael.lobstein@gmail.com>
bundles/org.openhab.binding.roku/src/main/java/org/openhab/binding/roku/internal/RokuHttpException.java
bundles/org.openhab.binding.roku/src/main/java/org/openhab/binding/roku/internal/communication/RokuCommunicator.java

index 73c638b15ed80e7e67115ae0819c62a5784723a4..b6dc1786b0ed6cd621a1fbaac19136ad232b792d 100644 (file)
@@ -23,6 +23,10 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
 public class RokuHttpException extends Exception {
     private static final long serialVersionUID = 1L;
 
+    public RokuHttpException(String errorMessage, Throwable t) {
+        super(errorMessage, t);
+    }
+
     public RokuHttpException(String errorMessage) {
         super(errorMessage);
     }
index ce3891c6855f9fbf5f23e6f0beb206b6196a3133..bf60c38fbebb2d81dbe581bc4439196a07d90b70 100644 (file)
@@ -35,8 +35,6 @@ import org.openhab.binding.roku.internal.dto.Player;
 import org.openhab.binding.roku.internal.dto.TvChannel;
 import org.openhab.binding.roku.internal.dto.TvChannels;
 import org.openhab.binding.roku.internal.dto.TvChannels.Channel;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
  * Methods for accessing the HTTP interface of the Roku
@@ -45,7 +43,6 @@ import org.slf4j.LoggerFactory;
  */
 @NonNullByDefault
 public class RokuCommunicator {
-    private final Logger logger = LoggerFactory.getLogger(RokuCommunicator.class);
     private final HttpClient httpClient;
 
     private final String urlKeyPress;
@@ -113,10 +110,10 @@ public class RokuCommunicator {
         try {
             JAXBContext ctx = JAXBUtils.JAXBCONTEXT_DEVICE_INFO;
             if (ctx != null) {
+                final String response = getCommand(urlQryDevice);
                 Unmarshaller unmarshaller = ctx.createUnmarshaller();
                 if (unmarshaller != null) {
-                    XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY
-                            .createXMLStreamReader(new StringReader(getCommand(urlQryDevice)));
+                    XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(response));
                     DeviceInfo device = (DeviceInfo) unmarshaller.unmarshal(xsr);
                     if (device != null) {
                         return device;
@@ -139,10 +136,10 @@ public class RokuCommunicator {
         try {
             JAXBContext ctx = JAXBUtils.JAXBCONTEXT_ACTIVE_APP;
             if (ctx != null) {
+                final String response = getCommand(urlQryActiveApp);
                 Unmarshaller unmarshaller = ctx.createUnmarshaller();
                 if (unmarshaller != null) {
-                    XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY
-                            .createXMLStreamReader(new StringReader(getCommand(urlQryActiveApp)));
+                    XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(response));
                     ActiveApp activeApp = (ActiveApp) unmarshaller.unmarshal(xsr);
                     if (activeApp != null) {
                         return activeApp;
@@ -165,10 +162,10 @@ public class RokuCommunicator {
         try {
             JAXBContext ctx = JAXBUtils.JAXBCONTEXT_APPS;
             if (ctx != null) {
+                final String response = getCommand(urlQryApps);
                 Unmarshaller unmarshaller = ctx.createUnmarshaller();
                 if (unmarshaller != null) {
-                    XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY
-                            .createXMLStreamReader(new StringReader(getCommand(urlQryApps)));
+                    XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(response));
                     Apps appList = (Apps) unmarshaller.unmarshal(xsr);
                     if (appList != null) {
                         return appList.getApp();
@@ -191,10 +188,10 @@ public class RokuCommunicator {
         try {
             JAXBContext ctx = JAXBUtils.JAXBCONTEXT_PLAYER;
             if (ctx != null) {
+                final String response = getCommand(urlQryPlayer);
                 Unmarshaller unmarshaller = ctx.createUnmarshaller();
                 if (unmarshaller != null) {
-                    XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY
-                            .createXMLStreamReader(new StringReader(getCommand(urlQryPlayer)));
+                    XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(response));
                     Player playerInfo = (Player) unmarshaller.unmarshal(xsr);
                     if (playerInfo != null) {
                         return playerInfo;
@@ -217,10 +214,10 @@ public class RokuCommunicator {
         try {
             JAXBContext ctx = JAXBUtils.JAXBCONTEXT_TVCHANNEL;
             if (ctx != null) {
+                final String response = getCommand(urlQryActiveTvChannel);
                 Unmarshaller unmarshaller = ctx.createUnmarshaller();
                 if (unmarshaller != null) {
-                    XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY
-                            .createXMLStreamReader(new StringReader(getCommand(urlQryActiveTvChannel)));
+                    XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(response));
                     TvChannel tvChannelInfo = (TvChannel) unmarshaller.unmarshal(xsr);
                     if (tvChannelInfo != null) {
                         return tvChannelInfo;
@@ -243,10 +240,10 @@ public class RokuCommunicator {
         try {
             JAXBContext ctx = JAXBUtils.JAXBCONTEXT_TVCHANNELS;
             if (ctx != null) {
+                final String response = getCommand(urlQryTvChannels);
                 Unmarshaller unmarshaller = ctx.createUnmarshaller();
                 if (unmarshaller != null) {
-                    XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY
-                            .createXMLStreamReader(new StringReader(getCommand(urlQryTvChannels)));
+                    XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(response));
                     TvChannels tvChannels = (TvChannels) unmarshaller.unmarshal(xsr);
                     if (tvChannels != null) {
                         return tvChannels.getChannel();
@@ -264,13 +261,16 @@ public class RokuCommunicator {
      *
      * @param url The url to send with the command embedded in the URI
      * @return The response content of the http request
+     * @throws RokuHttpException
      */
-    private String getCommand(String url) {
+    private String getCommand(String url) throws RokuHttpException {
         try {
             return httpClient.GET(url).getContentAsString();
-        } catch (InterruptedException | TimeoutException | ExecutionException e) {
-            logger.debug("Error executing player GET command, URL: {}, {} ", url, e.getMessage());
-            return "";
+        } catch (TimeoutException | ExecutionException e) {
+            throw new RokuHttpException("Error executing GET command for URL: " + url, e);
+        } catch (InterruptedException e) {
+            Thread.currentThread().interrupt();
+            throw new RokuHttpException("InterruptedException executing GET command for URL: " + url, e);
         }
     }
 
@@ -283,8 +283,11 @@ public class RokuCommunicator {
     private void postCommand(String url) throws RokuHttpException {
         try {
             httpClient.POST(url).method(HttpMethod.POST).send();
-        } catch (InterruptedException | TimeoutException | ExecutionException e) {
-            throw new RokuHttpException("Error executing player POST command, URL: " + url + e.getMessage());
+        } catch (TimeoutException | ExecutionException e) {
+            throw new RokuHttpException("Error executing POST command, URL: " + url, e);
+        } catch (InterruptedException e) {
+            Thread.currentThread().interrupt();
+            throw new RokuHttpException("InterruptedException executing POST command for URL: " + url, e);
         }
     }
 }