From bd9c194a2b1a33aba6497eebec63d966a7e279e3 Mon Sep 17 00:00:00 2001 From: Andrii Shvaika Date: Wed, 25 May 2022 17:20:14 +0300 Subject: [PATCH] Async REST API calls --- .../server/controller/AdminController.java | 32 ++++++++++--------- .../DefaultEntitiesVersionControlService.java | 21 ++++++------ .../vc/EntitiesVersionControlService.java | 6 ++-- 3 files changed, 30 insertions(+), 29 deletions(-) diff --git a/application/src/main/java/org/thingsboard/server/controller/AdminController.java b/application/src/main/java/org/thingsboard/server/controller/AdminController.java index ae9bf18bcf..cbc6ff2bca 100644 --- a/application/src/main/java/org/thingsboard/server/controller/AdminController.java +++ b/application/src/main/java/org/thingsboard/server/controller/AdminController.java @@ -16,12 +16,16 @@ package org.thingsboard.server.controller; import com.fasterxml.jackson.databind.node.ObjectNode; +import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.MoreExecutors; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; +import org.springframework.web.context.request.async.DeferredResult; import org.thingsboard.rule.engine.api.MailService; import org.thingsboard.rule.engine.api.SmsService; import org.thingsboard.server.common.data.AdminSettings; @@ -221,17 +225,15 @@ public class AdminController extends BaseController { notes = "Creates or Updates the version control settings object. " + TENANT_AUTHORITY_PARAGRAPH) @PreAuthorize("hasAuthority('TENANT_ADMIN')") @PostMapping("/vcSettings") - public EntitiesVersionControlSettings saveVersionControlSettings(@RequestBody EntitiesVersionControlSettings settings) throws ThingsboardException { - try { - accessControlService.checkPermission(getCurrentUser(), Resource.ADMIN_SETTINGS, Operation.WRITE); - EntitiesVersionControlSettings versionControlSettings = checkNotNull(versionControlService.saveVersionControlSettings(getTenantId(), settings)); - versionControlSettings.setPassword(null); - versionControlSettings.setPrivateKey(null); - versionControlSettings.setPrivateKeyPassword(null); - return versionControlSettings; - } catch (Exception e) { - throw handleException(e); - } + public DeferredResult saveVersionControlSettings(@RequestBody EntitiesVersionControlSettings settings) throws ThingsboardException { + accessControlService.checkPermission(getCurrentUser(), Resource.ADMIN_SETTINGS, Operation.WRITE); + ListenableFuture future = versionControlService.saveVersionControlSettings(getTenantId(), settings); + return wrapFuture(Futures.transform(future, savedSettings -> { + savedSettings.setPassword(null); + savedSettings.setPrivateKey(null); + savedSettings.setPrivateKeyPassword(null); + return savedSettings; + }, MoreExecutors.directExecutor())); } @ApiOperation(value = "Delete version control settings (deleteVersionControlSettings)", @@ -240,10 +242,10 @@ public class AdminController extends BaseController { @PreAuthorize("hasAuthority('TENANT_ADMIN')") @RequestMapping(value = "/vcSettings", method = RequestMethod.DELETE) @ResponseStatus(value = HttpStatus.OK) - public void deleteVersionControlSettings() throws ThingsboardException { + public DeferredResult deleteVersionControlSettings() throws ThingsboardException { try { accessControlService.checkPermission(getCurrentUser(), Resource.ADMIN_SETTINGS, Operation.DELETE); - versionControlService.deleteVersionControlSettings(getTenantId()); + return wrapFuture(versionControlService.deleteVersionControlSettings(getTenantId())); } catch (Exception e) { throw handleException(e); } @@ -253,13 +255,13 @@ public class AdminController extends BaseController { notes = "Attempts to check version control access. " + TENANT_AUTHORITY_PARAGRAPH) @PreAuthorize("hasAuthority('TENANT_ADMIN')") @RequestMapping(value = "/vcSettings/checkAccess", method = RequestMethod.POST) - public void checkVersionControlAccess( + public DeferredResult checkVersionControlAccess( @ApiParam(value = "A JSON value representing the Entities Version Control Settings.") @RequestBody EntitiesVersionControlSettings settings) throws ThingsboardException { try { accessControlService.checkPermission(getCurrentUser(), Resource.ADMIN_SETTINGS, Operation.READ); settings = checkNotNull(settings); - versionControlService.checkVersionControlAccess(getTenantId(), settings); + return wrapFuture(versionControlService.checkVersionControlAccess(getTenantId(), settings)); } catch (Exception e) { throw handleException(e); } diff --git a/application/src/main/java/org/thingsboard/server/service/sync/vc/DefaultEntitiesVersionControlService.java b/application/src/main/java/org/thingsboard/server/service/sync/vc/DefaultEntitiesVersionControlService.java index 7f791163a0..825171493e 100644 --- a/application/src/main/java/org/thingsboard/server/service/sync/vc/DefaultEntitiesVersionControlService.java +++ b/application/src/main/java/org/thingsboard/server/service/sync/vc/DefaultEntitiesVersionControlService.java @@ -310,31 +310,30 @@ public class DefaultEntitiesVersionControlService implements EntitiesVersionCont } @Override - public EntitiesVersionControlSettings saveVersionControlSettings(TenantId tenantId, EntitiesVersionControlSettings versionControlSettings) { - versionControlSettings = this.vcSettingsService.restore(tenantId, versionControlSettings); + public ListenableFuture saveVersionControlSettings(TenantId tenantId, EntitiesVersionControlSettings versionControlSettings) { + var restoredSettings = this.vcSettingsService.restore(tenantId, versionControlSettings); try { - //TODO: ashvayka: replace future.get with deferred result. Don't forget to call when tenant is deleted. - gitServiceQueue.initRepository(tenantId, versionControlSettings).get(); + var future = gitServiceQueue.initRepository(tenantId, restoredSettings); + return Futures.transform(future, f -> vcSettingsService.save(tenantId, restoredSettings), MoreExecutors.directExecutor()); } catch (Exception e) { throw new RuntimeException("Failed to init repository!", e); } - return vcSettingsService.save(tenantId, versionControlSettings); } @Override - public void deleteVersionControlSettings(TenantId tenantId) throws Exception { + public ListenableFuture deleteVersionControlSettings(TenantId tenantId) throws Exception { if (vcSettingsService.delete(tenantId)) { - //TODO: ashvayka: replace future.get with deferred result. Don't forget to call when tenant is deleted. - gitServiceQueue.clearRepository(tenantId).get(); + return gitServiceQueue.clearRepository(tenantId); + } else { + return Futures.immediateFuture(null); } } @Override - public void checkVersionControlAccess(TenantId tenantId, EntitiesVersionControlSettings settings) throws ThingsboardException { + public ListenableFuture checkVersionControlAccess(TenantId tenantId, EntitiesVersionControlSettings settings) throws ThingsboardException { settings = this.vcSettingsService.restore(tenantId, settings); try { - //TODO: ashvayka: replace future.get with deferred result. - gitServiceQueue.testRepository(tenantId, settings).get(); + return gitServiceQueue.testRepository(tenantId, settings); } catch (Exception e) { throw new ThingsboardException(String.format("Unable to access repository: %s", getCauseMessage(e)), ThingsboardErrorCode.GENERAL); diff --git a/application/src/main/java/org/thingsboard/server/service/sync/vc/EntitiesVersionControlService.java b/application/src/main/java/org/thingsboard/server/service/sync/vc/EntitiesVersionControlService.java index 1d8cfca00c..35e602711d 100644 --- a/application/src/main/java/org/thingsboard/server/service/sync/vc/EntitiesVersionControlService.java +++ b/application/src/main/java/org/thingsboard/server/service/sync/vc/EntitiesVersionControlService.java @@ -54,10 +54,10 @@ public interface EntitiesVersionControlService { EntitiesVersionControlSettings getVersionControlSettings(TenantId tenantId); - EntitiesVersionControlSettings saveVersionControlSettings(TenantId tenantId, EntitiesVersionControlSettings versionControlSettings); + ListenableFuture saveVersionControlSettings(TenantId tenantId, EntitiesVersionControlSettings versionControlSettings); - void deleteVersionControlSettings(TenantId tenantId) throws Exception; + ListenableFuture deleteVersionControlSettings(TenantId tenantId) throws Exception; - void checkVersionControlAccess(TenantId tenantId, EntitiesVersionControlSettings settings) throws Exception; + ListenableFuture checkVersionControlAccess(TenantId tenantId, EntitiesVersionControlSettings settings) throws Exception; }