Search in sources :

Example 1 with ConstraintValidator

use of org.thingsboard.server.dao.service.ConstraintValidator in project thingsboard by thingsboard.

the class TelemetryController method saveAttributes.

private DeferredResult<ResponseEntity> saveAttributes(TenantId srcTenantId, EntityId entityIdSrc, String scope, JsonNode json) throws ThingsboardException {
    if (!DataConstants.SERVER_SCOPE.equals(scope) && !DataConstants.SHARED_SCOPE.equals(scope)) {
        return getImmediateDeferredResult("Invalid scope: " + scope, HttpStatus.BAD_REQUEST);
    }
    if (json.isObject()) {
        List<AttributeKvEntry> attributes = extractRequestAttributes(json);
        attributes.forEach(ConstraintValidator::validateFields);
        if (attributes.isEmpty()) {
            return getImmediateDeferredResult("No attributes data found in request body!", HttpStatus.BAD_REQUEST);
        }
        for (AttributeKvEntry attributeKvEntry : attributes) {
            if (attributeKvEntry.getKey().isEmpty() || attributeKvEntry.getKey().trim().length() == 0) {
                return getImmediateDeferredResult("Key cannot be empty or contains only spaces", HttpStatus.BAD_REQUEST);
            }
        }
        SecurityUser user = getCurrentUser();
        return accessValidator.validateEntityAndCallback(getCurrentUser(), Operation.WRITE_ATTRIBUTES, entityIdSrc, (result, tenantId, entityId) -> {
            tsSubService.saveAndNotify(tenantId, entityId, scope, attributes, new FutureCallback<Void>() {

                @Override
                public void onSuccess(@Nullable Void tmp) {
                    logAttributesUpdated(user, entityId, scope, attributes, null);
                    result.setResult(new ResponseEntity(HttpStatus.OK));
                }

                @Override
                public void onFailure(Throwable t) {
                    logAttributesUpdated(user, entityId, scope, attributes, t);
                    AccessValidator.handleError(t, result, HttpStatus.INTERNAL_SERVER_ERROR);
                }
            });
        });
    } else {
        return getImmediateDeferredResult("Request is not a JSON object", HttpStatus.BAD_REQUEST);
    }
}
Also used : ConstraintValidator(org.thingsboard.server.dao.service.ConstraintValidator) ResponseEntity(org.springframework.http.ResponseEntity) SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry)

Aggregations

ResponseEntity (org.springframework.http.ResponseEntity)1 AttributeKvEntry (org.thingsboard.server.common.data.kv.AttributeKvEntry)1 BaseAttributeKvEntry (org.thingsboard.server.common.data.kv.BaseAttributeKvEntry)1 ConstraintValidator (org.thingsboard.server.dao.service.ConstraintValidator)1 SecurityUser (org.thingsboard.server.service.security.model.SecurityUser)1