Added generic validator fucntion to TenantConfigurationKey
Signed-off-by: Nonnenmacher Fabian <fabian.nonnenmacher@bosch-si.com>
This commit is contained in:
committed by
Nonnenmacher Fabian
parent
3f2c0d134a
commit
b56477191a
@@ -1,4 +1,4 @@
|
||||
package org.eclipse.hawkbit.repository.model.helper;
|
||||
package org.eclipse.hawkbit.tenancy.configuration;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.LocalTime;
|
||||
@@ -36,7 +36,7 @@ public class DurationHelper {
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a formatted Sting into a Duration object.
|
||||
* Converts a formatted String into a Duration object.
|
||||
*
|
||||
* @param formattedDuration
|
||||
* String in {@link #DURATION_FORMAT}
|
||||
@@ -67,4 +67,22 @@ public class DurationHelper {
|
||||
public Duration getDurationByTimeValues(final long hours, final long minutes, final long seconds) {
|
||||
return Duration.ofHours(hours).plusMinutes(minutes).plusSeconds(seconds);
|
||||
}
|
||||
|
||||
public DurationRangeValidator durationRangeValidator(final Duration min, final Duration max) {
|
||||
return new DurationRangeValidator(min, max);
|
||||
}
|
||||
|
||||
public static class DurationRangeValidator {
|
||||
final Duration min;
|
||||
final Duration max;
|
||||
|
||||
private DurationRangeValidator(final Duration min, final Duration max) {
|
||||
this.min = min;
|
||||
this.max = max;
|
||||
}
|
||||
|
||||
public boolean isWithinRange(final Duration duration) {
|
||||
return duration.compareTo(min) > 0 && duration.compareTo(max) < 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,12 @@
|
||||
*/
|
||||
package org.eclipse.hawkbit.tenancy.configuration;
|
||||
|
||||
import org.eclipse.hawkbit.tenancy.configuration.validator.BooleanValidator;
|
||||
import org.eclipse.hawkbit.tenancy.configuration.validator.PollTimeValidator;
|
||||
import org.eclipse.hawkbit.tenancy.configuration.validator.StringValidator;
|
||||
import org.eclipse.hawkbit.tenancy.configuration.validator.TenantConfigurationValidator;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
/**
|
||||
* An enum which defines the tenant specific configurations which can be
|
||||
* configured for each tenant seperately.
|
||||
@@ -22,54 +28,59 @@ public enum TenantConfigurationKey {
|
||||
* boolean value {@code true} {@code false}.
|
||||
*/
|
||||
AUTHENTICATION_MODE_HEADER_ENABLED("authentication.header.enabled",
|
||||
"hawkbit.server.controller.security.authentication.header.enabled", Boolean.class,
|
||||
Boolean.FALSE.toString()),
|
||||
"hawkbit.server.controller.security.authentication.header.enabled", Boolean.class, Boolean.FALSE.toString(),
|
||||
BooleanValidator.class),
|
||||
/**
|
||||
*
|
||||
*/
|
||||
AUTHENTICATION_MODE_HEADER_AUTHORITY_NAME("authentication.header.authority",
|
||||
"hawkbit.server.controller.security.authentication.header.authority", Boolean.class,
|
||||
Boolean.FALSE.toString()),
|
||||
Boolean.FALSE.toString(), BooleanValidator.class),
|
||||
/**
|
||||
* boolean value {@code true} {@code false}.
|
||||
*/
|
||||
AUTHENTICATION_MODE_TARGET_SECURITY_TOKEN_ENABLED("authentication.targettoken.enabled",
|
||||
"hawkbit.server.controller.security.authentication.targettoken.enabled", Boolean.class,
|
||||
Boolean.FALSE.toString()),
|
||||
Boolean.FALSE.toString(), BooleanValidator.class),
|
||||
|
||||
/**
|
||||
* boolean value {@code true} {@code false}.
|
||||
*/
|
||||
AUTHENTICATION_MODE_GATEWAY_SECURITY_TOKEN_ENABLED("authentication.gatewaytoken.enabled",
|
||||
"hawkbit.server.controller.security.authentication.gatewaytoken.enabled", Boolean.class,
|
||||
Boolean.FALSE.toString()),
|
||||
Boolean.FALSE.toString(), BooleanValidator.class),
|
||||
/**
|
||||
* string value which holds the name of the security token key.
|
||||
*/
|
||||
AUTHENTICATION_MODE_GATEWAY_SECURITY_TOKEN_NAME("authentication.gatewaytoken.name",
|
||||
"hawkbit.server.controller.security.authentication.gatewaytoken.name", String.class, null),
|
||||
"hawkbit.server.controller.security.authentication.gatewaytoken.name", String.class, null,
|
||||
StringValidator.class),
|
||||
|
||||
/**
|
||||
* string value which holds the actual security-key of the gateway security
|
||||
* token.
|
||||
*/
|
||||
AUTHENTICATION_MODE_GATEWAY_SECURITY_TOKEN_KEY("authentication.gatewaytoken.key",
|
||||
"hawkbit.server.controller.security.authentication.gatewaytoken.key", String.class, null),
|
||||
"hawkbit.server.controller.security.authentication.gatewaytoken.key", String.class, null,
|
||||
StringValidator.class),
|
||||
|
||||
/**
|
||||
* string value which holds the polling time interval in the format HH:mm:ss
|
||||
*/
|
||||
POLLING_TIME_INTERVAL("pollingOverdueTime", "hawkbit.controller.pollingOverdueTime", String.class, null),
|
||||
POLLING_TIME_INTERVAL("pollingOverdueTime", "hawkbit.controller.pollingOverdueTime", String.class, null,
|
||||
PollTimeValidator.class),
|
||||
|
||||
/**
|
||||
* string value which holds the polling time interval in the format HH:mm:ss
|
||||
*/
|
||||
POLLING_OVERDUE_TIME_INTERVAL("pollingTime", "hawkbit.controller.pollingTime", String.class, null);
|
||||
POLLING_OVERDUE_TIME_INTERVAL("pollingTime", "hawkbit.controller.pollingTime", String.class, null,
|
||||
PollTimeValidator.class);
|
||||
|
||||
private final String keyName;
|
||||
private final String defaultKeyName;
|
||||
private final Class<?> dataType;
|
||||
private final String defaultValue;
|
||||
private final Class<? extends TenantConfigurationValidator> validator;
|
||||
|
||||
/**
|
||||
* @param key
|
||||
@@ -78,11 +89,12 @@ public enum TenantConfigurationKey {
|
||||
* the allowed values for this specific key
|
||||
*/
|
||||
private TenantConfigurationKey(final String key, final String defaultKeyName, final Class<?> dataType,
|
||||
final String defaultValue) {
|
||||
final String defaultValue, final Class<? extends TenantConfigurationValidator> validator) {
|
||||
this.keyName = key;
|
||||
this.dataType = dataType;
|
||||
this.defaultKeyName = defaultKeyName;
|
||||
this.defaultValue = defaultValue;
|
||||
this.validator = validator;
|
||||
|
||||
}
|
||||
|
||||
@@ -114,4 +126,12 @@ public enum TenantConfigurationKey {
|
||||
public Class<?> getDataType() {
|
||||
return dataType;
|
||||
}
|
||||
|
||||
public boolean validate(final ApplicationContext context, final Object value) {
|
||||
final TenantConfigurationValidator createBean = context.getAutowireCapableBeanFactory().createBean(validator);
|
||||
final boolean isValid = createBean.validate(value);
|
||||
context.getAutowireCapableBeanFactory().destroyBean(createBean);
|
||||
return isValid;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package org.eclipse.hawkbit.tenancy.configuration.validator;
|
||||
|
||||
public class BooleanValidator implements TenantConfigurationValidator {
|
||||
|
||||
@Override
|
||||
public boolean validate(final Object tenantConfigurationValue) {
|
||||
if (tenantConfigurationValue instanceof Boolean) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package org.eclipse.hawkbit.tenancy.configuration.validator;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.format.DateTimeParseException;
|
||||
|
||||
import org.eclipse.hawkbit.ControllerPollProperties;
|
||||
import org.eclipse.hawkbit.tenancy.configuration.DurationHelper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
public class PollTimeValidator implements TenantConfigurationValidator {
|
||||
|
||||
// private final ControllerPollProperties properties;
|
||||
|
||||
private final DurationHelper durationHelper = new DurationHelper();
|
||||
|
||||
private final Duration minDuration;
|
||||
|
||||
private final Duration maxDuration;
|
||||
|
||||
@Autowired
|
||||
public PollTimeValidator(final ControllerPollProperties properties) {
|
||||
// this.properties = properties;
|
||||
|
||||
minDuration = durationHelper.formattedStringToDuration(properties.getMinPollingTime());
|
||||
maxDuration = durationHelper.formattedStringToDuration(properties.getMaxPollingTime());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean validate(final Object tenantConfigurationObject) {
|
||||
if (!(tenantConfigurationObject instanceof String)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final String tenantConfigurationString = (String) tenantConfigurationObject;
|
||||
|
||||
try {
|
||||
final Duration tenantConfigurationValue = durationHelper
|
||||
.formattedStringToDuration(tenantConfigurationString);
|
||||
|
||||
return durationHelper.durationRangeValidator(minDuration, maxDuration)
|
||||
.isWithinRange(tenantConfigurationValue);
|
||||
|
||||
} catch (final DateTimeParseException ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package org.eclipse.hawkbit.tenancy.configuration.validator;
|
||||
|
||||
public class StringValidator implements TenantConfigurationValidator {
|
||||
|
||||
@Override
|
||||
public boolean validate(final Object tenantConfigurationValue) {
|
||||
if (tenantConfigurationValue instanceof String) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package org.eclipse.hawkbit.tenancy.configuration.validator;
|
||||
|
||||
public interface TenantConfigurationValidator {
|
||||
|
||||
boolean validate(Object tenantConfigurationValue);
|
||||
}
|
||||
@@ -18,7 +18,7 @@ import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||
*/
|
||||
@JsonInclude(Include.NON_NULL)
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class AuthenticationConfigurationRest {
|
||||
public class TenantConfigurationRest {
|
||||
|
||||
private String key;
|
||||
private String value;
|
||||
Reference in New Issue
Block a user