[hydrawise] Remove the access token when the thing is removed (#14945)
Related to #14818 Signed-off-by: Laurent Garnier <lg.hc@free.fr>
This commit is contained in:
parent
2b674dde01
commit
da1a95fbdd
|
@ -68,18 +68,18 @@ public class HydrawiseAccountHandler extends BaseBridgeHandler implements Access
|
||||||
private static final String SCOPE = "all";
|
private static final String SCOPE = "all";
|
||||||
private final List<HydrawiseControllerListener> controllerListeners = Collections
|
private final List<HydrawiseControllerListener> controllerListeners = Collections
|
||||||
.synchronizedList(new ArrayList<HydrawiseControllerListener>());
|
.synchronizedList(new ArrayList<HydrawiseControllerListener>());
|
||||||
private final HydrawiseGraphQLClient apiClient;
|
private final HttpClient httpClient;
|
||||||
private final OAuthClientService oAuthService;
|
private final OAuthFactory oAuthFactory;
|
||||||
|
private @Nullable OAuthClientService oAuthService;
|
||||||
|
private @Nullable HydrawiseGraphQLClient apiClient;
|
||||||
private @Nullable ScheduledFuture<?> pollFuture;
|
private @Nullable ScheduledFuture<?> pollFuture;
|
||||||
private @Nullable Customer lastData;
|
private @Nullable Customer lastData;
|
||||||
private int refresh;
|
private int refresh;
|
||||||
|
|
||||||
public HydrawiseAccountHandler(final Bridge bridge, final HttpClient httpClient, final OAuthFactory oAuthFactory) {
|
public HydrawiseAccountHandler(final Bridge bridge, final HttpClient httpClient, final OAuthFactory oAuthFactory) {
|
||||||
super(bridge);
|
super(bridge);
|
||||||
this.oAuthService = oAuthFactory.createOAuthClientService(getThing().toString(), AUTH_URL, AUTH_URL, CLIENT_ID,
|
this.httpClient = httpClient;
|
||||||
CLIENT_SECRET, SCOPE, false);
|
this.oAuthFactory = oAuthFactory;
|
||||||
oAuthService.addAccessTokenRefreshListener(this);
|
|
||||||
this.apiClient = new HydrawiseGraphQLClient(httpClient, oAuthService);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -88,14 +88,34 @@ public class HydrawiseAccountHandler extends BaseBridgeHandler implements Access
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void initialize() {
|
public void initialize() {
|
||||||
|
OAuthClientService oAuthService = oAuthFactory.createOAuthClientService(getThing().toString(), AUTH_URL,
|
||||||
|
AUTH_URL, CLIENT_ID, CLIENT_SECRET, SCOPE, false);
|
||||||
|
this.oAuthService = oAuthService;
|
||||||
|
oAuthService.addAccessTokenRefreshListener(this);
|
||||||
|
this.apiClient = new HydrawiseGraphQLClient(httpClient, oAuthService);
|
||||||
logger.debug("Handler initialized.");
|
logger.debug("Handler initialized.");
|
||||||
scheduler.schedule(this::configure, 0, TimeUnit.SECONDS);
|
scheduler.schedule(() -> configure(oAuthService), 0, TimeUnit.SECONDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void dispose() {
|
public void dispose() {
|
||||||
logger.debug("Handler disposed.");
|
logger.debug("Handler disposed.");
|
||||||
clearPolling();
|
clearPolling();
|
||||||
|
OAuthClientService oAuthService = this.oAuthService;
|
||||||
|
if (oAuthService != null) {
|
||||||
|
oAuthService.removeAccessTokenRefreshListener(this);
|
||||||
|
oAuthFactory.ungetOAuthService(getThing().toString());
|
||||||
|
this.oAuthService = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handleRemoval() {
|
||||||
|
OAuthClientService oAuthService = this.oAuthService;
|
||||||
|
if (oAuthService != null) {
|
||||||
|
oAuthFactory.deleteServiceAndAccessToken(getThing().toString());
|
||||||
|
}
|
||||||
|
super.handleRemoval();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -134,7 +154,7 @@ public class HydrawiseAccountHandler extends BaseBridgeHandler implements Access
|
||||||
initPolling(delaySeconds, this.refresh);
|
initPolling(delaySeconds, this.refresh);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void configure() {
|
private void configure(OAuthClientService oAuthService) {
|
||||||
HydrawiseAccountConfiguration config = getConfig().as(HydrawiseAccountConfiguration.class);
|
HydrawiseAccountConfiguration config = getConfig().as(HydrawiseAccountConfiguration.class);
|
||||||
try {
|
try {
|
||||||
if (!config.userName.isEmpty() && !config.password.isEmpty()) {
|
if (!config.userName.isEmpty() && !config.password.isEmpty()) {
|
||||||
|
|
Loading…
Reference in New Issue