Deauthenticate session on dispose (#13081)

Fixes #13080

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
Jacob Laursen 2022-07-05 07:59:13 +02:00 committed by GitHub
parent 8a87450f4c
commit e0494bf44c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 0 deletions

View 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.

View 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