Improvements repository validation constraints (#626)
* Add html tag Validator on strings. Add string trim. Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com> * Revert unintended changes. Sonar issues. Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com> * Remove weired comment. Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com> * Raise EclipseLink due to validation problem. Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com> * Fix permission. Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com> * Colour field test. Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com> * Fix sonar issues. Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com>
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* Copyright (c) 2015 Bosch Software Innovations GmbH and others.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*/
|
||||
package org.eclipse.hawkbit.repository;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import javax.validation.Constraint;
|
||||
import javax.validation.Payload;
|
||||
|
||||
import org.hibernate.validator.constraints.SafeHtml;
|
||||
import org.hibernate.validator.constraints.SafeHtml.WhiteListType;
|
||||
|
||||
/**
|
||||
* Constraint for strings submitted into the repository.
|
||||
*
|
||||
*/
|
||||
@Constraint(validatedBy = {})
|
||||
@Target({ ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR,
|
||||
ElementType.PARAMETER, ElementType.TYPE_USE })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@SafeHtml(whitelistType = WhiteListType.NONE)
|
||||
public @interface ValidString {
|
||||
|
||||
String message() default "Invalid characters in string";
|
||||
|
||||
Class<?>[] groups() default {};
|
||||
|
||||
Class<? extends Payload>[] payload() default {};
|
||||
|
||||
}
|
||||
@@ -10,9 +10,13 @@ package org.eclipse.hawkbit.repository.builder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.eclipse.hawkbit.repository.ValidString;
|
||||
import org.eclipse.hawkbit.repository.model.Action.Status;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Create and update builder DTO.
|
||||
@@ -23,7 +27,9 @@ import org.eclipse.hawkbit.repository.model.Action.Status;
|
||||
public abstract class AbstractActionStatusCreate<T> {
|
||||
protected Status status;
|
||||
protected Long occurredAt;
|
||||
protected Collection<String> messages;
|
||||
|
||||
protected List<@ValidString String> messages;
|
||||
|
||||
protected Long actionId;
|
||||
|
||||
public Long getActionId() {
|
||||
@@ -36,7 +42,7 @@ public abstract class AbstractActionStatusCreate<T> {
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
public T occurredAt(final Long occurredAt) {
|
||||
public T occurredAt(final long occurredAt) {
|
||||
this.occurredAt = occurredAt;
|
||||
|
||||
return (T) this;
|
||||
@@ -44,9 +50,9 @@ public abstract class AbstractActionStatusCreate<T> {
|
||||
|
||||
public T messages(final Collection<String> messages) {
|
||||
if (this.messages == null) {
|
||||
this.messages = messages;
|
||||
this.messages = messages.stream().map(StringUtils::trimWhitespace).collect(Collectors.toList());
|
||||
} else {
|
||||
this.messages.addAll(messages);
|
||||
this.messages.addAll(messages.stream().map(StringUtils::trimWhitespace).collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
return (T) this;
|
||||
@@ -56,7 +62,7 @@ public abstract class AbstractActionStatusCreate<T> {
|
||||
if (this.messages == null) {
|
||||
this.messages = new ArrayList<>();
|
||||
}
|
||||
this.messages.add(message);
|
||||
this.messages.add(StringUtils.trimWhitespace(message));
|
||||
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
@@ -11,7 +11,9 @@ package org.eclipse.hawkbit.repository.builder;
|
||||
import java.util.Collection;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.eclipse.hawkbit.repository.ValidString;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Create and update builder DTO.
|
||||
@@ -20,8 +22,11 @@ import org.springframework.util.CollectionUtils;
|
||||
* update or create builder interface
|
||||
*/
|
||||
public abstract class AbstractDistributionSetTypeUpdateCreate<T> extends AbstractNamedEntityBuilder<T> {
|
||||
@ValidString
|
||||
protected String colour;
|
||||
@ValidString
|
||||
protected String key;
|
||||
|
||||
protected Collection<Long> mandatory;
|
||||
protected Collection<Long> optional;
|
||||
|
||||
@@ -52,7 +57,7 @@ public abstract class AbstractDistributionSetTypeUpdateCreate<T> extends Abstrac
|
||||
}
|
||||
|
||||
public T colour(final String colour) {
|
||||
this.colour = colour;
|
||||
this.colour = StringUtils.trimWhitespace(colour);
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
@@ -61,7 +66,7 @@ public abstract class AbstractDistributionSetTypeUpdateCreate<T> extends Abstrac
|
||||
}
|
||||
|
||||
public T key(final String key) {
|
||||
this.key = key;
|
||||
this.key = StringUtils.trimWhitespace(key);
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,9 @@ package org.eclipse.hawkbit.repository.builder;
|
||||
import java.util.Collection;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.eclipse.hawkbit.repository.ValidString;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Create and update builder DTO.
|
||||
*
|
||||
@@ -18,8 +21,11 @@ import java.util.Optional;
|
||||
* update or create builder interface
|
||||
*/
|
||||
public abstract class AbstractDistributionSetUpdateCreate<T> extends AbstractNamedEntityBuilder<T> {
|
||||
@ValidString
|
||||
protected String version;
|
||||
protected Boolean requiredMigrationStep;
|
||||
|
||||
@ValidString
|
||||
protected String type;
|
||||
protected Collection<Long> modules;
|
||||
|
||||
@@ -33,7 +39,7 @@ public abstract class AbstractDistributionSetUpdateCreate<T> extends AbstractNam
|
||||
}
|
||||
|
||||
public T type(final String type) {
|
||||
this.type = type;
|
||||
this.type = StringUtils.trimWhitespace(type);
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
@@ -51,7 +57,7 @@ public abstract class AbstractDistributionSetUpdateCreate<T> extends AbstractNam
|
||||
}
|
||||
|
||||
public T version(final String version) {
|
||||
this.version = version;
|
||||
this.version = StringUtils.trimWhitespace(version);
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,9 @@ package org.eclipse.hawkbit.repository.builder;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.eclipse.hawkbit.repository.ValidString;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Create and update builder DTO.
|
||||
*
|
||||
@@ -17,11 +20,14 @@ import java.util.Optional;
|
||||
* update or create builder interface
|
||||
*/
|
||||
public abstract class AbstractMetadataUpdateCreate<T> {
|
||||
@ValidString
|
||||
protected String key;
|
||||
|
||||
@ValidString
|
||||
protected String value;
|
||||
|
||||
public T key(final String key) {
|
||||
this.key = key;
|
||||
this.key = StringUtils.trimWhitespace(key);
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
@@ -30,7 +36,7 @@ public abstract class AbstractMetadataUpdateCreate<T> {
|
||||
}
|
||||
|
||||
public T value(final String value) {
|
||||
this.value = value;
|
||||
this.value = StringUtils.trimWhitespace(value);
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
|
||||
@@ -10,18 +10,24 @@ package org.eclipse.hawkbit.repository.builder;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.eclipse.hawkbit.repository.ValidString;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
public abstract class AbstractNamedEntityBuilder<T> extends AbstractBaseEntityBuilder {
|
||||
|
||||
@ValidString
|
||||
protected String name;
|
||||
|
||||
@ValidString
|
||||
protected String description;
|
||||
|
||||
public T name(final String name) {
|
||||
this.name = name;
|
||||
this.name = StringUtils.trimWhitespace(name);
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
public T description(final String description) {
|
||||
this.description = description;
|
||||
this.description = StringUtils.trimWhitespace(description);
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,9 @@
|
||||
*/
|
||||
package org.eclipse.hawkbit.repository.builder;
|
||||
|
||||
import org.eclipse.hawkbit.repository.ValidString;
|
||||
import org.eclipse.hawkbit.repository.model.RolloutGroupConditions;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Create builder DTO.
|
||||
@@ -17,12 +19,13 @@ import org.eclipse.hawkbit.repository.model.RolloutGroupConditions;
|
||||
* update or create builder interface
|
||||
*/
|
||||
public abstract class AbstractRolloutGroupCreate<T> extends AbstractNamedEntityBuilder<T> {
|
||||
@ValidString
|
||||
protected String targetFilterQuery;
|
||||
protected Float targetPercentage;
|
||||
protected RolloutGroupConditions conditions;
|
||||
|
||||
public T targetFilterQuery(final String targetFilterQuery) {
|
||||
this.targetFilterQuery = targetFilterQuery;
|
||||
this.targetFilterQuery = StringUtils.trimWhitespace(targetFilterQuery);
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,9 @@ package org.eclipse.hawkbit.repository.builder;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.eclipse.hawkbit.repository.ValidString;
|
||||
import org.eclipse.hawkbit.repository.model.Action.ActionType;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Create and update builder DTO.
|
||||
@@ -20,7 +22,10 @@ import org.eclipse.hawkbit.repository.model.Action.ActionType;
|
||||
*/
|
||||
public abstract class AbstractRolloutUpdateCreate<T> extends AbstractNamedEntityBuilder<T> {
|
||||
protected Long set;
|
||||
|
||||
@ValidString
|
||||
protected String targetFilterQuery;
|
||||
|
||||
protected ActionType actionType;
|
||||
protected Long forcedTime;
|
||||
protected Long startAt;
|
||||
@@ -31,7 +36,7 @@ public abstract class AbstractRolloutUpdateCreate<T> extends AbstractNamedEntity
|
||||
}
|
||||
|
||||
public T targetFilterQuery(final String targetFilterQuery) {
|
||||
this.targetFilterQuery = targetFilterQuery;
|
||||
this.targetFilterQuery = StringUtils.trimWhitespace(targetFilterQuery);
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,9 @@ package org.eclipse.hawkbit.repository.builder;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.eclipse.hawkbit.repository.ValidString;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Create and update builder DTO.
|
||||
*
|
||||
@@ -17,8 +20,11 @@ import java.util.Optional;
|
||||
* update or create builder interface
|
||||
*/
|
||||
public abstract class AbstractSoftwareModuleTypeUpdateCreate<T> extends AbstractNamedEntityBuilder<T> {
|
||||
@ValidString
|
||||
protected String colour;
|
||||
@ValidString
|
||||
protected String key;
|
||||
|
||||
protected int maxAssignments = 1;
|
||||
|
||||
public T maxAssignments(final int maxAssignments) {
|
||||
@@ -31,7 +37,7 @@ public abstract class AbstractSoftwareModuleTypeUpdateCreate<T> extends Abstract
|
||||
}
|
||||
|
||||
public T colour(final String colour) {
|
||||
this.colour = colour;
|
||||
this.colour = StringUtils.trimWhitespace(colour);
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
@@ -40,7 +46,7 @@ public abstract class AbstractSoftwareModuleTypeUpdateCreate<T> extends Abstract
|
||||
}
|
||||
|
||||
public T key(final String key) {
|
||||
this.key = key;
|
||||
this.key = StringUtils.trimWhitespace(key);
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,9 @@ package org.eclipse.hawkbit.repository.builder;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.eclipse.hawkbit.repository.ValidString;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Create and update builder DTO.
|
||||
*
|
||||
@@ -17,12 +20,17 @@ import java.util.Optional;
|
||||
* update or create builder interface
|
||||
*/
|
||||
public abstract class AbstractSoftwareModuleUpdateCreate<T> extends AbstractNamedEntityBuilder<T> {
|
||||
@ValidString
|
||||
protected String version;
|
||||
|
||||
@ValidString
|
||||
protected String vendor;
|
||||
|
||||
@ValidString
|
||||
protected String type;
|
||||
|
||||
public T type(final String type) {
|
||||
this.type = type;
|
||||
this.type = StringUtils.trimWhitespace(type);
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
@@ -31,7 +39,7 @@ public abstract class AbstractSoftwareModuleUpdateCreate<T> extends AbstractName
|
||||
}
|
||||
|
||||
public T vendor(final String vendor) {
|
||||
this.vendor = vendor;
|
||||
this.vendor = StringUtils.trimWhitespace(vendor);
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
@@ -40,7 +48,7 @@ public abstract class AbstractSoftwareModuleUpdateCreate<T> extends AbstractName
|
||||
}
|
||||
|
||||
public T version(final String version) {
|
||||
this.version = version;
|
||||
this.version = StringUtils.trimWhitespace(version);
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,9 @@ package org.eclipse.hawkbit.repository.builder;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.eclipse.hawkbit.repository.ValidString;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Create and update builder DTO.
|
||||
*
|
||||
@@ -17,10 +20,11 @@ import java.util.Optional;
|
||||
* update or create builder interface
|
||||
*/
|
||||
public class AbstractTagUpdateCreate<T> extends AbstractNamedEntityBuilder<T> {
|
||||
@ValidString
|
||||
protected String colour;
|
||||
|
||||
public T colour(final String colour) {
|
||||
this.colour = colour;
|
||||
this.colour = StringUtils.trimWhitespace(colour);
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,9 @@ package org.eclipse.hawkbit.repository.builder;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.eclipse.hawkbit.repository.ValidString;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Create and update builder DTO.
|
||||
*
|
||||
@@ -17,8 +20,12 @@ import java.util.Optional;
|
||||
* update or create builder interface
|
||||
*/
|
||||
public abstract class AbstractTargetFilterQueryUpdateCreate<T> extends AbstractBaseEntityBuilder {
|
||||
@ValidString
|
||||
protected String name;
|
||||
|
||||
@ValidString
|
||||
protected String query;
|
||||
|
||||
protected Long set;
|
||||
|
||||
public T set(final long set) {
|
||||
@@ -31,7 +38,7 @@ public abstract class AbstractTargetFilterQueryUpdateCreate<T> extends AbstractB
|
||||
}
|
||||
|
||||
public T name(final String name) {
|
||||
this.name = name;
|
||||
this.name = StringUtils.trimWhitespace(name);
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
@@ -40,7 +47,7 @@ public abstract class AbstractTargetFilterQueryUpdateCreate<T> extends AbstractB
|
||||
}
|
||||
|
||||
public T query(final String query) {
|
||||
this.query = query;
|
||||
this.query = StringUtils.trimWhitespace(query);
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
|
||||
@@ -11,8 +11,10 @@ package org.eclipse.hawkbit.repository.builder;
|
||||
import java.net.URI;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.eclipse.hawkbit.repository.ValidString;
|
||||
import org.eclipse.hawkbit.repository.exception.InvalidTargetAddressException;
|
||||
import org.eclipse.hawkbit.repository.model.TargetUpdateStatus;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Create and update builder DTO.
|
||||
@@ -21,14 +23,19 @@ import org.eclipse.hawkbit.repository.model.TargetUpdateStatus;
|
||||
* update or create builder interface
|
||||
*/
|
||||
public class AbstractTargetUpdateCreate<T> extends AbstractNamedEntityBuilder<T> {
|
||||
@ValidString
|
||||
protected String controllerId;
|
||||
|
||||
protected String address;
|
||||
|
||||
@ValidString
|
||||
protected String securityToken;
|
||||
|
||||
protected Long lastTargetQuery;
|
||||
protected TargetUpdateStatus status;
|
||||
|
||||
protected AbstractTargetUpdateCreate(final String controllerId) {
|
||||
this.controllerId = controllerId;
|
||||
this.controllerId = StringUtils.trimWhitespace(controllerId);
|
||||
}
|
||||
|
||||
public T status(final TargetUpdateStatus status) {
|
||||
@@ -40,7 +47,7 @@ public class AbstractTargetUpdateCreate<T> extends AbstractNamedEntityBuilder<T>
|
||||
// check if this is a real URI
|
||||
if (address != null) {
|
||||
try {
|
||||
URI.create(address);
|
||||
URI.create(StringUtils.trimWhitespace(address));
|
||||
} catch (final IllegalArgumentException e) {
|
||||
throw new InvalidTargetAddressException(
|
||||
"The given address " + address + " violates the RFC-2396 specification", e);
|
||||
@@ -51,7 +58,7 @@ public class AbstractTargetUpdateCreate<T> extends AbstractNamedEntityBuilder<T>
|
||||
}
|
||||
|
||||
public T securityToken(final String securityToken) {
|
||||
this.securityToken = securityToken;
|
||||
this.securityToken = StringUtils.trimWhitespace(securityToken);
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
@@ -61,7 +68,7 @@ public class AbstractTargetUpdateCreate<T> extends AbstractNamedEntityBuilder<T>
|
||||
}
|
||||
|
||||
public TargetCreate controllerId(final String controllerId) {
|
||||
this.controllerId = controllerId;
|
||||
this.controllerId = StringUtils.trimWhitespace(controllerId);
|
||||
return (TargetCreate) this;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user