Verify that the target controller id cannot be empty

Signed-off-by: SirWayne <dennis.melzer@bosch-si.com>
This commit is contained in:
SirWayne
2016-02-10 16:22:20 +01:00
parent ed5e985055
commit f78bd68c03
4 changed files with 45 additions and 11 deletions

View File

@@ -15,7 +15,6 @@ import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.validation.constraints.NotNull;
@@ -212,14 +211,9 @@ public class ControllerManagement implements EnvironmentAware {
@Modifying
@Transactional
@PreAuthorize(SpringEvalExpressions.IS_CONTROLLER)
public Target findOrRegisterTargetIfItDoesNotexist(@NotNull final String targetid, final URI address) {
final Specification<Target> spec = new Specification<Target>() {
@Override
public Predicate toPredicate(final Root<Target> targetRoot, final CriteriaQuery<?> query,
final CriteriaBuilder cb) {
return cb.equal(targetRoot.get(Target_.controllerId), targetid);
}
};
public Target findOrRegisterTargetIfItDoesNotexist(@NotEmpty final String targetid, final URI address) {
final Specification<Target> spec = (targetRoot, query, cb) -> cb.equal(targetRoot.get(Target_.controllerId),
targetid);
Target target = targetRepository.findOne(spec);
@@ -229,9 +223,9 @@ public class ControllerManagement implements EnvironmentAware {
target.setName(targetid);
return targetManagement.createTarget(target, TargetUpdateStatus.REGISTERED, System.currentTimeMillis(),
address);
} else {
return updateLastTargetQuery(target.getTargetInfo(), address).getTarget();
}
return updateLastTargetQuery(target.getTargetInfo(), address).getTarget();
}
/**

View File

@@ -27,8 +27,10 @@ import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Order;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.validation.ConstraintViolationException;
import javax.validation.constraints.NotNull;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.hawkbit.Constants;
import org.eclipse.hawkbit.eventbus.event.TargetTagAssigmentResultEvent;
import org.eclipse.hawkbit.executor.AfterTransactionCommitExecutor;
@@ -931,6 +933,11 @@ public class TargetManagement {
public Target createTarget(@NotNull final Target target, @NotNull final TargetUpdateStatus status,
final Long lastTargetQuery, final URI address) {
if (StringUtils.isEmpty(target.getControllerId())) {
throw new ConstraintViolationException("Empty string for controller id not allowed",
Collections.emptySet());
}
if (targetRepository.findByControllerId(target.getControllerId()) != null) {
throw new EntityAlreadyExistsException(target.getControllerId());
}