]> git.basschouten.com Git - openhab-addons.git/commitdiff
Deauthenticate session on dispose (#13081)
authorJacob Laursen <jacob-github@vindvejr.dk>
Tue, 5 Jul 2022 05:59:13 +0000 (07:59 +0200)
committerGitHub <noreply@github.com>
Tue, 5 Jul 2022 05:59:13 +0000 (07:59 +0200)
Fixes #13080

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
bundles/org.openhab.binding.boschindego/src/main/java/org/openhab/binding/boschindego/internal/IndegoController.java
bundles/org.openhab.binding.boschindego/src/main/java/org/openhab/binding/boschindego/internal/handler/BoschIndegoHandler.java

index 70c66c9f7b0f758a61597af03a40a18c1f851ce0..1fe36dd1be6fdb31ed18aa01cce47c2b11c8fdf6 100644 (file)
@@ -167,6 +167,21 @@ public class IndegoController {
                 });
     }
 
+    /**
+     * Deauthenticate session. This method should be called as part of cleanup to reduce
+     * lingering sessions. This can potentially avoid killed sessions in situation with
+     * multiple clients (e.g. openHAB and mobile app) if restrictions on concurrent
+     * number of sessions would be put on the service.
+     *
+     * @throws IndegoException if any communication or parsing error occurred
+     */
+    public void deauthenticate() throws IndegoException {
+        if (session.isValid()) {
+            deleteRequest("authenticate");
+            session.invalidate();
+        }
+    }
+
     /**
      * Wraps {@link #getRequest(String, Class)} into an authenticated session.
      *
@@ -432,6 +447,32 @@ public class IndegoController {
         }
     }
 
+    /**
+     * Sends a DELETE request to the server.
+     * 
+     * @param path the relative path to which the request should be sent
+     * @throws IndegoException if any communication or parsing error occurred
+     */
+    private void deleteRequest(String path) throws IndegoException {
+        try {
+            Request request = httpClient.newRequest(BASE_URL + path).method(HttpMethod.DELETE)
+                    .header(CONTEXT_HEADER_NAME, session.getContextId());
+            if (logger.isTraceEnabled()) {
+                logger.trace("DELETE request for {}", BASE_URL + path);
+            }
+            ContentResponse response = sendRequest(request);
+            int status = response.getStatus();
+            if (!HttpStatus.isSuccess(status)) {
+                throw new IndegoException("The request failed with error: " + status);
+            }
+        } catch (InterruptedException e) {
+            Thread.currentThread().interrupt();
+            throw new IndegoException(e);
+        } catch (TimeoutException | ExecutionException e) {
+            throw new IndegoException(e);
+        }
+    }
+
     /**
      * Send request. This method exists for the purpose of avoiding multiple calls to
      * the server at the same time.
index 0dec921f0763db08e516ee6b88a56ceb6e018f8c..f0c3e6774cffb94ea486c3a40f5916a000ce200c 100644 (file)
@@ -121,6 +121,14 @@ public class BoschIndegoHandler extends BaseThingHandler {
             pollFuture.cancel(true);
         }
         this.cuttingTimeMapPollFuture = null;
+
+        scheduler.execute(() -> {
+            try {
+                controller.deauthenticate();
+            } catch (IndegoException e) {
+                logger.debug("Deauthentication failed", e);
+            }
+        });
     }
 
     @Override