Async REST API calls

This commit is contained in:
Andrii Shvaika 2022-05-25 17:20:14 +03:00
parent 5bb9bdface
commit bd9c194a2b
3 changed files with 30 additions and 29 deletions

View File

@ -16,12 +16,16 @@
package org.thingsboard.server.controller; package org.thingsboard.server.controller;
import com.fasterxml.jackson.databind.node.ObjectNode; 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.ApiOperation;
import io.swagger.annotations.ApiParam; import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*; 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.MailService;
import org.thingsboard.rule.engine.api.SmsService; import org.thingsboard.rule.engine.api.SmsService;
import org.thingsboard.server.common.data.AdminSettings; 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) notes = "Creates or Updates the version control settings object. " + TENANT_AUTHORITY_PARAGRAPH)
@PreAuthorize("hasAuthority('TENANT_ADMIN')") @PreAuthorize("hasAuthority('TENANT_ADMIN')")
@PostMapping("/vcSettings") @PostMapping("/vcSettings")
public EntitiesVersionControlSettings saveVersionControlSettings(@RequestBody EntitiesVersionControlSettings settings) throws ThingsboardException { public DeferredResult<EntitiesVersionControlSettings> saveVersionControlSettings(@RequestBody EntitiesVersionControlSettings settings) throws ThingsboardException {
try { accessControlService.checkPermission(getCurrentUser(), Resource.ADMIN_SETTINGS, Operation.WRITE);
accessControlService.checkPermission(getCurrentUser(), Resource.ADMIN_SETTINGS, Operation.WRITE); ListenableFuture<EntitiesVersionControlSettings> future = versionControlService.saveVersionControlSettings(getTenantId(), settings);
EntitiesVersionControlSettings versionControlSettings = checkNotNull(versionControlService.saveVersionControlSettings(getTenantId(), settings)); return wrapFuture(Futures.transform(future, savedSettings -> {
versionControlSettings.setPassword(null); savedSettings.setPassword(null);
versionControlSettings.setPrivateKey(null); savedSettings.setPrivateKey(null);
versionControlSettings.setPrivateKeyPassword(null); savedSettings.setPrivateKeyPassword(null);
return versionControlSettings; return savedSettings;
} catch (Exception e) { }, MoreExecutors.directExecutor()));
throw handleException(e);
}
} }
@ApiOperation(value = "Delete version control settings (deleteVersionControlSettings)", @ApiOperation(value = "Delete version control settings (deleteVersionControlSettings)",
@ -240,10 +242,10 @@ public class AdminController extends BaseController {
@PreAuthorize("hasAuthority('TENANT_ADMIN')") @PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/vcSettings", method = RequestMethod.DELETE) @RequestMapping(value = "/vcSettings", method = RequestMethod.DELETE)
@ResponseStatus(value = HttpStatus.OK) @ResponseStatus(value = HttpStatus.OK)
public void deleteVersionControlSettings() throws ThingsboardException { public DeferredResult<Void> deleteVersionControlSettings() throws ThingsboardException {
try { try {
accessControlService.checkPermission(getCurrentUser(), Resource.ADMIN_SETTINGS, Operation.DELETE); accessControlService.checkPermission(getCurrentUser(), Resource.ADMIN_SETTINGS, Operation.DELETE);
versionControlService.deleteVersionControlSettings(getTenantId()); return wrapFuture(versionControlService.deleteVersionControlSettings(getTenantId()));
} catch (Exception e) { } catch (Exception e) {
throw handleException(e); throw handleException(e);
} }
@ -253,13 +255,13 @@ public class AdminController extends BaseController {
notes = "Attempts to check version control access. " + TENANT_AUTHORITY_PARAGRAPH) notes = "Attempts to check version control access. " + TENANT_AUTHORITY_PARAGRAPH)
@PreAuthorize("hasAuthority('TENANT_ADMIN')") @PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/vcSettings/checkAccess", method = RequestMethod.POST) @RequestMapping(value = "/vcSettings/checkAccess", method = RequestMethod.POST)
public void checkVersionControlAccess( public DeferredResult<Void> checkVersionControlAccess(
@ApiParam(value = "A JSON value representing the Entities Version Control Settings.") @ApiParam(value = "A JSON value representing the Entities Version Control Settings.")
@RequestBody EntitiesVersionControlSettings settings) throws ThingsboardException { @RequestBody EntitiesVersionControlSettings settings) throws ThingsboardException {
try { try {
accessControlService.checkPermission(getCurrentUser(), Resource.ADMIN_SETTINGS, Operation.READ); accessControlService.checkPermission(getCurrentUser(), Resource.ADMIN_SETTINGS, Operation.READ);
settings = checkNotNull(settings); settings = checkNotNull(settings);
versionControlService.checkVersionControlAccess(getTenantId(), settings); return wrapFuture(versionControlService.checkVersionControlAccess(getTenantId(), settings));
} catch (Exception e) { } catch (Exception e) {
throw handleException(e); throw handleException(e);
} }

View File

@ -310,31 +310,30 @@ public class DefaultEntitiesVersionControlService implements EntitiesVersionCont
} }
@Override @Override
public EntitiesVersionControlSettings saveVersionControlSettings(TenantId tenantId, EntitiesVersionControlSettings versionControlSettings) { public ListenableFuture<EntitiesVersionControlSettings> saveVersionControlSettings(TenantId tenantId, EntitiesVersionControlSettings versionControlSettings) {
versionControlSettings = this.vcSettingsService.restore(tenantId, versionControlSettings); var restoredSettings = this.vcSettingsService.restore(tenantId, versionControlSettings);
try { try {
//TODO: ashvayka: replace future.get with deferred result. Don't forget to call when tenant is deleted. var future = gitServiceQueue.initRepository(tenantId, restoredSettings);
gitServiceQueue.initRepository(tenantId, versionControlSettings).get(); return Futures.transform(future, f -> vcSettingsService.save(tenantId, restoredSettings), MoreExecutors.directExecutor());
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException("Failed to init repository!", e); throw new RuntimeException("Failed to init repository!", e);
} }
return vcSettingsService.save(tenantId, versionControlSettings);
} }
@Override @Override
public void deleteVersionControlSettings(TenantId tenantId) throws Exception { public ListenableFuture<Void> deleteVersionControlSettings(TenantId tenantId) throws Exception {
if (vcSettingsService.delete(tenantId)) { if (vcSettingsService.delete(tenantId)) {
//TODO: ashvayka: replace future.get with deferred result. Don't forget to call when tenant is deleted. return gitServiceQueue.clearRepository(tenantId);
gitServiceQueue.clearRepository(tenantId).get(); } else {
return Futures.immediateFuture(null);
} }
} }
@Override @Override
public void checkVersionControlAccess(TenantId tenantId, EntitiesVersionControlSettings settings) throws ThingsboardException { public ListenableFuture<Void> checkVersionControlAccess(TenantId tenantId, EntitiesVersionControlSettings settings) throws ThingsboardException {
settings = this.vcSettingsService.restore(tenantId, settings); settings = this.vcSettingsService.restore(tenantId, settings);
try { try {
//TODO: ashvayka: replace future.get with deferred result. return gitServiceQueue.testRepository(tenantId, settings);
gitServiceQueue.testRepository(tenantId, settings).get();
} catch (Exception e) { } catch (Exception e) {
throw new ThingsboardException(String.format("Unable to access repository: %s", getCauseMessage(e)), throw new ThingsboardException(String.format("Unable to access repository: %s", getCauseMessage(e)),
ThingsboardErrorCode.GENERAL); ThingsboardErrorCode.GENERAL);

View File

@ -54,10 +54,10 @@ public interface EntitiesVersionControlService {
EntitiesVersionControlSettings getVersionControlSettings(TenantId tenantId); EntitiesVersionControlSettings getVersionControlSettings(TenantId tenantId);
EntitiesVersionControlSettings saveVersionControlSettings(TenantId tenantId, EntitiesVersionControlSettings versionControlSettings); ListenableFuture<EntitiesVersionControlSettings> saveVersionControlSettings(TenantId tenantId, EntitiesVersionControlSettings versionControlSettings);
void deleteVersionControlSettings(TenantId tenantId) throws Exception; ListenableFuture<Void> deleteVersionControlSettings(TenantId tenantId) throws Exception;
void checkVersionControlAccess(TenantId tenantId, EntitiesVersionControlSettings settings) throws Exception; ListenableFuture<Void> checkVersionControlAccess(TenantId tenantId, EntitiesVersionControlSettings settings) throws Exception;
} }