Browse Source

Refactor BaseController.checkEntityId

pull/13285/head
ViacheslavKlimov 1 year ago
parent
commit
d54e7a0e78
  1. 109
      application/src/main/java/org/thingsboard/server/controller/BaseController.java

109
application/src/main/java/org/thingsboard/server/controller/BaseController.java

@ -385,7 +385,7 @@ public abstract class BaseController {
public void handleControllerException(Exception e, HttpServletResponse response) { public void handleControllerException(Exception e, HttpServletResponse response) {
ThingsboardException thingsboardException = handleException(e); ThingsboardException thingsboardException = handleException(e);
if (thingsboardException.getErrorCode() == ThingsboardErrorCode.GENERAL && thingsboardException.getCause() instanceof Exception if (thingsboardException.getErrorCode() == ThingsboardErrorCode.GENERAL && thingsboardException.getCause() instanceof Exception
&& StringUtils.equals(thingsboardException.getCause().getMessage(), thingsboardException.getMessage())) { && StringUtils.equals(thingsboardException.getCause().getMessage(), thingsboardException.getMessage())) {
e = (Exception) thingsboardException.getCause(); e = (Exception) thingsboardException.getCause();
} else { } else {
e = thingsboardException; e = thingsboardException;
@ -430,7 +430,7 @@ public abstract class BaseController {
if (exception instanceof ThingsboardException) { if (exception instanceof ThingsboardException) {
return (ThingsboardException) exception; return (ThingsboardException) exception;
} else if (exception instanceof IllegalArgumentException || exception instanceof IncorrectParameterException } else if (exception instanceof IllegalArgumentException || exception instanceof IncorrectParameterException
|| exception instanceof DataValidationException || cause instanceof IncorrectParameterException) { || exception instanceof DataValidationException || cause instanceof IncorrectParameterException) {
return new ThingsboardException(exception.getMessage(), ThingsboardErrorCode.BAD_REQUEST_PARAMS); return new ThingsboardException(exception.getMessage(), ThingsboardErrorCode.BAD_REQUEST_PARAMS);
} else if (exception instanceof MessagingException) { } else if (exception instanceof MessagingException) {
return new ThingsboardException("Unable to send mail", ThingsboardErrorCode.GENERAL); return new ThingsboardException("Unable to send mail", ThingsboardErrorCode.GENERAL);
@ -602,88 +602,39 @@ public abstract class BaseController {
} }
} }
protected void checkEntityId(EntityId entityId, Operation operation) throws ThingsboardException { protected HasId<? extends EntityId> checkEntityId(EntityId entityId, Operation operation) throws ThingsboardException {
try { try {
if (entityId == null) { if (entityId == null) {
throw new ThingsboardException("Parameter entityId can't be empty!", ThingsboardErrorCode.BAD_REQUEST_PARAMS); throw new ThingsboardException("Parameter entityId can't be empty!", ThingsboardErrorCode.BAD_REQUEST_PARAMS);
} }
validateId(entityId.getId(), id -> "Incorrect entityId " + id); validateId(entityId.getId(), id -> "Incorrect entityId " + id);
switch (entityId.getEntityType()) { return switch (entityId.getEntityType()) {
case ALARM: case ALARM -> checkAlarmId(new AlarmId(entityId.getId()), operation);
checkAlarmId(new AlarmId(entityId.getId()), operation); case DEVICE -> checkDeviceId(new DeviceId(entityId.getId()), operation);
return; case DEVICE_PROFILE -> checkDeviceProfileId(new DeviceProfileId(entityId.getId()), operation);
case DEVICE: case CUSTOMER -> checkCustomerId(new CustomerId(entityId.getId()), operation);
checkDeviceId(new DeviceId(entityId.getId()), operation); case TENANT -> checkTenantId(TenantId.fromUUID(entityId.getId()), operation);
return; case TENANT_PROFILE -> checkTenantProfileId(new TenantProfileId(entityId.getId()), operation);
case DEVICE_PROFILE: case RULE_CHAIN -> checkRuleChain(new RuleChainId(entityId.getId()), operation);
checkDeviceProfileId(new DeviceProfileId(entityId.getId()), operation); case RULE_NODE -> checkRuleNode(new RuleNodeId(entityId.getId()), operation);
return; case ASSET -> checkAssetId(new AssetId(entityId.getId()), operation);
case CUSTOMER: case ASSET_PROFILE -> checkAssetProfileId(new AssetProfileId(entityId.getId()), operation);
checkCustomerId(new CustomerId(entityId.getId()), operation); case DASHBOARD -> checkDashboardId(new DashboardId(entityId.getId()), operation);
return; case USER -> checkUserId(new UserId(entityId.getId()), operation);
case TENANT: case ENTITY_VIEW -> checkEntityViewId(new EntityViewId(entityId.getId()), operation);
checkTenantId(TenantId.fromUUID(entityId.getId()), operation); case EDGE -> checkEdgeId(new EdgeId(entityId.getId()), operation);
return; case WIDGETS_BUNDLE -> checkWidgetsBundleId(new WidgetsBundleId(entityId.getId()), operation);
case TENANT_PROFILE: case WIDGET_TYPE -> checkWidgetTypeId(new WidgetTypeId(entityId.getId()), operation);
checkTenantProfileId(new TenantProfileId(entityId.getId()), operation); case TB_RESOURCE -> checkResourceInfoId(new TbResourceId(entityId.getId()), operation);
return; case OTA_PACKAGE -> checkOtaPackageId(new OtaPackageId(entityId.getId()), operation);
case RULE_CHAIN: case QUEUE -> checkQueueId(new QueueId(entityId.getId()), operation);
checkRuleChain(new RuleChainId(entityId.getId()), operation); case OAUTH2_CLIENT -> checkOauth2ClientId(new OAuth2ClientId(entityId.getId()), operation);
return; case DOMAIN -> checkDomainId(new DomainId(entityId.getId()), operation);
case RULE_NODE: case MOBILE_APP -> checkMobileAppId(new MobileAppId(entityId.getId()), operation);
checkRuleNode(new RuleNodeId(entityId.getId()), operation); case MOBILE_APP_BUNDLE -> checkMobileAppBundleId(new MobileAppBundleId(entityId.getId()), operation);
return; case CALCULATED_FIELD -> checkCalculatedFieldId(new CalculatedFieldId(entityId.getId()), operation);
case ASSET: default -> (HasId<? extends EntityId>) checkEntityId(entityId, entitiesService::findEntityByTenantIdAndId, operation);
checkAssetId(new AssetId(entityId.getId()), operation); };
return;
case ASSET_PROFILE:
checkAssetProfileId(new AssetProfileId(entityId.getId()), operation);
return;
case DASHBOARD:
checkDashboardId(new DashboardId(entityId.getId()), operation);
return;
case USER:
checkUserId(new UserId(entityId.getId()), operation);
return;
case ENTITY_VIEW:
checkEntityViewId(new EntityViewId(entityId.getId()), operation);
return;
case EDGE:
checkEdgeId(new EdgeId(entityId.getId()), operation);
return;
case WIDGETS_BUNDLE:
checkWidgetsBundleId(new WidgetsBundleId(entityId.getId()), operation);
return;
case WIDGET_TYPE:
checkWidgetTypeId(new WidgetTypeId(entityId.getId()), operation);
return;
case TB_RESOURCE:
checkResourceInfoId(new TbResourceId(entityId.getId()), operation);
return;
case OTA_PACKAGE:
checkOtaPackageId(new OtaPackageId(entityId.getId()), operation);
return;
case QUEUE:
checkQueueId(new QueueId(entityId.getId()), operation);
return;
case OAUTH2_CLIENT:
checkOauth2ClientId(new OAuth2ClientId(entityId.getId()), operation);
return;
case DOMAIN:
checkDomainId(new DomainId(entityId.getId()), operation);
return;
case MOBILE_APP:
checkMobileAppId(new MobileAppId(entityId.getId()), operation);
return;
case MOBILE_APP_BUNDLE:
checkMobileAppBundleId(new MobileAppBundleId(entityId.getId()), operation);
return;
case CALCULATED_FIELD:
checkCalculatedFieldId(new CalculatedFieldId(entityId.getId()), operation);
return;
default:
checkEntityId(entityId, entitiesService::findEntityByTenantIdAndId, operation);
}
} catch (Exception e) { } catch (Exception e) {
throw handleException(e, false); throw handleException(e, false);
} }

Loading…
Cancel
Save