Browse Source

Refactoring ApiKeyService - merge save methods

pull/15167/head
Volodymyr Babak 5 months ago
parent
commit
552bf59680
  1. 2
      application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/apikey/BaseApiKeyProcessor.java
  2. 2
      common/dao-api/src/main/java/org/thingsboard/server/dao/pat/ApiKeyService.java
  3. 34
      dao/src/main/java/org/thingsboard/server/dao/pat/ApiKeyServiceImpl.java

2
application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/apikey/BaseApiKeyProcessor.java

@ -44,7 +44,7 @@ public abstract class BaseApiKeyProcessor extends BaseEdgeProcessor {
}
apiKey.setId(apiKeyId);
edgeCtx.getApiKeyService().saveApiKey(tenantId, apiKey);
edgeCtx.getApiKeyService().saveApiKey(tenantId, apiKey, apiKey.getValue(), false);
} catch (Exception e) {
log.error("[{}] Failed to process apiKey update msg [{}]", tenantId, apiKeyUpdateMsg, e);
throw e;

2
common/dao-api/src/main/java/org/thingsboard/server/dao/pat/ApiKeyService.java

@ -30,7 +30,7 @@ public interface ApiKeyService extends EntityDaoService {
ApiKey saveApiKey(TenantId tenantId, ApiKeyInfo apiKey);
ApiKey saveApiKey(TenantId tenantId, ApiKey apiKey);
ApiKey saveApiKey(TenantId tenantId, ApiKeyInfo apiKeyInfo, String value, boolean doValidate);
void deleteApiKey(TenantId tenantId, ApiKey apiKey, boolean force);

34
dao/src/main/java/org/thingsboard/server/dao/pat/ApiKeyServiceImpl.java

@ -76,13 +76,20 @@ public class ApiKeyServiceImpl extends AbstractCachedEntityService<ApiKeyCacheKe
@Override
public ApiKey saveApiKey(TenantId tenantId, ApiKeyInfo apiKeyInfo) {
return saveApiKey(tenantId, apiKeyInfo, null, true);
}
@Override
public ApiKey saveApiKey(TenantId tenantId, ApiKeyInfo apiKeyInfo, String value, boolean doValidate) {
log.trace("Executing saveApiKey [{}]", apiKeyInfo);
try {
var apiKey = new ApiKey(apiKeyInfo);
var old = apiKeyValidator.validate(apiKey, ApiKeyInfo::getTenantId);
if (old == null) {
String value = generateApiKeySecret();
ApiKey old = doValidate ? apiKeyValidator.validate(apiKey, ApiKeyInfo::getTenantId) :
(apiKey.getId() != null ? apiKeyDao.findById(tenantId, apiKey.getUuidId()) : null);
if (value != null) {
apiKey.setValue(value);
} else if (old == null) {
apiKey.setValue(generateApiKeySecret());
} else {
apiKey.setValue(old.getValue());
}
@ -98,27 +105,6 @@ public class ApiKeyServiceImpl extends AbstractCachedEntityService<ApiKeyCacheKe
}
}
@Override
public ApiKey saveApiKey(TenantId tenantId, ApiKey apiKey) {
log.trace("Executing saveApiKey with value [{}]", apiKey);
try {
ApiKey old = apiKey.getId() != null ? apiKeyDao.findById(tenantId, apiKey.getUuidId()) : null;
if (old != null && apiKey.getValue() == null) {
apiKey.setValue(old.getValue());
}
var savedApiKey = apiKeyDao.save(tenantId, apiKey);
eventPublisher.publishEvent(SaveEntityEvent.builder().tenantId(tenantId).entityId(savedApiKey.getId())
.entity(savedApiKey).created(old == null).build());
if (old != null && old.isEnabled() != apiKey.isEnabled()) {
publishEvictEvent(new ApiKeyEvictEvent(apiKey.getValue()));
}
return savedApiKey;
} catch (Exception e) {
checkConstraintViolation(e, "api_key_value_unq_key", "API Key with such value already exists!");
throw e;
}
}
@Override
public ApiKey findApiKeyById(TenantId tenantId, ApiKeyId apiKeyId) {
log.trace("Executing findApiKeyById [{}] [{}]", tenantId, apiKeyId);

Loading…
Cancel
Save