refactored queue validator

This commit is contained in:
YevhenBondarenko 2022-06-29 10:39:33 +02:00
parent c21a9c1164
commit ac3b133ec0
3 changed files with 30 additions and 32 deletions

View File

@ -17,6 +17,7 @@ package org.thingsboard.server.dao.service;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.thingsboard.server.common.data.BaseData;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.id.TenantId;
@ -36,6 +37,11 @@ public abstract class DataValidator<D extends BaseData<?>> {
private static final Pattern EMAIL_PATTERN =
Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,}$", Pattern.CASE_INSENSITIVE);
private static final Pattern QUEUE_PATTERN = Pattern.compile("^[a-zA-Z0-9_.\\-]+$");
private static final String NAME = "name";
private static final String TOPIC = "topic";
// Returns old instance of the same object that is fetched during validation.
public D validate(D data, Function<D, TenantId> tenantIdFunction) {
try {
@ -134,4 +140,22 @@ public abstract class DataValidator<D extends BaseData<?>> {
}
}
protected static void validateQueueName(String name) {
validateQueueNameOrTopic(name, NAME);
}
protected static void validateQueueTopic(String topic) {
validateQueueNameOrTopic(topic, TOPIC);
}
private static void validateQueueNameOrTopic(String value, String fieldName) {
if (StringUtils.isEmpty(value)) {
throw new DataValidationException(String.format("Queue %s should be specified!", fieldName));
}
if (!QUEUE_PATTERN.matcher(value).matches()) {
throw new DataValidationException(
String.format("Queue %s contains a character other than ASCII alphanumerics, '.', '_' and '-'!", fieldName));
}
}
}

View File

@ -15,7 +15,6 @@
*/
package org.thingsboard.server.dao.service.validator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.thingsboard.server.common.data.TenantProfile;
@ -29,8 +28,6 @@ import org.thingsboard.server.dao.queue.QueueDao;
import org.thingsboard.server.dao.service.DataValidator;
import org.thingsboard.server.dao.tenant.TbTenantProfileCache;
import java.util.regex.Pattern;
@Component
public class QueueValidator extends DataValidator<Queue> {
@ -40,8 +37,6 @@ public class QueueValidator extends DataValidator<Queue> {
@Autowired
private TbTenantProfileCache tenantProfileCache;
private final Pattern queueTopicPattern = Pattern.compile("^[a-zA-Z0-9_.\\-]+$");
@Override
protected void validateCreate(TenantId tenantId, Queue queue) {
if (queueDao.findQueueByTenantIdAndName(tenantId, queue.getName()) != null) {
@ -77,18 +72,9 @@ public class QueueValidator extends DataValidator<Queue> {
}
}
if (StringUtils.isEmpty(queue.getName())) {
throw new DataValidationException("Queue name should be specified!");
}
if (!queueTopicPattern.matcher(queue.getName()).matches()) {
throw new DataValidationException("Queue name contains a character other than ASCII alphanumerics, '.', '_' and '-'!");
}
if (StringUtils.isEmpty(queue.getTopic())) {
throw new DataValidationException("Queue topic should be specified!");
}
if (!queueTopicPattern.matcher(queue.getTopic()).matches()) {
throw new DataValidationException("Queue topic contains a character other than ASCII alphanumerics, '.', '_' and '-'!");
}
validateQueueName(queue.getName());
validateQueueTopic(queue.getTopic());
if (queue.getPollInterval() < 1) {
throw new DataValidationException("Queue poll interval should be more then 0!");
}

View File

@ -34,7 +34,6 @@ import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Pattern;
@Component
public class TenantProfileDataValidator extends DataValidator<TenantProfile> {
@ -46,8 +45,6 @@ public class TenantProfileDataValidator extends DataValidator<TenantProfile> {
@Lazy
private TenantProfileService tenantProfileService;
private final Pattern queueTopicPattern = Pattern.compile("^[a-zA-Z0-9_.\\-]+$");
@Override
protected void validateDataImpl(TenantId tenantId, TenantProfile tenantProfile) {
if (StringUtils.isEmpty(tenantProfile.getName())) {
@ -110,18 +107,9 @@ public class TenantProfileDataValidator extends DataValidator<TenantProfile> {
}
private void validateQueueConfiguration(TenantProfileQueueConfiguration queue) {
if (StringUtils.isEmpty(queue.getName())) {
throw new DataValidationException("Queue name should be specified!");
}
if (!queueTopicPattern.matcher(queue.getName()).matches()) {
throw new DataValidationException("Queue name contains a character other than ASCII alphanumerics, '.', '_' and '-'!");
}
if (StringUtils.isEmpty(queue.getTopic())) {
throw new DataValidationException("Queue topic should be specified!");
}
if (!queueTopicPattern.matcher(queue.getTopic()).matches()) {
throw new DataValidationException("Queue topic contains a character other than ASCII alphanumerics, '.', '_' and '-'!");
}
validateQueueName(queue.getName());
validateQueueTopic(queue.getTopic());
if (queue.getPollInterval() < 1) {
throw new DataValidationException("Queue poll interval should be more then 0!");
}