|
|
|
@ -15,16 +15,24 @@ |
|
|
|
*/ |
|
|
|
package org.thingsboard.server.dao.service; |
|
|
|
|
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.thingsboard.common.util.RegexUtils; |
|
|
|
import org.thingsboard.server.common.data.id.EntityId; |
|
|
|
import org.thingsboard.server.common.data.id.UUIDBased; |
|
|
|
import org.thingsboard.server.common.data.page.PageLink; |
|
|
|
import org.thingsboard.server.common.data.query.EntityDataPageLink; |
|
|
|
import org.thingsboard.server.common.data.query.EntityKey; |
|
|
|
import org.thingsboard.server.common.data.query.EntityKeyType; |
|
|
|
import org.thingsboard.server.dao.exception.IncorrectParameterException; |
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
import java.util.UUID; |
|
|
|
import java.util.regex.Pattern; |
|
|
|
|
|
|
|
public class Validator { |
|
|
|
|
|
|
|
public static final Pattern PROPERTY_PATTERN = Pattern.compile("^[\\p{L}0-9_-]+$"); // Unicode letters, numbers, '_' and '-' allowed
|
|
|
|
|
|
|
|
/** |
|
|
|
* This method validate <code>EntityId</code> entity id. If entity id is invalid than throw |
|
|
|
* <code>IncorrectParameterException</code> exception |
|
|
|
@ -122,7 +130,31 @@ public class Validator { |
|
|
|
throw new IncorrectParameterException("Incorrect page link page size '"+pageLink.getPageSize()+"'. Page size must be greater than zero."); |
|
|
|
} else if (pageLink.getPage() < 0) { |
|
|
|
throw new IncorrectParameterException("Incorrect page link page '"+pageLink.getPage()+"'. Page must be positive integer."); |
|
|
|
} else if (pageLink.getSortOrder() != null) { |
|
|
|
if (!isValidProperty(pageLink.getSortOrder().getProperty())) { |
|
|
|
throw new IncorrectParameterException("Invalid page link sort property"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public static void validateEntityDataPageLink(EntityDataPageLink pageLink) { |
|
|
|
if (pageLink == null) { |
|
|
|
throw new IncorrectParameterException("Entity Data Page link must be specified."); |
|
|
|
} else if (pageLink.getPageSize() < 1) { |
|
|
|
throw new IncorrectParameterException("Incorrect entity data page link page size '" + pageLink.getPageSize() + "'. Page size must be greater than zero."); |
|
|
|
} else if (pageLink.getPage() < 0) { |
|
|
|
throw new IncorrectParameterException("Incorrect entity data page link page '" + pageLink.getPage() + "'. Page must be positive integer."); |
|
|
|
} else if (pageLink.getSortOrder() != null && pageLink.getSortOrder().getKey() != null) { |
|
|
|
EntityKey sortKey = pageLink.getSortOrder().getKey(); |
|
|
|
if ((sortKey.getType() == EntityKeyType.ENTITY_FIELD || sortKey.getType() == EntityKeyType.ALARM_FIELD) |
|
|
|
&& !isValidProperty(sortKey.getKey())) { |
|
|
|
throw new IncorrectParameterException("Invalid entity data page link sort property"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public static boolean isValidProperty(String key) { |
|
|
|
return StringUtils.isEmpty(key) || RegexUtils.matches(key, PROPERTY_PATTERN); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|